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

[clydelhui] iP #373

Open
wants to merge 32 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 11 commits
Commits
Show all changes
32 commits
Select commit Hold shift + click to select a range
556af3f
Add Gradle support
May 24, 2020
5ba595d
Modified Duke.java
clydelhui Jan 19, 2023
8dfc573
Modified Duke.java
clydelhui Jan 19, 2023
808b76a
Added Task.java and Modified Duke.java
clydelhui Jan 23, 2023
3e0e70a
Added new task types, modified Duke.java
clydelhui Jan 23, 2023
1222458
Added Text UI Testing
clydelhui Jan 23, 2023
75ff48e
Mofiy runtest.bat to compile packages, created exceptions to handle i…
clydelhui Jan 26, 2023
b8795a2
Modify Duke.java to include delete functionality
clydelhui Jan 26, 2023
d44382f
Add saving functionality, add CommandHandler and TaskList classes
clydelhui Jan 31, 2023
4a5cb95
Change type of Deadline to LocalDate
clydelhui Feb 1, 2023
4949ee8
Merge branch 'branch-Level-7'
clydelhui Feb 1, 2023
27e8322
Merge branch 'branch-Level-8'
clydelhui Feb 1, 2023
1a3fb0a
Add Ui, Storage, Parser, and Command classes.
clydelhui Feb 8, 2023
08bad31
Merge remote-tracking branch 'origin/add-gradle-support'
clydelhui Feb 10, 2023
d231c99
Add checkstyle to gradle, fix style issues, add JUnit tests for AddCo…
clydelhui Feb 14, 2023
6943842
Add new StorageException, JavaDocs
clydelhui Feb 15, 2023
7603704
Add new VoidCommand type "find"
clydelhui Feb 16, 2023
fc4f9e3
Modify files to fit coding standard
clydelhui Feb 16, 2023
9b4cc31
Merge branch 'branch-A-JavaDoc'
clydelhui Feb 16, 2023
0bf4219
Merge branch 'branch-A-CodingStandard'
clydelhui Feb 16, 2023
3572669
Merge branch 'branch-Level-9'
clydelhui Feb 16, 2023
cad9b7a
add UI with JavaFX
clydelhui Feb 17, 2023
fa95494
Merge branch 'branch-Level-10'
clydelhui Feb 17, 2023
ad01988
add assertions
clydelhui Feb 18, 2023
4f0e56a
Improve code quality
clydelhui Feb 19, 2023
640b3e3
Merge pull request #2 from clydelhui/branch-A-Assertions
clydelhui Feb 19, 2023
47cc6af
Merge branch 'master' of https://github.com/clydelhui/ip
clydelhui Feb 19, 2023
9c2d01f
Merge pull request #3 from clydelhui/branch-CodeQuality
clydelhui Feb 19, 2023
d908fae
add sort functionality, empty list exception
clydelhui Feb 20, 2023
b65a8e2
Merge branch 'branch-BCD-Extension'
clydelhui Feb 20, 2023
9d7b757
update README.md, make minor bug fixes
clydelhui Feb 20, 2023
81c5f59
Modify README.md
clydelhui Feb 21, 2023
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
71 changes: 71 additions & 0 deletions src/main/java/CommandHandler.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
import dukeexceptions.IllegalCommandException;
import dukeexceptions.IllegalInputException;
import items.Deadline;
import items.Event;
import items.Task;
import items.ToDo;

import java.time.LocalDate;

public class CommandHandler {

private TaskList taskList;

public CommandHandler(TaskList taskList) {
this.taskList = taskList;
}

public void execute(String command) throws IllegalCommandException,

Choose a reason for hiding this comment

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

Perhaps you can consider having Javadocs for this method

IllegalInputException, NumberFormatException {
String[] parsedCommand = command.split(" ", 2);
switch (parsedCommand[0]) {

Choose a reason for hiding this comment

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

I like your implementation! Perhaps you can consider further abstraction, e.g having an abstract Command class that different types of commands can extend from :)

case "bye":
if (parsedCommand.length > 1) {
throw new IllegalCommandException("Not sure if you want to say goodbye?");
}
System.out.println("Goodbye!");
System.exit(0);
case "list":
if (parsedCommand.length > 1) {
throw new IllegalCommandException("Did you mean you want to list the items in your list?");
}
this.taskList.enumerate();
break;
case "mark":
if (parsedCommand.length > 2) {
throw new IllegalInputException("Too many arguments!");
}
int taskCode = Integer.parseInt(parsedCommand[1]);
this.taskList.markTask(taskCode);
break;
case "unmark":
int taskCodeUnmark = Integer.parseInt(parsedCommand[1]);
this.taskList.unmarkTask(taskCodeUnmark);
break;
case "todo":
try {
Task newToDo = new ToDo(parsedCommand[1]);
this.taskList.addTask(newToDo);
break;
} catch (ArrayIndexOutOfBoundsException e) {
throw new IllegalInputException("You did not key in a task name!");
}
case "deadline":
String[] paramsDeadline = parsedCommand[1].split("/");
Task newDeadline = new Deadline(paramsDeadline[0], LocalDate.parse(paramsDeadline[1]));
this.taskList.addTask(newDeadline);
break;
case "event":
String[] paramsEvent = parsedCommand[1].split("/");
Task newEvent = new Event(paramsEvent[0], paramsEvent[1], paramsEvent[2]);
this.taskList.addTask(newEvent);
break;
case "delete":
int taskCodeDelete = Integer.parseInt(parsedCommand[1]);
this.taskList.delete(taskCodeDelete);
break;
default:
throw new IllegalCommandException("You did not key in a valid command");
}
}
}
42 changes: 42 additions & 0 deletions src/main/java/Duke.java
Original file line number Diff line number Diff line change
@@ -1,3 +1,16 @@
import dukeexceptions.IllegalCommandException;
import dukeexceptions.IllegalInputException;
import items.Deadline;
import items.Event;
import items.Task;
import items.ToDo;

import java.io.File;
import java.io.IOException;
import java.time.LocalDate;
import java.util.ArrayList;
import java.util.List;
import java.util.Scanner;
public class Duke {
public static void main(String[] args) {
String logo = " ____ _ \n"
Expand All @@ -6,5 +19,34 @@ public static void main(String[] args) {
+ "| |_| | |_| | < __/\n"
+ "|____/ \\__,_|_|\\_\\___|\n";
System.out.println("Hello from\n" + logo);
Scanner usrInput = new Scanner(System.in);
String input = "";
File data = new File("data.txt");
try {
boolean createdFile = data.createNewFile();
if (createdFile){
System.out.println("Data file successfully created!");
} else {
System.out.println("Data file already exists!");
}
} catch (IOException e) {
System.err.println("Failed to create file :(");
e.printStackTrace();
}

TaskList taskList = new TaskList("data.txt");
CommandHandler commandHandler = new CommandHandler(taskList);

while (true) {
input = usrInput.nextLine();
try {

commandHandler.execute(input);
} catch (IllegalInputException | IllegalCommandException e) {

System.out.println(e.toString());
}
}

}
}
128 changes: 128 additions & 0 deletions src/main/java/TaskList.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,128 @@
import items.Deadline;
import items.Event;
import items.Task;
import items.ToDo;

import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileWriter;
import java.io.IOException;
import java.time.LocalDate;
import java.util.ArrayList;
import java.util.Scanner;

public class TaskList {
private final File storage;
private ArrayList<Task> taskList;

public TaskList(String filePath) {
this.storage = new File(filePath);
try {
refreshTaskList();
} catch (FileNotFoundException e) {
System.err.println("Data file could not be found :(");
e.printStackTrace();
}
}

public void delete(int index){
Task deleted = this.taskList.remove(index - 1);
Copy link

Choose a reason for hiding this comment

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

Can here be some input validity check to ensure index is never less than 1?

try {
refreshStorage();
System.out.println("I have deleted the following task: \n" + deleted.toString());
} catch (IOException e) {
e.printStackTrace();
System.out.println("Seems like something went wrong with the storage file... \n " +
"deleted task has been added to back of task list");
this.taskList.add(deleted);
}
}

public void enumerate(){
for (int i = 0; i < this.taskList.size(); i++){
System.out.println((i + 1) + ":" + this.taskList.get(i).toString());
}
}

public void addTask(Task newTask){
this.taskList.add(newTask);
try {
refreshStorage();
System.out.println("added:\n" + newTask.toString());
} catch (IOException e) {
e.printStackTrace();
System.out.println("Seems like something went wrong with the storage file... \n " +
"Please try again later");
this.taskList.remove(newTask);
}
}

public void markTask(int index){
Task mark = this.taskList.get(index - 1);
mark.setDone();
try {
refreshStorage();
System.out.println("I have set the following task to done: \n" + mark.toString());
} catch (IOException e) {
System.out.println("Seems like something went wrong with the storage file... \n " +
"Please try again later");
e.printStackTrace();
mark.setNotDone();
}
}

public void unmarkTask(int index){
Task unmark = this.taskList.get(index - 1);
unmark.setNotDone();
try {
refreshStorage();
System.out.println("I have set the following task to not done: \n" + unmark.toString());
} catch (IOException e) {
System.out.println("Seems like something went wrong with the storage file... \n " +
"Please try again later");
e.printStackTrace();
unmark.setDone();
}
}

private void refreshStorage() throws IOException{
FileWriter clearFile = new FileWriter(this.storage, false);
clearFile.close();
FileWriter writer = new FileWriter(this.storage);
for (int i = 0; i < this.taskList.size(); i++){
writer.write(this.taskList.get(i).generateStorageForm() + System.lineSeparator());
}
writer.close();
}

private void refreshTaskList() throws FileNotFoundException {
Scanner scanner = new Scanner(this.storage);
this.taskList = new ArrayList<>();

while(scanner.hasNextLine()){
String[] parsedInput = scanner.nextLine().split("@");
// for (int i = 0; i < parsedInput.length; i ++) {
// System.out.println(i + parsedInput[i]);
// }
boolean isDone = parsedInput[2].equals("X");
// System.out.println(isDone);
switch (parsedInput[0]){
case "T":
this.taskList.add(new ToDo(parsedInput[1], isDone));
break;
case "D":
this.taskList.add(new Deadline(parsedInput[1], isDone, LocalDate.parse(parsedInput[3])));
break;
case "E":
this.taskList.add(new Event(parsedInput[1], isDone, parsedInput[3], parsedInput[4]));
break;
}
}
scanner.close();
}





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

public class IllegalCommandException extends IllegalArgumentException{
public IllegalCommandException(String errorMessage){
super(errorMessage);
}

@Override
public String toString(){
return "Sorry, I do not understand your command." + getMessage();
}
}
12 changes: 12 additions & 0 deletions src/main/java/dukeexceptions/IllegalInputException.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
package dukeexceptions;
Copy link

Choose a reason for hiding this comment

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

Please conform to the camelNaming rule here to name the package.


public class IllegalInputException extends IllegalArgumentException{
public IllegalInputException(String s) {
super(s);
}

@Override
public String toString() {
return "Sorry, your input for the command is invalid:" + getMessage();
}
}
28 changes: 28 additions & 0 deletions src/main/java/items/Deadline.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
package items;

import java.time.LocalDate;

public class Deadline extends Task{
private LocalDate endDate;
public Deadline(String description, LocalDate endDate) {
super(description, "D");
this.endDate = endDate;
}

public Deadline(String description, boolean done, LocalDate endDate) {

Choose a reason for hiding this comment

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

perhaps the parameter "done" could be changed to "isDone" instead!

super(description, "D", done);
this.endDate = endDate;
}

@Override
public String generateStorageForm() {
return this.getTaskType() + "@" + this.getDescription() + "@"
+ this.getStatusIcon() + "@" + this.endDate;
}

@Override
public String toString(){
return "[" + this.getTaskType() + "]" + "[" + this.getStatusIcon() + "]"
+ this.description + "/" + this.endDate;
}
}
30 changes: 30 additions & 0 deletions src/main/java/items/Event.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
package items;

public class Event extends Task{
private String startDate;
private String endDate;

public Event(String description, String startDate, String endDate) {
super(description, "E");
this.startDate = startDate;
this.endDate = endDate;
}

public Event(String description, boolean done, String startDate, String endDate) {
super(description, "E", done);
this.startDate = startDate;
this.endDate = endDate;
}

@Override
public String generateStorageForm() {
return this.getTaskType() + "@" + this.getDescription() + "@" + this.getStatusIcon() + "@"
+ this.startDate + "@" + this.endDate;
}

@Override
public String toString() {
return "[" + this.getTaskType() + "]" + "[" + this.getStatusIcon() + "]"
+ this.description + "/" + this.startDate + "/" + this.endDate;
}
}
36 changes: 36 additions & 0 deletions src/main/java/items/Task.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
package items;
public abstract class Task {
protected String description;
protected boolean isDone;
protected String taskType;

public Task(String description, String taskType) {
this.description = description;
this.isDone = false;
this.taskType = taskType;
}

public Task(String description, String taskType, boolean done) {
this.description = description;
this.isDone = done;
this.taskType = taskType;
}

public String getStatusIcon() {
return (isDone ? "X" : " "); // mark done task with X
}

public String getTaskType(){ return this.taskType;}

public void setDone(){this.isDone = true;}

public void setNotDone(){this.isDone = false;}

public String getDescription(){return this.description;}

public abstract String generateStorageForm();

@Override
public abstract String toString();

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

public class ToDo extends Task{
public ToDo(String description) {
super(description, "T");
}

public ToDo(String description, boolean done) {
super(description, "T", done);
}

@Override
public String generateStorageForm() {
return this.getTaskType() + "@" + this.getDescription() + "@" + this.getStatusIcon();
}

@Override
public String toString() {
return "[" + this.getTaskType() + "]" + "[" + this.getStatusIcon() + "]" + this.description;
}
}
Loading