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

[Wang Guanlin] iP #468

Open
wants to merge 86 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 51 commits
Commits
Show all changes
86 commits
Select commit Hold shift + click to select a range
d839859
Add Gradle support
May 24, 2020
ab74434
Level-1: Added Greet, Echo, Exit functions for Duke
WangGLJoseph Aug 18, 2021
c8163cf
Included gitignore file, ignore all files with .class extension
WangGLJoseph Aug 18, 2021
7f6a9e4
Level-2: Added Add, List functions for Duke
WangGLJoseph Aug 18, 2021
073b8bc
Created a Task class to represent tasks added by user
WangGLJoseph Aug 18, 2021
94a2613
Changed Duke.java to represent userInputs as an ArrayList of Task obj…
WangGLJoseph Aug 18, 2021
29a7b43
Removed deprecated code representing userInputs as an ArrayList of St…
WangGLJoseph Aug 18, 2021
86734d8
Updated scanner to read userInput by using next() instead of nextLine()
WangGLJoseph Aug 18, 2021
0704af3
Level-3: Added Mark as Done function for Duke
WangGLJoseph Aug 18, 2021
840767d
Added field for task type and getter method for task type icon
WangGLJoseph Aug 18, 2021
bdeaad4
Updated instantiation of Task objects to include a taskType parameter
WangGLJoseph Aug 18, 2021
a792272
Abstracted out logic for beautifying task and list
WangGLJoseph Aug 18, 2021
1327162
Updated scanner to use nextLine after first command has been checked
WangGLJoseph Aug 18, 2021
541a1dd
Added constructor for Task that took in an additional string as remin…
WangGLJoseph Aug 18, 2021
c48c98d
Updated todo and deadline cases to be recognizable as userInput
WangGLJoseph Aug 18, 2021
57a57d3
Added task reminder for Deadline tasks
WangGLJoseph Aug 19, 2021
5b64f0e
Removed unnecesary use of delimiter for Deadline tasks
WangGLJoseph Aug 19, 2021
213d350
Updated listBeautify to use taskBeautify method
WangGLJoseph Aug 19, 2021
ff40331
Level-4: Added ToDos, Events, Deadlines functions for Duke
WangGLJoseph Aug 19, 2021
23a7854
Updated to use Inheritance to support multiple task types (ToDo, Even…
WangGLJoseph Aug 19, 2021
20904d1
Fixed bug where type of Event was displayed as [D]
WangGLJoseph Aug 19, 2021
abba5bc
A-TextUiTesting: Used the input/output redirection technique to semi-…
WangGLJoseph Aug 19, 2021
582d814
Removed deprecated method TaskBeautify() from Duke
WangGLJoseph Aug 19, 2021
0e724a6
Added DukeException to handle errors in Duke regarding invalid user i…
WangGLJoseph Aug 19, 2021
f854a11
Level-5: Added exceptions for unknown and incomplete user inputs
WangGLJoseph Aug 19, 2021
153032a
Level-6: Added Delete function to Duke, tasks stored in ArrayList<Task>
WangGLJoseph Aug 19, 2021
287a6b4
Updated text-ui-test to work with Duke Level-6
WangGLJoseph Aug 19, 2021
a008ab8
Change indentation for switch case to follow module coding standards
WangGLJoseph Aug 26, 2021
f2e7526
Add duke.txt with relative filePath
WangGLJoseph Aug 26, 2021
1a95507
Update Duke with filePath to duke.txt
WangGLJoseph Aug 26, 2021
51f6c23
Handle creation of folder and file if not already present
WangGLJoseph Aug 26, 2021
685755b
Add functinality to write to duke.txt
WangGLJoseph Aug 26, 2021
8688ae6
Add skeletal method for copying from duke.txt and parsing it
WangGLJoseph Aug 26, 2021
b12e45e
Updated constructor of various task types
WangGLJoseph Aug 26, 2021
845d1b1
Revert constructor of various task types
WangGLJoseph Aug 26, 2021
6da91b1
Add parse method to convert duke.txt to taskArrayList
WangGLJoseph Aug 26, 2021
54b588f
Fix bug where parse() was called twice incorrectly
WangGLJoseph Aug 26, 2021
d0a5a2e
Level-7: Add Save and Load duke.txt for Duke
WangGLJoseph Aug 26, 2021
bfb6cad
Update Deadline and Event to parse task description as string of date
WangGLJoseph Aug 27, 2021
609beb1
Level-8: Add Dates and Times functionality to Duke
WangGLJoseph Aug 27, 2021
ee37691
Increment Duke display level
WangGLJoseph Aug 27, 2021
281a595
Merge branch 'branch-Level-8'
WangGLJoseph Aug 27, 2021
9e9109a
Add skeletal classes for more OOP
WangGLJoseph Aug 27, 2021
7d0c2ff
Update skeletal class files to make Duke more OOP
WangGLJoseph Aug 31, 2021
6415efd
Put all classes into cs2103.duke package
WangGLJoseph Aug 31, 2021
1195f8d
Add JUnit Tests for Duke
WangGLJoseph Aug 31, 2021
9a5e6fc
Update duke.txt initialisation to enable use of .jar files
WangGLJoseph Aug 31, 2021
c276a32
Add complete JavaDoc for all classes in Duke
WangGLJoseph Aug 31, 2021
5524ba8
Remove unnecessary use of the 'this' keyword
WangGLJoseph Aug 31, 2021
c72dbd8
Level-9: Add find function for Duke
WangGLJoseph Aug 31, 2021
13d56db
Merge branch 'branch-A-CodingStandard'
WangGLJoseph Aug 31, 2021
596cb63
Merge branch 'branch-Level-9'
WangGLJoseph Aug 31, 2021
d6af729
Update README.md
WangGLJoseph Sep 1, 2021
e054745
Merge branch 'master' of https://github.com/WangGLJoseph/ip
WangGLJoseph Sep 2, 2021
0c633a2
Merge branch 'add-gradle-support' into branch-A-Gradle
WangGLJoseph Sep 2, 2021
613f9ce
Add DukeLauncher as part of JavaFX basics
WangGLJoseph Sep 2, 2021
60e607a
Update README.md
WangGLJoseph Sep 6, 2021
379ba30
Extract GUI logic out of Duke and into Main
WangGLJoseph Sep 8, 2021
1ac4e8f
Update README.md
WangGLJoseph Sep 8, 2021
40f61c6
Create new package for controllers
WangGLJoseph Sep 8, 2021
04bbebc
Merge branch 'master' of https://github.com/WangGLJoseph/ip
WangGLJoseph Sep 8, 2021
53dafd7
Level-10: Add GUI for Duke
WangGLJoseph Sep 8, 2021
8daf0b5
Merge branch 'branch-Level-10'
WangGLJoseph Sep 8, 2021
f83d529
Update Duke to use GUI
WangGLJoseph Sep 8, 2021
9c7e428
Set font in GUI as Courier New
WangGLJoseph Sep 18, 2021
b5c3742
Make Duke wait before closing the window
WangGLJoseph Sep 18, 2021
9821713
Add assertions for key assumptions in the code
WangGLJoseph Sep 19, 2021
3638fd6
Update method header comments to follow the coding standard
WangGLJoseph Sep 19, 2021
6af29d8
Update DialogBox to display more text
WangGLJoseph Sep 19, 2021
a70840f
TaskList class: Apply SLAP to shorten methods of adding tasks
WangGLJoseph Sep 19, 2021
c8a852a
Update .gitignore to ignore data/duke.txt
WangGLJoseph Sep 19, 2021
1e509d0
Remove data within data/duke.txt
WangGLJoseph Sep 19, 2021
bf8166f
DukeParser class: Apply SLAP to shorten methods of parsing data
WangGLJoseph Sep 19, 2021
0a6303e
Merge pull request #2 from WangGLJoseph/branch-A-Assertions
WangGLJoseph Sep 19, 2021
1d7cac7
Merge branch 'master' of https://github.com/WangGLJoseph/ip
WangGLJoseph Sep 19, 2021
b067a25
Merge branch 'master' into branch-A-CodeQuality
WangGLJoseph Sep 19, 2021
5721b1c
Merge pull request #3 from WangGLJoseph/branch-A-CodeQuality
WangGLJoseph Sep 19, 2021
e083605
Merge branch 'master' of https://github.com/WangGLJoseph/ip
WangGLJoseph Sep 19, 2021
75661c5
Add functionality to undo the last undo-able command
WangGLJoseph Sep 19, 2021
7f59eb6
Add Ui.png
WangGLJoseph Sep 19, 2021
49dca7b
Update user guide for Duke
WangGLJoseph Sep 19, 2021
e010b92
Set theme jekyll-theme-time-machine
WangGLJoseph Sep 19, 2021
5430e9f
Update formatting in user guide
WangGLJoseph Sep 19, 2021
86a2f73
Merge branch 'master' of https://github.com/WangGLJoseph/ip
WangGLJoseph Sep 19, 2021
4a7a59b
Remove emojis
WangGLJoseph Sep 19, 2021
cdb67d8
Update goodbye message
WangGLJoseph Sep 19, 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
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -15,3 +15,4 @@ bin/

/text-ui-test/ACTUAL.txt
text-ui-test/EXPECTED-UNIX.TXT
*.class
3 changes: 3 additions & 0 deletions data/duke.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
1.[T][ ] 1
2.[E][ ] 2 (at: 2022 02 02)
3.[D][ ] 3 (by: 2021 10 10)
10 changes: 0 additions & 10 deletions src/main/java/Duke.java

This file was deleted.

3 changes: 3 additions & 0 deletions src/main/java/META-INF/MANIFEST.MF
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
Manifest-Version: 1.0
Main-Class: cs2103.duke.Duke

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

import java.time.LocalDate;
import java.time.format.DateTimeFormatter;

/**
* This class encapsulates a Deadline Task object, which inherits from the Task class.
*/
public class Deadline extends Task {
protected int index;
Copy link
Member

Choose a reason for hiding this comment

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

I think it is better to use less general names and more specific names for better understanding by other readers.
For example, you can change the name here to indicate the context of the index.

protected String by;
Copy link
Member

Choose a reason for hiding this comment

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

Likewise for the variable "by", can change the name to be more specific.

protected LocalDate date;

public Deadline(int index, String description, String by) {
super(index, description);
this.index = index;
this.by = by;
this.date = LocalDate.parse(by);
}

@Override
public String toString() {
return "[D]" + super.toString() + " (by: " + date.format(DateTimeFormatter.ofPattern("yyyy MM dd")) + ")";
}
}
57 changes: 57 additions & 0 deletions src/main/java/cs2103/duke/Duke.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
package cs2103.duke;

import java.util.Scanner;
import java.io.IOException;

/**
* This class encapsulates a Duke chat-bot.
*/
public class Duke {
private final String dukeFilePath;
private final Storage storage;
private TaskList tasks;
private Ui ui;

public Duke(String dukeFilePath) {
this.dukeFilePath = dukeFilePath;
storage = new Storage(dukeFilePath);
try {
tasks = new TaskList(storage.load());
} catch (DukeException | IOException e) {
System.out.println(ui.showLoadingError());
tasks = new TaskList();
}
ui = new Ui(tasks);
}

public void run() {
System.out.println(ui.showWelcome());
boolean canExit = false;
Copy link
Member

Choose a reason for hiding this comment

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

Awesome! Good use of descriptive name for boolean variables!

// Scanner to read user inputs
Scanner scanner = new Scanner(System.in);

while (!canExit) {
String userInput = scanner.next();
try {
if (userInput.equals("bye")) { // user inputs "bye", set canExit to true and Exit
canExit = true;
// store task list
String temp = tasks.listBeautify();
storage.overwriteFile(dukeFilePath, temp);
System.out.println(ui.showGoodbye());
} else { // check first input
Copy link
Member

Choose a reason for hiding this comment

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

Shouldn't comments be on separate lines as code?

System.out.println(ui.handleInput(scanner, userInput, tasks));
}
} catch (DukeException | IOException e) {
e.printStackTrace(); // print stack trace for e
}
}
}

public static void main(String[] args) throws IOException {
String dukeFilePath = "./data/duke.txt";
Storage s = new Storage(dukeFilePath);
s.initialize();
new Duke(dukeFilePath).run();
}
}
18 changes: 18 additions & 0 deletions src/main/java/cs2103/duke/DukeException.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
package cs2103.duke;

/**
* This class encapsulates the exception thrown when the user enters an erroneous input.
*/
public class DukeException extends Exception {
private static String msg;

public DukeException(String errorMessage) {
super(errorMessage);
msg = errorMessage;
}

@Override
public String toString() {
return ("Oh No! I do not understand your input due to the following problem:\n" + msg);
}
}
111 changes: 111 additions & 0 deletions src/main/java/cs2103/duke/DukeParser.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,111 @@
package cs2103.duke;

import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Scanner;

/**
* This class encapsulates the parsing of duke.txt to convert the contents of the file
* into an ArrayList.
*/
public class DukeParser {
private String filePath;

public DukeParser(String dukeFilePath) {
this.filePath = dukeFilePath;
}

/**
* This method copies the contents of the file from filePath and
* converts them into Task objects into the taskArrayList with the
* help of the parse() method.
*
* @return TaskArrayList the ArrayList to copy the information to.
* @throws IOException If filePath does not exist.
*/
public ArrayList<Task> copyFileContents() throws IOException {
File f = new File(this.filePath); // create a File for the given file path
Scanner s = new Scanner(f); // create a Scanner using the File as the source
StringBuilder targetBuilder = new StringBuilder();
while (s.hasNext()) {
targetBuilder.append(s.nextLine()).append("\n");
}

return parse(targetBuilder.toString());
}

/**
* This method parses the string copied from duke.txt and converts them into task objects
* into the taskArrayList.
*
* @param toParse The string to parse.
* @return A taskArrayList made up of tasks.
*/
public static ArrayList<Task> parse(String toParse) {
ArrayList<Task> result = new ArrayList<>();
Scanner ps = new Scanner(toParse); // passes whole file into the scanner
// [[T][ ] 1, [E][ ] 2 (at: 2), [D][ ] 3 (by: 3)]
while (ps.hasNextLine()) {
String nLine = ps.nextLine(); // parse one line at a time
int ref = 3; // reference point
char taskType = nLine.charAt(ref);
boolean isDone = nLine.charAt(ref + 3) == 'X';
int strLength = nLine.length();
switch (taskType) {
case 'T':
Copy link
Member

Choose a reason for hiding this comment

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

Shouldn't case body contain less code? Maybe can try to put the code here into a function then call the function.

String todoName = nLine.substring(ref + 5, strLength);
Task newestTodo = new ToDo(result.size(), todoName);
if (isDone) {
newestTodo.markAsDone();
}
result.add(newestTodo);
break;
case 'D':
String deadlineInfo = nLine.substring(ref + 5, strLength);
String[] arrD = deadlineInfo.split("\\(by: ", 2);
String deadlineName = arrD[0];
String deadlineReminder = arrD[1].substring(0, arrD[1].length() - 1);
deadlineReminder = parseDate(deadlineReminder);
Task newestDeadline = new Deadline(result.size(), deadlineName, deadlineReminder);
result.add(newestDeadline);
if (isDone) {
newestDeadline.markAsDone();
}
break;
case 'E':
String eventInfo = nLine.substring(ref + 5, strLength);
String[] arrE = eventInfo.split("\\(at: ", 2);
String eventName = arrE[0];
String eventReminder = arrE[1].substring(0, arrE[1].length() - 1);
eventReminder = parseDate(eventReminder);
Task newestEvent = new Event(result.size(), eventName, eventReminder);
result.add(newestEvent);
if (isDone) {
newestEvent.markAsDone();
}
break;
default:
System.out.println("Unknown input");
break;
}
}
return result;
}

/**
* Parses input string in the format "yyyy MM dd" and returns it in the format
* "YYYY-MM-DD".
*
* @param input String in format "MMM d yyyy".
* @return String in format "YYYY-MM-DD".
*/
public static String parseDate(String input) {
String year = input.substring(0, 4);
String month = input.substring(5, 7);
String day = input.substring(8, 10);

return year + "-" + month + "-" + day;
}

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

import java.time.LocalDate;
import java.time.format.DateTimeFormatter;

/**
* This class encapsulates an Event Task object, which inherits from the Task class.
*/
public class Event extends Task {
protected int index;
protected String at;

Choose a reason for hiding this comment

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

Perhaps you could consider using a more descriptive variable name in place of at, so that other readers can more intuitively understand what at means. Maybe eventDate or unparsedEventDate?

protected LocalDate date;

public Event(int index, String description, String at) {
super(index, description);
this.index = index;
this.at = at;
this.date = LocalDate.parse(at);
}

@Override
public String toString() {
return "[E]" + super.toString() + " (at: " + date.format(DateTimeFormatter.ofPattern("yyyy MM dd")) + ")";
}
}
85 changes: 85 additions & 0 deletions src/main/java/cs2103/duke/Storage.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,85 @@
package cs2103.duke;

import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import java.util.ArrayList;

/**
* This class encapsulates a Storage object which saves the tasks that have been added by the user, it also
* performs the initialization for the duke.txt and its parent folder, if they are not already present.
*/
public class Storage {
private static ArrayList<Task> taskArrayList = new ArrayList<>();
private final String filePath;

public Storage(String dukeFilePath) {
this.filePath = dukeFilePath;
}

/**
* Checks the dukeFilePath for the existence of duke.txt, if the file or the folder containing it
* does not exist, create them.
*
* @throws IOException If an invalid input is detected.
*/
public void initialize() throws IOException {
String dukeFileDirectoryPath = "./data";
String dukeFilePath = "./data/duke.txt";
File dukeFileDirectory = new File(dukeFileDirectoryPath);
File dukeFile = new File(dukeFilePath);
// creates directory if it does not exist
if (dukeFileDirectory.mkdir()) {
// dukeFile.mkdir();

Choose a reason for hiding this comment

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

Maybe this comment could be considered as redundant? The code line above it might be self-explanatory enough.

System.out.println("folder: 'data/' has been created");
}
// creates file if it does not exist
if (dukeFile.createNewFile()) {
// dukeFile.createNewFile();

Choose a reason for hiding this comment

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

Maybe this comment could be considered as redundant? The code line above it might be self-explanatory enough.

System.out.println("'duke.txt' has been created in the 'data/' folder ");
}
}

/**
* Loads the data stored in duke.txt into a taskArrayList.
*
* @return The taskArrayList representation of the data stored in duke.txt, if any.
* @throws IOException If an invalid input is detected.
*/
public ArrayList<Task> load() throws IOException {
// initialize
initialize();
// load the data from the hard disk for dukeFile
DukeParser p = new DukeParser(filePath);
return p.copyFileContents();
}

/**
* This method overwrites the file.
*
* @param filePath The path to the file to append to.
* @param textToAppend The text to append.
* @throws IOException If filePath does not exist.
*/

public void overwriteFile(String filePath, String textToAppend) throws IOException {
FileWriter fw = new FileWriter(filePath, false); // create a FileWriter in append mode
fw.write(textToAppend);
fw.close();
}

/**
* This method appends to the file instead of overwrites.
*
* @param filePath The path to the file to append to.
* @param textToAppend The text to append.
* @throws IOException If filePath does not exist.
*/

public void appendToFile(String filePath, String textToAppend) throws IOException {
FileWriter fw = new FileWriter(filePath, true); // create a FileWriter in append mode
fw.write(textToAppend);
fw.close();
}

}
Loading