From 27efbb27f13f0e4acd57df0b917897baec68bcf4 Mon Sep 17 00:00:00 2001 From: Tan Wei Li Date: Tue, 29 Mar 2022 00:03:43 +0800 Subject: [PATCH 1/2] Refactor view appt To have the same flow between classes as the other classes. --- src/main/java/seedu/duke/Manager.java | 4 + .../seedu/duke/assets/AppointmentList.java | 182 +++++++++--------- src/main/java/seedu/duke/helper/Parser.java | 33 +--- .../command/FindAppointmentCommand.java | 39 ++++ .../command/ViewAppointmentCommand.java | 4 +- .../duke/helper/finder/AppointmentFinder.java | 81 ++++++++ 6 files changed, 225 insertions(+), 118 deletions(-) create mode 100644 src/main/java/seedu/duke/helper/command/FindAppointmentCommand.java create mode 100644 src/main/java/seedu/duke/helper/finder/AppointmentFinder.java diff --git a/src/main/java/seedu/duke/Manager.java b/src/main/java/seedu/duke/Manager.java index f41bfdf234..1b03afc2dc 100644 --- a/src/main/java/seedu/duke/Manager.java +++ b/src/main/java/seedu/duke/Manager.java @@ -108,6 +108,10 @@ private Status executeCommand(String commandWord, String parameters) throws Halp command = Parser.parseViewAppointment(parameters); status = command.execute(storage.appointments); break; + case "find appointment": + command = Parser.parseFindAppointment(parameters); + status = command.execute(storage.appointments); + break; case "delete appointment": command = Parser.parseDeleteAppointment(parameters); status = command.execute(storage.appointments); diff --git a/src/main/java/seedu/duke/assets/AppointmentList.java b/src/main/java/seedu/duke/assets/AppointmentList.java index 64d3c29720..bd649cf464 100644 --- a/src/main/java/seedu/duke/assets/AppointmentList.java +++ b/src/main/java/seedu/duke/assets/AppointmentList.java @@ -3,22 +3,29 @@ import seedu.duke.exception.DuplicateEntryException; import seedu.duke.exception.HalpmiException; import seedu.duke.exception.NotFoundException; -import seedu.duke.helper.UI; import seedu.duke.helper.CommandLineTable; +import seedu.duke.helper.UI; +import seedu.duke.helper.finder.AppointmentFinder; import java.util.ArrayList; public class AppointmentList extends List { private ArrayList appointments = new ArrayList<>(); + private ArrayList returnedFinderArray = new ArrayList<>(); + public Appointment getAppointment(String appointmentId) { + for (Appointment appointment : appointments) { + if (appointment.getAppointmentId().equals(appointmentId)) { + return appointment; + } + } + return null; + } + public ArrayList getList() { return appointments; } - public int getSize() { - return appointments.size(); - } - @Override public void add(String[] addAppointmentParameters) throws DuplicateEntryException { final int numberOfAppointmentsBefore = appointments.size(); @@ -83,99 +90,94 @@ public void view() throws HalpmiException { } @Override - public void view(String parameters) throws HalpmiException { - String[] parametersArray = parameters.split(","); - String criteria = parametersArray[0].trim(); - String input = parametersArray[1].trim(); - ArrayList foundAppointments = new ArrayList<>(); - switch (criteria) { - case "appointment id": - for (int i = 0; i < appointments.size(); i++) { - if (appointments.get(i).getAppointmentId().equals(input)) { - foundAppointments.add(appointments.get(i)); - assert foundAppointments.size() != 0; - } else { - throw new HalpmiException("Appointment Id doesnt exist, please try again"); - } - } - break; - case "patient name": - for (int i = 0; i < appointments.size(); i++) { - if (appointments.get(i).getPatientName().equals(input)) { - foundAppointments.add(appointments.get(i)); - assert foundAppointments.size() != 0; - } else { - throw new HalpmiException("Patient doesnt exist, please try again"); - } - } - break; - case "doctor name": - for (int i = 0; i < appointments.size(); i++) { - if (appointments.get(i).getDoctorName().equals(input)) { - foundAppointments.add(appointments.get(i)); - assert foundAppointments.size() != 0; - } else { - throw new HalpmiException("Doctor doesnt exist, please try again"); - } - } - break; - case "date": - for (int i = 0; i < appointments.size(); i++) { - if (appointments.get(i).getAppointmentDate().equals(input)) { - foundAppointments.add(appointments.get(i)); - assert foundAppointments.size() != 0; - } else { - throw new HalpmiException("Date doesnt exist, please try again"); - } - } - break; - case "patient nric": - for (int i = 0; i < appointments.size(); i++) { - if (appointments.get(i).getPatientNric().equals(input)) { - foundAppointments.add(appointments.get(i)); - assert foundAppointments.size() != 0; - } else { - throw new HalpmiException("Patient nric doesnt exist, please try again"); - } - } - break; - case "doctor nric": - for (int i = 0; i < appointments.size(); i++) { - if (appointments.get(i).getDoctorNric().equals(input)) { - foundAppointments.add(appointments.get(i)); - assert foundAppointments.size() != 0; - } else { - - throw new HalpmiException("Doctor doesnt exist, please try again"); - } - } - break; - default: - UI.printParagraph("Invalid search criteria! The valid criteria are:\n" - + "patient name\n" - + "patient name\n" - + "doctor name\n" - + "date\n" - + "nric\n" - + "Please try again!"); - assert foundAppointments.size() == 0; - return; + public void view(String appointmentId) throws HalpmiException { + Appointment foundAppointment = getAppointment(appointmentId); + if (foundAppointment == null) { + throw new HalpmiException("Appointment doesn't exist please try again!"); } CommandLineTable appointmentTable = new CommandLineTable(); appointmentTable.setShowVerticalLines(true); appointmentTable.setHeaders("Appointment Id", "Patient Name", "Patient NRIC", "Doctor Name", "Doctor NRIC", "Appointment Date", "Appointment Details"); - for (Appointment appointment: foundAppointments) { - if (appointmentTable == null) { - appointmentTable.addRow(appointment.getAppointmentId(), appointment.getPatientName(), - appointment.getPatientNric(), appointment.getDoctorName(), appointment.getDoctorNric(), - appointment.getAppointmentDate(), appointment.getAppointmentDetails()); - } - appointmentTable.print(); + appointmentTable.addRow(foundAppointment.getAppointmentId(), foundAppointment.getPatientName(), + foundAppointment.getPatientNric(), foundAppointment.getDoctorName(), foundAppointment.getDoctorNric(), + foundAppointment.getAppointmentDate(), foundAppointment.getAppointmentDetails()); + appointmentTable.print(); + } + + public void findById(String[] parameters) { + try { + this.returnedFinderArray = AppointmentFinder.findAppointmentById(appointments, parameters[1]); + createArrayOfFoundAppointments(); + } catch (NullPointerException e) { + UI.printParagraph("Appointment with given ID doesn't exist. Please try again!"); + } + } + + public void findByPatientName(String[] parameters) { + try { + this.returnedFinderArray = AppointmentFinder.findAppointmentByPatientName(appointments, parameters[1]); + createArrayOfFoundAppointments(); + } catch (NullPointerException e) { + UI.printParagraph("Appointment with given patient name doesn't exist. Please try again!"); + } + } + + public void findByPatientNric(String[] parameters) { + try { + this.returnedFinderArray = AppointmentFinder.findAppointmentByPatientNric(appointments, (parameters[1])); + createArrayOfFoundAppointments(); + } catch (NullPointerException e) { + UI.printParagraph("Appointment with given patient nric doesn't exist. Please try again!"); + } + } + + public void findByDoctorName(String[] parameters) { + try { + this.returnedFinderArray = AppointmentFinder.findAppointmentByDoctorName(appointments, parameters[1]); + createArrayOfFoundAppointments(); + } catch (NullPointerException e) { + UI.printParagraph("Appointment with given doctor name doesn't exist. Please try again!"); } - throw new HalpmiException("Appointment List is empty, please add appointment"); } + public void findByDoctorNric(String[] parameters) { + try { + this.returnedFinderArray = AppointmentFinder.findAppointmentByDoctorNric(appointments, parameters[1]); + createArrayOfFoundAppointments(); + } catch (NullPointerException e) { + UI.printParagraph("Appointment with given doctor nric doesn't exist. Please try again!"); + } + } + public void findByAppointmentDate(String[] parameters) { + try { + this.returnedFinderArray = AppointmentFinder.findAppointmentByDate(appointments, parameters[1]); + createArrayOfFoundAppointments(); + } catch (NullPointerException e) { + UI.printParagraph("Appointment with given date doesn't exist. Please try again!"); + } + } + + private void createArrayOfFoundAppointments() { + if (returnedFinderArray.isEmpty()) { + UI.printParagraph("Appointment doesn't exist please try again!"); + } else { + CommandLineTable findAppointmentTable = new CommandLineTable(); + findAppointmentTable.setShowVerticalLines(true); + findAppointmentTable.setHeaders("Appointment Id", "Patient Nric", "Patient Name", "Doctor Nric", + "Doctor Name", "Appointment Date", "Appointment Details"); + for (int i = 0; i < returnedFinderArray.size(); i++) { + findAppointmentTable.addRow(returnedFinderArray.get(i).getAppointmentId(), + returnedFinderArray.get(i).getPatientNric(), + returnedFinderArray.get(i).getPatientName(), + returnedFinderArray.get(i).getDoctorNric(), + returnedFinderArray.get(i).getDoctorName(), + returnedFinderArray.get(i).getAppointmentDate(), + returnedFinderArray.get(i).getAppointmentDetails()); + } + findAppointmentTable.print(); + } + } } diff --git a/src/main/java/seedu/duke/helper/Parser.java b/src/main/java/seedu/duke/helper/Parser.java index e442412ee5..6f58347798 100644 --- a/src/main/java/seedu/duke/helper/Parser.java +++ b/src/main/java/seedu/duke/helper/Parser.java @@ -1,28 +1,7 @@ package seedu.duke.helper; import seedu.duke.exception.HalpmiException; -import seedu.duke.helper.command.AddAppointmentCommand; -import seedu.duke.helper.command.AddDoctorCommand; -import seedu.duke.helper.command.AddMedicineCommand; -import seedu.duke.helper.command.AddPatientCommand; -import seedu.duke.helper.command.ClearExpiredMedicineCommand; -import seedu.duke.helper.command.Command; -import seedu.duke.helper.command.DeleteAppointmentCommand; -import seedu.duke.helper.command.DeleteDoctorCommand; -import seedu.duke.helper.command.DeleteMedicineCommand; -import seedu.duke.helper.command.DeletePatientCommand; -import seedu.duke.helper.command.EditAppointmentCommand; -import seedu.duke.helper.command.EditDoctorCommand; -import seedu.duke.helper.command.EditMedicineCommand; -import seedu.duke.helper.command.EditPatientCommand; -import seedu.duke.helper.command.FindDoctorCommand; -import seedu.duke.helper.command.FindMedicineCommand; -import seedu.duke.helper.command.FindPatientCommand; -import seedu.duke.helper.command.UpdateMedicineInventoryCommand; -import seedu.duke.helper.command.ViewAppointmentCommand; -import seedu.duke.helper.command.ViewDoctorCommand; -import seedu.duke.helper.command.ViewMedicineCommand; -import seedu.duke.helper.command.ViewPatientCommand; +import seedu.duke.helper.command.*; public class Parser { @@ -146,10 +125,14 @@ public static Command parseAddAppointment(String parameters) throws HalpmiExcept public static Command parseViewAppointment(String parameters) throws HalpmiException { if (isNull(parameters)) { - return new ViewMedicineCommand(null); + return new ViewAppointmentCommand(null); } - String[] viewAppointmentParameters = minParameterCheck(parameters, 2); - return new ViewAppointmentCommand(viewAppointmentParameters); + return parseFindAppointment(parameters); + } + + public static Command parseFindAppointment(String parameters) throws HalpmiException { + String[] findAppointmentParameters = minParameterCheck(parameters, 2); + return new FindAppointmentCommand(findAppointmentParameters); } public static Command parseEditAppointment(String parameters) throws HalpmiException { diff --git a/src/main/java/seedu/duke/helper/command/FindAppointmentCommand.java b/src/main/java/seedu/duke/helper/command/FindAppointmentCommand.java new file mode 100644 index 0000000000..4bb5b9e3ea --- /dev/null +++ b/src/main/java/seedu/duke/helper/command/FindAppointmentCommand.java @@ -0,0 +1,39 @@ +package seedu.duke.helper.command; + +import seedu.duke.assets.AppointmentList; +import seedu.duke.assets.List; +import seedu.duke.status.Status; + +public class FindAppointmentCommand extends Command { + public FindAppointmentCommand(String[] parameterArray) { + super(parameterArray); + } + + public Status execute(List appointmentList) { + if (appointmentList instanceof AppointmentList) { + switch (parameterArray[0]) { + case "id": + ((AppointmentList) appointmentList).findById(parameterArray); + break; + case "patient nric": + ((AppointmentList) appointmentList).findByPatientNric(parameterArray); + break; + case "patient name": + ((AppointmentList) appointmentList).findByPatientName(parameterArray); + break; + case "doctor nric": + ((AppointmentList) appointmentList).findByDoctorNric(parameterArray); + break; + case "doctor name": + ((AppointmentList) appointmentList).findByDoctorName(parameterArray); + break; + case "date": + ((AppointmentList) appointmentList).findByAppointmentDate(parameterArray); + break; + default: + break; + } + } + return Status.FIND_MEDICINE_SUCCESS; + } +} \ No newline at end of file diff --git a/src/main/java/seedu/duke/helper/command/ViewAppointmentCommand.java b/src/main/java/seedu/duke/helper/command/ViewAppointmentCommand.java index 3a38dfeb94..a28e715b00 100644 --- a/src/main/java/seedu/duke/helper/command/ViewAppointmentCommand.java +++ b/src/main/java/seedu/duke/helper/command/ViewAppointmentCommand.java @@ -2,7 +2,6 @@ import seedu.duke.assets.List; import seedu.duke.exception.HalpmiException; -import seedu.duke.exception.NotFoundException; import seedu.duke.status.Status; public class ViewAppointmentCommand extends Command { @@ -14,8 +13,7 @@ public Status execute(List appointmentList) throws HalpmiException { if (parameterArray == null) { appointmentList.view(); } else { - String parameters = String.join(",",parameterArray); - appointmentList.view(parameters); + throw new HalpmiException("View Appointment Command only accepts null parameters!"); } return Status.VIEW_SUCCESS; } diff --git a/src/main/java/seedu/duke/helper/finder/AppointmentFinder.java b/src/main/java/seedu/duke/helper/finder/AppointmentFinder.java new file mode 100644 index 0000000000..090ed0682d --- /dev/null +++ b/src/main/java/seedu/duke/helper/finder/AppointmentFinder.java @@ -0,0 +1,81 @@ +package seedu.duke.helper.finder; + +import seedu.duke.assets.Appointment; + +import java.util.ArrayList; + +public class AppointmentFinder { + public static ArrayList findAppointmentById(ArrayList appointments, + String appointmentId) { + ArrayList appointmentArrayList = new ArrayList<>(); + for (Appointment appointment : appointments) { + if (appointment.getAppointmentId().equals(appointmentId)) { + appointmentArrayList.add(appointment); + } + } + return getAppointments(appointmentArrayList); + } + + public static ArrayList findAppointmentByPatientName(ArrayList appointments, + String patientName) { + ArrayList appointmentArrayList = new ArrayList<>(); + for (Appointment appointment : appointments) { + if (appointment.getPatientName().contains(patientName)) { + appointmentArrayList.add(appointment); + } + } + return getAppointments(appointmentArrayList); + } + + public static ArrayList findAppointmentByPatientNric(ArrayList appointments, + String patientNric) { + ArrayList appointmentArrayList = new ArrayList<>(); + for (Appointment appointment : appointments) { + if (appointment.getPatientNric().equals(patientNric)) { + appointmentArrayList.add(appointment); + } + } + return getAppointments(appointmentArrayList); + } + + public static ArrayList findAppointmentByDoctorName(ArrayList appointments, + String doctorName) { + ArrayList appointmentArrayList = new ArrayList<>(); + for (Appointment appointment : appointments) { + if (appointment.getDoctorName().equals(doctorName)) { + appointmentArrayList.add(appointment); + } + } + return getAppointments(appointmentArrayList); + } + + public static ArrayList findAppointmentByDoctorNric(ArrayList appointments, + String doctorNric) { + ArrayList appointmentArrayList = new ArrayList<>(); + for (Appointment appointment : appointments) { + if (appointment.getDoctorNric().equals(doctorNric)) { + appointmentArrayList.add(appointment); + } + } + return getAppointments(appointmentArrayList); + } + + public static ArrayList findAppointmentByDate(ArrayList appointments, + String date) { + ArrayList appointmentArrayList = new ArrayList<>(); + for (Appointment appointment : appointments) { + if (appointment.getAppointmentDate().equals(date)) { + appointmentArrayList.add(appointment); + } + } + return getAppointments(appointmentArrayList); + } + + private static ArrayList getAppointments(ArrayList appointmentArrayList) { + if (appointmentArrayList.isEmpty()) { + return null; + } else { + return appointmentArrayList; + } + } +} From 7d229b5e5ed65bccf38aa1924852442dd40da95f Mon Sep 17 00:00:00 2001 From: Tan Wei Li Date: Tue, 29 Mar 2022 13:41:56 +0800 Subject: [PATCH 2/2] Update appt small bug where no error message for view appt When there is view appt with incorrect format, no error message is shown previously. --- .../seedu/duke/assets/AppointmentList.java | 2 +- .../java/seedu/duke/assets/MedicineList.java | 8 +++--- src/main/java/seedu/duke/helper/Parser.java | 25 ++++++++++++++++++- .../java/seedu/duke/helper/Validator.java | 20 +++++++++++++++ .../UpdateMedicineInventoryCommand.java | 4 +-- 5 files changed, 52 insertions(+), 7 deletions(-) diff --git a/src/main/java/seedu/duke/assets/AppointmentList.java b/src/main/java/seedu/duke/assets/AppointmentList.java index bd649cf464..87644fdbf4 100644 --- a/src/main/java/seedu/duke/assets/AppointmentList.java +++ b/src/main/java/seedu/duke/assets/AppointmentList.java @@ -38,7 +38,6 @@ public void add(String[] addAppointmentParameters) throws DuplicateEntryExceptio addAppointmentParameters[2], addAppointmentParameters[3], addAppointmentParameters[4], addAppointmentParameters[5], addAppointmentParameters[6]); appointments.add(newAppointment); - UI.printParagraph("Appointment has been added"); assert appointments.size() == numberOfAppointmentsBefore + 1; } @@ -149,6 +148,7 @@ public void findByDoctorNric(String[] parameters) { UI.printParagraph("Appointment with given doctor nric doesn't exist. Please try again!"); } } + public void findByAppointmentDate(String[] parameters) { try { this.returnedFinderArray = AppointmentFinder.findAppointmentByDate(appointments, parameters[1]); diff --git a/src/main/java/seedu/duke/assets/MedicineList.java b/src/main/java/seedu/duke/assets/MedicineList.java index a8fd4f7d7a..4deff489ff 100644 --- a/src/main/java/seedu/duke/assets/MedicineList.java +++ b/src/main/java/seedu/duke/assets/MedicineList.java @@ -114,12 +114,14 @@ public void edit(String[] parameterArray) throws NotFoundException { throw new NotFoundException("There are no medicines with given Batch ID!"); } - public void viewExpired() { + public void viewExpired() throws HalpmiException { CommandLineTable medicineTable = new CommandLineTable(); //st.setRightAlign(true);//if true then cell text is right aligned medicineTable.setShowVerticalLines(true); medicineTable.setHeaders("MedicineId", "MedicineName", "Dosage", "Expiry", "SideEffects", "Quantity"); - + if (expiredMedicines.size() == 0) { + throw new HalpmiException("There are no expired medicines."); + } for (Medicine medicine : expiredMedicines) { medicineTable.addRow(medicine.getMedicineId(), medicine.getMedicineName(), String.valueOf(medicine.getDosage()), @@ -129,7 +131,7 @@ public void viewExpired() { medicineTable.print(); } - public void updateStock() { + public void updateStock() throws HalpmiException { for (int i = 0; i < medicines.size(); i++) { LocalDate date = LocalDate.parse(medicines.get(i).getExpiry()); LocalDate today = LocalDate.now(); diff --git a/src/main/java/seedu/duke/helper/Parser.java b/src/main/java/seedu/duke/helper/Parser.java index 6f58347798..cf49074201 100644 --- a/src/main/java/seedu/duke/helper/Parser.java +++ b/src/main/java/seedu/duke/helper/Parser.java @@ -1,7 +1,29 @@ package seedu.duke.helper; import seedu.duke.exception.HalpmiException; -import seedu.duke.helper.command.*; +import seedu.duke.helper.command.AddAppointmentCommand; +import seedu.duke.helper.command.AddDoctorCommand; +import seedu.duke.helper.command.AddMedicineCommand; +import seedu.duke.helper.command.AddPatientCommand; +import seedu.duke.helper.command.ClearExpiredMedicineCommand; +import seedu.duke.helper.command.Command; +import seedu.duke.helper.command.DeleteAppointmentCommand; +import seedu.duke.helper.command.DeleteDoctorCommand; +import seedu.duke.helper.command.DeleteMedicineCommand; +import seedu.duke.helper.command.DeletePatientCommand; +import seedu.duke.helper.command.EditAppointmentCommand; +import seedu.duke.helper.command.EditDoctorCommand; +import seedu.duke.helper.command.EditMedicineCommand; +import seedu.duke.helper.command.EditPatientCommand; +import seedu.duke.helper.command.FindAppointmentCommand; +import seedu.duke.helper.command.FindDoctorCommand; +import seedu.duke.helper.command.FindMedicineCommand; +import seedu.duke.helper.command.FindPatientCommand; +import seedu.duke.helper.command.UpdateMedicineInventoryCommand; +import seedu.duke.helper.command.ViewAppointmentCommand; +import seedu.duke.helper.command.ViewDoctorCommand; +import seedu.duke.helper.command.ViewMedicineCommand; +import seedu.duke.helper.command.ViewPatientCommand; public class Parser { @@ -132,6 +154,7 @@ public static Command parseViewAppointment(String parameters) throws HalpmiExcep public static Command parseFindAppointment(String parameters) throws HalpmiException { String[] findAppointmentParameters = minParameterCheck(parameters, 2); + Validator.validateFindAppointment(findAppointmentParameters); return new FindAppointmentCommand(findAppointmentParameters); } diff --git a/src/main/java/seedu/duke/helper/Validator.java b/src/main/java/seedu/duke/helper/Validator.java index 7d63d0df88..3659c97759 100644 --- a/src/main/java/seedu/duke/helper/Validator.java +++ b/src/main/java/seedu/duke/helper/Validator.java @@ -318,6 +318,26 @@ public static void validateFindPatient(String[] parameters) throws HalpmiExcepti } } + public static void validateFindAppointment(String[] parameters) throws HalpmiException { + switch (parameters[0]) { + case "id": + break; + case "patient name": + case "doctor name": + validateFullName(parameters[1]); + break; + case "patient nric": + case "doctor nric": + validateNric(parameters[1]); + break; + case "date": + validateDate(parameters[1],"find appointment"); + break; + default: + throw new HalpmiException("Input must be an attribute of Appointment"); + } + } + public static void validateFindMedicine(String[] parameters) throws HalpmiException { boolean check = true; diff --git a/src/main/java/seedu/duke/helper/command/UpdateMedicineInventoryCommand.java b/src/main/java/seedu/duke/helper/command/UpdateMedicineInventoryCommand.java index 8e5f7f8606..e55f518b39 100644 --- a/src/main/java/seedu/duke/helper/command/UpdateMedicineInventoryCommand.java +++ b/src/main/java/seedu/duke/helper/command/UpdateMedicineInventoryCommand.java @@ -1,9 +1,9 @@ package seedu.duke.helper.command; -import seedu.duke.assets.AppointmentList; import seedu.duke.assets.List; import seedu.duke.assets.MedicineList; import seedu.duke.exception.DuplicateEntryException; +import seedu.duke.exception.HalpmiException; import seedu.duke.exception.NotFoundException; import seedu.duke.status.Status; @@ -14,7 +14,7 @@ public UpdateMedicineInventoryCommand() { } @Override - public Status execute(List medicineList) throws DuplicateEntryException, NotFoundException { + public Status execute(List medicineList) throws DuplicateEntryException, NotFoundException, HalpmiException { if (medicineList instanceof MedicineList) { ((MedicineList) medicineList).updateStock(); return Status.UPDATE_MEDICINE_INVENTORY_SUCCESS;