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

[Sim Jia Ming] iP #316

Open
wants to merge 57 commits into
base: master
Choose a base branch
from
Open
Changes from 1 commit
Commits
Show all changes
57 commits
Select commit Hold shift + click to select a range
d839859
Add Gradle support
May 24, 2020
6b8c2f2
Level1. Greet,Echo,Exit
SimJM Jan 20, 2022
778d1e0
add Action class
SimJM Jan 20, 2022
8009280
Level-2: added readBook(), returnBook()
SimJM Jan 20, 2022
8fa8a55
Level-3: Mark as Done. Added Task class
SimJM Jan 20, 2022
3357573
Level-4: todo, deadline, event
SimJM Jan 20, 2022
f2fabf0
A-TextUiTesting
SimJM Jan 20, 2022
a210a0f
Level-5 Handle Errors
SimJM Jan 20, 2022
8a7ee50
Level-6 delete
SimJM Jan 20, 2022
a51d3f3
A-Enums
SimJM Jan 20, 2022
5b8561d
refactor code (add TaskBank, Parser, User class)
SimJM Feb 6, 2022
3d7329e
Level-7 Save
SimJM Feb 6, 2022
5dcf7b0
minor edits
SimJM Feb 6, 2022
6b65fea
incomplete branch
SimJM Feb 8, 2022
4ab3dac
Add LocalDate and format date
SimJM Feb 15, 2022
832f67b
implement date format
SimJM Feb 15, 2022
507c6a3
Revert "implement date format"
SimJM Feb 15, 2022
7f7f081
Revert "Revert "implement date format""
SimJM Feb 15, 2022
b8d7f64
Level 8 - add date format
SimJM Feb 15, 2022
70d14c8
A-MoreOOP - Ui class
SimJM Feb 15, 2022
8695409
A-Packages: organize classes into duke package
SimJM Feb 15, 2022
4ac330a
Add JUnit tests
SimJM Feb 15, 2022
26bba93
Package the App as a JAR file
SimJM Feb 16, 2022
95823e5
A-JavaDoc - Add JavaDoc comments
SimJM Feb 16, 2022
81232fa
A-CodingStandard - Tweak the code to comply with a coding standard
SimJM Feb 16, 2022
726b27f
Merge branch 'branch-Level-8' into branch-A-CodingStandard
SimJM Feb 16, 2022
52b6b7c
Level-9 Find - give users a way to find a task by searching for a key…
SimJM Feb 16, 2022
dee52c7
Merge branch 'branch-A-CodingStandard'
SimJM Feb 16, 2022
e1ccf9d
Merge branch 'branch-Level-9'
SimJM Feb 16, 2022
3c83a87
Merge commit 'd8398594b7bc43da5eb865321c5a50cec4b3923d'
SimJM Feb 16, 2022
a018811
A-CheckStyle - edit coding style violations
SimJM Feb 16, 2022
fe06e8f
Level-10 GUI - add a GUI to Duke. Use the JavaFX technology to implem…
SimJM Feb 17, 2022
b3a9390
A-Varargs - not applicable
SimJM Feb 17, 2022
53330a6
Add assertion
SimJM Feb 17, 2022
8cfee6f
Improve code quality
SimJM Feb 17, 2022
4aaed72
Merge branch 'branch-A-CodeQuality'
SimJM Feb 17, 2022
25c776d
C-update - easily edit description of the Task
SimJM Feb 17, 2022
807bab5
Improve the GUI
SimJM Feb 18, 2022
ba460f1
Improve GUI
SimJM Feb 18, 2022
ae805b1
Edit on code quality
SimJM Feb 18, 2022
70a7cf8
Add update into help function
SimJM Feb 18, 2022
b5a30d2
Merge branch 'master' into branch-C-update
SimJM Feb 18, 2022
6992122
Merge pull request #2 from SimJM/branch-C-update
SimJM Feb 18, 2022
9e89a02
tidy up code
SimJM Feb 18, 2022
f56ff47
Set theme jekyll-theme-merlot
SimJM Feb 18, 2022
9181b89
upload Ui.png
SimJM Feb 18, 2022
a914019
Merge branch 'master' of https://github.com/SimJM/ip
SimJM Feb 18, 2022
046d881
Update README.md
SimJM Mar 12, 2022
000f5a1
Update README.md
SimJM Mar 12, 2022
8ed816b
Add exception handling in parser
SimJM Mar 20, 2022
b7ec4fe
Add exception handling
SimJM Mar 20, 2022
09593f8
Add output
SimJM Mar 20, 2022
98566a3
Update README
SimJM Mar 20, 2022
d232468
Update README.md
SimJM Mar 20, 2022
4352935
Update README.md
SimJM Mar 20, 2022
f3191a8
Update README.md
SimJM Mar 20, 2022
3bc0613
Update README.md
SimJM Mar 20, 2022
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
Prev Previous commit
Next Next commit
refactor code (add TaskBank, Parser, User class)
SimJM committed Feb 6, 2022
commit 5b8561de7f6f6f0045eb47a9f7b93577c5ce099f
24 changes: 11 additions & 13 deletions src/main/java/Action.java
Original file line number Diff line number Diff line change
@@ -4,32 +4,30 @@ public class Action {

Action() {}

void greet() {
String greet = "Hello! I'm JiaMing\nWhat can I do for you?\n";
public static void greet() {
String logo = " ____ _ \n"
+ "| _ \\ _ _| | _____ \n"
+ "| | | | | | | |/ / _ \\\n"
+ "| |_| | |_| | < __/\n"
+ "|____/ \\__,_|_|\\_\\___|\n";
String greet = "Hello! I'm JiaMing aka\n"+ logo + "\nWhat can I do for you?\nTip: use 'help' for help\n";
System.out.println(greet);
}

void echo(String phrase) {
public static void echo(String phrase) {
System.out.println(phrase);
}

void bye() {
public static void bye() {
String bye = "Bye. Hope to see you again soon!\n";
System.out.println(bye);
}

void showList(ArrayList<Task> arrlst) {
public static void showList(ArrayList<Task> arrlst) {
System.out.println("Here are the tasks in your list:");
for (int i = 0; i < arrlst.size(); i++) {
String output = String.format("%d.[%s][%s]%s\n", i + 1, arrlst.get(i).sym,
arrlst.get(i).getStatusIcon(), arrlst.get(i).description);
String output = String.format("%d.%s\n", i + 1, arrlst.get(i));
System.out.println(output);
}
}

@Override
public String toString() {
return "this is an Action (Parent)";
}

}
8 changes: 3 additions & 5 deletions src/main/java/Deadline.java
Original file line number Diff line number Diff line change
@@ -1,20 +1,18 @@
public class Deadline extends Task {
protected final String sym = "D";
protected String day;
private final String sym = "D";
private String day;

Deadline (String description, String day) {
super(description);
this.day = day;
}

@Override
String getSym() {
Copy link

Choose a reason for hiding this comment

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

I would opt for a more intuitive name rather than sym.

Copy link

Choose a reason for hiding this comment

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

I would opt for a more intuitive name rather than sym.

Maybe you can change it to a more descriptive name such as symbol?

return this.sym;
}

@Override
public String toString() {
return String.format("[%s][%s]%s (by:%s)", sym, super.getStatusIcon(), super.description, this.day);
return String.format("[%s][%s]%s (by:%s)", sym, super.getStatusIcon(), super.getDescription(), this.day);
}

}
102 changes: 1 addition & 101 deletions src/main/java/Duke.java
Original file line number Diff line number Diff line change
@@ -1,107 +1,7 @@
// enums not applicable

import java.util.Scanner;
import java.util.ArrayList;

public class Duke {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
ArrayList<Task> arrlst = new ArrayList<>();
String DASH = "____________________________________________________________";
Action act = new Action();

act.greet();

while (sc.hasNext()) {
String phrase = sc.nextLine();
System.out.println(DASH);

if (phrase.equals("list")) {
act.showList(arrlst);
} else if (phrase.equals("bye")) {
act.bye();
System.out.println(DASH);
break;
} else {
String[] arrWords = phrase.split(" ");
try { // mark or unmark
int num = Integer.valueOf(arrWords[1]);
if (arrWords[0].equals("mark")) {
arrlst.get(num - 1).markAsDone();
} else if (arrWords[0].equals("unmark")) {
arrlst.get(num - 1).markAsNotDone();
} else if (arrWords[0].equals("delete")) {
System.out.println("Noted. I've removed this task:");
Task t = arrlst.get(num - 1);
String output = String.format("[%s][%s]%s", t.getSym(), t.getStatusIcon(), t.description);
System.out.println(output);
System.out.println("Now you have 4 tasks in the list.");
arrlst.remove(num - 1);
} else {
System.out.println("siao eh");
}
} catch (NumberFormatException nfe) { // add new task like read book, return book
String gotIt = "Got it. I've added this task:";

if (arrWords[0].equals("todo")) {
String remainingWords = "";
for (int i = 1; i < arrWords.length; i++) {
remainingWords = remainingWords + " " + arrWords[i];
}
Todo t = new Todo(remainingWords);
arrlst.add(new Task(t.description, t.sym));
System.out.println(gotIt);
System.out.println(t);
String noOfTask = String.format("Now you have %d tasks in the list.", arrlst.size());
System.out.println(noOfTask);
} else if (arrWords[0].equals("deadline")) {
String remainingWords = "";
String dLine = "";
for (int i = 1; i < arrWords.length; i++) {
if (arrWords[i].equals("/by")) {
for (int j = i + 1; j < arrWords.length; j++) {
dLine = dLine + " " + arrWords[j];
}
break;
} else {
remainingWords = remainingWords + " " + arrWords[i];
}
}
Deadline t = new Deadline(remainingWords, dLine);
arrlst.add(new Task(t.description, t.sym));
System.out.println(gotIt);
System.out.println(t);
String noOfTask = String.format("Now you have %d tasks in the list.", arrlst.size());
System.out.println(noOfTask);
} else if (arrWords[0].equals("event")) {
String remainingWords = "";
String dayAndTime = "";
for (int i = 1; i < arrWords.length; i++) {
if (arrWords[i].equals("/at")) {
for (int j = i + 1; j < arrWords.length; j++) {
dayAndTime = dayAndTime + " " + arrWords[j];
}
break;
} else {
remainingWords = remainingWords + " " + arrWords[i];
}
}
Event t = new Event(remainingWords, dayAndTime);
arrlst.add(new Task(t.description, t.sym));
System.out.println(gotIt);
System.out.println(t);
String noOfTask = String.format("Now you have %d tasks in the list.", arrlst.size());
System.out.println(noOfTask);
}
} catch (ArrayIndexOutOfBoundsException aioobe) { // echo
if (arrWords[0].equals("todo")) {
System.out.println("OOPS!!! The description of a todo cannot be empty.");
} else {
System.out.println("OOPS!!! I'm sorry, but I don't know what that means :-(");
}
}
}
System.out.println(DASH);
}
User.start();
}
}
7 changes: 2 additions & 5 deletions src/main/java/Event.java
Original file line number Diff line number Diff line change
@@ -1,21 +1,18 @@
public class Event extends Task {
protected final String sym = "E";
private final String sym = "E";
private String dayAndTime;

Event (String description, String dayAndTime) {
super(description);
this.dayAndTime = dayAndTime;
}

@Override
String getSym() {
return this.sym;
}

@Override
public String toString() {
return String.format("[%s][%s]%s (at:%s)", sym, super.getStatusIcon(), super.description, this.dayAndTime);
return String.format("[%s][%s] %s (at:%s)", sym, super.getStatusIcon(), super.getDescription(), this.dayAndTime);
}


}
123 changes: 123 additions & 0 deletions src/main/java/Parser.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,123 @@
import java.util.ArrayList;

public class Parser {
Parser() {}

public String parse(String input) {
String DASH = "____________________________________________________________";
String[] inputArr = input.split(" ");
String output = "";
String command = inputArr[0];
ArrayList<Task> taskList = TaskBank.getBank();

switch(command) {
case "list":
Action.showList(taskList);
break;
case "bye":
Action.bye();
System.out.println(DASH);
break;
case "mark":
try {
int num = Integer.valueOf(inputArr[1]) - 1;
taskList.get(num).markAsDone();
output = String.format("Nice! I've marked this task as done: %s\n", taskList.get(num));
} catch (NumberFormatException error) {
output = "The index you have entered is not a number.\n";
} catch (ArrayIndexOutOfBoundsException error) {
output = "The index you have entered is out of range.\n";
}
break;
case "unmark":
try {
int num = Integer.valueOf(inputArr[1]) - 1;
taskList.get(num).markAsNotDone();
output = String.format("OK, I've marked this task as not done yet: %s\n", taskList.get(num));
} catch (NumberFormatException error) {
output = "The index you have entered is not a number.\n";
} catch (ArrayIndexOutOfBoundsException error) {
output = "The index you have entered is out of range.\n";
}
break;
case "todo":
String remainingWordsTodo = "";
for (int i = 1; i < inputArr.length; i++) {
remainingWordsTodo = remainingWordsTodo + " " + inputArr[i];
}
Todo td = new Todo(remainingWordsTodo);
taskList.add(td);
output = String.format("Got it. I've added this task:\n %s\n", td);
String noOfTaskTodo = String.format("Now you have %d tasks in the list.\n", taskList.size());
output = output + noOfTaskTodo;
break;
case "deadline":
String remainingWordsDeadline = "";
String deadline = "";
for (int i = 1; i < inputArr.length; i++) {
if (inputArr[i].equals("/by")) {
for (int j = i + 1; j < inputArr.length; j++) {
deadline = deadline + " " + inputArr[j];
}
break;
} else {
remainingWordsDeadline = remainingWordsDeadline + " " + inputArr[i];
}
}
Deadline dl = new Deadline(remainingWordsDeadline, deadline);
taskList.add(dl);
output = String.format("Got it. I've added this task:\n %s\n", dl);
String noOfTaskDeadline = String.format("Now you have %d tasks in the list.\n", taskList.size());
output = output + noOfTaskDeadline;
break;
case "event":
String remainingWordsEvent = "";
String dayAndTime = "";
for (int i = 1; i < inputArr.length; i++) {
if (inputArr[i].equals("/at")) {
for (int j = i + 1; j < inputArr.length; j++) {
dayAndTime = dayAndTime + " " + inputArr[j];
}
break;
} else {
remainingWordsEvent = remainingWordsEvent + " " + inputArr[i];
}
}
Event e = new Event(remainingWordsEvent, dayAndTime);
taskList.add(e);
output = String.format("Got it. I've added this task:\n %s\n", e);
String noOfTaskEvent = String.format("Now you have %d tasks in the list.\n", taskList.size());
output = output + noOfTaskEvent;
break;
case "delete":
int num = Integer.valueOf(inputArr[1]) - 1;
try {
Task d = taskList.get(num);
taskList.remove(num);
output = String.format("Noted. I've removed this task:\n %s\n", d);
String noOfTaskDelete = String.format("Now you have %d tasks in the list.\n", taskList.size());
output = output + noOfTaskDelete;
} catch (NumberFormatException error) {
output = "The index you have entered is not a number.\n";
} catch (ArrayIndexOutOfBoundsException error) {
output = "The index you have entered is out of range.\n";
}
break;
case "help":
output = "Here are all the commands available:\n"
+ "list - to list out all the task\n"
+ "bye - to end program\n"
+ "todo <input something you want to do> - to add something you want to tasks\n"
+ "deadline <input what is going be due> /by <input date> - to add a deadline to tasks\n"
+ "event <input event description> /at <input date>\n - to add an event to tasks"
+ "mark <input task number> - to mark task as done\n"
+ "unmark <input task number> - to mark task as not done\n"
+ "delete <input task number> - to delete task from tasks\n";
break;
default:
output = "invalid command! Use 'help' for help";
}

return output;
}
}
29 changes: 7 additions & 22 deletions src/main/java/Task.java
Original file line number Diff line number Diff line change
@@ -1,45 +1,30 @@
public class Task {
Copy link

Choose a reason for hiding this comment

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

It is possible to change Task to an abstract class?

protected String description;
protected boolean isDone;
protected String sym;
private String description;
private boolean isDone;

public Task(String description) {
this.description = description;
this.isDone = false;
this.sym = " ";
}

public Task(String description, String sym) {
this.description = description;
this.isDone = false;
this.sym = sym;
public String getStatusIcon() {
return isDone ? "X" : " ";
}

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

public void markAsDone() {
this.isDone = true;
System.out.println("Nice! I've marked this task as done:");
String output = String.format(" [%s][%s] %s", this.sym, this.getStatusIcon(), this.description);
System.out.println(output);
}

public void markAsNotDone() {
this.isDone = false;
System.out.println("OK, I've marked this task as not done yet:");
String output = String.format(" [%s] %s", this.getStatusIcon(), this.description);
System.out.println(output);
}

String getSym() {
return this.sym;
}

@Override
public String toString() {
return String.format("added: %s", description);
return String.format("[%s] added: %s", this.getStatusIcon(), this.getDescription());
}

}
19 changes: 19 additions & 0 deletions src/main/java/TaskBank.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
import java.util.ArrayList;

public class TaskBank {
private static ArrayList<Task> bank = new ArrayList<Task>();

public TaskBank() {}

public static void addTask(Task task) {
bank.add(task);
}

public static void deleteTask(int index) {
bank.remove(index);
}

public static ArrayList<Task> getBank() {
return bank;
}
}
Loading