Skip to content

Commit

Permalink
Add Command classes for encapsulation
Browse files Browse the repository at this point in the history
  • Loading branch information
tan-zhuoyao committed Aug 25, 2020
1 parent 97ca55c commit af46f55
Show file tree
Hide file tree
Showing 11 changed files with 258 additions and 87 deletions.
Binary file modified data/duke.ser
Binary file not shown.
58 changes: 58 additions & 0 deletions src/main/java/duke/Parser.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
package duke;

import duke.command.*;

public class Parser {
public static Command parse(String command, TaskList list, Storage storage) {
String hor_line = "____________________________________\n";
Command c = new Command();
try{
if (command.equals("bye")) {
c = new ByeCommand(command, list, storage);
} else if (command.equals("list")) {
c = new ListCommand(command, list, storage);
} else if (command.startsWith("delete")) {
if (command.equals("delete")) {
throw new ResponseException(hor_line + "☹ AIYO!!! Please state which task to delete la... \n" +
hor_line);
} else {
c = new DeleteCommand(command, list, storage);
}
} else if (command.startsWith("done")) {
if (command.equals("done")) {
throw new ResponseException(hor_line + "☹ AIYO!!! Please state which task is done la... \n" +
hor_line);
} else {
c = new DoneCommand(command, list, storage);
}
} else if (command.startsWith("todo")) {
if (command.equals("todo")) {
throw new ResponseException(hor_line + "☹ AIYOYO!!! The description of a todo cannot be empty la... \n"
+ hor_line);
} else {
c = new TodoCommand(command, list, storage);
}
} else if (command.startsWith("deadline")) {
if (command.equals("deadline")) {
throw new ResponseException(hor_line +
"☹ AIYO!!! The description of a deadline cannot be empty la... \n" + hor_line);
} else {
c = new DeadlineCommand(command, list, storage);
}
} else if (command.startsWith("event")) {
if (command.equals("event")) {
throw new ResponseException(hor_line + "☹ AIYOYO!!! The description of a event cannot be empty la... \n"
+ hor_line);
} else {
c = new EventCommand(command, list, storage);
}
} else {
throw new ResponseException(hor_line + "☹ AIYO!!! What do you mean sia? :-( \n"
+ hor_line);
}
} catch (ResponseException ex) {
System.out.println(ex.getMessage());
}
return c;
}
}
95 changes: 8 additions & 87 deletions src/main/java/duke/Ui.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
package duke;

import duke.command.Command;

import java.util.List;
import java.util.Scanner;

Expand Down Expand Up @@ -28,95 +30,14 @@ public void initialise(TaskList list, Storage storage) throws Exception {
String hor_line = "____________________________________\n";
List<Task> todo_list = list.getList();
showWelcome();
int counter = todo_list.size();
boolean isExit = false;
Scanner sc = new Scanner(System.in);
while (sc.hasNextLine()) {
while (!isExit) {
String command = sc.nextLine();
try {
if (command.equals("bye")) {
System.out.println(hor_line + "Bye bye. See you soon bro!\n" + hor_line);
sc.close();
break;
} else if (command.equals("list")) {
System.out.println(hor_line + "Here are the things you need to do lor: \n");
System.out.println(list.iterateToDo() + hor_line);
} else if (command.startsWith("delete")) {
if (command.equals("delete")) {
throw new ResponseException(hor_line + "☹ AIYO!!! Please state which task to delete la... \n" +
hor_line);
} else {
String index = command.substring(command.length() - 1);
int number = Integer.parseInt(index);
System.out.println(hor_line + "Task deleted liao: \n" + todo_list.get(number - 1).toString() + "\n" +
"You got " + Integer.toString(counter - 1) + " tasks left. \n" + hor_line);
list.deleteTask(number);
storage.writeData(todo_list);
counter --;
}
} else if (command.startsWith("done")) {
if (command.equals("done")) {
throw new ResponseException(hor_line + "☹ AIYO!!! Please state which task is done la... \n" +
hor_line);
} else {
String index = command.substring(command.length() - 1);
int number = Integer.parseInt(index) - 1;
todo_list.set(number, todo_list.get(number).markDone());
System.out.println(hor_line + "Swee! Now I will mark this as done: \n" +
todo_list.get(number).toString() + "\n" + hor_line);
storage.writeData(todo_list);
}
} else if (command.startsWith("todo")) {
if (command.equals("todo")) {
throw new ResponseException(hor_line + "☹ AIYOYO!!! The description of a todo cannot be empty la... \n"
+ hor_line);
} else {
String instructions = command.substring(5);
list.addTask(counter, new Todo(false, counter + 1, instructions));
System.out.println(hor_line + "Okok. I add for you: \n" +
todo_list.get(counter).toString() + "\n" +
"You got " + Integer.toString(counter + 1) + " tasks in the list.\n" + hor_line);
storage.writeData(todo_list);
counter++;
}
} else if (command.startsWith("deadline")) {
if (command.equals("deadline")) {
throw new ResponseException(hor_line +
"☹ AIYO!!! The description of a deadline cannot be empty la... \n" + hor_line);
} else {
String instructions = command.substring(9);
String[] arr = instructions.split("/by");
instructions = arr[0].substring(0, arr[0].length() - 1);
String date = arr[1].substring(1);
list.addTask(counter, new Deadline(false, counter + 1, instructions, date));
System.out.println(hor_line + "Okok. I help you add this task: \n" +
todo_list.get(counter).toString() + "\n" + "You got " +
Integer.toString(counter + 1) + " tasks in the list.\n" + hor_line);
storage.writeData(todo_list);
counter++;
}
} else if (command.startsWith("event")) {
if (command.equals("event")) {
throw new ResponseException(hor_line + "☹ AIYOYO!!! The description of a event cannot be empty la... \n"
+ hor_line);
} else {
String instructions = command.substring(5);
String[] arr = instructions.split("/at");
instructions = arr[0].substring(0, arr[0].length() - 1);
String time = arr[1].substring(1);
list.addTask(counter, new Event(false, counter + 1, instructions, time));
System.out.println(hor_line + "Okay. I've added this task: \n"
+ todo_list.get(counter).toString() + "\n" + "You got " +
Integer.toString(counter + 1) + " tasks in the list.\n" + hor_line);
storage.writeData(todo_list);
counter++;
}
}else {
throw new ResponseException(hor_line + "☹ AIYO!!! What do you mean sia? :-( \n"
+ hor_line);
}
} catch (ResponseException exception){
System.out.println(exception.getMessage());
}
// parses into a command
Command c = Parser.parse(command, list, storage);
c.execute(command, list, storage);
isExit = c.isExit();
}
}
}
21 changes: 21 additions & 0 deletions src/main/java/duke/command/ByeCommand.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
package duke.command;

import duke.Storage;
import duke.TaskList;

public class ByeCommand extends Command {
public ByeCommand(String command, TaskList list, Storage storage) {
super(command, list, storage);
}

@Override
public void execute(String command, TaskList list, Storage storage) {
String hor_line = "____________________________________\n";
System.out.println(hor_line + "Bye bye. See you soon bro!\n" + hor_line);
}

@Override
public boolean isExit() {
return true;
}
}
29 changes: 29 additions & 0 deletions src/main/java/duke/command/Command.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
package duke.command;

import duke.Storage;
import duke.TaskList;

public class Command {
protected String command;
protected TaskList list;
protected Storage storage;

public Command(String command, TaskList list, Storage storage) {
this.command = command;
this.list = list;
this.storage = storage;
}
// empty command
public Command() {
this.command = null;
this.list = null;
this.storage = null;
}

public void execute(String command, TaskList list, Storage storage) {
}

public boolean isExit() {
return false;
}
}
28 changes: 28 additions & 0 deletions src/main/java/duke/command/DeadlineCommand.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
package duke.command;

import duke.Deadline;
import duke.Storage;
import duke.TaskList;
import duke.Todo;

public class DeadlineCommand extends Command {
public DeadlineCommand(String command, TaskList list, Storage storage) {
super(command, list, storage);
}

@Override
public void execute(String command, TaskList list, Storage storage) {
String hor_line = "____________________________________\n";
String instructions = command.substring(9);
String[] arr = instructions.split("/by");
instructions = arr[0].substring(0, arr[0].length() - 1);
String date = arr[1].substring(1);
int counter = list.getList().size();
list.addTask(counter, new Deadline(false, counter + 1, instructions, date));
String taskMessage = list.getList().get(counter).toString();
System.out.println(hor_line + "Okok. I help you add this task: \n" +
taskMessage + "\n" + "You got " +
(counter + 1) + " tasks in the list.\n" + hor_line);
storage.writeData(list.getList());
}
}
22 changes: 22 additions & 0 deletions src/main/java/duke/command/DeleteCommand.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
package duke.command;

import duke.Storage;
import duke.TaskList;

public class DeleteCommand extends Command {
public DeleteCommand(String command, TaskList list, Storage storage) {
super(command, list, storage);
}

@Override
public void execute(String command, TaskList list, Storage storage) {
String hor_line = "____________________________________\n";
String index = command.substring(command.length() - 1);
int number = Integer.parseInt(index);
String taskMessage = list.getList().get(number - 1).toString();
list.deleteTask(number);
System.out.println(hor_line + "Task deleted liao: \n" + taskMessage + "\n" +
"You got " + list.getList().size() + " tasks left. \n" + hor_line);
storage.writeData(list.getList());
}
}
22 changes: 22 additions & 0 deletions src/main/java/duke/command/DoneCommand.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
package duke.command;

import duke.Storage;
import duke.TaskList;

public class DoneCommand extends Command {
public DoneCommand(String command, TaskList list, Storage storage) {
super(command, list, storage);
}

@Override
public void execute(String command, TaskList list, Storage storage) {
String hor_line = "____________________________________\n";
String index = command.substring(command.length() - 1);
int number = Integer.parseInt(index) - 1;
list.getList().set(number, list.getList().get(number).markDone());
String taskMessage = list.getList().get(number).toString();
System.out.println(hor_line + "Swee! Now I will mark this as done: \n" +
taskMessage + "\n" + hor_line);
storage.writeData(list.getList());
}
}
28 changes: 28 additions & 0 deletions src/main/java/duke/command/EventCommand.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
package duke.command;

import duke.Deadline;
import duke.Event;
import duke.Storage;
import duke.TaskList;

public class EventCommand extends Command {
public EventCommand(String command, TaskList list, Storage storage) {
super(command, list, storage);
}

@Override
public void execute(String command, TaskList list, Storage storage) {
String hor_line = "____________________________________\n";
String instructions = command.substring(9);
String[] arr = instructions.split("/at");
instructions = arr[0].substring(0, arr[0].length() - 1);
String date = arr[1].substring(1);
int counter = list.getList().size();
list.addTask(counter, new Event(false, counter + 1, instructions, date));
String taskMessage = list.getList().get(counter).toString();
System.out.println(hor_line + "Okok. I help you add this task: \n" +
taskMessage + "\n" + "You got " +
(counter + 1) + " tasks in the list.\n" + hor_line);
storage.writeData(list.getList());
}
}
17 changes: 17 additions & 0 deletions src/main/java/duke/command/ListCommand.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
package duke.command;

import duke.Storage;
import duke.TaskList;

public class ListCommand extends Command {
public ListCommand(String command, TaskList list, Storage storage) {
super(command, list, storage);
}

@Override
public void execute(String command, TaskList list, Storage storage) {
String hor_line = "____________________________________\n";
System.out.println(hor_line + "Here are the things you need to do lor: \n");
System.out.println(list.iterateToDo() + hor_line);
}
}
25 changes: 25 additions & 0 deletions src/main/java/duke/command/TodoCommand.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
package duke.command;

import duke.Storage;
import duke.TaskList;
import duke.Todo;

public class TodoCommand extends Command {
public TodoCommand(String command, TaskList list, Storage storage) {
super(command, list, storage);
}

@Override
public void execute(String command, TaskList list, Storage storage) {
String hor_line = "____________________________________\n";
String instructions = command.substring(5);
int counter = list.getList().size();
list.addTask(counter, new Todo(false, counter + 1, instructions));
String taskMessage = list.getList().get(counter).toString();
System.out.println(hor_line + "Okok. I add for you: \n" +
taskMessage + "\n" +
"You got " + (counter + 1) + " tasks in the list.\n" + hor_line);
storage.writeData(list.getList());
counter++;
}
}

0 comments on commit af46f55

Please sign in to comment.