Skip to content

Commit

Permalink
Update Parser class and Byecommand class
Browse files Browse the repository at this point in the history
  • Loading branch information
Royxuzeng committed Oct 13, 2020
1 parent c9aa251 commit 97f8035
Show file tree
Hide file tree
Showing 2 changed files with 105 additions and 75 deletions.
178 changes: 103 additions & 75 deletions src/main/java/Parser.java
Original file line number Diff line number Diff line change
Expand Up @@ -30,87 +30,115 @@ public static Command parse(String commandMessage) throws DukeException {
int priority = parseInt(message.substring(message.indexOf(' ') + 1));
command = new PriorityCommand("priority", order, priority);
} else {
String type;
if (commandMessage.contains(" ")) {
type = commandMessage.substring(0, commandMessage.indexOf(' '));
command = parseTask(commandMessage);
}
return command;

}

public static Command parseTask(String commandMessage) throws DukeException {
String type;
Command command;
if (commandMessage.contains(" ")) {
type = commandMessage.substring(0, commandMessage.indexOf(' '));
} else {
String str = "";
switch (commandMessage) {
case "todo":
str = "☹ OOPS!!! The description of a todo cannot be empty.";
break;
case "deadline":
str = "☹ OOPS!!! The description of a deadline cannot be empty.";
break;
case "event":
str = "☹ OOPS!!! The description of an event cannot be empty.";
break;
default:
str = "☹ OOPS!!! I'm sorry, but I don't know what that means :-(";
}
throw new DukeException(str);
}

if (type.equals("deadline")) {
command = parseDeadline(commandMessage);
} else if (type.equals("event")) {
command = parseEvent(commandMessage);
} else if (type.equals("todo")) {
command = parseTodo(commandMessage);
} else {
throw new DukeException("☹ OOPS!!! I'm sorry, but I don't know what that means :-(");
}

return command;
}

public static Command parseDeadline(String commandMessage) throws DukeException {
Command command;
String description = commandMessage.substring(commandMessage.indexOf(' ') + 1,
commandMessage.indexOf('/') - 1);
String by = commandMessage.substring(commandMessage.indexOf("/by") + 4);

String s = "";

if (by.charAt(0) >= '0' && by.charAt(0) <= '9') {
if (by.charAt(1) == '/') {
s = s + by.substring(5, 9);
s = s + '-';
s = s + by.substring(2, 4);
s = s + '-';
s = s + '0' + by.substring(0, 1);
} else {
String str = "";
switch (commandMessage) {
case "todo":
str = "☹ OOPS!!! The description of a todo cannot be empty.";
break;
case "deadline":
str = "☹ OOPS!!! The description of a deadline cannot be empty.";
break;
case "event":
str = "☹ OOPS!!! The description of an event cannot be empty.";
break;
default:
str = "☹ OOPS!!! I'm sorry, but I don't know what that means :-(";
}
throw new DukeException(str);
s = s + by.substring(6, 10);
s = s + '-';
s = s + by.substring(3, 5);
s = s + '-';
s = s + '0' + by.substring(0, 2);
}
LocalDate date = LocalDate.parse(s);
command = new DeadlineCommand("deadline", description, by, date, true);
} else {
command = new DeadlineCommand("deadline", description, by, null, false);
}

return command;
}

public static Command parseEvent(String commandMessage) throws DukeException {
Command command;
String time = commandMessage.substring(commandMessage.indexOf("/at") + 4);
String description = commandMessage.substring(commandMessage.indexOf(' ') + 1,
commandMessage.indexOf('/') - 1);

if (type.equals("deadline")) {
String description = commandMessage.substring(commandMessage.indexOf(' ') + 1,
commandMessage.indexOf('/') - 1);
String by = commandMessage.substring(commandMessage.indexOf("/by") + 4);

String s = "";

if (by.charAt(0) >= '0' && by.charAt(0) <= '9') {
if (by.charAt(1) == '/') {
s = s + by.substring(5, 9);
s = s + '-';
s = s + by.substring(2, 4);
s = s + '-';
s = s + '0' + by.substring(0, 1);
} else {
s = s + by.substring(6, 10);
s = s + '-';
s = s + by.substring(3, 5);
s = s + '-';
s = s + '0' + by.substring(0, 2);
}
LocalDate date = LocalDate.parse(s);
command = new DeadlineCommand("deadline", description, by, date, true);
} else {
command = new DeadlineCommand("deadline", description, by, null, false);
}

} else if (type.equals("event")) {
String time = commandMessage.substring(commandMessage.indexOf("/at") + 4);
String description = commandMessage.substring(commandMessage.indexOf(' ') + 1,
commandMessage.indexOf('/') - 1);

String s = "";

if (time.charAt(0) >= '0' && time.charAt(0) <= '9') {
if (time.charAt(1) == '/') {
s = s + time.substring(5, 9);
s = s + '-';
s = s + time.substring(2, 4);
s = s + '-';
s = s + '0' + time.substring(0, 1);
} else {
s = s + time.substring(6, 10);
s = s + '-';
s = s + time.substring(3, 5);
s = s + '-';
s = s + '0' + time.substring(0, 2);
}
LocalDate date = LocalDate.parse(s);
command = new EventCommand("event", description, time, date, true);
} else {
command = new EventCommand("event", description, time, null, false);
}
} else if (type.equals("todo")) {
String description = commandMessage.substring(commandMessage.indexOf(' ') + 1);
command = new ToDoCommand("todo", description, "", null, false);
String s = "";

if (time.charAt(0) >= '0' && time.charAt(0) <= '9') {
if (time.charAt(1) == '/') {
s = s + time.substring(5, 9);
s = s + '-';
s = s + time.substring(2, 4);
s = s + '-';
s = s + '0' + time.substring(0, 1);
} else {
throw new DukeException("☹ OOPS!!! I'm sorry, but I don't know what that means :-(");
s = s + time.substring(6, 10);
s = s + '-';
s = s + time.substring(3, 5);
s = s + '-';
s = s + '0' + time.substring(0, 2);
}
LocalDate date = LocalDate.parse(s);
command = new EventCommand("event", description, time, date, true);
} else {
command = new EventCommand("event", description, time, null, false);
}

return command;
}

public static Command parseTodo(String commandMessage) throws DukeException {
Command command;
String description = commandMessage.substring(commandMessage.indexOf(' ') + 1);
command = new ToDoCommand("todo", description, "", null, false);
return command;
}
}

2 changes: 2 additions & 0 deletions src/main/java/command/ByeCommand.java
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,8 @@ public String execute(TaskList tasks, Ui ui, Storage storage) {
isExit = true;
message = message + " Bye. Hope to see you again soon!";

System.exit(0);

return message;
}
}

0 comments on commit 97f8035

Please sign in to comment.