-
Notifications
You must be signed in to change notification settings - Fork 590
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
[izruff] iP #626
base: master
Are you sure you want to change the base?
[izruff] iP #626
Changes from 21 commits
68c58c1
03523ec
81a9c53
edaa970
2f4d559
dd1abfc
5c2c821
d83548d
bdecfd1
729d3fd
5b35bb3
e5cbbeb
857012c
c93d0cf
55e872b
8a7b166
75cdc30
7b2770c
d13673b
b838af6
4fc5773
972b076
7b930d7
2cfe70b
40ac380
833c3a6
266b31a
7cfde52
67c4c4f
0351a2f
841b6b9
5f638fc
f44fa76
f601341
790aea2
d64de43
100f96b
6cecc3c
8d684da
0d99b06
696e9ba
5b91e9d
f843463
9d36f62
f90170a
a66297a
5454335
42cb70c
18f66e6
c66182d
be8d194
39164a7
9e8333d
f097245
22079d3
f6fb1bc
8be896b
9c6640e
0fe3fdc
eea6246
611e063
5e7b078
2fc2af5
331666e
3e55b1a
3f45c68
d272df3
a470a49
0339cd4
507f25e
10d0fe9
2da1cfd
ae4f677
48add82
23b66b6
331651c
c47210d
d88c6c2
fa54976
1a98c3f
dbe4fba
15c92b5
88ff04d
240fd19
2d252ab
5cd7674
95d14a3
7e209a9
a93b874
cd0ffc2
083c7c6
8a9c0ef
54339a5
2003c7d
9ce94a4
894d8b9
e604d36
9b28d4c
6f25f39
340e6fe
f8596c1
4dc21dc
a931cdd
94db3cf
0917c2b
07c74b9
b20c07b
5e8c468
80f59aa
53c9f07
d12070e
197289d
c3d4a54
e13dc97
c15f4b5
2bab46e
ef88c69
32e0dbc
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,9 @@ | ||
public class BadInputException extends MittensException { | ||
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. Perhaps you should have all files in a package? |
||
private static final String MITTENS_MESSAGE = "Meow?! What does that mean?"; | ||
private static final String HELP_MESSAGE = "Type 'help' to see a list of commands," + | ||
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. Good naming of final variables and ensuring line length is consistent |
||
" or 'help <command>' to see more about a specific command."; | ||
|
||
public BadInputException(String message) { | ||
super(message, MITTENS_MESSAGE, HELP_MESSAGE); | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
public class Deadline extends Task { | ||
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. Would be great if you add a header comment for all public classes and methods |
||
protected String by; | ||
|
||
public Deadline(String description, String by) { | ||
super(description); | ||
this.by = by; | ||
} | ||
|
||
@Override | ||
public String toString() { | ||
return "[D]" + super.toString() + " (due " + 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() + " (" + this.from + " -- " + this.to + ")"; | ||
} | ||
} |
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. Good variable names. Cute cat! |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,208 @@ | ||
import java.util.ArrayList; | ||
import java.util.Scanner; | ||
|
||
public class Mittens { | ||
|
||
// Unfortunately we were unable to use the initial cat ASCII art due to the | ||
// Unicode characters interfering with the UI tests. | ||
private final static String GREETING_MESSAGE = """ | ||
|
||
/\\_/\\ ____________________ | ||
>^,^< / Hi, I'm Mittens! \\ | ||
/ \\ \\ I'm a cat! Meow :3 / | ||
(___)_/ -------------------- | ||
"""; | ||
|
||
private final static String EXIT_MESSAGE = """ | ||
|
||
/\\_/\\ _____________ | ||
>^,^< ( Bye-bye! :3 ) | ||
/ \\ ------------- | ||
(___)_/ | ||
"""; | ||
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. Modifiers should be given in this order |
||
|
||
private static ArrayList<Task> tasks = new ArrayList<>(); | ||
|
||
public static void greet() { | ||
System.out.println(GREETING_MESSAGE); | ||
} | ||
|
||
public static void echo(String command) { | ||
int len = command.length(); | ||
String message = """ | ||
|
||
/\\_/\\ %s | ||
>^,^< ( %s ) | ||
/ \\ %s | ||
(___)_/ | ||
""".formatted("_".repeat(len + 2), | ||
command, "-".repeat(len + 2)); | ||
|
||
System.out.println(message); | ||
} | ||
|
||
public static void addTask(Task task) { | ||
tasks.add(task); | ||
System.out.printf("\nI've added \"%s\" to your list :3\n\n", task.getDescription()); | ||
} | ||
|
||
public static void listTasks() { | ||
if (tasks.size() == 0) { | ||
System.out.println("\nMeow?! Your list is empty!\n"); | ||
return; | ||
} | ||
System.out.printf("\nYou have %d tasks in your list, here they are :3\n", tasks.size()); | ||
for (int i = 0; i < tasks.size(); i++) { | ||
Task task = tasks.get(i); | ||
System.out.printf("%d. %s\n", i + 1, task.toString()); | ||
} | ||
System.out.print("\n"); | ||
} | ||
|
||
public static void markTaskAsDone(int index) throws BadInputException { | ||
if (index > tasks.size()) { | ||
throw new BadInputException("Task index is out of range"); | ||
} | ||
Task task = tasks.get(index - 1); | ||
task.markAsDone(); | ||
System.out.printf("\nMeow, I scratched the check box for you:\n%s\n\n", task.toString()); | ||
} | ||
|
||
public static void markTaskAsNotDone(int index) throws BadInputException { | ||
if (index > tasks.size()) { | ||
throw new BadInputException("Task index is out of range"); | ||
} | ||
Task task = tasks.get(index - 1); | ||
task.markAsNotDone(); | ||
System.out.printf("\nMeow, I unscratched the check box for you:\n%s\n\n", task.toString()); | ||
} | ||
|
||
public static void deleteTask(int index) throws BadInputException { | ||
if (index > tasks.size()) { | ||
throw new BadInputException("Task index is out of range"); | ||
} | ||
Task task = tasks.remove(index - 1); | ||
System.out.printf("\nMeow, I deleted the task '%s' for you :3\n\n", task.getDescription()); | ||
} | ||
|
||
public static void exit() { | ||
System.out.println(EXIT_MESSAGE); | ||
} | ||
|
||
public static void main(String[] args) { | ||
Scanner scanner = new Scanner(System.in); | ||
|
||
greet(); | ||
|
||
while (true) { | ||
System.out.print("> "); | ||
String input = scanner.nextLine(); | ||
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. Would be great if you handled whitespaces after a command (e.g. "bye ") as well |
||
|
||
try { | ||
if (input.equals("bye")) { | ||
break; | ||
} else if (input.equals("list")) { | ||
listTasks(); | ||
} else if (input.startsWith("mark")) { | ||
try { | ||
int index = Integer.parseInt(input.split(" ")[1]); | ||
markTaskAsDone(index); | ||
} catch (NumberFormatException e) { | ||
throw new BadInputException("Argument for command 'mark' must be a number"); | ||
} | ||
} else if (input.startsWith("unmark")) { | ||
try { | ||
int index = Integer.parseInt(input.split(" ")[1]); | ||
markTaskAsNotDone(index); | ||
} catch (NumberFormatException e) { | ||
throw new BadInputException("Argument for command 'mark' must be a number"); | ||
} | ||
} else if (input.startsWith("delete")) { | ||
try { | ||
int index = Integer.parseInt(input.split(" ")[1]); | ||
deleteTask(index); | ||
} catch (NumberFormatException e) { | ||
throw new BadInputException("Argument for command 'delete' must be a number"); | ||
} | ||
} else if (input.startsWith("todo")) { | ||
String description = input.substring(5); | ||
|
||
Todo newTodo = new Todo(description); | ||
addTask(newTodo); | ||
} else if (input.startsWith("deadline")) { | ||
// Separate the inputs so that the first element contains the description while | ||
// the rest contains flags. | ||
String[] inputs = input.split(" /"); | ||
String description = inputs[0].substring(9); | ||
|
||
String by = null; | ||
for (int i = 1; i < inputs.length; i++) { | ||
String[] flagWords = inputs[i].split(" "); | ||
if (flagWords[0].equals("by")) { | ||
if (by == null) { | ||
by = inputs[i].substring(3); | ||
} else { | ||
throw new BadInputException("Found duplicate of 'by' flag"); | ||
} | ||
} else { | ||
throw new BadInputException("'%s' is not a known flag".formatted(flagWords[0])); | ||
} | ||
} | ||
|
||
if (by == null) { | ||
throw new BadInputException("Command 'deadline' must have a 'by' flag"); | ||
} | ||
|
||
Deadline newDeadline = new Deadline(description, by); | ||
addTask(newDeadline); | ||
} else if (input.startsWith("event")) { | ||
// Separate the inputs so that the first element contains the description while | ||
// the rest contains flags. | ||
String[] inputs = input.split(" /"); | ||
String description = inputs[0].substring(6); | ||
|
||
String from = null; | ||
String to = null; | ||
for (int i = 1; i < inputs.length; i++) { | ||
String[] flagWords = inputs[i].split(" "); | ||
if (flagWords[0].equals("from")) { | ||
if (from == null) { | ||
from = inputs[i].substring(5); | ||
} else { | ||
throw new BadInputException("Found duplicate of 'from' flag"); | ||
} | ||
} else if (flagWords[0].equals("to")) { | ||
if (to == null) { | ||
to = inputs[i].substring(3); | ||
} else { | ||
throw new BadInputException("Found duplicate of 'to' flag"); | ||
} | ||
} else { | ||
throw new BadInputException("'%s' is not a known flag".formatted(flagWords[0])); | ||
} | ||
} | ||
|
||
if (from == null) { | ||
throw new BadInputException("Command 'event' must have a 'from' flag"); | ||
} | ||
|
||
if (to == null) { | ||
throw new BadInputException("Command 'event' must have a 'to' flag"); | ||
} | ||
|
||
Event newEvent = new Event(description, from, to); | ||
addTask(newEvent); | ||
} else { | ||
throw new BadInputException("'%s' is not a known command".formatted(input)); | ||
} | ||
} catch (MittensException e) { | ||
e.echo(); | ||
} catch (Exception e) { | ||
UnknownException newException = new UnknownException(e.getMessage()); | ||
newException.echo(); | ||
} | ||
} | ||
|
||
exit(); | ||
} | ||
} |
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. Good variable names. Cute cat. |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
public class MittensException extends Exception { | ||
private final String mittensMessage; | ||
private final String helpMessage; | ||
|
||
public MittensException(String message, String mittensMessage, String helpMessage) { | ||
super(message); | ||
this.mittensMessage = mittensMessage; | ||
this.helpMessage = helpMessage; | ||
} | ||
|
||
public String getMittensMessage() { | ||
return this.mittensMessage; | ||
} | ||
|
||
public String getHelpMessage() { | ||
return this.helpMessage; | ||
} | ||
|
||
public void echo() { | ||
String message = """ | ||
|
||
/\\_/\\ %s | ||
>x.x< ( %s ) | ||
/ \\ %s | ||
(___)_/ | ||
|
||
Error: %s | ||
%s | ||
""".formatted("_".repeat(this.getMittensMessage().length() + 2), | ||
this.getMittensMessage(), "-".repeat(this.getMittensMessage().length() + 2), | ||
this.getMessage(), this.helpMessage); | ||
|
||
System.out.println(message); | ||
} | ||
} |
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. Good use of boolean variable names |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
public abstract class Task { | ||
protected String description; | ||
protected boolean isDone; | ||
|
||
public Task(String description) { | ||
this.description = description; | ||
this.isDone = false; | ||
} | ||
|
||
public String getDescription() { | ||
return this.description; | ||
} | ||
|
||
public String getStatusIcon() { | ||
return (this.isDone ? "X" : " "); | ||
} | ||
|
||
public void markAsDone() { | ||
this.isDone = true; | ||
} | ||
|
||
public void markAsNotDone() { | ||
this.isDone = false; | ||
} | ||
|
||
@Override | ||
public String toString() { | ||
return "[" + 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 |
---|---|---|
@@ -0,0 +1,9 @@ | ||
public class UnknownException extends MittensException { | ||
private static final String MITTENS_MESSAGE = "Meow?! Something went wrong..."; | ||
private static final String HELP_MESSAGE = "I'm not sure what went wrong. You can try again," + | ||
" or type 'help' to see a list of commands."; | ||
|
||
public UnknownException(String message) { | ||
super(message, MITTENS_MESSAGE, HELP_MESSAGE); | ||
} | ||
} |
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.
Good variable names