-
Notifications
You must be signed in to change notification settings - Fork 228
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
[Andrea Tan] iP #263
base: master
Are you sure you want to change the base?
[Andrea Tan] iP #263
Changes from 6 commits
af7e8f9
c621220
b3a5323
25b1d6b
dd45d1b
26418a3
0a0fc47
18b425d
a7089c6
75632ce
2061878
b785eec
5ea5f47
1f42308
8b0510b
e7cb203
3038258
c475678
b5e8f0f
a61b6df
62293f2
081fe4e
c9172b6
1abb403
ae13b46
382cb20
e6d3f36
be967e3
ddd5d12
b7d0bbe
7e15137
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,29 @@ | ||
package com.jetbrains; | ||
|
||
import java.lang.Throwable; | ||
public class Deadline extends Task { | ||
String by; | ||
|
||
Deadline(String input) throws DukeIncompleteCommandException { | ||
input = input.substring(8).trim(); | ||
|
||
if (input.equals("")) { | ||
throw new DukeIncompleteCommandException(); | ||
} | ||
|
||
String[] inputs = input.split("/by"); | ||
|
||
if (!input.contains("/by") || inputs.length < 2) { | ||
throw new DukeIncompleteCommandException("Oh no! Please enter an due date. :P"); | ||
} | ||
this.task = inputs[0]; | ||
this.isDone = false; | ||
this.by = inputs[1].trim(); | ||
} | ||
|
||
@Override | ||
public String toString() { | ||
return String.format("DDLN%s (by: %s)" , | ||
super.toString(), by); | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,79 @@ | ||
package com.jetbrains; | ||
|
||
import java.util.Scanner; | ||
import java.util.ArrayList; | ||
|
||
public class Duke { | ||
public static void main(String[] args) { | ||
String logo = " ____ _ \n" | ||
+ "| _ \\ _ _| | _____ \n" | ||
+ "| | | | | | | |/ / _ \\\n" | ||
+ "| |_| | |_| | < __/\n" | ||
+ "|____/ \\__,_|_|\\_\\___|\n"; | ||
System.out.println(logo); | ||
System.out.println("Hello! I'm Duke! \n" + | ||
"What would you like to do today? \n" + | ||
"***********************************"); | ||
ArrayList<Task> list = new ArrayList<>(); | ||
Scanner sc = new Scanner(System.in); | ||
String input = sc.nextLine(); | ||
while (!input.equals("bye")) { | ||
Task task; | ||
try { | ||
if (input.equals("list")) { | ||
displayList(list); | ||
System.out.println("\n"); | ||
} else if (input.contains("done")) { | ||
String[] command = input.split(" "); | ||
task = list.get(Integer.parseInt(command[1]) - 1); | ||
System.out.println("Good job! I've marked this task as done:\n " + | ||
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 think it's good that you kept the line of print statement short:> |
||
task.markDone() + | ||
"\n"); | ||
} else if (input.contains("delete")) { | ||
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. do you think maybe switch statements could be used to make the code look more concise? |
||
String[] command = input.split(" "); | ||
int index = Integer.parseInt(command[1]) - 1; | ||
task = list.get(index); | ||
System.out.println("Alright, I've deleted this task:\n " + | ||
task + | ||
"\n"); | ||
list.remove(index); | ||
System.out.println("Now you have " + list.size() + | ||
" task(s) in the list. \n"); | ||
} else if (input.contains("todo") || | ||
input.contains("deadline") || | ||
input.contains("event")) { | ||
|
||
if (input.contains("todo")) { | ||
task = new ToDo(input); | ||
} else if (input.contains("deadline")) { | ||
task = new Deadline(input); | ||
} else { | ||
task = new Event(input); | ||
} | ||
list.add(task); | ||
System.out.println("Alright! I've added this task: \n " + | ||
task + "\nNow you have " + list.size() + | ||
" task(s) in the list. \n"); | ||
} else { | ||
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 think it is great that you implemented Duke Exceptions early! |
||
throw new DukeInvalidCommandException(); | ||
} | ||
} catch (IndexOutOfBoundsException e) { | ||
System.out.println("Oh no! This task does not exist. D:" ); | ||
} catch (Exception e) { | ||
System.out.println(e.getMessage()); | ||
} | ||
input = sc.nextLine(); | ||
} | ||
|
||
sc.close(); | ||
System.out.println("Bye! Stay on task!"); | ||
} | ||
|
||
static void displayList(ArrayList<Task> list) { | ||
for(int i = 0; i < list.size(); i++) { | ||
System.out.printf("%d. %s%n",i + 1, list.get(i).toString()); | ||
} | ||
} | ||
} | ||
|
||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
package com.jetbrains; | ||
|
||
public class DukeIncompleteCommandException extends Exception { | ||
public String message; | ||
DukeIncompleteCommandException() { | ||
this.message = "Oh no! Task cannot be empty. ):"; | ||
} | ||
DukeIncompleteCommandException(String message) { | ||
this.message = message; | ||
} | ||
@Override | ||
public String getMessage() { | ||
return message; | ||
} | ||
public String toString() { | ||
return message; | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
package com.jetbrains; | ||
|
||
public class DukeInvalidCommandException extends Exception { | ||
public String message = "Oh no! I don't know what that means. ):"; | ||
DukeInvalidCommandException() { | ||
} | ||
|
||
@Override | ||
public String getMessage() { | ||
return message; | ||
} | ||
|
||
public String toString() { | ||
return message; | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
package com.jetbrains; | ||
|
||
import java.lang.Throwable; | ||
public class Event extends Task { | ||
String at; | ||
|
||
Event(String input) throws DukeIncompleteCommandException { | ||
input = input.substring(5).trim(); | ||
|
||
if (input.equals("")) { | ||
throw new DukeIncompleteCommandException(); | ||
} | ||
|
||
String[] inputs = input.split("/at"); | ||
|
||
if (!input.contains("/at") || inputs.length < 2) { | ||
throw new DukeIncompleteCommandException("Oh no! Please enter an event date. :P"); | ||
} | ||
|
||
this.task = inputs[0]; | ||
this.isDone = false; | ||
this.at = inputs[1].trim(); | ||
} | ||
|
||
|
||
@Override | ||
public String toString() { | ||
return String.format("EVNT%s (at: %s)" , | ||
super.toString(), at); | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
package com.jetbrains; | ||
|
||
class Task { | ||
String task; | ||
boolean isDone; | ||
|
||
Task() { | ||
} | ||
|
||
Task(String task) { | ||
this.task = task; | ||
isDone = false; | ||
} | ||
|
||
Task markDone() { | ||
isDone = true; | ||
return this; | ||
} | ||
|
||
@Override | ||
public String toString() { | ||
if (isDone) { | ||
return "[X] " + task; | ||
} else { | ||
return "[ ] " + task; | ||
} | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
package com.jetbrains; | ||
|
||
import java.lang.Throwable; | ||
public class ToDo extends Task { | ||
ToDo(String input) throws DukeIncompleteCommandException { | ||
input = input.substring(4).trim(); | ||
if (input.equals("")) { | ||
throw new DukeIncompleteCommandException(); | ||
} | ||
this.task = input; | ||
this.isDone = false; | ||
} | ||
|
||
@Override | ||
public String toString() { | ||
return "TODO" + 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.
Maybe some java docs will be useful to make the user understand the code better!