-
Notifications
You must be signed in to change notification settings - Fork 187
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
[Ma Jiajun] iP #197
base: master
Are you sure you want to change the base?
[Ma Jiajun] iP #197
Changes from 12 commits
7e3eb8d
b246b61
4e8d3b2
774fee3
583d937
776bbad
4ff88d1
a59c96e
78c3ec6
195a073
bf630f3
1195965
6f480ed
f575da9
25281d9
4cde7f8
d5c4f4f
8dfd01e
c28e998
da4cb5e
33b8043
fb94eb6
00b0a4c
e8ab793
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 |
---|---|---|
|
@@ -15,3 +15,4 @@ bin/ | |
|
||
/text-ui-test/ACTUAL.TXT | ||
text-ui-test/EXPECTED-UNIX.TXT | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
public class CommandParser { | ||
private final TaskList taskList; | ||
|
||
|
||
public CommandParser(TaskList taskList) { | ||
this.taskList = taskList; | ||
} | ||
|
||
public void parseCommand(String userInput) throws HandleException { | ||
if (userInput.trim().isEmpty()) { | ||
throw new HandleException("The input cannot be empty!"); | ||
} else if (userInput.equalsIgnoreCase("list")) { | ||
taskList.listTasks(); | ||
} else if (!userInput.startsWith("todo") && !userInput.startsWith("deadline") && !userInput.startsWith("event")) { | ||
throw new HandleException("OOPS!!! I'm sorry, but I don't know what that means :-("); | ||
} else { | ||
taskList.addTask(userInput); | ||
} | ||
} | ||
} | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
public class DeadLine extends Task { | ||
protected String by; | ||
|
||
public DeadLine(String description, String by) { | ||
super(description); | ||
this.by = by; | ||
} | ||
|
||
@Override | ||
public String toString() { | ||
return "[D]" + super.toString() + " (by: " + this.by + ")"; | ||
} | ||
} |
This file was deleted.
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
public class Event extends Task { | ||
protected String from; | ||
protected String to; | ||
|
||
public Event(String description, String from, String to) { | ||
super(description); | ||
this.from = from; | ||
this.to = to; | ||
} | ||
|
||
@Override | ||
public String toString() { | ||
return "[E]" + super.toString() + " (from: " + this.from + " to: " + this.to + ")"; | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
public class HandleException extends Exception { | ||
public HandleException(String message) { | ||
super(message); | ||
} | ||
} | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
public class Task { | ||
protected String description; | ||
protected boolean isDone; | ||
|
||
public Task(String description) { | ||
this.description = description; | ||
this.isDone = false; | ||
} | ||
|
||
public String getStatusIcon() { | ||
return (isDone ? "X" : " "); // mark done task with X | ||
} | ||
|
||
|
||
public String toString() { | ||
return "[" + this.getStatusIcon() + "] " + this.description; | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
public class TaskFactory { | ||
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. Avoid long methods. Take corrective action when the method exceeds 30 lines of code. |
||
public static Task createTask(String userInput) throws HandleException { | ||
String[] parts = userInput.split(" ", 2); | ||
String type = parts[0]; | ||
Task task = null; | ||
|
||
if (parts.length < 2 || parts[1].isEmpty()) { | ||
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. Avoid the use of Magic numbers. |
||
throw new HandleException("OOPS!!! The description of a task cannot be empty."); | ||
} | ||
|
||
switch (type) { | ||
case "todo": | ||
task = new Todo(parts[1]); | ||
break; | ||
case "deadline": | ||
String[] details = parts[1].split(" /by ", 2); | ||
if (details.length < 2 || details[1].isEmpty()) { | ||
throw new HandleException("OOPS!!! The date of a deadline cannot be empty."); | ||
} | ||
task = new DeadLine(details[0], details[1]); | ||
break; | ||
case "event": | ||
details = parts[1].split(" /from ", 2); | ||
if (details.length < 2 || details[1].isEmpty()) { | ||
throw new HandleException("OOPS!!! The start time of an event cannot be empty."); | ||
} | ||
String[] times = details[1].split(" /to ", 2); | ||
if (times.length < 2 || times[1].isEmpty()) { | ||
throw new HandleException("OOPS!!! The end time of an event cannot be empty."); | ||
} | ||
task = new Event(details[0], times[0], times[1]); | ||
break; | ||
default: | ||
throw new HandleException("OOPS!!! I'm sorry, but I don't know what that means :-("); | ||
} | ||
|
||
return task; | ||
} | ||
} | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
import java.util.ArrayList; | ||
public class TaskList { | ||
private final ArrayList<Task> tasks; | ||
|
||
public TaskList() { | ||
this.tasks = new ArrayList<>(); | ||
} | ||
|
||
public void addTask(String userInput) throws HandleException { | ||
// Similar to addTask method in TaskManager, but refactored for this class | ||
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. Make sure the comments are for the reader and not private notes for yourself. It should help in increasing code readability. |
||
Task task = TaskFactory.createTask(userInput); | ||
if (task != null) { | ||
tasks.add(task); | ||
System.out.println("Got it. I've added this task:"); | ||
System.out.println(" " + task); | ||
System.out.println("Now you have " + tasks.size() + " tasks in the list."); | ||
} | ||
} | ||
|
||
public void listTasks() { | ||
System.out.println("Here are the tasks in your list:"); | ||
for (int i = 0; i < tasks.size(); i++) { | ||
System.out.println((i + 1) + "." + tasks.get(i)); | ||
} | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
import java.util.Scanner; | ||
|
||
public class TaskManager { | ||
public static void main(String[] args) { | ||
Scanner scanner = new Scanner(System.in); | ||
TaskList taskList = new TaskList(); | ||
CommandParser commandParser = new CommandParser(taskList); | ||
|
||
System.out.println("Hello! I'm TaskManager Jamarcus \nWhat can I do for you?"); | ||
|
||
|
||
while (true) { | ||
try { | ||
String userInput = scanner.nextLine().trim(); | ||
if (userInput.equalsIgnoreCase("bye")) { | ||
System.out.println("Bye. Hope to see you again soon!"); | ||
break; | ||
} else { | ||
commandParser.parseCommand(userInput); | ||
} | ||
} catch (HandleException e) { | ||
System.out.println(e.getMessage()); | ||
} catch (Exception e) { | ||
System.out.println("An unexpected error occurred: " + e.getMessage()); | ||
} | ||
} | ||
|
||
scanner.close(); | ||
} | ||
} | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
public class Todo extends Task { | ||
public Todo(String description) { | ||
super(description); | ||
} | ||
|
||
@Override | ||
public String toString() { | ||
return "[T]" + super.toString(); | ||
} | ||
} |
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.
This is a fairly complex statement. You might want to break this into sub-expressions and store values into some boolean variables to check for the "if" condition.