Computer Science 1 : Java Programming Dr. David Cline Program : Hospital Record
ID: 3725983 • Letter: C
Question
Computer Science 1 : Java Programming Dr. David Cline Program : Hospital Record Sorting Sorting is one of the most common applications of computer science. In this program, you will read in patient records from a file, sort them based on one of the fields of the records using Java's built-in sorting routines, and then write the sorted list to the console. Things you will learn Creating simple classes .Comparators and Java's sorting routines Reading input from files and formatting output. Specification In this assignment, you will implement a Patient Record sorter program. Your program must include the following elements: . A class called "Patient" to hold a patient record. 2. Different comparators to compare the different attributes of Patient. Patient records will be specified in a file, one record per line. Each line of the file will have five comma-separted fields with no spaces between them. The fields will be: lastName, firstName, age, insuranceCompany, roomNum, in that order. An example file is shown below Franklin, Benjamin, 74,bebs of oklahoma, 18 Hamilton, Alexander,199,aetna, 25 Thatcher, Margaret, 65,aflack, 3 Nixon, Richard, 45, kaiser permanente,7 The program must take two command line arguments: the name of a file containing patient records. the name of a field (attribute) of the records on which to sort, which must be one of first, last, age, room, insurance I. 2. your name and the program name, followed by a When run, your program must first print out formatted, sorted list of the records from the file, pr output must be aligned, with a header printed above them, and reasonable spacing between columns The numbers must be right aligned, and the word fields must be left aligned. inted to the console. The fields of the formatted If there are not enough command line arguments, or if the specified file cannot be found, the should print out a usage statement and exit. It must not crash. programExplanation / Answer
here is your program : -------------------------->>>>>>>>>>>>..
Patient.java : --------->>>>>>
import java.util.*;
public class Patient{
private String fname;
private String lname;
private String company;
private int age;
private int roomNumber;
public Patient(){
fname = "";
lname = "";
company = "";
age = 0;
roomNumber = 0;
}
public void setFirstName(String fn){
fname = fn;
}
public void setLastName(String ln){
lname = ln;
}
public void setAge(int a){
age = a;
}
public void setCompany(String c){
company = c;
}
public void setRoomNumber(int rn){
roomNumber = rn;
}
public String getFirstName(){
return fname;
}
public String getLastName(){
return lname;
}
public String getCompany(){
return company;
}
public int getAge(){
return age;
}
public int getRoomNumber(){
return roomNumber;
}
public static Comparator<Patient> firstNameComparator = new Comparator<Patient>(){
public int compare(Patient p1,Patient p2){
return p1.getFirstName().compareTo(p2.getFirstName());
}
};
public static Comparator<Patient> lastNameComparator = new Comparator<Patient>(){
public int compare(Patient p1,Patient p2){
return p1.getLastName().compareTo(p2.getLastName());
}
};
public static Comparator<Patient> ageComparator = new Comparator<Patient>(){
public int compare(Patient p1,Patient p2){
if(p1.getAge() == p2.getAge()){
return 0;
}
if(p1.getAge() < p2.getAge()){
return -1;
}
if(p1.getAge() > p2.getAge()){
return 1;
}
return -1;
}
};
public static Comparator<Patient> companyComparator = new Comparator<Patient>(){
public int compare(Patient p1,Patient p2){
return p1.getCompany().compareTo(p2.getCompany());
}
};
public static Comparator<Patient> roomNumberComparator = new Comparator<Patient>(){
public int compare(Patient p1,Patient p2){
if(p1.getRoomNumber() == p2.getRoomNumber()){
return 0;
}
if(p1.getRoomNumber() < p2.getRoomNumber()){
return -1;
}
if(p1.getRoomNumber() > p2.getRoomNumber()){
return 1;
}
return -1;
}
};
}
HospitalSorting.java : ---------------->>>>>>>>
import java.util.*;
import java.io.*;
public class HospitalSorting{
public static void getInput(ArrayList<Patient> patients){
try{
Scanner sc = new Scanner(new File("patients.txt"));
while(sc.hasNextLine()){
String line = sc.nextLine();
String[] arr = line.split(",");
if(arr.length == 5){
Patient temp = new Patient();
temp.setFirstName(arr[1]);
temp.setLastName(arr[0]);
temp.setAge(Integer.parseInt(arr[2]));
temp.setCompany(arr[3]);
temp.setRoomNumber(Integer.parseInt(arr[4]));
patients.add(temp);
}
}
}catch(Exception e){
System.out.println("File Not Found");
}
}
public static void print(ArrayList<Patient> patients){
System.out.println("Hospital Sorting Program By <Your Name Here> ");
System.out.println("Last First Age Insurrence Room Number");
for(int i = 0;i<patients.size();i++){
Patient p = patients.get(i);
System.out.println(p.getLastName()+" "+p.getFirstName()+" "+p.getAge()+" "+p.getCompany()+" "+p.getRoomNumber());
}
}
public static void main(String[] args) {
ArrayList<Patient> patients = new ArrayList<>();
getInput(patients);
patients.sort(Patient.firstNameComparator);
System.out.println();
print(patients);
patients.sort(Patient.lastNameComparator);
System.out.println();
print(patients);
patients.sort(Patient.ageComparator);
System.out.println();
print(patients);
patients.sort(Patient.companyComparator);
System.out.println();
print(patients);
patients.sort(Patient.roomNumberComparator);
System.out.println();
print(patients);
}
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.