Skip to content

Commit

Permalink
Merge pull request #122 from tanweili/WeiLi-RefactorViewAppt
Browse files Browse the repository at this point in the history
Refactor view appt
  • Loading branch information
shxr3f authored Mar 29, 2022
2 parents fa1f3ff + 7d229b5 commit b65ed84
Show file tree
Hide file tree
Showing 9 changed files with 254 additions and 102 deletions.
4 changes: 4 additions & 0 deletions src/main/java/seedu/duke/Manager.java
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
184 changes: 93 additions & 91 deletions src/main/java/seedu/duke/assets/AppointmentList.java
Original file line number Diff line number Diff line change
Expand Up @@ -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<Appointment> appointments = new ArrayList<>();
private ArrayList<Appointment> returnedFinderArray = new ArrayList<>();

public Appointment getAppointment(String appointmentId) {
for (Appointment appointment : appointments) {
if (appointment.getAppointmentId().equals(appointmentId)) {
return appointment;
}
}
return null;
}

public ArrayList<Appointment> getList() {
return appointments;
}

public int getSize() {
return appointments.size();
}

@Override
public void add(String[] addAppointmentParameters) throws DuplicateEntryException {
final int numberOfAppointmentsBefore = appointments.size();
Expand All @@ -31,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;
}

Expand Down Expand Up @@ -83,99 +89,95 @@ 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<Appointment> 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();
}
}
}


8 changes: 5 additions & 3 deletions src/main/java/seedu/duke/assets/MedicineList.java
Original file line number Diff line number Diff line change
Expand Up @@ -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()),
Expand All @@ -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();
Expand Down
12 changes: 9 additions & 3 deletions src/main/java/seedu/duke/helper/Parser.java
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
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;
Expand Down Expand Up @@ -146,10 +147,15 @@ 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);
Validator.validateFindAppointment(findAppointmentParameters);
return new FindAppointmentCommand(findAppointmentParameters);
}

public static Command parseEditAppointment(String parameters) throws HalpmiException {
Expand Down
20 changes: 20 additions & 0 deletions src/main/java/seedu/duke/helper/Validator.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
Original file line number Diff line number Diff line change
@@ -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;
}
}
Original file line number Diff line number Diff line change
@@ -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;

Expand All @@ -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;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand All @@ -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;
}
Expand Down
Loading

0 comments on commit b65ed84

Please sign in to comment.