-
Notifications
You must be signed in to change notification settings - Fork 77
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
Shen Zheqi #59
base: master
Are you sure you want to change the base?
Shen Zheqi #59
Changes from 14 commits
b76cb0d
aed9a47
152c991
a54482c
aa1c3ef
4db3198
18197b3
a45536c
102bac6
0e67341
56457ac
0201672
d5765a9
a85507c
903a7cf
26ca4d2
b46cc81
dcefdd6
b165da1
eb3ea33
e50a633
2ec514f
89d8414
1749807
502caa5
5b7b9c3
e3a5721
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,197 @@ | ||
import duke.Deadline; | ||
import duke.Event; | ||
import duke.Task; | ||
import duke.ToDo; | ||
|
||
import java.util.Scanner; | ||
import java.util.Arrays; | ||
import java.util.ArrayList; | ||
|
||
public class Duke { | ||
|
||
public static final String BYE = "bye"; | ||
|
||
public static void main(String[] args) { | ||
String logo = " ____ _ \n" | ||
+ "| _ \\ _ _| | _____ \n" | ||
+ "| | | | | | | |/ / _ \\\n" | ||
+ "| |_| | |_| | < __/\n" | ||
+ "|____/ \\__,_|_|\\_\\___|\n"; | ||
System.out.println("Hello from\n" + logo); | ||
printWelcomeMessage(); | ||
ArrayList<Task> taskLists = new ArrayList<Task>(); | ||
|
||
|
||
String userInput = getUserInput(); | ||
|
||
while(!userInput.equals("bye")){ | ||
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. Explicit use of switch 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. Since you have already declared the constant string |
||
String[] userInputSplit = userInput.split(" "); | ||
switch(userInputSplit[0]){ | ||
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 job on using correct indent on Switch! |
||
case "list": | ||
printTaskList(taskLists, Task.numOfTasks); | ||
break; | ||
|
||
case "mark": | ||
int markDoneIndex = Integer.parseInt(userInputSplit[1]) - 1; | ||
taskLists.get(markDoneIndex).setIsDone(true); | ||
printSetDoneMessage(taskLists.get(markDoneIndex)); | ||
break; | ||
|
||
case "todo": | ||
try{ | ||
addTodo(taskLists, userInputSplit); | ||
} | ||
catch(EmptyDescriptionException e){ | ||
System.out.println(e); | ||
} | ||
|
||
break; | ||
|
||
case "event": | ||
try{ | ||
addEvent(taskLists, userInputSplit); | ||
} | ||
catch(StringIndexOutOfBoundsException e){ | ||
System.out.println(e.toString()); | ||
}; | ||
break; | ||
|
||
case "deadline": | ||
addDeadline(taskLists, userInputSplit); | ||
break; | ||
|
||
|
||
case "unmark": | ||
int markNotDoneIndex = Integer.parseInt(userInputSplit[1]) - 1; | ||
taskLists.get(markNotDoneIndex).setIsDone(false); | ||
printSetNotDoneMessage(taskLists.get(markNotDoneIndex)); | ||
break; | ||
|
||
case "delete": | ||
int deleteIndex = Integer.parseInt(userInputSplit[1]) - 1; | ||
deleteTask(taskLists, deleteIndex); | ||
break; | ||
|
||
default: | ||
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. Default should have the same indent level as your other cases in |
||
printDontKnowMessage(); | ||
} | ||
userInput = getUserInput(); | ||
} | ||
|
||
printByeMessage(); | ||
} | ||
|
||
private static void deleteTask(ArrayList<Task> taskLists, int deleteIndex) { | ||
System.out.println(" ____________________________________________________________"); | ||
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. Try to avoid magic strings, if there are strings like this that need to be reused, it is good to create a constant called Code Quality Guideline: Avoid Magic Literals |
||
System.out.println(" Noted. I've removed this task:"); | ||
System.out.println(String.format(" %s", taskLists.get(deleteIndex).toString())); | ||
taskLists.remove(deleteIndex); | ||
Task.numOfTasks --; | ||
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. No need to leave a space for post Increments as per the Java coding standard |
||
System.out.println(" Now you have " + Task.numOfTasks + " tasks in the list."); | ||
System.out.println(" ____________________________________________________________"); | ||
} | ||
|
||
private static void addTodo(ArrayList<Task> taskLists, String[] userInputSplit) throws EmptyDescriptionException { | ||
if (userInputSplit.length < 2){ | ||
throw new EmptyDescriptionException(); | ||
} | ||
String[] inputArrayWithoutType = Arrays.copyOfRange(userInputSplit,1, userInputSplit.length); | ||
String description = String.join(" ", inputArrayWithoutType); | ||
ToDo newToDo = new ToDo(description); | ||
taskLists.add(newToDo); | ||
Task.numOfTasks ++; | ||
printEchoInput(newToDo); | ||
} | ||
|
||
private static void addEvent(ArrayList<Task> taskLists, String[] userInputSplit) throws StringIndexOutOfBoundsException{ | ||
|
||
String[] inputArrayWithoutType = Arrays.copyOfRange(userInputSplit, 1, userInputSplit.length); | ||
String inputWithoutType = String.join(" ", inputArrayWithoutType); | ||
if (inputWithoutType.length() < 3){ | ||
throw new StringIndexOutOfBoundsException(); | ||
} | ||
String description = inputWithoutType.split(" /at ")[0]; | ||
String time = inputWithoutType.split(" /at ")[1]; | ||
Event newEvent = new Event(description, time); | ||
taskLists.add(newEvent); | ||
Task.numOfTasks ++; | ||
printEchoInput(newEvent); | ||
} | ||
// add 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. This seems like a reminder to yourself to add exceptions. Avoid personal comments that are private notes to yourself, instead comments should be written to the reader to improve readability. If not necessary here, can remove it altogether. Code quality guideline: Write to the Reader |
||
|
||
private static void addDeadline(ArrayList<Task> taskLists, String[] userInputSplit) { | ||
String[] inputArrayWithoutType = Arrays.copyOfRange(userInputSplit,1, userInputSplit.length); | ||
String inputWithoutType = String.join(" ", inputArrayWithoutType); | ||
String description = inputWithoutType.split(" /by ")[0]; | ||
String deadline = inputWithoutType.split(" /by ")[1]; | ||
Deadline newDeadline = new Deadline(description, deadline); | ||
taskLists.add(newDeadline); | ||
Task.numOfTasks ++; | ||
printEchoInput(newDeadline); | ||
} | ||
// add exception | ||
|
||
private static void printDontKnowMessage(){ | ||
String dontKnowMessage = " ____________________________________________________________\n" | ||
+ " ☹ OOPS!!! I'm sorry, but I don't know what that means :-(\n" | ||
+ " ____________________________________________________________"; | ||
System.out.println(dontKnowMessage); | ||
} | ||
|
||
public static String getUserInput(){ | ||
String line; | ||
Scanner in = new Scanner(System.in); | ||
line = in.nextLine(); | ||
return line; | ||
} | ||
|
||
public static void printEchoInput(Task task){ | ||
System.out.println(" ____________________________________________________________"); | ||
System.out.println(" Got it. I've added this task:"); | ||
System.out.println(String.format(" %s", task.toString())); | ||
System.out.println(String.format(" Now you have %d tasks in the list.", Task.numOfTasks)); | ||
System.out.println(" ____________________________________________________________"); | ||
} | ||
|
||
public static void printWelcomeMessage() { | ||
String welcomeMessage = | ||
" ____________________________________________________________\n" | ||
+ " Hello! I'm Duke\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. Maybe using System.lineSeparator() is better than \n |
||
+ " What can I do for you?\n" | ||
+ " ____________________________________________________________"; | ||
System.out.println(welcomeMessage); | ||
} | ||
|
||
public static void printByeMessage() { | ||
String byeMessage = | ||
" ____________________________________________________________\n" | ||
+ " Bye. Hope to see you again soon!\n" | ||
+ " ____________________________________________________________"; | ||
System.out.println(byeMessage); | ||
} | ||
|
||
// replace count | ||
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. Write comments to the reader. Can remove unnecessary comments if they are not to the reader. Coding Standard Guideline: Comment minimally, but sufficiently |
||
public static void printTaskList(ArrayList<Task> taskLists, int count){ | ||
System.out.println(" ____________________________________________________________"); | ||
System.out.println(" Here are the tasks in your list:"); | ||
for (int i=0; i<count; i++){ | ||
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 = 0; i < count , maybe more spaces |
||
Task currTask = taskLists.get(i); | ||
|
||
int index = i + 1; | ||
System.out.println(" " + index + "." + currTask.toString()); | ||
} | ||
System.out.println(" ____________________________________________________________"); | ||
} | ||
|
||
public static void printSetDoneMessage(Task task){ | ||
System.out.println(" ____________________________________________________________"); | ||
System.out.println(" Nice! I've marked this task as done:"); | ||
System.out.println(" " + task.toString()); | ||
System.out.println(" ____________________________________________________________"); | ||
} | ||
|
||
public static void printSetNotDoneMessage(Task task){ | ||
System.out.println(" ____________________________________________________________"); | ||
System.out.println(" OK, I've marked this task as not done yet:"); | ||
System.out.println(" " + task.toString()); | ||
System.out.println(" ____________________________________________________________"); | ||
} | ||
|
||
|
||
} | ||
|
||
//Impliment save |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
public class EmptyDescriptionException extends Exception{ | ||
public String toString(){ | ||
String errorMessage = " ____________________________________________________________\n" | ||
+ " ☹ OOPS!!! The description of a todo cannot be empty.\n" | ||
+ " ____________________________________________________________\n"; | ||
return errorMessage; | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
public class StringIndexOutOfBoundsException extends Exception{ | ||
public String toString(){ | ||
String errorMessage = " ____________________________________________________________\n" | ||
+ " ☹ OOPS!!! The please give a valid input.\n" | ||
+ " ____________________________________________________________\n"; | ||
return errorMessage; | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
package duke; | ||
|
||
public class Deadline extends Task{ | ||
protected String deadline; | ||
|
||
public Deadline(String description, String deadline){ | ||
super(description); | ||
this.deadline = deadline; | ||
} | ||
|
||
public String toString(){ | ||
return String.format("[D]%s (by: %s)", super.toString(), deadline); | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
package duke; | ||
|
||
public class Event extends Task{ | ||
protected String time; | ||
|
||
public Event(String description, String time){ | ||
super(description); | ||
this.time = time; | ||
} | ||
|
||
public String toString(){ | ||
return String.format("[E]%s (at: %s)", super.toString(), time); | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
package duke; | ||
|
||
public class Task { | ||
private boolean isDone; | ||
String description; | ||
public static int numOfTasks = 0; | ||
|
||
public Task (String description){ | ||
this.description = description; | ||
isDone = false; | ||
} | ||
|
||
public boolean getIsDone(){ | ||
return isDone; | ||
} | ||
|
||
public String getDescription(){ | ||
return description; | ||
} | ||
|
||
public void setIsDone(boolean isDone){ | ||
this.isDone = isDone; | ||
} | ||
|
||
public static int getNumOfTasks(){ | ||
return numOfTasks; | ||
} | ||
|
||
public String toString(){ | ||
String isDoneNotation; | ||
if (isDone == true){ | ||
isDoneNotation = "[X]"; | ||
} | ||
else {isDoneNotation = "[ ]";} | ||
|
||
return isDoneNotation + " " + description; | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
package duke; | ||
|
||
public class ToDo extends Task{ | ||
|
||
public ToDo(String description){ | ||
super(description); | ||
} | ||
|
||
public String toString(){ | ||
return String.format("[T]%s", 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.
Avoid long methods, try to break down methods longer than 30 lines of code into smaller methods.
Code Quality Guideline: Avoid Long Methods