-
Notifications
You must be signed in to change notification settings - Fork 193
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
iP #187
base: master
Are you sure you want to change the base?
iP #187
Changes from 17 commits
0033e6e
9b01139
c113fdf
6db8c92
9df50eb
d779c13
d25eed0
87826c1
2d8013a
8857a32
2aaa1e0
9b1d3fa
c30246f
5531ec3
7695559
8b1d778
bef204e
d3e43c0
6ac8bb0
27a1bf8
2d29b00
6340b5b
560e56c
9dd7b4f
39c63ba
e8b3be1
11a1fda
b07fb0e
c3bf73e
ab62a67
c8541d5
89962ef
1ebb51f
b65f74b
3a328f1
434010a
0a61184
11e68d9
7552d82
3536322
1ffb222
eac98bb
b66e3a0
ad649c6
4093607
0ff28b1
daabba7
0e89a93
cf715c5
3a9590d
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,2 @@ | ||
todo hello | 0 | ||
deadline hello /by Sunday | 1 |
This file was deleted.
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
package duke; | ||
|
||
public class Deadline extends Task { | ||
|
||
protected String by; | ||
|
||
public Deadline(String description, String by) { | ||
super(description); | ||
this.by = by; | ||
} | ||
|
||
@Override | ||
public String toString() { | ||
return "[D][" + super.getStatusIcon() + "] " + super.toString() + "(by: " + by + ")"; | ||
} | ||
|
||
@Override | ||
public String toSave() { | ||
int done = 0; | ||
if (this.isDone) { | ||
done = 1; | ||
} | ||
return "deadline " + description + "/by " + by + " | " + done; | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,265 @@ | ||
package duke; | ||
import java.nio.file.Files; | ||
import java.nio.file.Paths; | ||
import java.nio.file.StandardOpenOption; | ||
import java.util.Scanner; | ||
import java.util.ArrayList; | ||
import java.io.*; | ||
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 could avoid using a wildcard in the import statement by configuring your IDE to make all imported classes to be listed explicitly. |
||
|
||
public class Duke { | ||
//declarations | ||
public static String line = "------------------------------------------------------------------------------------------\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. Perhaps you could make |
||
private static int quitFlag = 0; //if 0, take user input. Otherwise, don't. | ||
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. Lines 11 and 12 are quite obvious and self-explanatory, perhaps you could refrain from repeating the description in a comment. |
||
|
||
//declare task array and keep track of how many tasks stored | ||
public static ArrayList<Task> t = new ArrayList<>(); | ||
public static int taskCount = 0; | ||
|
||
//Program starts with this greeting | ||
public static void start() throws FileNotFoundException, DukeException { | ||
String logo = " ____ _ \n" | ||
+ "| _ \\ _ _| | _____ \n" | ||
+ "| | | | | | | |/ / _ \\\n" | ||
+ "| |_| | |_| | < __/\n" | ||
+ "|____/ \\__,_|_|\\_\\___|\n"; | ||
System.out.println(line + "Hello! I'm Duke.\n" + logo + "What can i do for you?\n" + line); | ||
loadData(); | ||
} | ||
|
||
//Saves Task list into local file | ||
public static void saveData(ArrayList<Task> t) { | ||
String path = "D:\\Documents\\NUS\\Y2S1\\CS2113T\\IP\\UserData.txt"; | ||
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 could use a relative path here rather than an absolute path because your app might cause unpredictable results when used in another computer. |
||
try { | ||
FileWriter fw = new FileWriter(path, false); | ||
PrintWriter pw = new PrintWriter(fw, false); | ||
pw.flush(); | ||
pw.close(); | ||
fw.close(); | ||
for (int i = 0; i < taskCount; i++) { | ||
String input = t.get(i).toSave() + "\n"; | ||
Files.write(Paths.get(path), input.getBytes(), StandardOpenOption.APPEND); | ||
} | ||
} catch (IOException e) { | ||
e.printStackTrace(); | ||
} | ||
} | ||
|
||
//Loads Task list into Duke | ||
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 like how you employ such a great comment which follows "Explain WHAT and WHY, not HOW"! From this week onwards, you could make it even better by adding JavaDoc comments for non-private classes / methods and non-trivial private methods. |
||
public static void loadData() throws FileNotFoundException, DukeException { | ||
File f = new File("D:\\Documents\\NUS\\Y2S1\\CS2113T\\IP\\UserData.txt"); | ||
|
||
Scanner scan = new Scanner(f); | ||
String taskType; | ||
String s; | ||
int taskNumber = 1; //tracks how many tasks read from .txt file so far | ||
|
||
while(scan.hasNext()) //todo hello | 1 | ||
{ //deadline hello /by Sunday | 1 | ||
String data = scan.nextLine(); //event project meeting /at Mon 2-4pm | 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. I am not very sure what these comments mean (go to the extreme right). Perhaps you could rephrase them, or delete them if they are not required. Similar practices are observed in line 91 as well. 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 could rewrite your while loop as per https://se-education.org/guides/conventions/java/index.html#layout so that |
||
String[] arrayString = data.split(" \\| "); | ||
String[] arrayString2 = arrayString[0].split(" "); | ||
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. According to https://nus-cs2113-ay2122s1.github.io/website/se-book-adapted/chapters/codeQuality.html#avoid-magic-numbers, perhaps you could spend some time trying to avoid any sorts of magic literals (there are also found elsewhere such as lines 65 and 67 and |
||
taskType = arrayString2[0]; | ||
s = Integer.toString(taskNumber); | ||
|
||
switch (taskType) { | ||
case "todo": | ||
sayTodo(arrayString[0]); | ||
if (arrayString[1].equals("1")) { | ||
sayDone(s); | ||
} | ||
break; | ||
case "deadline": | ||
sayDeadline(arrayString[0]); | ||
if (arrayString[1].equals("1")) { | ||
sayDone(s); | ||
} | ||
break; | ||
case "event": | ||
sayEvent(arrayString[0]); | ||
if (arrayString[1].equals("1")) { | ||
sayDone(s); | ||
} | ||
break; | ||
default: | ||
} | ||
taskNumber++; | ||
} | ||
scan.close(); | ||
} | ||
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 exceeds 30 LOC, perhaps you could shorten it (e.g., by extracting lines 64 to lines 84 into a method on its own). |
||
|
||
//Program exits with this ending | ||
public static void sayBye(String input) throws DukeException{ //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. Perhaps you could leave a space here before |
||
if (input.length() != 3) { | ||
throw new DukeException("You didn't say bye properly, but i get it! Adios for now!\n"); //bye 9 | ||
} else { | ||
System.out.println(line + "\n" + "Ciao! More tasks to do later!\n" + line); //bye | ||
updateQuitFlag(); | ||
} | ||
System.exit(0); | ||
} | ||
|
||
//updates flag so that no more user input is taken | ||
public static void updateQuitFlag() { | ||
quitFlag = 1; | ||
} | ||
|
||
//Lists out all tasks stored and their statuses | ||
public static void sayList(String input) throws DukeException{ //list | ||
if (input.length() == 4) { | ||
if (taskCount > 0) { | ||
System.out.println(line); | ||
for (int i = 0; i < taskCount; i++) { | ||
System.out.println((i + 1) + ". " + t.get(i).toString() + "\n"); | ||
} | ||
System.out.println(line); | ||
} | ||
else { | ||
throw new DukeException("Hold your horses, we haven't even started listing yet!"); //list when taskCount = 0 | ||
} | ||
} else { | ||
throw new DukeException("Invalid input! Ask me nicely to list!"); //list 7 | ||
} | ||
} | ||
|
||
//Marks a stored task as done | ||
public static void sayDone(String input) throws DukeException { //done 1 | ||
if (taskCount != 0) { | ||
try { | ||
String taskNumber = input.substring(input.lastIndexOf(" ") + 1); | ||
int finalTaskNumber = Integer.parseInt(taskNumber) - 1; | ||
t.get(finalTaskNumber).markAsDone(); | ||
System.out.println(line + "\n" + "Kudos! One less thing to stress about!\n"); //done 1 | ||
System.out.println(" " + t.get(finalTaskNumber).toString() + "\n" + line); | ||
} catch (NullPointerException e) { | ||
System.out.println(line + "\nInvalid task number entered! Please try again!\n" + line); //done 101 | ||
} | ||
} else { | ||
throw new DukeException("Calm down, we haven't even started listing out tasks yet!"); //done 1 when taskCount = 0 | ||
} | ||
} | ||
|
||
//Adds a new todo task and prints it //todo borrow book | ||
public static void sayTodo(String input) { | ||
if (input.length() == 4) { | ||
System.out.println(line + "\nDid you forget what you were gonna say?\n" + line); //todo | ||
} else { | ||
int endIndex = input.length(); | ||
String taskName = input.substring(5, endIndex); | ||
t.add(taskCount, new Todo(taskName)); | ||
System.out.println(line + "\n"); | ||
System.out.println("That's the spirit! I've added this task:\n"); //todo borrow book | ||
System.out.println(t.get(taskCount).toString()); | ||
taskCount++; | ||
System.out.println("\nNow you have " + taskCount + " tasks in the list.\n" + line); | ||
} | ||
} | ||
|
||
//Adds a new deadline task and prints it | ||
public static void sayDeadline(String input) { //deadline return book /by Sunday | ||
if (input.length() == 8) { | ||
System.out.println(line + "\nAre you kidding me? You didn't even tell me what you were supposed to do!\n"); //deadline | ||
System.out.println(line); | ||
} else if (!input.contains("/")) { | ||
System.out.println(line + "\nAre you kidding me? You forgot to tell me your deadline again?\n" + line); //deadline return book | ||
} else { | ||
int endIndex = input.lastIndexOf("/"); | ||
String taskName = input.substring(9, endIndex); | ||
int endIndex2 = input.length(); | ||
String by = input.substring(endIndex + 4, endIndex2); | ||
t.add(taskCount, new Deadline(taskName, by)); | ||
System.out.println(line + "\n"); | ||
System.out.println("Got it. I've added this task:\n"); //deadline return book /by Sunday | ||
System.out.println(t.get(taskCount).toString()); | ||
taskCount++; | ||
System.out.println("\nNow you have " + taskCount + " tasks in the list.\n" + line); | ||
} | ||
} | ||
|
||
//Adds a new event task and prints it | ||
public static void sayEvent(String input) { //event project meeting /at Mon 2-4pm | ||
if (input.length() == 5) { | ||
System.out.println(line + "\nAre you kidding me? You didn't even tell me what you were supposed to do!\n"); //event | ||
System.out.println(line); | ||
} else if (!input.contains("/")) { | ||
System.out.println(line + "\nAre you kidding me? You forgot to tell me your event timing again?\n" + line); //event project meeting | ||
} else { | ||
int endIndex = input.lastIndexOf("/"); | ||
String taskName = input.substring(6, endIndex); | ||
int endIndex2 = input.length(); | ||
String at = input.substring(endIndex + 4, endIndex2); | ||
t.add(taskCount, new Event(taskName, at)); | ||
System.out.println(line + "\n"); | ||
System.out.println("Got it. I've added this task:\n"); //event project meeting /at Tue 3-7pm | ||
System.out.println(t.get(taskCount).toString()); | ||
taskCount++; | ||
System.out.println("\nNow you have " + taskCount + " tasks in the list.\n" + line); | ||
} | ||
} | ||
|
||
//Deletes a stored task | ||
public static void sayDelete(String input) throws DukeException { //delete 1 | ||
if (taskCount != 0) { | ||
try { | ||
String taskNumber = input.substring(input.lastIndexOf(" ") + 1); | ||
int finalTaskNumber = Integer.parseInt(taskNumber) - 1; | ||
Task taskRemoved = t.get(finalTaskNumber); | ||
t.remove(finalTaskNumber); | ||
taskCount--; | ||
System.out.println(line + "\n" + "One more thing outta your life as always...\n"); //delete 1 | ||
System.out.println(" " + taskRemoved.toString() + "\n" + "\nYou now have " + taskCount + " tasks left.\n"); | ||
System.out.println(line); | ||
} catch (IndexOutOfBoundsException e) { | ||
System.out.println(line + "\nInvalid task number entered! Please try again!\n" + line); //delete 101 | ||
} | ||
} else { | ||
throw new DukeException("Calm down, we don't even have tasks yet!"); //delete 1 when taskCount = 0 | ||
} | ||
} | ||
|
||
//Creates scanner, takes in user input & filters it to different methods | ||
public static void inputSort() throws DukeException { | ||
System.out.println("Enter your wish: " + "\n" + line); | ||
while (quitFlag == 0) { | ||
Scanner scan = new Scanner(System.in); | ||
String input = scan.nextLine(); | ||
String actionWord = input.split(" ")[0]; | ||
switch (actionWord) { | ||
case "bye": | ||
sayBye(input); | ||
break; | ||
case "list": | ||
sayList(input); | ||
break; | ||
case "done": | ||
sayDone(input); | ||
saveData(t); | ||
break; | ||
case "todo": | ||
sayTodo(input); | ||
saveData(t); | ||
break; | ||
case "deadline": | ||
sayDeadline(input); | ||
saveData(t); | ||
break; | ||
case "event": | ||
sayEvent(input); | ||
saveData(t); | ||
break; | ||
case "delete": //lvl 7 | ||
sayDelete(input); | ||
saveData(t); | ||
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. Looks like the |
||
break; | ||
default: | ||
System.out.println(line + "\n☹ OOPS!!! I'm sorry, but I don't know what that means :-(\n" + line); | ||
} | ||
} | ||
} | ||
|
||
//Main | ||
public static void main(String[] args) throws DukeException, IOException { | ||
start(); | ||
inputSort(); | ||
} | ||
} | ||
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. All statements here are at the same level of abstraction, great! You have followed of Single Level of Abstraction Principle (SLAP) very well! |
||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
package duke; | ||
|
||
public class DukeException extends Exception { | ||
public DukeException(String errorMessage) { | ||
super(errorMessage); | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
package duke; | ||
|
||
public class Event extends Task { | ||
|
||
protected String at; | ||
|
||
public Event(String description, String at) { | ||
super(description); | ||
this.at = at; | ||
} | ||
|
||
@Override | ||
public String toString() { | ||
return "[E][" + super.getStatusIcon() + "] " + super.toString() + "(at: " + at + ")"; | ||
} | ||
|
||
@Override | ||
public String toSave() { | ||
int done = 0; | ||
if (this.isDone) { | ||
done = 1; | ||
} | ||
return "event " + description + "/at " + at + " | " + done; | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
package duke; | ||
|
||
public class Task { | ||
//attributes | ||
protected String description; | ||
protected boolean isDone; | ||
|
||
//constructor | ||
public Task(String description) { | ||
this.description = description; | ||
this.isDone = false; | ||
} | ||
|
||
//getters | ||
public String getStatusIcon() { | ||
return (isDone ? "X" : " "); | ||
} | ||
|
||
//setters | ||
public void markAsDone() { | ||
isDone = true; | ||
} | ||
|
||
//Returns description of task added | ||
public String toString() { | ||
return description; | ||
} | ||
|
||
public String toSave() { | ||
int done = 0; | ||
if (this.isDone) { | ||
done = 1; | ||
} | ||
return "taskType" + description + " | " + done; | ||
} | ||
} | ||
|
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.
Great to see that you organise your types (e.g., classes) into a package for easier management!