-
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
Increments for iP #370
base: master
Are you sure you want to change the base?
Increments for iP #370
Changes from 9 commits
aa829ae
99c297e
38c6d7f
cda40e0
1b106fd
5c50031
4cbf56c
f28eaf2
4e33f21
a31cf0c
3d845a9
d339498
5c3b10d
72d0760
f9a3ce9
91d7ec7
34281ac
cb2ebec
b2049d6
5baab08
cf661a8
f16ad85
7aac4f7
d86e756
95b378a
2e1292d
f7ca754
af32516
165951c
ae5d801
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,10 +1,96 @@ | ||
import java.util.ArrayList; | ||
import java.util.Scanner; | ||
|
||
public class Duke { | ||
public static void main(String[] args) { | ||
String logo = " ____ _ \n" | ||
+ "| _ \\ _ _| | _____ \n" | ||
+ "| | | | | | | |/ / _ \\\n" | ||
+ "| |_| | |_| | < __/\n" | ||
+ "|____/ \\__,_|_|\\_\\___|\n"; | ||
System.out.println("Hello from\n" + logo); | ||
ArrayList<Task> taskList = new ArrayList<Task>(); | ||
System.out.println("Hello I'm Duke\nWhat can I do for you?"); | ||
Scanner scan = new Scanner(System.in); | ||
|
||
while (true) { | ||
String textInput = scan.nextLine(); | ||
|
||
if (textInput.equalsIgnoreCase("bye")) { | ||
System.out.println("Bye. Hope to see you again soon!"); | ||
scan.close(); | ||
return; | ||
} | ||
|
||
if (textInput.equalsIgnoreCase("list")) { | ||
System.out.println("Here are the tasks in your list:"); | ||
for (int i = 0; i < taskList.size(); i++) { | ||
System.out.println(i + 1 + ". " + taskList.get(i).toString()); | ||
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. This manual string formatting works, however, have you considered using Java's builtin String.format method? |
||
} | ||
continue; | ||
} | ||
|
||
if (textInput.length() >= 8 && | ||
textInput.substring(0, 6).equalsIgnoreCase("delete")) { | ||
int i = Integer.parseInt(textInput.substring(7)); | ||
Task t = taskList.get(i - 1); | ||
taskList.remove(i - 1); | ||
String output = String.format("Got it. I've removed this task:\n%s\nNow you have %d tasks in the list", t.toString(), taskList.size()); | ||
System.out.println(output); | ||
continue; | ||
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 find the use of multiple Rather, I would suggest the following structure: boolean isRunning = true;
while (isRunning) {
if (textInput.equalsIgnoreCase("bye")) {
isRunning = false;
} else if (stmt) {
code;
} else {
code;
}
} Alternatively, you may consider parsing the command first, then using a switch-case statement to handle various cases. |
||
} | ||
|
||
if (textInput.length() >= 6 && | ||
textInput.substring(0, 4).equalsIgnoreCase("mark")) { | ||
int i = Integer.parseInt(textInput.substring(5)); | ||
Task currTask = taskList.get(i - 1); | ||
currTask.markDone(); | ||
System.out.println("Nice! I've marked this task as done\n" + currTask.toString()); | ||
continue; | ||
} | ||
|
||
if (textInput.length() >= 8 && | ||
textInput.substring(0, 6).equalsIgnoreCase("unmark")) { | ||
int i = Integer.parseInt(textInput.substring(7)); | ||
Task currTask = taskList.get(i - 1); | ||
currTask.markUndone(); | ||
System.out.println("OK, I've marked this task as not done yet:\n" + currTask.toString()); | ||
continue; | ||
} | ||
|
||
if (textInput.length() >= 4 && | ||
textInput.substring(0, 4).equalsIgnoreCase("todo")) { | ||
try { | ||
String[] parts = textInput.split(" ", 2); | ||
if (parts.length == 1 || parts[1] == "") { | ||
throw new DukeException("☹ OOPS!!! The description of a todo cannot be empty."); | ||
} | ||
Task t = new Task.Todo(textInput.substring(5)); | ||
taskList.add(t); | ||
String output = String.format("Got it. I've added this task:\n%s\nNow you have %d tasks in the list", t.toString(), taskList.size()); | ||
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. This output line seems very long, is it possible to split them into 2 lines? |
||
System.out.println(output); | ||
continue; | ||
} catch (DukeException e) { | ||
System.out.println(e); | ||
continue; | ||
} | ||
} | ||
|
||
if (textInput.length() >= 10 && | ||
textInput.substring(0, 8).equalsIgnoreCase("deadline")) { | ||
String[] parts = textInput.split("/"); | ||
Task t = new Task.Deadline(parts[0].substring(9), parts[1].substring(3)); | ||
taskList.add(t); | ||
String output = String.format("Got it. I've added this task:\n%s\nNow you have %d tasks in the list", t.toString(), taskList.size()); | ||
System.out.println(output); | ||
continue; | ||
} | ||
|
||
if (textInput.length() >= 7 && | ||
textInput.substring(0, 5).equalsIgnoreCase("event")) { | ||
String[] parts = textInput.split("/"); | ||
Task t = new Task.Event(parts[0].substring(6), parts[1].substring(5), parts[2].substring(3)); | ||
taskList.add(t); | ||
String output = String.format("Got it. I've added this task:\n%s\nNow you have %d tasks in the list", t.toString(), taskList.size()); | ||
System.out.println(output); | ||
continue; | ||
} | ||
|
||
System.out.println("☹ OOPS!!! I'm sorry, but I don't know what that means :-("); | ||
} | ||
} | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
public class DukeException extends Exception{ | ||
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. For this class and your Task class, can you indent it with 4 spaces instead of 2? |
||
public DukeException(String e) { | ||
super(e); | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,72 @@ | ||
public class Task { | ||
protected String description; | ||
protected boolean isDone; | ||
|
||
public Task(String description) { | ||
this.description = description; | ||
this.isDone = false; | ||
} | ||
|
||
public static class Todo extends Task { | ||
|
||
public Todo(String description) { | ||
super(description); | ||
} | ||
|
||
@Override | ||
public String toString() { | ||
return "[T]" + super.toString(); | ||
} | ||
} | ||
|
||
public static 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 + ")"; | ||
} | ||
} | ||
|
||
public static 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: " + from + "to: " + to + ")"; | ||
} | ||
} | ||
|
||
public String getStatusIcon() { | ||
return (isDone ? "X" : " "); // mark done task with X | ||
} | ||
|
||
public void markDone() { | ||
this.isDone = true; | ||
} | ||
|
||
public void markUndone() { | ||
this.isDone = false; | ||
} | ||
|
||
@Override | ||
public String toString() { | ||
String result = String.format("[%s] %s", this.getStatusIcon(), this.description); | ||
return result; | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,7 +1,3 @@ | ||
Hello from | ||
____ _ | ||
| _ \ _ _| | _____ | ||
| | | | | | | |/ / _ \ | ||
| |_| | |_| | < __/ | ||
|____/ \__,_|_|\_\___| | ||
|
||
Hello I'm Duke | ||
What can I do for you? | ||
☹ OOPS!!! I'm sorry, but I don't know what that means :-( |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
Hello |
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.
Would prefer if the code was in a package, eg: package duke.