-
Notifications
You must be signed in to change notification settings - Fork 270
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
[Weiye] iP #295
base: master
Are you sure you want to change the base?
[Weiye] iP #295
Changes from 25 commits
356f7d2
5741e28
ad6aed9
44b496e
ae02dd3
bee688a
6fe8ceb
3910a1f
8874be8
042bfe3
256377e
7e35173
0762e16
9fe0dc0
325bed9
ebfabea
c0f4f9e
96e5e0a
3aad3db
53c09c9
3b36a90
76d9ffa
b4ab6bc
4c48792
f4e7989
fb5007d
91075bd
746eb08
7cae665
eecd61c
d1ed7b3
8ad414d
28b855f
c8a15da
1bd155d
108359b
c488e8e
ce71260
4aad472
13a66ce
67f7f67
befb8c4
61216bf
13f4072
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
D~0~1~1111-11-11 1111 |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
T|0|1 |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
Manifest-Version: 1.0 | ||
Main-Class: main.duke.Duke | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
Manifest-Version: 1.0 | ||
Main-Class: main.duke.Duke | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
package main.duke; | ||
|
||
import main.duke.commands.Command; | ||
import main.duke.io.Parser; | ||
import main.duke.io.Storage; | ||
|
||
import java.util.Scanner; | ||
|
||
public class Duke { | ||
private static final String WELCOME_MESSAGE = "Hello! I'm Duke \n" + "What can I do for you"; | ||
|
||
private Storage storage; | ||
private TaskList taskList; | ||
private Ui ui; | ||
private Parser parser; | ||
|
||
public Duke(String dirname, String filename) { | ||
this.storage = new Storage(dirname, filename); | ||
this.taskList = new TaskList(); | ||
this.ui = new Ui(); | ||
this.parser = new Parser(); | ||
} | ||
|
||
public void run() throws DukeException{ | ||
Scanner sc = new Scanner(System.in); | ||
|
||
this.storage.readFile(this.taskList); | ||
|
||
System.out.println(WELCOME_MESSAGE); | ||
|
||
while (!Command.getIsExit()) { | ||
Command newCommand = this.parser.parse(sc.nextLine()); | ||
newCommand.runCommand(this.ui, this.taskList); | ||
} | ||
|
||
storage.writeFile(this.taskList); | ||
} | ||
|
||
public static void main(String[] args) throws DukeException { | ||
new Duke("data", "duke.txt").run(); | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
package main.duke; | ||
|
||
public class DukeException extends Exception{ | ||
public DukeException (String errorMessage){ | ||
super(errorMessage); | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
package main.duke; | ||
|
||
import main.duke.tasks.Task; | ||
|
||
import java.util.ArrayList; | ||
|
||
public class TaskList { | ||
private ArrayList<Task> Tasks; | ||
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. I think the variable name should be in camelCase. I suggest it should be tasks |
||
|
||
public TaskList() { | ||
this.Tasks = new ArrayList<>(); | ||
} | ||
|
||
public Task getTask(int taskIndex) { | ||
return this.Tasks.get(taskIndex); | ||
} | ||
|
||
public void addTask(Task newTask) { | ||
this.Tasks.add(newTask); | ||
} | ||
|
||
public void deleteTask(int taskIndex) { | ||
this.Tasks.remove(taskIndex); | ||
} | ||
|
||
public int getTasksCount() { | ||
return this.Tasks.size(); | ||
} | ||
|
||
public String taskCountToString() { | ||
return String.format("Now you have %d task(s) in the list.", this.getTasksCount()); | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,82 @@ | ||
package main.duke; | ||
|
||
import main.duke.tasks.Task; | ||
|
||
import java.util.ArrayList; | ||
|
||
public class Ui { | ||
private static final String GOODBYE_MESSAGE = "Bye. Hope to see you again soon!"; | ||
|
||
public void respondBye() { | ||
System.out.println(Ui.GOODBYE_MESSAGE); | ||
} | ||
|
||
/** | ||
* prints out all the tasks in the current list | ||
* @param taskList the current list of tasks | ||
*/ | ||
public void respondList(TaskList taskList) { | ||
int n = taskList.getTasksCount(); | ||
if (n == 0) { | ||
System.out.println("The list is currently empty."); | ||
} else { | ||
for (int i = 0; i < n; i++) { | ||
System.out.printf("%d.%s%n", i + 1, taskList.getTask(i)); | ||
} | ||
} | ||
} | ||
|
||
/** | ||
* prints out the task that has been marked | ||
* @param markTask the targeted task to mark | ||
*/ | ||
public void respondMark(Task markTask) { | ||
System.out.printf("Nice! I've marked this task as done: \n" | ||
+ " %s\n", markTask); | ||
} | ||
|
||
/** | ||
* prints out the task that has been unmarked | ||
* @param unmarkTask the targeted task to unmark | ||
*/ | ||
public void respondUnmark(Task unmarkTask) { | ||
System.out.printf("Nice! I've marked this task as done: \n" | ||
+ " %s\n", unmarkTask); | ||
} | ||
|
||
/** | ||
* prints out the task that has been added as well as the current number of tasks after adding | ||
* @param newTask the targeted task to add | ||
* @param taskList the current list of tasks | ||
*/ | ||
public void respondAddTask(Task newTask, TaskList taskList) { | ||
System.out.printf("Got it. I've added this task:\n" + "%s\n" + "%s\n", | ||
newTask, taskList.taskCountToString()); | ||
} | ||
|
||
/** | ||
* prints out the task that has been added as well as the current number of tasks after removing | ||
* @param deleteTask the targeted task to add | ||
* @param taskList the current list of tasks | ||
*/ | ||
public void respondDeleteTask(Task deleteTask, TaskList taskList) { | ||
System.out.printf("Noted. I've removed this task: \n" + " %s\n" | ||
+ "%s\n", deleteTask, taskList.taskCountToString()); | ||
} | ||
|
||
/** | ||
* prints out the task that was filtered by the user | ||
* @param foundTasks the targeted task to add | ||
*/ | ||
public void respondFindTask(ArrayList<Task> foundTasks) { | ||
int n = foundTasks.size(); | ||
if (n == 0) { | ||
System.out.println("Cannot find any related tasks."); | ||
} else { | ||
System.out.println("Here are the matching tasks in your list:"); | ||
for (int i = 0; i < n; i++) { | ||
System.out.printf("%d.%s%n", i + 1, foundTasks.get(i)); | ||
} | ||
} | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
package main.duke.commands; | ||
|
||
import main.duke.TaskList; | ||
import main.duke.Ui; | ||
import main.duke.enums.CommandType; | ||
|
||
public class CBye extends Command{ | ||
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. Should the name of class be CommandBye? I think it is more meaningful. Similar with other command names. |
||
|
||
public CBye() { | ||
super(CommandType.BYE); | ||
} | ||
|
||
@Override | ||
public void runCommand(Ui ui, TaskList taskList) { | ||
Command.exitDuke(); | ||
ui.respondBye(); | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
package main.duke.commands; | ||
|
||
import main.duke.TaskList; | ||
import main.duke.Ui; | ||
import main.duke.enums.CommandType; | ||
import main.duke.tasks.Deadline; | ||
import main.duke.tasks.Task; | ||
|
||
public class CDeadline extends Command { | ||
protected String description; | ||
protected String dueDate; | ||
|
||
public CDeadline(String description, String dueDate) { | ||
super(CommandType.DEADLINE); | ||
this.description = description; | ||
this.dueDate = dueDate; | ||
} | ||
|
||
public String getDueDate() { | ||
return this.dueDate; | ||
} | ||
|
||
public String getDescription() { | ||
return this.description; | ||
} | ||
|
||
@Override | ||
public void runCommand(Ui ui, TaskList taskList) { | ||
Task newDeadline = new Deadline(this.getDescription(), this.getDueDate()); | ||
taskList.addTask(newDeadline); | ||
ui.respondAddTask(newDeadline, taskList); | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
package main.duke.commands; | ||
|
||
import main.duke.DukeException; | ||
import main.duke.TaskList; | ||
import main.duke.Ui; | ||
import main.duke.enums.CommandType; | ||
import main.duke.tasks.Task; | ||
|
||
public class CDelete extends Command{ | ||
protected int deleteIndex; | ||
|
||
public CDelete(int deleteIndex) { | ||
super(CommandType.DELETE); | ||
this.deleteIndex = deleteIndex; | ||
} | ||
|
||
public int getDeleteIndex() { | ||
return this.deleteIndex; | ||
} | ||
|
||
@Override | ||
public void runCommand(Ui ui, TaskList taskList) throws DukeException { | ||
try { | ||
Task deleteTask = taskList.getTask(this.getDeleteIndex()); | ||
taskList.deleteTask(this.getDeleteIndex()); | ||
ui.respondDeleteTask(deleteTask, taskList); | ||
} catch (IndexOutOfBoundsException e) { | ||
throw new DukeException("Please check that you have entered the correct index."); | ||
} | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
package main.duke.commands; | ||
|
||
import main.duke.TaskList; | ||
import main.duke.Ui; | ||
import main.duke.enums.CommandType; | ||
import main.duke.tasks.Event; | ||
import main.duke.tasks.Task; | ||
|
||
public class CEvent extends Command { | ||
protected String description; | ||
protected String dateTime; | ||
|
||
public CEvent(String description, String dateTime) { | ||
super(CommandType.EVENT); | ||
this.description = description; | ||
this.dateTime = dateTime; | ||
} | ||
|
||
public String getDateTime() { | ||
return this.dateTime; | ||
} | ||
|
||
public String getDescription() { | ||
return this.description; | ||
} | ||
|
||
|
||
@Override | ||
public void runCommand(Ui ui, TaskList taskList) { | ||
Task newEvent = new Event(this.getDescription(), this.getDateTime()); | ||
taskList.addTask(newEvent); | ||
ui.respondAddTask(newEvent, taskList); | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
package main.duke.commands; | ||
|
||
import main.duke.DukeException; | ||
import main.duke.TaskList; | ||
import main.duke.Ui; | ||
import main.duke.enums.CommandType; | ||
import main.duke.tasks.Task; | ||
|
||
import java.util.ArrayList; | ||
|
||
public class CFind extends Command{ | ||
protected String findString; | ||
|
||
public CFind(String findString) { | ||
super(CommandType.FIND); | ||
this.findString = findString; | ||
} | ||
|
||
public String getFindString() { return this.findString; } | ||
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. I suggest follow EgyptianStyle when we make a function which has one line only. I noticed the same issue in several other places too. |
||
|
||
@Override | ||
public void runCommand(Ui ui, TaskList taskList) throws DukeException { | ||
ArrayList<Task> foundTasks = new ArrayList<>(); | ||
for (int i = 0; i < taskList.getTasksCount(); i++) { | ||
Task curTask = taskList.getTask(i); | ||
if (curTask.getDescription().contains(this.getFindString())) { | ||
foundTasks.add(curTask); | ||
} | ||
} | ||
ui.respondFindTask(foundTasks); | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
package main.duke.commands; | ||
|
||
import main.duke.TaskList; | ||
import main.duke.Ui; | ||
import main.duke.enums.CommandType; | ||
|
||
public class CList extends Command { | ||
public CList() { | ||
super(CommandType.LIST); | ||
} | ||
|
||
@Override | ||
public void runCommand(Ui ui, TaskList taskList) { | ||
ui.respondList(taskList); | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
package main.duke.commands; | ||
|
||
import main.duke.DukeException; | ||
import main.duke.TaskList; | ||
import main.duke.Ui; | ||
import main.duke.enums.CommandType; | ||
import main.duke.tasks.Task; | ||
|
||
public class CMark extends Command{ | ||
protected int markIndex; | ||
|
||
public CMark(int markIndex) { | ||
super(CommandType.MARK); | ||
this.markIndex = markIndex; | ||
} | ||
|
||
public int getMarkIndex() { | ||
return this.markIndex; | ||
} | ||
|
||
@Override | ||
public void runCommand(Ui ui, TaskList taskList) throws DukeException { | ||
try { | ||
Task markTask = taskList.getTask(this.getMarkIndex()); | ||
markTask.setIsDone(true); | ||
ui.respondMark(markTask); | ||
} catch (IndexOutOfBoundsException e) { | ||
throw new DukeException("Please check that you have entered the correct index."); | ||
} | ||
} | ||
} |
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.
I think it should be a white space before the open curly bracket. I noticed the same issue in several other places too.