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 1 commit
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
3 changes: 1 addition & 2 deletions data/duke.txt
Original file line number Diff line number Diff line change
@@ -1,2 +1 @@
todo | 0 | borrow book
event | 0 | dinner with friends | 2021-12-03
todo | 1 | test test
38 changes: 38 additions & 0 deletions src/main/java/AddCommand.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
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]);

} 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;
}
}
5 changes: 5 additions & 0 deletions src/main/java/Command.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
public abstract class Command {
public abstract void execute(TaskList tasks, Ui ui, Storage storage) throws DukeException;

public abstract boolean isExit();

Choose a reason for hiding this comment

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

You might want to consider setting isExit to False in the abstract, then only changing it to True when it is an ExitCommand. This way you do not have to constantly check what Command it is

}
21 changes: 21 additions & 0 deletions src/main/java/DeleteCommand.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
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;
}
}
20 changes: 20 additions & 0 deletions src/main/java/DoneCommand.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
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;
}
}
157 changes: 26 additions & 131 deletions src/main/java/Duke.java
Original file line number Diff line number Diff line change
@@ -1,146 +1,41 @@
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.Scanner;
import java.util.ArrayList;


public class Duke {
public static void main(String[] args) {
System.out.println("Hello! I'm Magnolia\n" + "What can I do for you?");
Scanner sc = new Scanner(System.in);
ArrayList<Task> list = new ArrayList<>();
try {
File list_data = new File("data/duke.txt");
Scanner reader = new Scanner(list_data);
while (reader.hasNextLine()) {
String line = reader.nextLine();
String[] data = line.split(" \\| ");
switch (data[0]) {
case "todo":
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();
} catch (FileNotFoundException e) {
File newFile = new File("data/duke.txt");
try {
Files.createDirectories(Paths.get("data/"));
newFile.createNewFile();
} catch (IOException a) {
System.out.println("error");
a.printStackTrace();
}

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

while(true) {
public void run() {
ui.greet();
boolean isExit = false;
while (!isExit) {
try {
String input = sc.nextLine();
String first_word = input.split(" ")[0];


if (input.equals("bye")) {
System.out.println("Bye. Hope to see you again soon!");
break;
} else if (input.equals("list")) {
System.out.println("All tasks:");
int i = 1;
for (Task item : list) {
System.out.println(i + ". " + item.getTaskType() + item.getStatusIcon() + " " + item.getDescription());
i++;
}

} 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)");
}
Task temp;
try {
temp = list.get(index);
} catch (IndexOutOfBoundsException e) {
throw new DukeException("Sorry, there are only " + list.size() + " tasks.");
}
temp.setStatus(true);
System.out.println("Marked as done:\n" + temp.getDescription());
save(list);

} 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)");
}

Task temp = list.get(index);
System.out.println("Deleted:\n" + temp.getDescription());
list.remove(index);
System.out.println("There are " + list.size() + " tasks remaining in the list");
save(list);
}
else {
Task task = null;
String remaining;
try {
remaining = input.split(" ", 2)[1];
} catch (ArrayIndexOutOfBoundsException e) {
throw new DukeException("Sorry, tasks must include descriptions.");
}
if(first_word.equals("todo")) {
task = new Task.Todo(remaining, false);

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

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

} else {
throw new DukeException("Sorry, I don't understand what that means. :(");
}
list.add(task);
System.out.println("Added: " + task.getTaskType() + task.getStatusIcon() + " " + task.getDescription());
System.out.println("There are " + list.size() + " tasks in the list");
save(list);
}
String input = ui.readCommand();
Command c = Parser.parse(input);
c.execute(tasks, ui, storage);
isExit = c.isExit();
} catch (DukeException e) {
ui.showError(e.getMessage());
}
catch (DukeException e) {
System.out.println(e.getMessage());
}

}

}
}

public static void save(ArrayList<Task> list) {
try {
File list_data = new File("data/duke.txt");
FileWriter myWriter = new FileWriter(list_data);
String data = "";
for (Task task : list) {
data += task.toString() + "\n";
}
myWriter.write(data);
myWriter.close();
} catch (IOException e) {
System.out.println("File not found");
}

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

12 changes: 12 additions & 0 deletions src/main/java/ExitCommand.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
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;
}
}
13 changes: 13 additions & 0 deletions src/main/java/ListCommand.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
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;
}
}
40 changes: 40 additions & 0 deletions src/main/java/Parser.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
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 {
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);
}


}
}
58 changes: 58 additions & 0 deletions src/main/java/Storage.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
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":
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