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

[Ma Jiajun] iP #197

Open
wants to merge 24 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 12 commits
Commits
Show all changes
24 commits
Select commit Hold shift + click to select a range
7e3eb8d
Level-0
Jamarcus111 Jan 31, 2024
b246b61
Level-0, deleting Duke
Jamarcus111 Jan 31, 2024
4e8d3b2
level1
Jamarcus111 Jan 31, 2024
774fee3
level2
Jamarcus111 Jan 31, 2024
583d937
Level3
Jamarcus111 Jan 31, 2024
776bbad
restructure the code into OOP style,separate the functionality
Jamarcus111 Feb 11, 2024
4ff88d1
checked coding standard and quality
Jamarcus111 Feb 15, 2024
a59c96e
add function to handle exception
Jamarcus111 Feb 16, 2024
78c3ec6
Modifed exception handlers.
Jamarcus111 Feb 16, 2024
195a073
Merge branch 'branch-Level-5'
Jamarcus111 Feb 16, 2024
bf630f3
Cleaned some unnecessary comments
Jamarcus111 Feb 16, 2024
1195965
Merged branch-Level-5 into main branch
Jamarcus111 Feb 16, 2024
6f480ed
Implement Level-6: Add support for deleting tasks
Jamarcus111 Feb 23, 2024
f575da9
Implement Level-7
Jamarcus111 Feb 23, 2024
25281d9
Merge branch 'branch-Level-6'
Jamarcus111 Feb 23, 2024
4cde7f8
solved merge conflict
Jamarcus111 Feb 23, 2024
d5c4f4f
more-oop
Jamarcus111 Mar 4, 2024
8dfd01e
checked coding standard and quality
Jamarcus111 Mar 4, 2024
c28e998
Merge pull request #1 from Jamarcus111/branch-Level-9
Jamarcus111 Mar 4, 2024
da4cb5e
checked coding standard and quality
Jamarcus111 Mar 4, 2024
33b8043
Merge pull request #2 from Jamarcus111/branch-A-JavaDoc
Jamarcus111 Mar 4, 2024
fb94eb6
checked coding standard and quality
Jamarcus111 Mar 4, 2024
00b0a4c
checked coding standard and quality
Jamarcus111 Mar 4, 2024
e8ab793
Update README.md
Jamarcus111 Jun 26, 2024
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

4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
# Duke project template
# Ma project template

This is a project template for a greenfield Java project. It's named after the Java mascot _Duke_. Given below are instructions on how to use it.

Expand All @@ -13,7 +13,7 @@ Prerequisites: JDK 11, update Intellij to the most recent version.
1. If there are any further prompts, accept the defaults.
1. Configure the project to use **JDK 11** (not other versions) as explained in [here](https://www.jetbrains.com/help/idea/sdk.html#set-up-jdk).<br>
In the same dialog, set the **Project language level** field to the `SDK default` option.
3. After that, locate the `src/main/java/Duke.java` file, right-click it, and choose `Run Duke.main()` (if the code editor is showing compile errors, try restarting the IDE). If the setup is correct, you should see something like the below as the output:
3. After that, locate the `src/main/java/Ma.java` file, right-click it, and choose `Run Ma.main()` (if the code editor is showing compile errors, try restarting the IDE). If the setup is correct, you should see something like the below as the output:
```
Hello from
____ _
Expand Down
21 changes: 21 additions & 0 deletions src/main/java/CommandParser.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
public class CommandParser {
private final TaskList taskList;


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

public void parseCommand(String userInput) throws HandleException {
if (userInput.trim().isEmpty()) {
throw new HandleException("The input cannot be empty!");
} else if (userInput.equalsIgnoreCase("list")) {
taskList.listTasks();
} else if (!userInput.startsWith("todo") && !userInput.startsWith("deadline") && !userInput.startsWith("event")) {

Choose a reason for hiding this comment

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

This is a fairly complex statement. You might want to break this into sub-expressions and store values into some boolean variables to check for the "if" condition.

throw new HandleException("OOPS!!! I'm sorry, but I don't know what that means :-(");
} else {
taskList.addTask(userInput);
}
}
}

13 changes: 13 additions & 0 deletions src/main/java/DeadLine.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
public class DeadLine extends Task {
protected String by;

public DeadLine(String description, String by) {
super(description);
this.by = by;
}

@Override
public String toString() {
return "[D]" + super.toString() + " (by: " + this.by + ")";
}
}
10 changes: 0 additions & 10 deletions src/main/java/Duke.java

This file was deleted.

15 changes: 15 additions & 0 deletions src/main/java/Event.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
public class Event extends Task {
protected String from;
protected String to;

public Event(String description, String from, String to) {
super(description);
this.from = from;
this.to = to;
}

@Override
public String toString() {
return "[E]" + super.toString() + " (from: " + this.from + " to: " + this.to + ")";
}
}
6 changes: 6 additions & 0 deletions src/main/java/HandleException.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
public class HandleException extends Exception {
public HandleException(String message) {
super(message);
}
}

18 changes: 18 additions & 0 deletions src/main/java/Task.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
public class Task {
protected String description;
protected boolean isDone;

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

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


public String toString() {
return "[" + this.getStatusIcon() + "] " + this.description;
}
}
40 changes: 40 additions & 0 deletions src/main/java/TaskFactory.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
public class TaskFactory {

Choose a reason for hiding this comment

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

Avoid long methods. Take corrective action when the method exceeds 30 lines of code.

public static Task createTask(String userInput) throws HandleException {
String[] parts = userInput.split(" ", 2);
String type = parts[0];
Task task = null;

if (parts.length < 2 || parts[1].isEmpty()) {

Choose a reason for hiding this comment

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

Avoid the use of Magic numbers.

throw new HandleException("OOPS!!! The description of a task cannot be empty.");
}

switch (type) {
case "todo":
task = new Todo(parts[1]);
break;
case "deadline":
String[] details = parts[1].split(" /by ", 2);
if (details.length < 2 || details[1].isEmpty()) {
throw new HandleException("OOPS!!! The date of a deadline cannot be empty.");
}
task = new DeadLine(details[0], details[1]);
break;
case "event":
details = parts[1].split(" /from ", 2);
if (details.length < 2 || details[1].isEmpty()) {
throw new HandleException("OOPS!!! The start time of an event cannot be empty.");
}
String[] times = details[1].split(" /to ", 2);
if (times.length < 2 || times[1].isEmpty()) {
throw new HandleException("OOPS!!! The end time of an event cannot be empty.");
}
task = new Event(details[0], times[0], times[1]);
break;
default:
throw new HandleException("OOPS!!! I'm sorry, but I don't know what that means :-(");
}

return task;
}
}

26 changes: 26 additions & 0 deletions src/main/java/TaskList.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
import java.util.ArrayList;
public class TaskList {
private final ArrayList<Task> tasks;

public TaskList() {
this.tasks = new ArrayList<>();
}

public void addTask(String userInput) throws HandleException {
// Similar to addTask method in TaskManager, but refactored for this class

Choose a reason for hiding this comment

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

Make sure the comments are for the reader and not private notes for yourself. It should help in increasing code readability.

Task task = TaskFactory.createTask(userInput);
if (task != null) {
tasks.add(task);
System.out.println("Got it. I've added this task:");
System.out.println(" " + task);
System.out.println("Now you have " + tasks.size() + " tasks in the list.");
}
}

public void listTasks() {
System.out.println("Here are the tasks in your list:");
for (int i = 0; i < tasks.size(); i++) {
System.out.println((i + 1) + "." + tasks.get(i));
}
}
}
31 changes: 31 additions & 0 deletions src/main/java/TaskManager.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
import java.util.Scanner;

public class TaskManager {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
TaskList taskList = new TaskList();
CommandParser commandParser = new CommandParser(taskList);

System.out.println("Hello! I'm TaskManager Jamarcus \nWhat can I do for you?");


while (true) {
try {
String userInput = scanner.nextLine().trim();
if (userInput.equalsIgnoreCase("bye")) {
System.out.println("Bye. Hope to see you again soon!");
break;
} else {
commandParser.parseCommand(userInput);
}
} catch (HandleException e) {
System.out.println(e.getMessage());
} catch (Exception e) {
System.out.println("An unexpected error occurred: " + e.getMessage());
}
}

scanner.close();
}
}

10 changes: 10 additions & 0 deletions src/main/java/Todo.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
public class Todo extends Task {
public Todo(String description) {
super(description);
}

@Override
public String toString() {
return "[T]" + super.toString();
}
}
2 changes: 1 addition & 1 deletion text-ui-test/runtest.bat
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ IF ERRORLEVEL 1 (
REM no error here, errorlevel == 0

REM run the program, feed commands from input.txt file and redirect the output to the ACTUAL.TXT
java -classpath ..\bin Duke < input.txt > ACTUAL.TXT
java -classpath ..\bin Ma < input.txt > ACTUAL.TXT

REM compare the output to the expected output
FC ACTUAL.TXT EXPECTED.TXT