Skip to content

Commit

Permalink
Merge pull request #36 from daryltay415/master
Browse files Browse the repository at this point in the history
Add new exceptions
  • Loading branch information
daryltay415 committed Mar 17, 2024
2 parents 2294257 + 63537ea commit 87ed3d1
Show file tree
Hide file tree
Showing 5 changed files with 74 additions and 56 deletions.
80 changes: 34 additions & 46 deletions src/main/java/seedu/duke/Duke.java
Original file line number Diff line number Diff line change
@@ -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();
}



}
Expand Down
16 changes: 15 additions & 1 deletion src/main/java/seedu/duke/Parser.java
Original file line number Diff line number Diff line change
@@ -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();
Expand All @@ -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 {
Expand Down
14 changes: 9 additions & 5 deletions src/main/java/seedu/duke/TravelActivityList.java
Original file line number Diff line number Diff line change
Expand Up @@ -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!";
}

/**
Expand All @@ -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";
}

/**
Expand All @@ -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);
Expand All @@ -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!";
}


Expand All @@ -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());
Expand Down
16 changes: 14 additions & 2 deletions src/main/java/seedu/duke/Ui.java
Original file line number Diff line number Diff line change
@@ -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
Expand All @@ -9,7 +11,7 @@ public class Ui {
/**
* Prints the greetings
*/
public void printGreeting() {
public static void printGreeting() {
printLine();
System.out.println(" ____ _ _ _ _____ ____ ____ _ _____ _ \n" +
"/ _ \\/ \\__/|/ \\ /|/ \\/__ __\\/ __\\/ _ \\/ \\ |\\/ __// \\ \n" +
Expand All @@ -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!");
Expand All @@ -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());
}




Expand Down
4 changes: 2 additions & 2 deletions src/test/java/seedu/duke/DukeTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -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"));
Expand All @@ -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"));
Expand Down

0 comments on commit 87ed3d1

Please sign in to comment.