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

iP #187

Open
wants to merge 50 commits into
base: master
Choose a base branch
from
Open

iP #187

Show file tree
Hide file tree
Changes from 17 commits
Commits
Show all changes
50 commits
Select commit Hold shift + click to select a range
0033e6e
Add Level 0
Aug 26, 2021
9b01139
Echo's user input
Aug 26, 2021
c113fdf
Add Level-2
Aug 26, 2021
6db8c92
add level-3
Aug 29, 2021
9df50eb
Code quality improved
Aug 29, 2021
d779c13
Add level-4
Sep 2, 2021
d25eed0
Improve Code Quality
Sep 2, 2021
87826c1
auto testing
Sep 2, 2021
2d8013a
Add A-Exceptions class
Sep 9, 2021
8857a32
Merge branch 'branch-Level-5'
Sep 9, 2021
2aaa1e0
Add A-Packages
Sep 9, 2021
9b1d3fa
Merge branch 'branch-A-Packages'
Sep 9, 2021
c30246f
Add Level-6
Sep 13, 2021
5531ec3
Add Level-7
Sep 13, 2021
7695559
Add Level-7 (latest)
Sep 14, 2021
8b1d778
Merge branch 'branch-Level-6'
Sep 14, 2021
bef204e
Merge branch 'branch-Level-7'
Sep 14, 2021
d3e43c0
Add A-Jar
Sep 16, 2021
6ac8bb0
Add A-Jar updated
Sep 16, 2021
27a1bf8
Add Level-8
Sep 22, 2021
2d29b00
Add Level-9
Sep 22, 2021
6340b5b
Add new classes v1
Sep 22, 2021
560e56c
Merge pull request #1 from pragyan01/branch-Level-8
pragyan01 Sep 22, 2021
9dd7b4f
Merge branch 'master' of https://github.com/pragyan01/ip
Sep 22, 2021
39c63ba
Merge branch 'master' into branch-Level-9
Sep 22, 2021
e8b3be1
Merge pull request #2 from pragyan01/branch-Level-9
pragyan01 Sep 22, 2021
11a1fda
Merge branch 'master' of https://github.com/pragyan01/ip
Sep 22, 2021
b07fb0e
txt file
Sep 22, 2021
c3bf73e
level-9 latest
Sep 22, 2021
ab62a67
Merge branch 'branch-Level-9'
Sep 22, 2021
c8541d5
Level-8 latest
Sep 22, 2021
89962ef
Updated Merge of Level-8 & Level-9 into Master. Final
Sep 22, 2021
1ebb51f
Add documentation
Sep 24, 2021
b65f74b
All A-JavaDoc comments added
Sep 27, 2021
3a328f1
Merge branch 'master' into branch-A-JavaDoc
Sep 27, 2021
434010a
add final copy of java doc commends
Sep 27, 2021
0a61184
Merge branch 'branch-A-JavaDoc'
Sep 27, 2021
11e68d9
Add documentation-latest copy
Sep 27, 2021
7552d82
Set theme jekyll-theme-cayman
pragyan01 Sep 28, 2021
3536322
Add UG
Sep 28, 2021
1ffb222
Merge branch 'master' of https://github.com/pragyan01/ip
Sep 28, 2021
eac98bb
Add UG-latest version
Sep 29, 2021
b66e3a0
final version of ip
Sep 29, 2021
ad649c6
added some small changes to UG
Sep 30, 2021
4093607
Set theme jekyll-theme-hacker
pragyan01 Sep 30, 2021
0ff28b1
Set theme jekyll-theme-time-machine
pragyan01 Sep 30, 2021
daabba7
Merge branch 'master' of https://github.com/pragyan01/ip
Sep 30, 2021
0e89a93
add final version ip
Sep 30, 2021
cf715c5
Set theme jekyll-theme-hacker
pragyan01 Sep 30, 2021
3a9590d
Merge branch 'master' of https://github.com/pragyan01/ip
Sep 30, 2021
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
2 changes: 2 additions & 0 deletions UserData.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
todo hello | 0
deadline hello /by Sunday | 1
10 changes: 0 additions & 10 deletions src/main/java/Duke.java

This file was deleted.

25 changes: 25 additions & 0 deletions src/main/java/duke/Deadline.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
package duke;
Copy link

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!


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;
}
}
265 changes: 265 additions & 0 deletions src/main/java/duke/Duke.java
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.*;
Copy link

Choose a reason for hiding this comment

The 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";
Copy link

@zikunz zikunz Sep 14, 2021

Choose a reason for hiding this comment

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

Perhaps you could make line final if it does not change, and change the naming accordingly (i.e., LINE).

private static int quitFlag = 0; //if 0, take user input. Otherwise, don't.
Copy link

Choose a reason for hiding this comment

The 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";
Copy link

Choose a reason for hiding this comment

The 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
Copy link

Choose a reason for hiding this comment

The 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
Copy link

@zikunz zikunz Sep 14, 2021

Choose a reason for hiding this comment

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

Copy link

Choose a reason for hiding this comment

The 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 { appear as the same line as while().

String[] arrayString = data.split(" \\| ");
String[] arrayString2 = arrayString[0].split(" ");
Copy link

@zikunz zikunz Sep 14, 2021

Choose a reason for hiding this comment

The 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 T in Todo class) to further improve readability?

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();
}
Copy link

Choose a reason for hiding this comment

The 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
Copy link

@zikunz zikunz Sep 14, 2021

Choose a reason for hiding this comment

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

Perhaps you could leave a space here before { for layout consistency.

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);
Copy link

Choose a reason for hiding this comment

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

Looks like the "saveData(t)" is repeated mutiple times under different case statements, you could consider calling it only once to avoid repetitive code.

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();
}
}
Copy link

Choose a reason for hiding this comment

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


7 changes: 7 additions & 0 deletions src/main/java/duke/DukeException.java
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);
}
}
25 changes: 25 additions & 0 deletions src/main/java/duke/Event.java
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;
}
}
37 changes: 37 additions & 0 deletions src/main/java/duke/Task.java
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;
}
}

Loading