Skip to content

Commit

Permalink
Merge pull request #78 from ivanderjmw/Ivander-NewTripExceptions
Browse files Browse the repository at this point in the history
More Error Handling on New Trip
  • Loading branch information
ivanderjmw authored Oct 27, 2020
2 parents a565585 + fa27b8a commit 910f2aa
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 9 deletions.
3 changes: 2 additions & 1 deletion src/main/java/seedu/trippie/Trippie.java
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package seedu.trippie;

import seedu.trippie.command.Command;
import seedu.trippie.command.ExitCommand;
import seedu.trippie.command.NewTripCommand;
import seedu.trippie.data.TrippieData;

Expand Down Expand Up @@ -39,7 +40,7 @@ public void run() {
Command c = parseCommand();

if (isFirstRun) {
if (!(c instanceof NewTripCommand)) {
if (!(c instanceof NewTripCommand) && !(c instanceof ExitCommand)) {
c = promptNewTripCommand();
}
isFirstRun = false;
Expand Down
26 changes: 18 additions & 8 deletions src/main/java/seedu/trippie/command/NewTripCommand.java
Original file line number Diff line number Diff line change
Expand Up @@ -17,11 +17,18 @@ public class NewTripCommand extends Command {
private boolean isFilenameValid(String file) {
Pattern p = Pattern.compile("[<>:\"/\\\\|?*]");

if (p.matcher(file).find()) {
return false;
} else {
return true;
return !p.matcher(file).find();
}

// Implemented with reference to
// https://www.rgagnon.com/javadetails/java-check-if-a-filename-is-valid.html
private boolean doesTripNameExist(TrippieData trippieData, String name) {
for (Trip trip: trippieData.getTripList()) {
if (trip.getName().equals(name)) {
return true;
}
}
return false;
}

public NewTripCommand() {
Expand All @@ -40,7 +47,10 @@ public void execute(Ui ui, TrippieData trippieData) {
name = ui.getLine();

if (!isFilenameValid(name)) {
System.out.println("New trip should not contain invalid characters like <>:\"/\\|?*");
System.out.println("New trip should not contain invalid characters like <>:\"/\\|?*!");
name = null;
} else if (doesTripNameExist(trippieData, name)) {
System.out.println("A trip with that name already exists!");
name = null;
}

Expand Down Expand Up @@ -78,13 +88,13 @@ public void execute(Ui ui, TrippieData trippieData) {
}

} catch (NumberFormatException e) {
System.out.println("Give a valid exchange rate (in decimals)");
System.out.println("Give a valid exchange rate (in decimals)!");
}
} while (forEx == null);


// Get currency abbreviation
String currencyAbbreviation = null;
String currencyAbbreviation;
do {
System.out.print("Enter the foreign currency abbreviation (eg. MYR):");
currencyAbbreviation = ui.getLine();
Expand All @@ -104,7 +114,7 @@ public void execute(Ui ui, TrippieData trippieData) {
}

} catch (NumberFormatException e) {
System.out.println("Give a valid budget value (in decimals)");
System.out.println("Give a valid budget value (in decimals)!");
}
} while (budget == null);

Expand Down

0 comments on commit 910f2aa

Please sign in to comment.