forked from nus-cs2103-AY2021S1/ip
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add Command classes for encapsulation
- Loading branch information
1 parent
97ca55c
commit af46f55
Showing
11 changed files
with
258 additions
and
87 deletions.
There are no files selected for viewing
Binary file not shown.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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()); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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()); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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()); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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()); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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++; | ||
} | ||
} |