Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

This program is supposed to be a log of patients in a hospital, but it is not running. there is an error message coming from

This program is supposed to be a log of patients in a hospital, but it is not running. there is an error message coming from line 21 that reads, Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 1.

import java.io.BufferedReader; import java.io.BufferedWriter; import java.io.FileReader; import java.io.FileWriter; import java.io.IOException; import java.util.ArrayList; import java.util.Date; import java.util.List; import java.util.Scanner;

public class HospitalManagement { public static void main(String args[]) throws IOException { String inputFileName = "/home/zubair/Desktop/a.txt"; new HospitalManagement(inputFileName); }

private PatientList patientLogData; public HospitalManagement(String inputFilePath) throws IOException { patientLogData = loadFromFile(inputFilePath); start(); } public void start() throws IOException { Scanner scanner = new Scanner(System.in); while (true) { displayMenu(); String choice = scanner.nextLine().toUpperCase(); switch(choice) { case "A" : displayList(); break; case "B" : addNewPatient(scanner); break; case "C" : showInfoOfAPatient(scanner); break; case "D" : deleteAPatient(scanner); break; case "E" : showAverageAgeOfPatient(); break; case "F" : showInfoOfYoungestPatient(); break; case "G" : showNotificationList(); break; case "H" : Quit(scanner); scanner.close(); return; default : System.out.println(System.lineSeparator()); System.out.println("Invalid Choice"); System.out.println(System.lineSeparator()); } } } private void displayList() { List patientList = patientLogData.getList(); for (PatientInfo patient : patientList) { System.out.println(patient.getName() + " " + patient.getId()); } }

private void deleteAPatient(Scanner scanner) { System.out.println("Enter id of the patient : "); String id = scanner.nextLine(); patientLogData.remove(id); }

@SuppressWarnings("deprecation") private void showAverageAgeOfPatient() { List patientList = patientLogData.getList(); Date currDate = new Date(System.currentTimeMillis());

int averageAge = 0; for (PatientInfo patient : patientList) { averageAge += (currDate.getYear() - patient.getDateOfBirth().getYear()); } if (patientList.size()!=0) averageAge /= patientList.size(); System.out.println(System.lineSeparator()); System.out.println("Average age of the patients is : "+ averageAge + " years"); System.out.println(System.lineSeparator()); }

private void showInfoOfAPatient(Scanner scanner) { System.out.println("Enter id of the patient : "); String id = scanner.nextLine(); PatientInfo patient = patientLogData.find(id); if (patient != null) { System.out.println(patient.toString()); } else { System.out.println("paitent not found"); } System.out.println(System.lineSeparator());

}

@SuppressWarnings("deprecation") private void showInfoOfYoungestPatient() { List patientList = patientLogData.getList(); Date currDate = new Date(System.currentTimeMillis());

if (patientList.size() <= 0) { System.out.println(System.lineSeparator()); System.out.println("No patient data is available"); return; } PatientInfo youngPatient = patientList.get(0); int smallest = (currDate.getYear() - youngPatient.getDateOfBirth().getYear());

for (PatientInfo patient : patientList) { int age = (currDate.getYear() - patient.getDateOfBirth().getYear()); if (age < smallest) { smallest = age; youngPatient = patient; } } System.out.println(System.lineSeparator()); System.out.println("Youngest patient is : "+ youngPatient); System.out.println(System.lineSeparator()); }

@SuppressWarnings("deprecation") private void showNotificationList() { List patientList = patientLogData.getList(); Date currDate = new Date(System.currentTimeMillis());

for (PatientInfo patient : patientList) { int time = (currDate.getYear() - patient.getLastVisit().getYear()); if (time >= 3) { System.out.println(patient.getId()+", "+patient.getName()+", is overdue for a visit , been " + time + " years"); } } }

private void addNewPatient(Scanner scanner) { }

public void Quit(Scanner sc) throws IOException { System.out.println(System.lineSeparator()); System.out.print("Do you wan to write the output (Y/N)? : "); while (true) { String userChoice = sc.nextLine(); if (userChoice.equalsIgnoreCase("Y")) { System.out.print("Enter output file : "); writeBackToFile(sc.nextLine(), patientLogData); return; } else if (userChoice.equalsIgnoreCase("N")) { return; } else { System.out.println("Invalid Option"); System.out.println(System.lineSeparator()); } } } /** * Reads the patient data from a given file * @param filePath * @return * @throws IOException */ public PatientList loadFromFile(String filePath) throws IOException { PatientList patientLogData = new PatientList(); BufferedReader bufferedInput = new BufferedReader(new FileReader(filePath)); String inputLine; while ((inputLine = bufferedInput.readLine()) != null) { String[] patientData = inputLine.split(" "); String id = patientData[0]; String name = patientData[1]; Date doB = new Date(Long.valueOf(patientData[2])); String address = patientData[3]; int height = Integer.valueOf(patientData[4]); double weight = Double.valueOf(patientData[5]); Date lastVisit = new Date(Long.valueOf(patientData[6])); Date initialVisit = new Date(Long.valueOf(patientData[7])); PatientInfo patient = new PatientInfo(name, id, doB, address, height, weight, initialVisit, lastVisit); patientLogData.add(patient); } bufferedInput.close(); return patientLogData; }

/** * Writes the output to a file * @param filePath * @param patientList * @throws IOException */ public void writeBackToFile(String filePath, PatientList patientList) throws IOException { if (filePath == null || filePath.isEmpty()) { return; } BufferedWriter bufferedWriter = new BufferedWriter(new FileWriter(filePath)); for (PatientInfo patient : patientList.getList()) { String id = patient.getId(); String name = patient.getName(); int height = patient.getHeight(); double weight = patient.getWieght(); String address = patient.getAddress().replaceAll(" ", "-"); Date doB = patient.getDateOfBirth(); Date lastVisit = patient.getLastVisit(); Date initialVisit = patient.getIntialVisit(); bufferedWriter.write(id + " " + name + " " + doB.getTime() + " " + address + " " + height + " " + weight + " " + lastVisit.getTime() + " " + initialVisit.getTime() + " "); } bufferedWriter.close(); } public void displayMenu() { System.out.println(System.lineSeparator()); System.out.println(System.lineSeparator()); System.out.println("Menu"); System.out.println("A.Display List"); System.out.println("B.Add a new patient"); System.out.println("C.Show info for a patient"); System.out.println("D.Delete a patient"); System.out.println("E.Show average age of patient"); System.out.println("F.Show info of youngest patient"); System.out.println("G.Show notification list"); System.out.println("H.Quit"); System.out.print("Enter your choice : " ); } }

class PatientList {

private List patientList; public PatientList() { patientList = new ArrayList<>(); } public List getList() { return patientList; } public PatientInfo find(String patientID) { for (PatientInfo patient : patientList) { if (patient.getId().equalsIgnoreCase(patientID)) { return patient; } } return null; } public int find(PatientInfo inputPatient) { int i=0; for (PatientInfo patient : patientList) { if (patient.getId().equalsIgnoreCase(inputPatient.getId())) { return i; } i++; } return -1; } public void remove(String patientID) { PatientInfo element = null; for (PatientInfo patient : patientList) { if (patient.getId().equalsIgnoreCase(patientID)) { element = patient; break; } } if (element != null) { patientList.remove(element); } }

public boolean contains(PatientInfo patient) { if (find(patient.getId()) == null) { return false; } return true; }

public void remove(PatientInfo patient) { patientList.remove(patient); }

public void add(PatientInfo patient) { patientList.add(patient); }

public int size() { return patientList.size(); } @Override public String toString() { StringBuffer buffer = new StringBuffer(); for (PatientInfo patient : patientList) { buffer.append(patient.toString() + System.lineSeparator()); } return buffer.toString(); } }

class PatientInfo { private String name; private String id; private Date dateOfBirth; private String address; private int height; private double wieght; private Date intialVisit; private Date lastVisit; public PatientInfo(String name, String id, Date dateOfBirth, String address, int height, double wieght, Date intialVisit, Date lastVisit) { super(); this.name = name; this.id = id; this.dateOfBirth = dateOfBirth; this.address = address; this.height = height; this.wieght = wieght; this.intialVisit = intialVisit; this.lastVisit = lastVisit; }

public String getName() { return name; }

public void setName(String name) { this.name = name; }

public String getId() { return id; }

public void setId(String id) { this.id = id; }

public String getAddress() { return address; }

public void setAddress(String address) { this.address = address; }

public int getHeight() { return height; }

public void setHeight(int height) { this.height = height; }

public double getWieght() { return wieght; }

public void setWieght(double wieght) { this.wieght = wieght; }

public Date getDateOfBirth() { return dateOfBirth; }

public void setDateOfBirth(Date dateOfBirth) { this.dateOfBirth = dateOfBirth; }

public Date getIntialVisit() { return intialVisit; }

public void setIntialVisit(Date intialVisit) { this.intialVisit = intialVisit; }

public Date getLastVisit() { return lastVisit; }

public void setLastVisit(Date lastVisit) { this.lastVisit = lastVisit; }

@Override public String toString() { return "PatientInfo [name=" + name + ", id=" + id + ", address=" + address + ", height=" + height + ", wieght=" + wieght + ", dateOfBirth=" + dateOfBirth + ", intialVisit=" + intialVisit + ", lastVisit=" + lastVisit+ "]"; } }

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image_2

Step: 3

blur-text-image_3

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Students also viewed these Databases questions

Question

Can users decide how and if their sensitive information is shared?

Answered: 1 week ago