diff --git a/src/main/java/seedu/duke/Duke.java b/src/main/java/seedu/duke/Duke.java index ce2af00477..3f35f32f1d 100644 --- a/src/main/java/seedu/duke/Duke.java +++ b/src/main/java/seedu/duke/Duke.java @@ -1,76 +1,64 @@ package seedu.duke; +import java.util.NoSuchElementException; import java.util.Scanner; public class Duke { - private Ui ui = new Ui(); public static void main(String[] args) { - new Duke().runBot(); + Ui.printGreeting(); boolean userSaysBye = false; TravelActivityList list = new TravelActivityList(); String line; Scanner in = new Scanner(System.in); while (!userSaysBye) { - line = in.nextLine(); - String[] command = line.split(" "); + try { + line = in.nextLine(); + String[] command = line.split(" "); - switch (command[0].toLowerCase()) { - case "list": - Ui.printLine(); - Parser.getList(list); - Ui.printLine(); - break; - case "add": - try { + switch (command[0].toLowerCase()) { + case "list": Ui.printLine(); - Parser.addCommand(line, command, list); + Parser.getList(list); Ui.printLine(); - } - catch (OmniException exception){ + break; + + case "add": Ui.printLine(); - System.out.println("Warning! " + exception.getMessage()); - } - break; - case "delete": - try { + Parser.addCommand(line, command, list); Ui.printLine(); - Parser.deleteCommand(command, list); + break; + + case "delete": Ui.printLine(); - } - catch (OmniException exception){ + Parser.deleteCommand(command, list); Ui.printLine(); - System.out.println("Warning! " + exception.getMessage()); - } - break; - case "find": - try { + break; + + case "find": Ui.printLine(); Parser.findCommand(command, list); Ui.printLine(); - } - catch (OmniException exception){ + break; + + case "bye": + Ui.printBye(); + userSaysBye = true; + break; + + default: + Ui.printLine(); + System.out.println("This is not a valid command"); Ui.printLine(); - System.out.println("Warning! " + exception.getMessage()); } - break; - case "bye": - Ui.printLine(); - System.out.println("Hope to see you again soon. Bye!"); - Ui.printLine(); - userSaysBye = true; - break; - default: - Ui.printLine(); - System.out.println("This is not a valid command"); - Ui.printLine(); - + } catch (OmniException exception){ + Ui.printException(exception); + } catch (NoSuchElementException exception){ + Ui.printNoSuchElementException(exception); } } } - public void runBot(){ - ui.printGreeting(); - } + } diff --git a/src/main/java/seedu/duke/Parser.java b/src/main/java/seedu/duke/Parser.java index e242ebe171..f276076d06 100644 --- a/src/main/java/seedu/duke/Parser.java +++ b/src/main/java/seedu/duke/Parser.java @@ -1,6 +1,20 @@ package seedu.duke; public class Parser { + /** + * Checks if the string is a number + * @param str The string that is to be defined as a number or sentence + * @return true or false + */ + public static boolean isNumeric(String str) { + try { + Double.parseDouble(str); + return true; + } catch(NumberFormatException e){ + return false; + } + } + public static void getList(TravelActivityList list){ System.out.println("Here are the travel activities in your list:"); list.listTravelActivities(); @@ -18,7 +32,7 @@ public static void addCommand(String line, String[] command, TravelActivityList } public static void deleteCommand(String[] command, TravelActivityList list) throws OmniException { - if (command.length == 2){ + if (command.length == 2 && isNumeric(command[1])){ int listNumber = Integer.parseInt(command[1]); list.removeTravelActivity(listNumber); } else { diff --git a/src/main/java/seedu/duke/TravelActivityList.java b/src/main/java/seedu/duke/TravelActivityList.java index c53879ed84..b1a9d92256 100644 --- a/src/main/java/seedu/duke/TravelActivityList.java +++ b/src/main/java/seedu/duke/TravelActivityList.java @@ -20,7 +20,7 @@ public void addTravelActivity(TravelActivity travelActivity){ travelActivities.add(travelActivity); noOfTasks += 1; int newSize = noOfTasks; - assert newSize != initialListSize + 1 : "There is an error with list size!"; + assert newSize == initialListSize + 1 : "There is an error with list size!"; } /** @@ -35,6 +35,8 @@ public void listTravelActivities(){ taskCount++; System.out.println(" " + taskCount +"." + task); } + int finalTaskCount = noOfTasks; + assert finalTaskCount == taskCount : "Index out of bounds while listing activities"; } /** @@ -49,7 +51,10 @@ public int getNoOfTravelActivities(){ * Removes travel activity from the travel activity list * @param taskNumber The travel activity number on the list */ - public void removeTravelActivity(int taskNumber){ + public void removeTravelActivity(int taskNumber) throws OmniException{ + if(taskNumber > travelActivities.size()){ + throw new OmniException("Travel activity cannot be found!"); + } int indexOfTask = taskNumber - 1; int initialListSize = noOfTasks; TravelActivity removedTask = travelActivities.get(indexOfTask); @@ -58,7 +63,7 @@ public void removeTravelActivity(int taskNumber){ System.out.println(removedTask); noOfTasks -=1; int newSize = noOfTasks; - assert newSize != initialListSize - 1 : "There is an error with list size!"; + assert newSize == initialListSize - 1 : "There is an error with list size!"; } @@ -83,8 +88,7 @@ public void searchKeyword (String taskName) { } if (temporaryArrayCounter == 0) { System.out.println("Sorry I could not find what you are looking for."); - } - else { + } else { System.out.println("Here are what you are looking for:"); for (int newIterator = 0; newIterator < temporaryArray.size(); newIterator += 1) { System.out.println((newIterator + 1) + ". " + temporaryArray.get(newIterator).getPlan()); diff --git a/src/main/java/seedu/duke/Ui.java b/src/main/java/seedu/duke/Ui.java index beb18ef17b..f486bcf2d5 100644 --- a/src/main/java/seedu/duke/Ui.java +++ b/src/main/java/seedu/duke/Ui.java @@ -1,6 +1,8 @@ package seedu.duke; +import java.util.NoSuchElementException; + /** * Represents the user interface of the Omnitravel bot * It contains all the responses of the Omnitravel bot to the user's commands @@ -9,7 +11,7 @@ public class Ui { /** * Prints the greetings */ - public void printGreeting() { + public static void printGreeting() { printLine(); System.out.println(" ____ _ _ _ _____ ____ ____ _ _____ _ \n" + "/ _ \\/ \\__/|/ \\ /|/ \\/__ __\\/ __\\/ _ \\/ \\ |\\/ __// \\ \n" + @@ -24,7 +26,7 @@ public void printGreeting() { /** * Prints the farewell greetings */ - public void printBye(){ + public static void printBye(){ printLine(); System.out.println("Thank you for using Omnitravel"); System.out.println("We hope to see you again! Goodbye!"); @@ -35,6 +37,16 @@ public static void printLine(){ System.out.println("____________________________________________________________"); } + public static void printException(OmniException exception){ + printLine(); + System.out.println("Warning! " + exception.getMessage()); + } + + public static void printNoSuchElementException(NoSuchElementException exception){ + printLine(); + System.out.println("Warning! " + exception.getMessage()); + } + diff --git a/src/test/java/seedu/duke/DukeTest.java b/src/test/java/seedu/duke/DukeTest.java index 8efa3408b0..5cc60213c6 100644 --- a/src/test/java/seedu/duke/DukeTest.java +++ b/src/test/java/seedu/duke/DukeTest.java @@ -15,7 +15,7 @@ public void addTest() { } @Test - public void deleteTest() { + public void deleteTest() throws OmniException { //add the plan TravelActivityList travelActivityList = new TravelActivityList(); travelActivityList.addTravelActivity(new TravelActivity("visit museum")); @@ -26,7 +26,7 @@ public void deleteTest() { } @Test - public void getNoTasksTest() { + public void getNoTasksTest() throws OmniException { //add the first plan TravelActivityList travelActivityList = new TravelActivityList(); travelActivityList.addTravelActivity(new TravelActivity("visit museum"));