In: Computer Science
You are working as a software developer for a large insurance company. Your company is planning to migrate the existing systems from Visual Basic to Java and this will require new calculations. You will be creating a program that calculates the insurance payment category based on the BMI score.
Your Java program should perform the following things:
You need to submit the following things:
Thanks for the question. Here is the completed code for this problem. Comments are included so that you understand whats going on. Let me know if you have any doubts or if you need anything to change. Thank You !! ================================================================================================ import java.io.FileWriter; import java.io.IOException; import java.io.PrintWriter; import java.util.ArrayList; import java.util.Scanner; public class Patient { private String patientName; private String dob; private double weight; private double height; // constructor takes all the details - name, dob, height and weight public Patient(String patientName, String dob, double weight, double height) { this.patientName = patientName; this.dob = dob; if (weight < 0 || height < 0) throw new IllegalArgumentException("Invalid Weight/Height entered"); this.weight = weight; this.height = height; } public String getPatientName() { return patientName; } public String getDob() { return dob; } public double getWeight() { return weight; } public double getHeight() { return height; } // calculate the BMI and returns the value public double calculateBMI() { return weight / (height * height); } public static void main(String[] args) { ArrayList<Patient> patients = new ArrayList<Patient>(); Scanner scanner = new Scanner(System.in); // loop until user presses Q while (true) { System.out.print("Enter patient name: "); String patientName = scanner.nextLine(); System.out.print("Enter birthdate(mm/dd/yyyy): "); String dob = scanner.nextLine(); System.out.print("Enter weight (kg): "); double wt = scanner.nextDouble(); System.out.print("Enter height (meters): "); double height = scanner.nextDouble(); try { Patient aPatient = new Patient(patientName, dob, wt, height); patients.add(aPatient); } catch (IllegalArgumentException exception) { System.out.println(exception.getMessage()); } scanner.nextLine(); System.out.print("Do you want to quit(press q/Q):"); String quit = scanner.nextLine(); if (quit.equalsIgnoreCase("q")) break; } try { saveToFile(patients); System.out.println("Data saved in file successfully."); } catch (IOException e) { System.out.println("Unable to write datat to file."); } } // takes in the list of patient objects and write them to file private static void saveToFile(ArrayList<Patient> patients) throws IOException { PrintWriter writer = new PrintWriter(new FileWriter("F:\\patients.txt")); for (Patient patient : patients) { double bmi = patient.calculateBMI(); StringBuilder builder = new StringBuilder(); builder.append(patient.getPatientName()).append(","); builder.append(patient.getDob()).append(","); builder.append(patient.getHeight()).append(" meters,"); builder.append(patient.getWeight()).append(" kg(s), "); if (bmi <= 18.5) builder.append("Insurance Category: Low"); else if (bmi <= 24.9) builder.append("Insurance Category: Low"); else if (bmi <= 29.9) builder.append("Insurance Category: High"); else builder.append("Insurance Category: Highest"); builder.append("\r\n"); writer.write(builder.toString()); writer.flush(); } writer.close(); } }