-
Notifications
You must be signed in to change notification settings - Fork 118
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
[Guoyi] ip #109
base: master
Are you sure you want to change the base?
[Guoyi] ip #109
Changes from all commits
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 |
---|---|---|
@@ -1,3 +1,5 @@ | ||
import java.util.Scanner; | ||
|
||
public class Duke { | ||
public static void main(String[] args) { | ||
String logo = " ____ _ \n" | ||
|
@@ -6,5 +8,129 @@ public static void main(String[] args) { | |
+ "| |_| | |_| | < __/\n" | ||
+ "|____/ \\__,_|_|\\_\\___|\n"; | ||
System.out.println("Hello from\n" + logo); | ||
System.out.println("Hello! I'm Duke"); | ||
System.out.println("What can I do for you?"); | ||
|
||
Scanner scanner = new Scanner(System.in); | ||
Task[] tasks = new Task[100]; | ||
int taskCount = 0; | ||
String input; | ||
|
||
while (true) { | ||
input = scanner.nextLine(); | ||
if (input.equals("bye")) { | ||
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. Instead of using a long list of else ifs you could consider re factoring this into switch case form to improve readability. Just a suggestion! |
||
System.out.println("Bye. Hope to see you again soon!"); | ||
break; | ||
} else if (input.equals("list")) { | ||
System.out.println("Here are the tasks in your list:"); | ||
for (int i = 0; i < taskCount; i++) { | ||
System.out.println((i + 1) + "." + tasks[i]); | ||
} | ||
} else if (input.startsWith("mark ")) { | ||
int taskIndex = Integer.parseInt(input.split(" ")[1]) - 1; | ||
tasks[taskIndex].markAsDone(); | ||
System.out.println("Nice! I've marked this task as done:"); | ||
System.out.println(" " + tasks[taskIndex]); | ||
} else if (input.startsWith("unmark ")) { | ||
int taskIndex = Integer.parseInt(input.split(" ")[1]) - 1; | ||
tasks[taskIndex].markAsNotDone(); | ||
System.out.println("OK, I've marked this task as not done yet:"); | ||
System.out.println(" " + tasks[taskIndex]); | ||
} else if (input.startsWith("todo ")) { | ||
String description = input.substring(5); | ||
tasks[taskCount] = new ToDo(description); | ||
taskCount++; | ||
System.out.println("Got it. I've added this task:"); | ||
System.out.println(" " + tasks[taskCount - 1]); | ||
System.out.println("Now you have " + taskCount + " tasks in the list."); | ||
} else if (input.startsWith("deadline ")) { | ||
String[] parts = input.substring(9).split(" /by "); | ||
tasks[taskCount] = new Deadline(parts[0], parts[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. You could consider adding some edge case catchers here to remind user the correct argument usage. |
||
taskCount++; | ||
System.out.println("Got it. I've added this task:"); | ||
System.out.println(" " + tasks[taskCount - 1]); | ||
System.out.println("Now you have " + taskCount + " tasks in the list."); | ||
} else if (input.startsWith("event ")) { | ||
String[] parts = input.substring(6).split(" /from | /to "); | ||
tasks[taskCount] = new Event(parts[0], parts[1], parts[2]); | ||
taskCount++; | ||
System.out.println("Got it. I've added this task:"); | ||
System.out.println(" " + tasks[taskCount - 1]); | ||
System.out.println("Now you have " + taskCount + " tasks in the list."); | ||
} else { | ||
System.out.println("I'm sorry, I don't understand that command."); | ||
} | ||
} | ||
|
||
scanner.close(); | ||
} | ||
} | ||
|
||
class Task { | ||
protected String description; | ||
protected boolean isDone; | ||
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. Great job following the naming conventions for boolean variables! |
||
|
||
public Task(String description) { | ||
this.description = description; | ||
this.isDone = false; | ||
} | ||
|
||
public void markAsDone() { | ||
this.isDone = true; | ||
} | ||
|
||
public void markAsNotDone() { | ||
this.isDone = false; | ||
} | ||
|
||
public String getStatusIcon() { | ||
return (isDone ? "X" : " "); | ||
} | ||
|
||
@Override | ||
public String toString() { | ||
return "[" + getStatusIcon() + "] " + description; | ||
} | ||
} | ||
|
||
class ToDo extends Task { | ||
|
||
public ToDo(String description) { | ||
super(description); | ||
} | ||
|
||
@Override | ||
public String toString() { | ||
return "[T]" + super.toString(); | ||
} | ||
} | ||
|
||
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: " + by + ")"; | ||
} | ||
} | ||
|
||
class Event extends Task { | ||
protected String from; | ||
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. Great job keeping all variables in protected accessibility to improve security of your object's members! |
||
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: " + from + " to: " + to + ")"; | ||
} | ||
} |
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.
You can avoid the use of 100 here which is a magic number.