-
Notifications
You must be signed in to change notification settings - Fork 361
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
[clydelhui] iP #373
base: master
Are you sure you want to change the base?
[clydelhui] iP #373
Changes from 11 commits
556af3f
5ba595d
8dfc573
808b76a
3e0e70a
1222458
75ff48e
b8795a2
d44382f
4a5cb95
4949ee8
27e8322
1a3fb0a
08bad31
d231c99
6943842
7603704
fc4f9e3
9b4cc31
0bf4219
3572669
cad9b7a
fa95494
ad01988
4f0e56a
640b3e3
47cc6af
9c2d01f
d908fae
b65a8e2
9d7b757
81c5f59
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,71 @@ | ||
import dukeexceptions.IllegalCommandException; | ||
import dukeexceptions.IllegalInputException; | ||
import items.Deadline; | ||
import items.Event; | ||
import items.Task; | ||
import items.ToDo; | ||
|
||
import java.time.LocalDate; | ||
|
||
public class CommandHandler { | ||
|
||
private TaskList taskList; | ||
|
||
public CommandHandler(TaskList taskList) { | ||
this.taskList = taskList; | ||
} | ||
|
||
public void execute(String command) throws IllegalCommandException, | ||
IllegalInputException, NumberFormatException { | ||
String[] parsedCommand = command.split(" ", 2); | ||
switch (parsedCommand[0]) { | ||
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 like your implementation! Perhaps you can consider further abstraction, e.g having an abstract Command class that different types of commands can extend from :) |
||
case "bye": | ||
if (parsedCommand.length > 1) { | ||
throw new IllegalCommandException("Not sure if you want to say goodbye?"); | ||
} | ||
System.out.println("Goodbye!"); | ||
System.exit(0); | ||
case "list": | ||
if (parsedCommand.length > 1) { | ||
throw new IllegalCommandException("Did you mean you want to list the items in your list?"); | ||
} | ||
this.taskList.enumerate(); | ||
break; | ||
case "mark": | ||
if (parsedCommand.length > 2) { | ||
throw new IllegalInputException("Too many arguments!"); | ||
} | ||
int taskCode = Integer.parseInt(parsedCommand[1]); | ||
this.taskList.markTask(taskCode); | ||
break; | ||
case "unmark": | ||
int taskCodeUnmark = Integer.parseInt(parsedCommand[1]); | ||
this.taskList.unmarkTask(taskCodeUnmark); | ||
break; | ||
case "todo": | ||
try { | ||
Task newToDo = new ToDo(parsedCommand[1]); | ||
this.taskList.addTask(newToDo); | ||
break; | ||
} catch (ArrayIndexOutOfBoundsException e) { | ||
throw new IllegalInputException("You did not key in a task name!"); | ||
} | ||
case "deadline": | ||
String[] paramsDeadline = parsedCommand[1].split("/"); | ||
Task newDeadline = new Deadline(paramsDeadline[0], LocalDate.parse(paramsDeadline[1])); | ||
this.taskList.addTask(newDeadline); | ||
break; | ||
case "event": | ||
String[] paramsEvent = parsedCommand[1].split("/"); | ||
Task newEvent = new Event(paramsEvent[0], paramsEvent[1], paramsEvent[2]); | ||
this.taskList.addTask(newEvent); | ||
break; | ||
case "delete": | ||
int taskCodeDelete = Integer.parseInt(parsedCommand[1]); | ||
this.taskList.delete(taskCodeDelete); | ||
break; | ||
default: | ||
throw new IllegalCommandException("You did not key in a valid command"); | ||
} | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,128 @@ | ||
import items.Deadline; | ||
import items.Event; | ||
import items.Task; | ||
import items.ToDo; | ||
|
||
import java.io.File; | ||
import java.io.FileNotFoundException; | ||
import java.io.FileWriter; | ||
import java.io.IOException; | ||
import java.time.LocalDate; | ||
import java.util.ArrayList; | ||
import java.util.Scanner; | ||
|
||
public class TaskList { | ||
private final File storage; | ||
private ArrayList<Task> taskList; | ||
|
||
public TaskList(String filePath) { | ||
this.storage = new File(filePath); | ||
try { | ||
refreshTaskList(); | ||
} catch (FileNotFoundException e) { | ||
System.err.println("Data file could not be found :("); | ||
e.printStackTrace(); | ||
} | ||
} | ||
|
||
public void delete(int index){ | ||
Task deleted = this.taskList.remove(index - 1); | ||
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 here be some input validity check to ensure index is never less than 1? |
||
try { | ||
refreshStorage(); | ||
System.out.println("I have deleted the following task: \n" + deleted.toString()); | ||
} catch (IOException e) { | ||
e.printStackTrace(); | ||
System.out.println("Seems like something went wrong with the storage file... \n " + | ||
"deleted task has been added to back of task list"); | ||
this.taskList.add(deleted); | ||
} | ||
} | ||
|
||
public void enumerate(){ | ||
for (int i = 0; i < this.taskList.size(); i++){ | ||
System.out.println((i + 1) + ":" + this.taskList.get(i).toString()); | ||
} | ||
} | ||
|
||
public void addTask(Task newTask){ | ||
this.taskList.add(newTask); | ||
try { | ||
refreshStorage(); | ||
System.out.println("added:\n" + newTask.toString()); | ||
} catch (IOException e) { | ||
e.printStackTrace(); | ||
System.out.println("Seems like something went wrong with the storage file... \n " + | ||
"Please try again later"); | ||
this.taskList.remove(newTask); | ||
} | ||
} | ||
|
||
public void markTask(int index){ | ||
Task mark = this.taskList.get(index - 1); | ||
mark.setDone(); | ||
try { | ||
refreshStorage(); | ||
System.out.println("I have set the following task to done: \n" + mark.toString()); | ||
} catch (IOException e) { | ||
System.out.println("Seems like something went wrong with the storage file... \n " + | ||
"Please try again later"); | ||
e.printStackTrace(); | ||
mark.setNotDone(); | ||
} | ||
} | ||
|
||
public void unmarkTask(int index){ | ||
Task unmark = this.taskList.get(index - 1); | ||
unmark.setNotDone(); | ||
try { | ||
refreshStorage(); | ||
System.out.println("I have set the following task to not done: \n" + unmark.toString()); | ||
} catch (IOException e) { | ||
System.out.println("Seems like something went wrong with the storage file... \n " + | ||
"Please try again later"); | ||
e.printStackTrace(); | ||
unmark.setDone(); | ||
} | ||
} | ||
|
||
private void refreshStorage() throws IOException{ | ||
FileWriter clearFile = new FileWriter(this.storage, false); | ||
clearFile.close(); | ||
FileWriter writer = new FileWriter(this.storage); | ||
for (int i = 0; i < this.taskList.size(); i++){ | ||
writer.write(this.taskList.get(i).generateStorageForm() + System.lineSeparator()); | ||
} | ||
writer.close(); | ||
} | ||
|
||
private void refreshTaskList() throws FileNotFoundException { | ||
Scanner scanner = new Scanner(this.storage); | ||
this.taskList = new ArrayList<>(); | ||
|
||
while(scanner.hasNextLine()){ | ||
String[] parsedInput = scanner.nextLine().split("@"); | ||
// for (int i = 0; i < parsedInput.length; i ++) { | ||
// System.out.println(i + parsedInput[i]); | ||
// } | ||
boolean isDone = parsedInput[2].equals("X"); | ||
// System.out.println(isDone); | ||
switch (parsedInput[0]){ | ||
case "T": | ||
this.taskList.add(new ToDo(parsedInput[1], isDone)); | ||
break; | ||
case "D": | ||
this.taskList.add(new Deadline(parsedInput[1], isDone, LocalDate.parse(parsedInput[3]))); | ||
break; | ||
case "E": | ||
this.taskList.add(new Event(parsedInput[1], isDone, parsedInput[3], parsedInput[4])); | ||
break; | ||
} | ||
} | ||
scanner.close(); | ||
} | ||
|
||
|
||
|
||
|
||
|
||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
package dukeexceptions; | ||
|
||
public class IllegalCommandException extends IllegalArgumentException{ | ||
public IllegalCommandException(String errorMessage){ | ||
super(errorMessage); | ||
} | ||
|
||
@Override | ||
public String toString(){ | ||
return "Sorry, I do not understand your command." + getMessage(); | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
package dukeexceptions; | ||
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. Please conform to the camelNaming rule here to name the package. |
||
|
||
public class IllegalInputException extends IllegalArgumentException{ | ||
public IllegalInputException(String s) { | ||
super(s); | ||
} | ||
|
||
@Override | ||
public String toString() { | ||
return "Sorry, your input for the command is invalid:" + getMessage(); | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
package items; | ||
|
||
import java.time.LocalDate; | ||
|
||
public class Deadline extends Task{ | ||
private LocalDate endDate; | ||
public Deadline(String description, LocalDate endDate) { | ||
super(description, "D"); | ||
this.endDate = endDate; | ||
} | ||
|
||
public Deadline(String description, boolean done, LocalDate endDate) { | ||
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. perhaps the parameter "done" could be changed to "isDone" instead! |
||
super(description, "D", done); | ||
this.endDate = endDate; | ||
} | ||
|
||
@Override | ||
public String generateStorageForm() { | ||
return this.getTaskType() + "@" + this.getDescription() + "@" | ||
+ this.getStatusIcon() + "@" + this.endDate; | ||
} | ||
|
||
@Override | ||
public String toString(){ | ||
return "[" + this.getTaskType() + "]" + "[" + this.getStatusIcon() + "]" | ||
+ this.description + "/" + this.endDate; | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
package items; | ||
|
||
public class Event extends Task{ | ||
private String startDate; | ||
private String endDate; | ||
|
||
public Event(String description, String startDate, String endDate) { | ||
super(description, "E"); | ||
this.startDate = startDate; | ||
this.endDate = endDate; | ||
} | ||
|
||
public Event(String description, boolean done, String startDate, String endDate) { | ||
super(description, "E", done); | ||
this.startDate = startDate; | ||
this.endDate = endDate; | ||
} | ||
|
||
@Override | ||
public String generateStorageForm() { | ||
return this.getTaskType() + "@" + this.getDescription() + "@" + this.getStatusIcon() + "@" | ||
+ this.startDate + "@" + this.endDate; | ||
} | ||
|
||
@Override | ||
public String toString() { | ||
return "[" + this.getTaskType() + "]" + "[" + this.getStatusIcon() + "]" | ||
+ this.description + "/" + this.startDate + "/" + this.endDate; | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
package items; | ||
public abstract class Task { | ||
protected String description; | ||
protected boolean isDone; | ||
protected String taskType; | ||
|
||
public Task(String description, String taskType) { | ||
this.description = description; | ||
this.isDone = false; | ||
this.taskType = taskType; | ||
} | ||
|
||
public Task(String description, String taskType, boolean done) { | ||
this.description = description; | ||
this.isDone = done; | ||
this.taskType = taskType; | ||
} | ||
|
||
public String getStatusIcon() { | ||
return (isDone ? "X" : " "); // mark done task with X | ||
} | ||
|
||
public String getTaskType(){ return this.taskType;} | ||
|
||
public void setDone(){this.isDone = true;} | ||
|
||
public void setNotDone(){this.isDone = false;} | ||
|
||
public String getDescription(){return this.description;} | ||
|
||
public abstract String generateStorageForm(); | ||
|
||
@Override | ||
public abstract String toString(); | ||
|
||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
package items; | ||
|
||
public class ToDo extends Task{ | ||
public ToDo(String description) { | ||
super(description, "T"); | ||
} | ||
|
||
public ToDo(String description, boolean done) { | ||
super(description, "T", done); | ||
} | ||
|
||
@Override | ||
public String generateStorageForm() { | ||
return this.getTaskType() + "@" + this.getDescription() + "@" + this.getStatusIcon(); | ||
} | ||
|
||
@Override | ||
public String toString() { | ||
return "[" + this.getTaskType() + "]" + "[" + this.getStatusIcon() + "]" + this.description; | ||
} | ||
} |
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 consider having Javadocs for this method