-
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
[kenzantonius] iP #378
base: master
Are you sure you want to change the base?
[kenzantonius] iP #378
Changes from 6 commits
556af3f
d0da323
9395dc6
2a3787e
c76707f
2d45761
5b12a41
5506608
30e51fb
e125b5f
c47d8ac
462d127
502bbbc
cedbe55
14a39a6
0843b84
1fcdf42
1bbfa6a
f8939fc
90e04a4
96be1dc
e864b36
d70d794
44be10d
fb0b9d4
710f5ef
bbce7ed
913f4bf
f8f798a
3f90dae
2ba8057
e16deb6
0e40946
cd96457
6bd37c2
94f55bf
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,14 @@ | ||
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: " + by + ")"; | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,10 +1,143 @@ | ||
import java.util.Scanner; | ||
|
||
import java.util.ArrayList; | ||
|
||
public class Duke { | ||
private Scanner scanner = new Scanner(System.in); | ||
private ArrayList<Task> list = new ArrayList<>(); | ||
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. Maybe use plural form in a collection name? Like "tasks"? |
||
private void Input() { | ||
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. Shouldn't this method name be in camelCase? |
||
while (true) { | ||
String userInput = this.scanner.nextLine(); | ||
try{ | ||
if (userInput.equals("bye")) { | ||
this.exit(); | ||
break; | ||
} | ||
if (userInput.equals("list")) { | ||
this.showList(); | ||
continue; | ||
} | ||
if (userInput.startsWith("mark")) { | ||
int taskNum = Integer.parseInt(userInput.substring(5)); | ||
this.markTask(taskNum); | ||
continue; | ||
} | ||
if (userInput.startsWith("unmark")) { | ||
int taskNum = Integer.parseInt(userInput.substring(7)); | ||
this.unmarkTask(taskNum); | ||
continue; | ||
} | ||
if (userInput.startsWith("todo")) { | ||
String todo = userInput.replace("todo", ""); | ||
emptyTodo(todo); | ||
addTodo(todo); | ||
continue; | ||
} | ||
if (userInput.startsWith("event")) { | ||
String[] event = ErrorEventOrDeadline(userInput, "event", "/at"); | ||
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. Maybe use plural form for array name? |
||
addEvent(event[0], event[1]); | ||
continue; | ||
} | ||
if (userInput.startsWith("deadline")) { | ||
String[] deadline = ErrorEventOrDeadline(userInput, "deadline", "/by"); | ||
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. Maybe use plural form for array name? |
||
addDeadline(deadline[0], deadline[1]); | ||
continue; | ||
} | ||
if (userInput.startsWith("delete")) { | ||
deleteTask(Integer.parseInt(userInput.substring(7))); | ||
continue; | ||
} | ||
throw new DukeException("☹ OOPS!!! I'm sorry, but I don't know what that means :-("); | ||
} catch (DukeException exception) { | ||
printMessage(exception.getMessage()); | ||
} | ||
} | ||
} | ||
|
||
private void emptyTodo(String todo) throws DukeException { | ||
if (todo.isEmpty()) { | ||
throw new DukeException("☹ OOPS!!! The description of a todo cannot be empty."); | ||
} | ||
} | ||
|
||
private String[] ErrorEventOrDeadline(String input, String textToReplace, String textToSplit) throws DukeException { | ||
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. Maybe use verbs and camelCase for method names? Say, "makeErrEventOrDdl" |
||
String[] splitInput = input.replaceFirst(textToReplace, "") | ||
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. Maybe use plural form for array name? |
||
.trim().split(textToSplit); | ||
|
||
for (int i = 0; i < splitInput.length; i++) { | ||
splitInput[i] = splitInput[i].trim(); | ||
} | ||
|
||
if (splitInput.length != 2 || splitInput[0].isBlank() || splitInput[1].isBlank()) { | ||
throw new DukeException("☹ OOPS!!! Please make sure the date is not empty"); | ||
} | ||
return splitInput; | ||
} | ||
|
||
private void addTodo(String description) { | ||
Todo newTodo = new Todo(description); | ||
list.add(newTodo); | ||
System.out.println("\tGot it. I've added this task:"); | ||
System.out.println("\t\t" + newTodo); | ||
System.out.println("\tNow you have " + list.size() + " tasks in the list."); | ||
} | ||
|
||
private void addDeadline(String description, String by) { | ||
Deadline newDeadline = new Deadline(description, by); | ||
list.add(newDeadline); | ||
System.out.println("\tGot it. I've added this task:"); | ||
System.out.println("\t\t" + newDeadline); | ||
System.out.println("\tNow you have " + list.size() + " tasks in the list."); | ||
} | ||
|
||
private void addEvent(String description, String from) { | ||
Event newEvent = new Event(description, from); | ||
list.add(newEvent); | ||
System.out.println("\tGot it. I've added this task:"); | ||
System.out.println("\t\t" + newEvent); | ||
System.out.println("\tNow you have " + list.size() + " tasks in the list."); | ||
} | ||
|
||
private void markTask(int taskNum) { | ||
this.list.get(taskNum - 1).markAsDone(); | ||
System.out.println("\tNice! I've marked this task as done:"); | ||
System.out.println("\t" + this.list.get(taskNum - 1) + "\n"); | ||
} | ||
|
||
private void unmarkTask(int taskNum) { | ||
this.list.get(taskNum - 1).markAsUndone(); | ||
System.out.println("\tOK, I've marked this task as not done yet:"); | ||
System.out.println("\t" + this.list.get(taskNum - 1) + "\n"); | ||
} | ||
|
||
private void deleteTask(int taskNum) { | ||
Task deletedTask = list.get(taskNum - 1); | ||
list.remove(taskNum - 1); | ||
System.out.println("\tNoted. I've removed this task:"); | ||
System.out.println("\t\t" + deletedTask); | ||
System.out.printf("\tNow you have " + list.size() + " tasks in the list." + "\n"); | ||
} | ||
|
||
private void showList() { | ||
System.out.println("\tHere are the tasks in your list:"); | ||
for (int i = 1; i <= this.list.size(); i++) { | ||
System.out.println("\t" + i + ". " + this.list.get(i - 1)); | ||
} | ||
System.out.println(); | ||
} | ||
|
||
private void exit() { | ||
System.out.println("\tBye, hope to see you again!"); | ||
} | ||
|
||
private void printMessage(String message) { | ||
System.out.println("\t" + message); | ||
} | ||
|
||
public static void main(String[] args) { | ||
String logo = " ____ _ \n" | ||
+ "| _ \\ _ _| | _____ \n" | ||
+ "| | | | | | | |/ / _ \\\n" | ||
+ "| |_| | |_| | < __/\n" | ||
+ "|____/ \\__,_|_|\\_\\___|\n"; | ||
System.out.println("Hello from\n" + logo); | ||
} | ||
} | ||
Duke duke = new Duke(); | ||
System.out.println("Welcome! I'm Duke."); | ||
System.out.println("What can I do for you?\n"); | ||
duke.Input(); | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
class DukeException extends Exception { | ||
DukeException(String message) { | ||
super(message); | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
public class Event extends Task { | ||
private 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. similarly, conisder changing from to a noun |
||
|
||
public Event(String description, String from) { | ||
super(description); | ||
this.from = from; | ||
} | ||
|
||
@Override | ||
public String toString() { | ||
return "[E]" + super.toString() + " (from: " + this.from + ")"; | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
public 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. nice following convention |
||
|
||
public Task(String description) { | ||
this.description = description; | ||
this.isDone = false; | ||
} | ||
|
||
public String getStatusIcon() { | ||
return (isDone ? "X" : " "); // mark done task with X | ||
} | ||
|
||
public void markAsDone(){ | ||
this.isDone = true; | ||
} | ||
|
||
public void markAsUndone(){ | ||
this.isDone = false; | ||
} | ||
|
||
@Override | ||
public String toString() { | ||
return String.format("[%s] %s", this.getStatusIcon(), this.description); | ||
} | ||
} |
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(); | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,7 +1,2 @@ | ||
Hello from | ||
____ _ | ||
| _ \ _ _| | _____ | ||
| | | | | | | |/ / _ \ | ||
| |_| | |_| | < __/ | ||
|____/ \__,_|_|\_\___| | ||
|
||
Welcome! I'm Duke. | ||
What can I do for you? |
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.
Maybe change name from by to something that is a noun