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

[Bernard Wan De Yuan] iP #461

Open
wants to merge 40 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 17 commits
Commits
Show all changes
40 commits
Select commit Hold shift + click to select a range
d839859
Add Gradle support
May 24, 2020
f38a22a
Level-1
bernardwan Aug 20, 2021
b98c16f
Level-2
bernardwan Aug 20, 2021
feac61e
Level-3
bernardwan Aug 20, 2021
83381b8
Level-4
bernardwan Aug 20, 2021
3c1011c
Added automated text UI testing
bernardwan Aug 25, 2021
7551481
Implemented Level-5, updated test cases
bernardwan Aug 25, 2021
2565300
Implemented Level-6. updated tests
bernardwan Aug 25, 2021
5ff4233
Implemented Level-7
bernardwan Aug 26, 2021
0266c78
Implemented Level-8
bernardwan Aug 26, 2021
33921a6
Merge branch 'branch-Level-8'
bernardwan Aug 26, 2021
48d97f1
Updated tests
bernardwan Aug 26, 2021
09721a2
Resolved conflict errors
bernardwan Aug 26, 2021
e4d0342
Refactor code to make it more OOP
bernardwan Aug 27, 2021
c3e9b99
Organise into package
bernardwan Aug 28, 2021
6db07e9
Add Junit tests
bernardwan Aug 28, 2021
fe6833c
Create JAR File
bernardwan Aug 28, 2021
5f6bf33
Add Javadocs
bernardwan Aug 31, 2021
9dbf7dd
Tweak code to follow coding standard
bernardwan Aug 31, 2021
06609b4
Implement Level-9 Find feature
bernardwan Aug 31, 2021
d093898
Merge branch 'branch-A-CodingStandard'
bernardwan Aug 31, 2021
d2d6805
Merge branch 'branch-Level-9'
bernardwan Aug 31, 2021
bcefd1c
Fix minor javadocs errors
bernardwan Aug 31, 2021
679bfa8
Merge remote-tracking branch 'origin/add-gradle-support'
bernardwan Sep 3, 2021
341051c
Add Gradle to project
bernardwan Sep 4, 2021
5dcd832
Add ability to use checkstyle
bernardwan Sep 4, 2021
62293f6
Add GUI
bernardwan Sep 5, 2021
c786acd
Modify config for proper Gradle usage
bernardwan Sep 5, 2021
9d41498
Add Assert statements
bernardwan Sep 9, 2021
e710891
Check code for code quality and clean up code
bernardwan Sep 9, 2021
f787e9d
Merge pull request #1 from bernardwan/branch-A-Assertions
bernardwan Sep 9, 2021
879b551
Merge branch 'master' into branch-A-CodeQuality
bernardwan Sep 9, 2021
558ea60
Merge pull request #2 from bernardwan/branch-A-CodeQuality
bernardwan Sep 9, 2021
fceed2d
Implement B-DoWithinPeriodTasks
bernardwan Sep 9, 2021
fd12c10
Refactor code
bernardwan Sep 16, 2021
a57ca3c
Add User Guide
bernardwan Sep 16, 2021
d45e077
Rename boolean variable
bernardwan Sep 16, 2021
6e830cf
Improve GUI
bernardwan Sep 16, 2021
4714d9b
Handle some exceptions and change font
bernardwan Sep 17, 2021
0d11fb8
Update README.md
bernardwan Sep 17, 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
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: duke.Duke

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

import duke.*;

Choose a reason for hiding this comment

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

I think perhaps can consider importing individual classes instead of using wildcard import!


public class AddCommand extends Command{
private String type;
private String description;

public AddCommand(String type, String description) {
this.type = type;
this.description = description;
}

@Override
public void execute(TaskList tasks, Ui ui, Storage storage) throws DukeException {
Task task;
if(type.equals("todo")) {
task = new Task.Todo(description, false);

} else if(type.equals("deadline")) {
String[] temp = description.split("by ", 2);
task = new Task.Deadline(temp[0], false, temp[1]);

} else if(type.equals("event")) {
String[] temp = description.split("at ", 2);
task = new Task.Event(temp[0], false, temp[1]);

Choose a reason for hiding this comment

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

Maybe can consider using enum for this!


} else {
throw new DukeException("Sorry, I don't understand what that means. :(");
}

tasks.addTask(task);
ui.print("Added: " + task.getTaskType() + task.getStatusIcon() + " " + task.getDescription());
ui.print("There are " + tasks.size() + " tasks in the list");
storage.save(tasks);
}

@Override
public boolean isExit() {
return false;
}
}
12 changes: 12 additions & 0 deletions src/main/java/duke/Command/Command.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
package duke.Command;

import duke.DukeException;
import duke.Storage;
import duke.TaskList;
import duke.Ui;

public abstract class Command {
public abstract void execute(TaskList tasks, Ui ui, Storage storage) throws DukeException;

public abstract boolean isExit();
}
25 changes: 25 additions & 0 deletions src/main/java/duke/Command/DeleteCommand.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
package duke.Command;

import duke.*;

public class DeleteCommand extends Command{

private int index;

public DeleteCommand(int index) {
this.index = index;
}

@Override
public void execute(TaskList tasks, Ui ui, Storage storage) throws DukeException {
Task task = tasks.deleteTask(index);
ui.print("Deleted:\n" + task.getDescription());
ui.print("There are " + tasks.size() + " tasks remaining in the list");
storage.save(tasks);
}

@Override
public boolean isExit() {
return false;
}
}
24 changes: 24 additions & 0 deletions src/main/java/duke/Command/DoneCommand.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
package duke.Command;

import duke.*;

public class DoneCommand extends Command{

private int index;

public DoneCommand(int index) {
this.index = index;
}

@Override
public void execute(TaskList tasks, Ui ui, Storage storage) throws DukeException {
Task task = tasks.doneTask(this.index);
ui.print("Marked as done:\n" + task.getDescription());
storage.save(tasks);
}

@Override
public boolean isExit() {
return false;
}
}
18 changes: 18 additions & 0 deletions src/main/java/duke/Command/ExitCommand.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
package duke.Command;

import duke.Storage;
import duke.TaskList;
import duke.Ui;

public class ExitCommand extends Command {

@Override
public void execute(TaskList tasks, Ui ui, Storage storage) {
System.out.println("Bye. Hope to see you again soon!");
}

@Override
public boolean isExit() {
return true;
}
}
30 changes: 30 additions & 0 deletions src/main/java/duke/Command/FindCommand.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
package duke.Command;

import duke.*;

Choose a reason for hiding this comment

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

I think you should import package by package, for example import duke.TaskList rather than using import duke.*


import java.util.ArrayList;

public class FindCommand extends Command{
String keyword;

public FindCommand(String keyword) {
this.keyword = keyword;
}

@Override
public void execute(TaskList tasks, Ui ui, Storage storage) throws DukeException {
ArrayList<Task> list = tasks.getList();
list.removeIf(task -> !task.getDescription().contains(this.keyword));
if (list.size() == 0) {
ui.print("No matching tasks found.");
} else {
ui.print(list.size() + " matching task(s):");
ui.print(new TaskList(list).allTasks());
}
}

@Override
public boolean isExit() {
return false;
}
}
20 changes: 20 additions & 0 deletions src/main/java/duke/Command/ListCommand.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
package duke.Command;

import duke.Command.Command;
import duke.Storage;
import duke.TaskList;
import duke.Ui;

public class ListCommand extends Command {

@Override
public void execute(TaskList tasks, Ui ui, Storage storage) {
ui.print("All tasks:");
ui.print(tasks.allTasks());
}

@Override
public boolean isExit() {
return false;
}
}
43 changes: 43 additions & 0 deletions src/main/java/duke/Duke.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
package duke;

import duke.Command.Command;

public class Duke {

private Storage storage;
private TaskList tasks;
private Ui ui;

public Duke() {
ui = new Ui();
storage = new Storage("data/duke.txt");
try {
tasks = new TaskList(storage.load());
} catch (DukeException e) {
tasks = new TaskList();
}
}

public void run() {
ui.greet();
boolean isExit = false;
while (!isExit) {
try {
String input = ui.readCommand();
Command c = Parser.parse(input);
c.execute(tasks, ui, storage);
isExit = c.isExit();
} catch (DukeException e) {
ui.showError(e.getMessage());
}
}


}


public static void main(String[] args) {
new Duke().run();
}
}

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);
}
}
51 changes: 51 additions & 0 deletions src/main/java/duke/Parser.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
package duke;

import duke.Command.*;

public class Parser {

public static Command parse(String input) throws DukeException{
String first_word = input.split(" ")[0];


if (input.equals("bye")) {
return new ExitCommand();
} else if (input.equals("list")) {
return new ListCommand();
} else if (first_word.equals("done")) {
int index;
try {
index = Integer.parseInt(input.split(" ")[1]) - 1;
} catch (ArrayIndexOutOfBoundsException e) {
throw new DukeException("Sorry, please enter an integer after 'done'. (e.g. done 2)");
}
return new DoneCommand(index);
} else if (first_word.equals("delete")) {
int index;
try {
index = Integer.parseInt(input.split(" ")[1]) - 1;
} catch (ArrayIndexOutOfBoundsException e) {
throw new DukeException("Sorry, please enter an integer after 'delete'. (e.g. delete 2)");
}
return new DeleteCommand(index);
} else if (first_word.equals("find")){
String remaining;
try {
remaining = input.split(" ", 2)[1];
} catch (ArrayIndexOutOfBoundsException e) {
throw new DukeException("Sorry, please enter a keyword after 'find'.");
}
return new FindCommand(remaining);
} else {
String remaining;
try {
remaining = input.split(" ", 2)[1];
} catch (ArrayIndexOutOfBoundsException e) {
throw new DukeException("Sorry, tasks must include descriptions.");
}
return new AddCommand(first_word, remaining);
}

Choose a reason for hiding this comment

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

Perhaps we can use enum here to simplify the logic here! Also, personally I fell that all the try-catch statement here can be abstracted to other class method e.g. createDone() etc. to prevent arrowhead style code, and that one single catch for ArrayIndexOutOfBoundsException could be used for the entire method. Don't know if I articulate my idea in a very accurate way 😂



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

import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileWriter;
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Paths;
import java.util.ArrayList;
import java.util.Scanner;

public class Storage {
private String filepath;

public Storage(String filepath) {
this.filepath = filepath;
}

public ArrayList<Task> load() throws DukeException {
ArrayList<Task> list = new ArrayList<>();
File list_data = new File(filepath);
Scanner reader;
try {
reader = new Scanner(list_data);
} catch (FileNotFoundException e) {
throw new DukeException("File not found");
}

while (reader.hasNextLine()) {
String line = reader.nextLine();
String[] data = line.split(" \\| ");
switch (data[0]) {
case "todo":

Choose a reason for hiding this comment

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

Should case be on the same indent level as switch?

list.add(new Task.Todo(data[2], data[1].equals("0") ? false : true));
break;
case "deadline":
list.add(new Task.Deadline(data[2], data[1].equals("0") ? false : true, data[3]));
break;
case "event":
list.add(new Task.Event(data[2], data[1].equals("0") ? false : true, data[3]));
break;
}
}
reader.close();
return list;
}

public void save(TaskList list) {
try {
File list_data = new File(filepath);
Files.createDirectories(Paths.get("data/"));
FileWriter myWriter = new FileWriter(list_data);
myWriter.write(list.toString());
myWriter.close();
} catch (IOException e) {
System.out.println("File not found");
}
}

}
Loading