Skip to content
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

Open
wants to merge 27 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 14 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Empty file added .Rhistory
Empty file.
199 changes: 193 additions & 6 deletions src/main/java/Duke.java
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>();


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

String userInput = getUserInput();

while(!userInput.equals("bye")){

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Explicit use of switch

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Since you have already declared the constant string BYE, can be reused here

String[] userInputSplit = userInput.split(" ");
switch(userInputSplit[0]){

Choose a reason for hiding this comment

The 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:

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Default should have the same indent level as your other cases in switch, as per the Java coding standard.

printDontKnowMessage();
}
userInput = getUserInput();
}

printByeMessage();
}

private static void deleteTask(ArrayList<Task> taskLists, int deleteIndex) {
System.out.println(" ____________________________________________________________");

Choose a reason for hiding this comment

The 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 DIVIDER and reusing it at every occurence to improve readability.

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 --;

Choose a reason for hiding this comment

The 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

Choose a reason for hiding this comment

The 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"

Choose a reason for hiding this comment

The 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

Choose a reason for hiding this comment

The 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++){

Choose a reason for hiding this comment

The 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
8 changes: 8 additions & 0 deletions src/main/java/EmptyDescriptionException.java
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;
}
}
8 changes: 8 additions & 0 deletions src/main/java/StringIndexOutOfBoundsException.java
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;
}
}
14 changes: 14 additions & 0 deletions src/main/java/duke/Deadline.java
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);
}
}
14 changes: 14 additions & 0 deletions src/main/java/duke/Event.java
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);
}
}
38 changes: 38 additions & 0 deletions src/main/java/duke/Task.java
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;
}
}
12 changes: 12 additions & 0 deletions src/main/java/duke/ToDo.java
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());
}
}