Skip to content

Commit

Permalink
Merge pull request #48 from JordanKwua/FlightDeleteBug
Browse files Browse the repository at this point in the history
Fix bug in invalid flight number format for flight delete function
  • Loading branch information
ivanthengwr authored Oct 24, 2022
2 parents a554ecc + c42ecde commit ee7156e
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 24 deletions.
30 changes: 11 additions & 19 deletions src/main/java/seedu/duke/operationlist/FlightList.java
Original file line number Diff line number Diff line change
Expand Up @@ -78,28 +78,18 @@ public void addOperation(String command) throws SkyControlException {

//@@author JordanKwua
@Override
public void deleteOperation(String detail) {
boolean isFlightFound;
try {
checkCommandLength(detail.substring(FLIGHT_DELETE_COMMAND.length()));
checkValidFlightNumber(detail.substring("flight delete ".length()));
String flightNum = detail.substring("flight delete ".length()).toUpperCase();
isFlightFound = findAndRemoveFlight(flightNum);
if (!isFlightFound) {
ui.showFlightNotFoundMessage(flightNum);
}
} catch (SkyControlException e) {
ui.showEmptyDescriptionMessage();
} catch (NumberFormatException e) {
ui.showWrongFlightFormatMessage();
}
public void deleteOperation(String detail) throws SkyControlException {
checkCommandLength(detail.substring(FLIGHT_DELETE_COMMAND.length()));
checkValidFlightNumber(detail.substring("flight delete ".length()));
String flightNum = detail.substring("flight delete ".length()).toUpperCase();
findAndRemoveFlight(flightNum);
}

private void checkValidFlightNumber(String substring) throws NumberFormatException {
private void checkValidFlightNumber(String substring) throws SkyControlException {
String[] letters = substring.split("");
for (int i = 0; i < letters.length; i++) {
if (!Character.isLetterOrDigit(substring.charAt(i))) {
throw new NumberFormatException(substring);
throw new SkyControlException(ui.getWrongFlightFormatErrorMessage());
}
}
}
Expand All @@ -111,7 +101,7 @@ public void listOperation() {
}

//@@author JordanKwua
private static boolean findAndRemoveFlight(String flightNumber) {
private static void findAndRemoveFlight(String flightNumber) throws SkyControlException {
boolean isFlightFound = false;
assert !flights.isEmpty();
for (FlightInfo flight : flights) {
Expand All @@ -123,7 +113,9 @@ private static boolean findAndRemoveFlight(String flightNumber) {
break;
}
}
return isFlightFound;
if (!isFlightFound) {
throw new SkyControlException(ui.getFlightNotFoundMessage(flightNumber));
}
}

//@@author Franky4566
Expand Down
8 changes: 4 additions & 4 deletions src/main/java/seedu/duke/ui/Ui.java
Original file line number Diff line number Diff line change
Expand Up @@ -208,8 +208,8 @@ public void showEmptyFlightList() {
"The Flight schedule is empty.", " ");
}

public void showFlightNotFoundMessage(String flightNum) {
System.out.println("FLIGHT " + flightNum + " NOT FOUND.");
public String getFlightNotFoundMessage(String flightNum) {
return "FLIGHT " + flightNum + " NOT FOUND.";
}

public String showEmptyDescriptionMessage() {
Expand Down Expand Up @@ -252,8 +252,8 @@ public String getLoggerStartUpMessage() {
}

//@@author JordanKwua
public void showWrongFlightFormatMessage() {
System.out.println("Error: Flight number should start with 2 letters and trail with 1-4 numbers.");
public String getWrongFlightFormatErrorMessage() {
return "Flight number should start with 2 letters and trail with 1-4 numbers.";
}

//@@author shengiv
Expand Down
2 changes: 1 addition & 1 deletion text-ui-test/EXPECTED.TXT
Original file line number Diff line number Diff line change
Expand Up @@ -106,7 +106,7 @@ You did not input an operation.
The system is unable to read your command, please try again.

+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
Error: Flight number should start with 2 letters and trail with 1-4 numbers.
Flight number should start with 2 letters and trail with 1-4 numbers.

+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
Thank you, come again! :)
Expand Down

0 comments on commit ee7156e

Please sign in to comment.