Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add parser and exceptions #33

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
81 changes: 52 additions & 29 deletions src/main/java/seedu/duke/Duke.java
Original file line number Diff line number Diff line change
Expand Up @@ -6,45 +6,68 @@ public class Duke {

private Ui ui = new Ui();
public static void main(String[] args) {

new Duke().runBot();
boolean userSaysBye = false;
TravelActivityList travelActivityList = new TravelActivityList();
TravelActivityList list = new TravelActivityList();
String line;
Scanner in = new Scanner(System.in);
while (!userSaysBye) {
line = in.nextLine();
if (line.equals("list")) {
// Prints the all the tasks in the list
System.out.println("Here are the travel activities in your list:");
travelActivityList.listTravelActivities();
} else if (line.startsWith("add")){
// Adds a travel activity into the list
String[] sentence = line.split(" ");
// Checks if the description of the task is empty
TravelActivity newTask = new TravelActivity(sentence[1]);
travelActivityList.addTravelActivity(newTask);
System.out.println("I added a new travel activity");
System.out.println(newTask);

} else if(line.startsWith("delete")){
// Deletes the task in the list
String[] sentence = line.split(" ");
// Checks if the description of the task is empty or non-numerical

int taskNumber = Integer.parseInt(sentence[1]);
travelActivityList.removeTravelActivity(taskNumber);
} else if(line.startsWith("find")) {
String[] taskName = line.split(" ");
travelActivityList.searchTask(taskName[1]);
}
else if(line.startsWith("bye")){
String[] command = line.split(" ");

switch (command[0].toLowerCase()) {
case "list":
Ui.printLine();
Parser.getList(list);
Ui.printLine();
break;
case "add":
try {
Ui.printLine();
Parser.addCommand(line, command, list);
Ui.printLine();
}
catch (OmniException exception){
Ui.printLine();
System.out.println("Warning! " + exception.getMessage());
}
break;
case "delete":
try {
Ui.printLine();
Parser.deleteCommand(command, list);
Ui.printLine();
}
catch (OmniException exception){
Ui.printLine();
System.out.println("Warning! " + exception.getMessage());
}
break;
case "find":
try {
Ui.printLine();
Parser.findCommand(command, list);
Ui.printLine();
}
catch (OmniException exception){
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();
Comment on lines +16 to +60

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nice catching of exceptions

userSaysBye = true;
}
break;
default:
Ui.printLine();
System.out.println("This is not a valid command");
Ui.printLine();

}
}
}

public void runBot(){
ui.printGreeting();
}
Expand Down
7 changes: 7 additions & 0 deletions src/main/java/seedu/duke/OmniException.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
package seedu.duke;

public class OmniException extends Exception {
public OmniException(String errorMessage){
super(errorMessage);
}
}
37 changes: 37 additions & 0 deletions src/main/java/seedu/duke/Parser.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
package seedu.duke;

public class Parser {
public static void getList(TravelActivityList list){
System.out.println("Here are the travel activities in your list:");
list.listTravelActivities();
}

public static void addCommand(String line, String[] command, TravelActivityList list) throws OmniException{
if (command.length >= 2) {
TravelActivity newActivity = new TravelActivity(line.substring(4));
list.addTravelActivity(newActivity);
System.out.println("I added a new travel activity");
System.out.println(newActivity);
} else {
throw new OmniException("The description of add cannot be empty!");
}
}

public static void deleteCommand(String[] command, TravelActivityList list) throws OmniException {
if (command.length == 2){
int listNumber = Integer.parseInt(command[1]);
list.removeTravelActivity(listNumber);
} else {
throw new OmniException("Please specify which task to delete");
}
}

public static void findCommand(String[] command, TravelActivityList list) throws OmniException{
if (command.length == 2) {
String keyword = command[1];
list.searchKeyword(keyword);
} else {
throw new OmniException("Please specify which keyword you want to find!");
}
}
}
Comment on lines +1 to +37

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM

2 changes: 1 addition & 1 deletion src/main/java/seedu/duke/TravelActivityList.java
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ public String getDescription(String plan){
return "cant be found";
}

public void searchTask (String taskName) {
public void searchKeyword (String taskName) {
ArrayList<TravelActivity> temporaryArray = new ArrayList<TravelActivity>();;
int temporaryArrayCounter = 0;
boolean isFound = false;
Expand Down
6 changes: 5 additions & 1 deletion src/main/java/seedu/duke/Ui.java
Original file line number Diff line number Diff line change
Expand Up @@ -10,24 +10,28 @@ public class Ui {
* Prints the greetings
*/
public void printGreeting() {
printLine();
System.out.println(" ____ _ _ _ _____ ____ ____ _ _____ _ \n" +
"/ _ \\/ \\__/|/ \\ /|/ \\/__ __\\/ __\\/ _ \\/ \\ |\\/ __// \\ \n" +
"| / \\|| |\\/||| |\\ ||| | / \\ | \\/|| / \\|| | //| \\ | | \n" +
"| \\_/|| | ||| | \\||| | | | | /| |-||| \\// | /_ | |_/\\\n" +
"\\____/\\_/ \\|\\_/ \\|\\_/ \\_/ \\_/\\_\\\\_/ \\|\\__/ \\____\\\\____/)");
System.out.println("Hello");
System.out.println("How may I assist you?");
printLine();
}

/**
* Prints the farewell greetings
*/
public void printBye(){
printLine();
System.out.println("Thank you for using Omnitravel");
System.out.println("We hope to see you again! Goodbye!");
printLine();
}

public void printLine(){
public static void printLine(){
System.out.println("____________________________________________________________");
}

Expand Down
18 changes: 9 additions & 9 deletions text-ui-test/EXPECTED.TXT
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
Hello from
____ _
| _ \ _ _| | _____
| | | | | | | |/ / _ \
| |_| | |_| | < __/
|____/ \__,_|_|\_\___|

What is your name?
Hello James Gosling
____________________________________________________________
____ _ _ _ _____ ____ ____ _ _____ _
/ _ \/ \__/|/ \ /|/ \/__ __\/ __\/ _ \/ \ |\/ __// \
| / \|| |\/||| |\ ||| | / \ | \/|| / \|| | //| \ | |
| \_/|| | ||| | \||| | | | | /| |-||| \// | /_ | |_/\
\____/\_/ \|\_/ \|\_/ \_/ \_/\_\\_/ \|\__/ \____\\____/)
Hello
How may I assist you?
____________________________________________________________
1 change: 0 additions & 1 deletion text-ui-test/input.txt
Original file line number Diff line number Diff line change
@@ -1 +0,0 @@
James Gosling
6 changes: 3 additions & 3 deletions text-ui-test/runtest.sh
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,9 @@ cd text-ui-test

java -jar $(find ../build/libs/ -mindepth 1 -print -quit) < input.txt > ACTUAL.TXT

cp EXPECTED.TXT EXPECTED-UNIX.TXT
dos2unix EXPECTED-UNIX.TXT ACTUAL.TXT
diff EXPECTED-UNIX.TXT ACTUAL.TXT
#cp EXPECTED.TXT EXPECTED-UNIX.TXT
#dos2unix EXPECTED-UNIX.TXT ACTUAL.TXT
diff EXPECTED.TXT ACTUAL.TXT
if [ $? -eq 0 ]
then
echo "Test passed!"
Expand Down
Loading