-
Notifications
You must be signed in to change notification settings - Fork 187
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
[Guanlin] iP #187
base: master
Are you sure you want to change the base?
[Guanlin] iP #187
Changes from 11 commits
8d473bd
902153b
37892d1
8c29b77
373d959
6da7d59
b832517
ab3c2a2
a5e25f4
c771a07
068ec72
a0124d8
91efd3c
4ceb8bc
fd754ce
4b7773e
2e7a416
3d32a68
abec695
5b96b6d
2bfc693
c447112
f55da6d
0bc30b2
e69231e
976d60c
96b0adc
d447eeb
48ebd3d
fc09fdc
6bd7337
09bebe3
7107311
6c21a23
f462c50
9b642bc
8f6b521
61d2e5a
0e69b9e
d3581b7
b0ff90a
9f54241
35b1b33
a177300
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
This file was deleted.
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
package Exceptions; | ||
|
||
public class EmptyTaskException extends Exception{ | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
package Exceptions; | ||
|
||
public class MissingDeadlineException extends Exception{ | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
package Exceptions; | ||
|
||
public class MissingStartDateException extends Exception{ | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
package Exceptions; | ||
|
||
public class UnknownInputException extends Exception{ | ||
} |
Original file line number | Diff line number | Diff line change | ||||
---|---|---|---|---|---|---|
@@ -0,0 +1,167 @@ | ||||||
package MainRuntime; | ||||||
|
||||||
import Exceptions.EmptyTaskException; | ||||||
import Exceptions.MissingDeadlineException; | ||||||
import Exceptions.MissingStartDateException; | ||||||
import Exceptions.UnknownInputException; | ||||||
import Tasks.Deadline; | ||||||
import Tasks.Event; | ||||||
import Tasks.Task; | ||||||
import Tasks.ToDo; | ||||||
|
||||||
import java.util.Scanner; | ||||||
|
||||||
public class GermaBot { | ||||||
static int counter = 0; | ||||||
static final String LINE= "____________________________________________"; | ||||||
static Task[] toDoList = new Task[100]; | ||||||
public static int getIdx(String input) { | ||||||
return Integer.parseInt(input.substring(input.indexOf(" ") + 1)) - 1; | ||||||
} | ||||||
|
||||||
public static void createTodo(String input) throws EmptyTaskException { | ||||||
String toDoTask = input.substring(input.indexOf("todo ") + 5); | ||||||
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. what is 5? do avoid magic numbers |
||||||
if (toDoTask.isBlank()) { | ||||||
throw new EmptyTaskException(); | ||||||
} | ||||||
toDoList[counter] = new ToDo(toDoTask); | ||||||
counter++; | ||||||
System.out.println("Gotcha! Added '" + toDoTask + "' to your To Do List!"); | ||||||
} | ||||||
|
||||||
public static void createDeadline(String input) throws EmptyTaskException, MissingDeadlineException { | ||||||
String description = input.replaceFirst("deadline ", ""); | ||||||
if (description.equals("deadline")) { | ||||||
throw new EmptyTaskException(); | ||||||
} | ||||||
int idxOfEndDate = description.indexOf("/"); | ||||||
if (idxOfEndDate == -1) { | ||||||
throw new MissingDeadlineException(); | ||||||
} | ||||||
String date = description.substring( idxOfEndDate + 4); | ||||||
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. what does 4 here stand for? do check through your code for more magic numbers 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.
Suggested change
|
||||||
String toDoTask = description.substring(0, idxOfEndDate - 1); | ||||||
if (toDoTask.isBlank()) { | ||||||
throw new EmptyTaskException(); | ||||||
} | ||||||
toDoList[counter] = new Deadline(description.substring(0, idxOfEndDate - 1), date); | ||||||
counter++; | ||||||
System.out.println("Gotcha! Added '" + toDoTask + "' to your To Do List!" + | ||||||
" You have to finish this by " + date + ", so be reminded!"); | ||||||
} | ||||||
|
||||||
public static void createEvent(String input) throws EmptyTaskException, MissingDeadlineException, | ||||||
MissingStartDateException { | ||||||
String description = input.replaceFirst("event ", ""); | ||||||
int idxOfFrom = description.indexOf("/from"); | ||||||
if (idxOfFrom == -1) { | ||||||
throw new EmptyTaskException(); | ||||||
} | ||||||
int idxOfBy = description.indexOf("/to"); | ||||||
if (idxOfBy == -1) { | ||||||
throw new MissingDeadlineException(); | ||||||
} | ||||||
String startDate = description.substring(idxOfFrom + 6, idxOfBy - 1); | ||||||
if (startDate.isBlank()) { | ||||||
throw new MissingStartDateException(); | ||||||
} | ||||||
String endDate = description.substring(idxOfBy + 4); | ||||||
if (endDate.isBlank()) { | ||||||
throw new MissingDeadlineException(); | ||||||
} | ||||||
String toDoTask = description.substring(0, idxOfFrom - 1); | ||||||
if (toDoTask.isBlank()) { | ||||||
throw new EmptyTaskException(); | ||||||
} | ||||||
toDoList[counter] = new Event(description.substring(0, idxOfFrom - 1), startDate, endDate); | ||||||
counter++; | ||||||
System.out.println("Gotcha! Added '" + toDoTask + "' to your To Do List!" + | ||||||
" This will happen from " + startDate + " to " + endDate + ", so please remember to mark it" + | ||||||
" on your calender! (Or ask me!)"); | ||||||
} | ||||||
|
||||||
public static void createTask(String input) throws UnknownInputException { | ||||||
if (input.contains("todo")) { | ||||||
try { | ||||||
createTodo(input); | ||||||
} catch (EmptyTaskException e) { | ||||||
System.out.println("Uh oh, you did not specify a task to add! Let's try again!"); | ||||||
} | ||||||
} | ||||||
else if (input.contains("deadline")) { | ||||||
try { | ||||||
createDeadline(input); | ||||||
} catch (EmptyTaskException e) { | ||||||
System.out.println("Uh oh, you did not specify a task to add! Let's try again!"); | ||||||
} catch (MissingDeadlineException e) { | ||||||
System.out.println("Uh oh, you did not specify the deadline! Let's try again!"); | ||||||
} | ||||||
|
||||||
} | ||||||
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. why is there a newline between these 2 brackets? |
||||||
else if (input.contains("event")) { | ||||||
try { | ||||||
createEvent(input); | ||||||
} catch (EmptyTaskException e) { | ||||||
System.out.println("Uh oh, you did not specify a task to add! Let's try again!"); | ||||||
} catch (MissingStartDateException e) { | ||||||
System.out.println("Uh oh, you did not specify a start time! Let's try again!"); | ||||||
} catch (MissingDeadlineException e) { | ||||||
System.out.println("Uh oh, you did not specify the deadline! Let's try again!"); | ||||||
} | ||||||
} else { | ||||||
throw new UnknownInputException(); | ||||||
} | ||||||
} | ||||||
|
||||||
public static void main(String[] args) { | ||||||
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 method is very long, try to shorten it |
||||||
String WelcomeMessage = "Hello! MainRuntime.GermaBot here! \n" | ||||||
+ "What may I do for you this fine day?"; | ||||||
System.out.println(LINE); | ||||||
System.out.println(WelcomeMessage); | ||||||
System.out.println(LINE); | ||||||
while (true) { | ||||||
String input; | ||||||
Scanner in = new Scanner(System.in); | ||||||
input = in.nextLine(); | ||||||
if (input.equals("bye")) { | ||||||
break; | ||||||
} | ||||||
if (input.equals("list")) { | ||||||
if (toDoList[0] == null) { | ||||||
System.out.println("Umm... You haven't added any Tasks yet... Let's try adding " + | ||||||
"some now!"); | ||||||
} else { | ||||||
int printCounter = 1; | ||||||
System.out.println("Gotcha! Here are your tasks:"); | ||||||
for (int i = 0; i < counter; i++) { | ||||||
if (toDoList[i] == null) { | ||||||
break; | ||||||
} | ||||||
System.out.println(printCounter + ". " + toDoList[i].toString()); | ||||||
printCounter++; | ||||||
} | ||||||
} | ||||||
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 part is deeply nested, try to avoid deep nesting |
||||||
} else if (input.contains("unmark")) { | ||||||
int idx = getIdx(input); | ||||||
toDoList[idx].setDone(false); | ||||||
System.out.println("Aww, not done? Okay, I'll mark this task as undone: " | ||||||
+ "[" + toDoList[idx].getStatusIcon() + "] " + toDoList[idx].getDescription()); | ||||||
} else if (input.contains("mark")) { | ||||||
int idx = getIdx(input); | ||||||
toDoList[idx].setDone(true); | ||||||
System.out.println("Good job! I'll mark this task as done: " | ||||||
+ "[" + toDoList[idx].getStatusIcon() + "] " + toDoList[idx].getDescription()); | ||||||
} else { | ||||||
try { | ||||||
createTask(input); | ||||||
} catch (UnknownInputException e){ | ||||||
System.out.println("Uhh.. I'm sorry but I'm not quite sure what '" + input + "' means..." + | ||||||
" Remember to include the Task Type in front of the input!!"); | ||||||
} | ||||||
} | ||||||
} | ||||||
String GoodbyeMessage = "Thanks for using me! Hope to see you again soon~!"; | ||||||
System.out.println(LINE); | ||||||
System.out.println(GoodbyeMessage); | ||||||
System.out.println(LINE); | ||||||
} | ||||||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
package Tasks; | ||
|
||
public class Deadline extends Task { | ||
|
||
protected String by; | ||
|
||
public Deadline(String description, String by) { | ||
super(description); | ||
this.by = by; | ||
} | ||
|
||
public String getBy() { | ||
return by; | ||
} | ||
|
||
public void setBy(String date) { | ||
this.by = date; | ||
} | ||
|
||
@Override | ||
public String toString() { | ||
return "[D]" + super.toString() + " (by: " + by + ")"; | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
package Tasks; | ||
|
||
import Tasks.Task; | ||
|
||
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; | ||
} | ||
|
||
public String getFrom() { | ||
return from; | ||
} | ||
|
||
public void setFrom(String from) { | ||
this.from = from; | ||
} | ||
|
||
public String getTo() { | ||
return to; | ||
} | ||
|
||
public void setTo(String to) { | ||
this.to = to; | ||
} | ||
|
||
@Override | ||
public String toString() { | ||
return "[E]" + super.toString() + " (from: " + from + " to: " + to + ")"; | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
package Tasks; | ||
|
||
public class Task { | ||
protected String description; | ||
protected boolean isDone; | ||
|
||
public Task(String description) { | ||
this.description = description; | ||
this.isDone = false; | ||
} | ||
|
||
public String getDescription() { | ||
return description; | ||
} | ||
|
||
public void setDescription(String description) { | ||
this.description = description; | ||
} | ||
|
||
public boolean isDone() { | ||
return isDone; | ||
} | ||
|
||
public void setDone(boolean done) { | ||
isDone = done; | ||
} | ||
|
||
public String getStatusIcon() { | ||
return (isDone ? "X" : " "); // mark done task with X | ||
} | ||
|
||
@Override | ||
public String toString() { | ||
return "[" + this.getStatusIcon() + "] " + this.getDescription(); | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
package Tasks; | ||
|
||
import Tasks.Task; | ||
|
||
public class ToDo extends Task { | ||
public ToDo(String description) { | ||
super(description); | ||
} | ||
|
||
@Override | ||
public String toString() { | ||
return "[T]" + 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.
good job on declaring constant