-
Notifications
You must be signed in to change notification settings - Fork 73
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
[Wang Qin] iP #54
base: master
Are you sure you want to change the base?
[Wang Qin] iP #54
Changes from 19 commits
65f72a8
0112efe
cfd6da7
6e6ace1
a3ca5a4
7b60e81
c4678f7
30efbae
a98cf99
f028311
6d67bb1
587ae62
49ef42a
d795532
226493b
4af20dd
d9f4039
5ccd523
d89e3ea
88ccaac
761daa8
bb6ad8a
1934726
d9075cd
b880add
42fcc41
7da099d
023f9de
d12b5ae
8a1d643
1ebb042
3df331f
21ae69e
31521de
b70463e
393ce9a
52a9177
984f0ce
ae1c499
82fb76e
1040e6e
84a81ca
8ea0311
42e31b7
33f3c41
f345eee
1302922
b03f6a6
4a2578d
4a79188
c5796c1
be00e66
c14c810
4b5c6ce
8eb6c95
219a22f
d0b0db4
8319b6b
52e8c83
8221f37
32ff89d
548acb3
3b1aeda
66fff65
8d17754
0240dac
474cc1b
a133f34
2576d5b
74f10b8
8ea2766
b5c89dc
ef546f3
b403940
75fe1d7
b0cd83b
8cb50da
49c8065
fcba7be
103230c
b08601e
c42b4a8
653183d
763b336
b316034
700f419
2aefeae
dffa9ec
2c4b046
63bfbfb
ec733fa
304be96
a77f1c4
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
This file was deleted.
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,56 @@ | ||
import commands.Command; | ||
import commands.ExitCommand; | ||
import data.Duke; | ||
import data.exceptions.StorageOperationException; | ||
import parser.Parser; | ||
import storage.StorageFile; | ||
import ui.TextUi; | ||
import java.util.Scanner; | ||
|
||
|
||
public class Main { | ||
|
||
private StorageFile storage = new StorageFile(); | ||
Duke duke = new Duke(); | ||
|
||
public Main() throws StorageOperationException { | ||
} | ||
//private TextUi ui; | ||
|
||
public static void main(String[] args) throws StorageOperationException { | ||
new Main().run(); | ||
} | ||
|
||
private void run() throws StorageOperationException { | ||
TextUi.showWelcomeMessage(); | ||
runCommandLoopUntilExitCommand(); | ||
} | ||
|
||
private void runCommandLoopUntilExitCommand() throws StorageOperationException { | ||
Command command; | ||
Scanner scanner = new Scanner(System.in); | ||
do { | ||
//read in user input | ||
String userCommandText = scanner.nextLine(); | ||
//parse input from String to command | ||
command = new Parser().parseCommand(userCommandText);; | ||
executeCommand(command); | ||
} while (!ExitCommand.isExit(command)); | ||
} | ||
|
||
|
||
private void executeCommand(Command command) throws StorageOperationException { | ||
try { | ||
//Supplies the data the command will operate on. | ||
//if there is no file to load or the file is empty, setData will instantiate an empty task list | ||
command.setData(duke); | ||
// Execute according to the command itself | ||
command.execute(); | ||
storage.save(duke); | ||
} catch (Exception ex) { | ||
System.out.println(ex); | ||
} | ||
} | ||
} | ||
|
||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
package commands; | ||
|
||
/** | ||
* Clears the Task List. | ||
*/ | ||
public class ClearCommand extends Command { | ||
|
||
public static final String COMMAND_WORD = "clear"; | ||
public static final String MESSAGE_USAGE = "Clears task list permanently.\n" | ||
+ " Example: " + COMMAND_WORD; | ||
|
||
public static final String MESSAGE_SUCCESS = "Task List has been cleared!"; | ||
|
||
@Override | ||
public CommandResult execute() { | ||
duke.clear(); | ||
return new CommandResult(MESSAGE_SUCCESS); | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,52 @@ | ||
package commands; | ||
|
||
import data.Duke; | ||
import data.task.Task; | ||
import ui.TextUi; | ||
|
||
public abstract class Command { | ||
protected Duke duke; | ||
public String COMMAND_WORD; | ||
private int targetIndex = -1; | ||
|
||
//constructor | ||
public Command() { | ||
} | ||
|
||
/** | ||
* @param targetIndex last visible listing index of the target person | ||
*/ | ||
public Command(int targetIndex) { | ||
this.setTargetIndex(targetIndex); | ||
} | ||
|
||
/** | ||
* @param targetIndex last visible listing index of the target person | ||
*/ | ||
public void setTargetIndex(int targetIndex) { | ||
this.targetIndex = targetIndex; | ||
} | ||
|
||
/** | ||
* Supplies the data the command will operate on. | ||
*/ | ||
public void setData(Duke duke) { | ||
this.duke = duke; | ||
} | ||
|
||
/** | ||
* Extracts the the target person in the last shown list from the given arguments. | ||
* | ||
* @throws IndexOutOfBoundsException if the target index is out of bounds of the last viewed listing | ||
*/ | ||
protected Task getTargetTask() throws IndexOutOfBoundsException { | ||
return duke.getTaskList().getInternalList().get(getTargetIndex() - TextUi.DISPLAYED_INDEX_OFFSET); | ||
} | ||
|
||
public int getTargetIndex() { | ||
return targetIndex; | ||
} | ||
|
||
public abstract CommandResult execute () ; | ||
|
||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
package commands; | ||
|
||
import ui.TextUi; | ||
|
||
/** | ||
* Display the execute result, by constructing a CommandResult object, | ||
* parameter: Execute feedback to user | ||
*/ | ||
|
||
public class CommandResult { | ||
|
||
public final String feedbackToUser; | ||
public CommandResult(String feedbackToUser) { | ||
this.feedbackToUser = feedbackToUser; | ||
TextUi.showResult(this.feedbackToUser); | ||
} | ||
|
||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
package commands; | ||
|
||
import common.Messages; | ||
import data.exceptions.TaskNotFoundException; | ||
import data.task.Task; | ||
|
||
/** | ||
* Deletes a task identified using it's last displayed index from the task list. | ||
*/ | ||
public class DeleteCommand extends Command { | ||
|
||
public static final String COMMAND_WORD = "delete"; | ||
|
||
public static final String MESSAGE_USAGE = COMMAND_WORD | ||
+ ": Deletes the task identified by the index number used in the last task listing.\n" | ||
+ " Parameters: INDEX\n" | ||
+ " Example: " + COMMAND_WORD + " 1"; | ||
public static final String MESSAGE_DELETE_TASK_SUCCESS = "Deleted Task: %1$s"; | ||
|
||
public DeleteCommand(int targetVisibleIndex) { | ||
|
||
super(targetVisibleIndex); | ||
// System.out.println("test"); | ||
} | ||
|
||
@Override | ||
public CommandResult execute() { | ||
try { | ||
final Task target = getTargetTask(); | ||
duke.removeTask(target); | ||
return new CommandResult(String.format(MESSAGE_DELETE_TASK_SUCCESS, target.getTaskDescription())); | ||
|
||
} catch (IndexOutOfBoundsException ie) { | ||
return new CommandResult(Messages.MESSAGE_INVALID_TASK_DISPLAYED_INDEX); | ||
} catch (TaskNotFoundException tnfe) { | ||
return new CommandResult(Messages.MESSAGE_TASK_NOT_IN_TASKLIST); | ||
} | ||
} | ||
|
||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
package commands; | ||
|
||
public class DoneCommand extends Command { | ||
|
||
public static final String COMMAND_WORD = "done"; | ||
public static final String MESSAGE_DONE = " Nice! I've marked this task as done: \n"+" [✓] %s"; | ||
private final int toDoneIndex; | ||
|
||
public DoneCommand(int toDoneIndex) { | ||
this.toDoneIndex = toDoneIndex-1; | ||
} | ||
|
||
@Override | ||
public CommandResult execute() { | ||
duke.doneTask(toDoneIndex); | ||
return new CommandResult(String.format(MESSAGE_DONE, | ||
duke.getTaskList().getInternalList().get(toDoneIndex).getTaskDescription())); | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
package commands; | ||
|
||
|
||
/** | ||
* Terminates the program. | ||
*/ | ||
|
||
public class ExitCommand extends Command { | ||
|
||
|
||
|
||
public ExitCommand() { | ||
|
||
} | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. These constructors can be deleted since they are empty. |
||
|
||
public static final String COMMAND_WORD = "bye"; | ||
|
||
public static final String MESSAGE_EXIT_ACKNOWEDGEMENT = " Bye. Hope to see you again soon!"; | ||
public static final String MESSAGE_USAGE = COMMAND_WORD + ": Exit the program.\n" | ||
+ " Example: " + COMMAND_WORD; | ||
|
||
@Override | ||
public CommandResult execute() { | ||
return new CommandResult(String.format(MESSAGE_EXIT_ACKNOWEDGEMENT)); | ||
} | ||
|
||
public static boolean isExit(Command command) { | ||
return command instanceof ExitCommand; // instanceof returns false if it is null | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
package commands; | ||
|
||
import commands.Command; | ||
import commands.CommandResult; | ||
|
||
public class FindCommand extends Command { | ||
|
||
public static final String COMMAND_WORD = "find"; | ||
public static final String MESSAGE_USAGE = COMMAND_WORD | ||
+ ": Find all tasks matches the given description.\n" | ||
+ " Example: " + COMMAND_WORD + " read a book."; | ||
|
||
|
||
public FindCommand() { | ||
|
||
} | ||
|
||
@Override | ||
public CommandResult execute() { | ||
return null; | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
package commands; | ||
|
||
/** | ||
* Shows help instructions. (after user input is wrong) | ||
*/ | ||
public class HelpCommand extends Command { | ||
|
||
public static final String COMMAND_WORD = "help"; | ||
public static final String MESSAGE_USAGE = COMMAND_WORD + ": Shows program usage instructions.\n" | ||
+ " Example: " + COMMAND_WORD; | ||
|
||
public HelpCommand() { | ||
} | ||
|
||
@Override | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Can you add a comment to explain the logic here? |
||
public CommandResult execute() { | ||
return new CommandResult( | ||
commands.add.AddTodoCommand.MESSAGE_USAGE | ||
+ "\n" + " "+ commands.add.AddDeadlineCommand.MESSAGE_USAGE | ||
+ "\n" + " "+ commands.add.AddEventCommand.MESSAGE_USAGE | ||
//+ "\n" + " "+DeleteCommand.MESSAGE_USAGE | ||
+ "\n" + " "+ClearCommand.MESSAGE_USAGE | ||
//+ "\n" + " "+FindCommand.MESSAGE_USAGE | ||
+ "\n" + " "+ ListCommand.MESSAGE_USAGE | ||
//+ "\n" + " "+ ViewCommand.MESSAGE_USAGE | ||
//+ "\n" + ViewAllCommand.MESSAGE_USAGE | ||
+ "\n" + " "+ HelpCommand.MESSAGE_USAGE | ||
+ "\n" + " "+ ExitCommand.MESSAGE_USAGE | ||
); | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
package commands; | ||
|
||
import common.Messages; | ||
|
||
/** | ||
* Represents an incorrect command. Upon execution, produces some feedback to the user. | ||
*/ | ||
|
||
public class IncorrectCommand extends Command{ | ||
|
||
public final String feedbackToUser; | ||
|
||
public IncorrectCommand(String commandWord) { | ||
this.feedbackToUser = commandWord; | ||
} | ||
|
||
@Override | ||
public CommandResult execute() { | ||
//for future implement | ||
//return new CommandResult(feedbackToUser); | ||
return null; | ||
} | ||
|
||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Perhaps you can use some intermediate variables to make this line easier to understand.