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

[ChangruHenryQian] iP #544

Open
wants to merge 59 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 16 commits
Commits
Show all changes
59 commits
Select commit Hold shift + click to select a range
28ad2b8
Add Gradle support
May 24, 2020
ed6d4d2
Bump gradle and lib version
Eclipse-Dominator Aug 5, 2023
5080e17
Finish Level 0. Rename, Greet, Exit.
Aug 24, 2023
3d4a50f
Finish Level-1: Echo
Aug 24, 2023
fe29b0f
Finish Level 2. Add, List
Aug 26, 2023
02481a9
Finish Level 3. Mark as Done
Aug 27, 2023
91639a8
Finish Level 4. ToDos, Events, Deadlines
Aug 27, 2023
cab0141
Finish A-TextUiTesting
Aug 27, 2023
28266b3
Finish Level 5. Handle Errors
Aug 27, 2023
611f1ef
Finish Level 6. Delete
Aug 27, 2023
4197612
Finish Level 7. Save
Sep 5, 2023
c0ea8a2
Update data/duke.txt
Sep 5, 2023
bf86dc1
Merge branch 'branch-Level-7'
Sep 5, 2023
992ec01
Finish Level 8. Dates and Times
Sep 9, 2023
113e1d3
Merge branch 'branch-Level-8'
Sep 9, 2023
1f284c9
Finish A-MoreOOP: Use More OOP
Sep 9, 2023
92e988c
Fix bug on delete command
Sep 9, 2023
5c4a3ba
Finish A-Packages: Organize into Packages
Sep 9, 2023
d197fc2
Merge branch 'add-gradle-support'
Sep 9, 2023
e450bed
Finish A-Gradle: Use Gradle
Sep 9, 2023
2a0dedd
Finish A-JUnit: Add JUnit Tests
Sep 9, 2023
8ac5391
Finish A-Jar: Create a JAR File
Sep 9, 2023
dcc4829
Add part of Java Doc.
Sep 10, 2023
98baa49
Merge branch 'branch-A-JavaDoc'
Sep 10, 2023
cb8b5d6
Merge branch 'master' into branch-A-CodingStandard
Sep 10, 2023
a011a9e
Implement coding standard.
Sep 10, 2023
af45b36
Merge branch 'branch-A-CodingStandard'
Sep 10, 2023
b0af1ec
Update JavaDoc
Sep 10, 2023
1d67ccf
Merge branch 'branch-A-JavaDoc'
Sep 10, 2023
b47f650
Update JavaDoc
Sep 10, 2023
8e4c58e
Merge branch 'branch-A-JavaDoc'
Sep 10, 2023
c529dc9
Start Level 9. Find
Sep 10, 2023
b88d605
Merge branch 'master' into branch-Level-9
Sep 10, 2023
cfc25a9
Implement Level 9. Find
Sep 10, 2023
2a192a3
Merge branch 'branch-Level-9'
Sep 10, 2023
9ff4bbc
Start Level 10. GUI
Sep 10, 2023
76ab079
Implement Level 10. GUI
Sep 10, 2023
d1c94f8
Merge branch 'branch-Level-10'
Sep 10, 2023
3b3712a
Update build.gradle
Sep 15, 2023
b76decc
Implement A-Assertions
Sep 15, 2023
59658eb
Improve code quality
Sep 15, 2023
e20422e
Merge pull request #1 from ChangruHenryQian/branch-A-CodeQuality
ChangruHenryQian Sep 15, 2023
f1c1337
Merge branch 'master' into branch-A-Assertions
Sep 15, 2023
ed26f34
Merge pull request #2 from ChangruHenryQian/branch-A-Assertions
ChangruHenryQian Sep 15, 2023
a146f5a
Implement B-FixedDurationTasks
Sep 16, 2023
fe210a3
Add welcome message into GUI
Sep 18, 2023
b761c70
Add a representative screenshot
Sep 18, 2023
3f8b8db
Implement A-UserGuide: User Guide
Sep 18, 2023
9e2ec38
Update user guide
Sep 19, 2023
925e859
Implement CheckStyle
Sep 21, 2023
5c6a409
Add test cases
Sep 21, 2023
950bfdf
Update test cases
Sep 21, 2023
b02a0ee
Update test cases
Sep 21, 2023
bf0487d
Improve JavaDoc
Sep 21, 2023
bac1dfd
Update README.md
Sep 21, 2023
3600c25
Improve JavaDoc
Sep 21, 2023
fd641b2
Update user guide
Sep 22, 2023
c32f9db
Add test cases
Sep 22, 2023
5e157e9
Update JavaDoc
Sep 22, 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
Empty file added data/tasks.txt
Empty file.
10 changes: 0 additions & 10 deletions src/main/java/Duke.java

This file was deleted.

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

import java.time.LocalDate;
import java.time.format.DateTimeFormatter;
import java.util.Locale;

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

public Deadline(String description, boolean isDone, LocalDate by) {
super(description, isDone);
this.by = by;
}

@Override
public String toString() {
return "[D]" + super.toString() + " (by: "
+ this.by.format(DateTimeFormatter.ofPattern("MMM dd yyyy", Locale.ENGLISH)) + ")";
}

@Override
public String toTxt() {
return "D | " + super.toTxt()
+ " | " + this.by.format(DateTimeFormatter.ofPattern("MMM dd yyyy", Locale.ENGLISH));
}
}
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 java.util.Scanner;

import java.io.IOException;

public class Duke {

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

public Duke(String filePath) {
ui = new Ui();
storage = new Storage(filePath);
try {
tasks = new TaskList(storage.load(), storage);
} catch (DukeException e) {
ui.printException(e);
tasks = new TaskList(storage);
} catch (IOException e) {
ui.printException(new DukeException("Unable to find storage file."));
tasks = new TaskList(storage);
}
}

public void run() {
Ui.welcomeMessage();
Scanner userInput = new Scanner(System.in);
while (true) {
String command = userInput.nextLine();
Parser p = new Parser(command, this.storage, this.tasks);
p.parse();
if (p.isEnd()) {
break;
}
}
}

public static void main(String[] args) {
new Duke("data/tasks.txt").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 RuntimeException{
public DukeException(String message) {
super(message);
}
}
33 changes: 33 additions & 0 deletions src/main/java/duke/Event.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
package duke;

import java.time.LocalDate;
import java.time.format.DateTimeFormatter;
import java.util.Locale;

public class Event extends Task{
private LocalDate startTime;
private LocalDate endTime;
public Event(String description, LocalDate startTime, LocalDate endTime) {
super(description);
this.startTime = startTime;
this.endTime = endTime;
}

public Event(String description, boolean isDone, LocalDate startTime, LocalDate endTime) {
super(description, isDone);
this.startTime = startTime;
this.endTime = endTime;
}

@Override
public String toString() {
return "[E]" + super.toString() + " (from: " + startTime.format(DateTimeFormatter.ofPattern("MMM dd yyyy", Locale.ENGLISH))
+ " to: " + endTime.format(DateTimeFormatter.ofPattern("MMM dd yyyy", Locale.ENGLISH)) + ")";
}

@Override
public String toTxt() {
return "E | " + super.toTxt() + " | " + this.startTime.format(DateTimeFormatter.ofPattern("MMM dd yyyy", Locale.ENGLISH))
+ " | " + this.endTime.format(DateTimeFormatter.ofPattern("MMM dd yyyy", Locale.ENGLISH));
}
}
57 changes: 57 additions & 0 deletions src/main/java/duke/Parser.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
package duke;

import java.time.LocalDate;

public class Parser {
private String command;
private Storage storage;
private TaskList taskList;
private boolean isEnd = false;

public Parser(String command, Storage storage, TaskList taskList) {
this.command = command;
this.storage = storage;
this.taskList = taskList;
}

public void parse() {
try{

Choose a reason for hiding this comment

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

using switch statements may help reduce nesting. what do you think?

if (command.startsWith("list")) {
this.taskList.listTasks();
} else if (command.startsWith("mark")) {
this.taskList.markTask(Integer.valueOf(command.split(" ")[1]) - 1);
} else if (command.startsWith("todo")) {
if (command.split(" ", 2).length == 1) {
throw new DukeException(" OOPS!!! The description of a todo cannot be empty.");
}
ToDo newToDo = new ToDo(command.split(" ", 2)[1]);
this.taskList.addTask(newToDo);
} else if (command.startsWith("deadline")) {
LocalDate deadline = LocalDate.parse(command.split(" /by ", 2)[1]);
String name = command.split(" /by ", 2)[0].split(" ", 2)[1];

Choose a reason for hiding this comment

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

Perhaps splitting them into multiple statements could improve the readability of the code. It can make it easier for you to debug your code too :)

Deadline newDeadline = new Deadline(name, deadline);
this.taskList.addTask(newDeadline);
} else if (command.startsWith("event")) {
LocalDate startTime = LocalDate.parse(command.split(" /from ", 2)[1]
.split(" /to ", 2)[0]);
LocalDate endTime = LocalDate.parse(command.split(" /to ", 2)[1]);
String name = command.split(" /from ", 2)[0].split(" ", 2)[1];
Event newEvent = new Event(name, startTime, endTime);
this.taskList.addTask(newEvent);
} else if (command.startsWith("delete")) {
this.taskList.deleteTask(Integer.valueOf(command.split(" ")[1]) - 1);
} else if (command.startsWith("bye")) {
this.isEnd = true;
Ui.farewellMessage();
} else {
throw new DukeException(" OOPS!!! I'm sorry, but I don't know what that means :-(");
}
} catch (DukeException e) {
Ui.printException(e);
}
}

public boolean isEnd() {
return this.isEnd;
}
}
83 changes: 83 additions & 0 deletions src/main/java/duke/Storage.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,83 @@
package duke;

import java.io.File;

Choose a reason for hiding this comment

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

I like how you have organised your imports according to their functionality. It's clear and easy to read. Great job!

import java.io.FileWriter;
import java.io.IOException;

import java.util.Locale;
import java.time.LocalDate;
import java.time.format.DateTimeFormatter;

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

public class Storage {
private final String filePath;

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

public ArrayList<Task> load() throws IOException {
File dir = new File("./data");
if (!dir.exists()) {
dir.mkdir();
}
File f = new File(filePath);
f.createNewFile();
Scanner s = new Scanner(f);
ArrayList<Task> tasks = new ArrayList<>();
while (s.hasNext()) {
tasks.add(addTask(s.nextLine()));
}
return tasks;
}

private Task addTask(String input) {
String taskType = input.split(" \\| ")[0];
boolean isComplete = input.split(" \\| ")[1].equals("1");
String description = input.split(" \\| ")[2];
if (taskType.equals("T")) {
return new ToDo(description, isComplete);
} else if (taskType.equals("D")) {
LocalDate d = LocalDate.parse(input.split(" \\| ")[3], DateTimeFormatter.ofPattern("MMM dd yyyy", Locale.ENGLISH));
return new Deadline(description, isComplete, d);
} else {
LocalDate start = LocalDate.parse(input.split(" \\| ")[3], DateTimeFormatter.ofPattern("MMM dd yyyy", Locale.ENGLISH));
LocalDate end = LocalDate.parse(input.split(" \\| ")[4], DateTimeFormatter.ofPattern("MMM dd yyyy", Locale.ENGLISH));
return new Event(description, isComplete, start, end);
}
}


public void addOneLineToFile(ArrayList<Task> list) throws DukeException {
try {
FileWriter fw = new FileWriter(filePath, true);
if (list.size() == 1) {
fw.write(list.get(0).toTxt());
} else {
fw.write("\n" + list.get(list.size() - 1).toTxt());
}
fw.close();
} catch (IOException e) {
throw new DukeException("Unable to write to file.");
}
}

public void rewriteFile(ArrayList<Task> list) throws DukeException {
try {
FileWriter fw = new FileWriter(filePath);
for (int i = 0; i < list.size(); i++) {
if (i == list.size() - 1) {
fw.write(list.get(i).toTxt());
} else {
fw.write(list.get(i).toTxt() + "\n");
}
}
fw.close();
} catch (IOException e) {
throw new DukeException("Unable to write to file.");
}
}
}

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

public class Task {
protected String description;
protected boolean isDone;

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

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

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

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

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

public String toTxt() {

Choose a reason for hiding this comment

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

just a trivial issue but would spelling out the txt improve the readability of your code?

return (this.isDone ? "1" : "0") + " | " + this.description;
}

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

import java.util.ArrayList;

public class TaskList {
protected ArrayList<Task> tasks;
private Storage storage;

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

public TaskList(ArrayList<Task> tasks, Storage storage) {
this.tasks = tasks;
this.storage = storage;
}

public boolean isEmpty() {
return tasks.isEmpty();
}
public void listTasks() {
Ui.listTasks(this.tasks);
}

public void addTask(Task t) {
this.tasks.add(t);
Ui.addTask(t, this.tasks);
this.storage.addOneLineToFile(this.tasks);
}

public void deleteTask(int num) {
Task re = tasks.remove(num);
this.storage.rewriteFile(tasks);
Ui.deleteTask(re, tasks);
}

public void markTask(int num) {
Task t = tasks.get(num);
t.markAsDone();
Ui.markTask(t);
this.storage.rewriteFile(tasks);
}
}
21 changes: 21 additions & 0 deletions src/main/java/duke/ToDo.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
package duke;

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

public ToDo(String description, boolean isDone) {
super(description, isDone);
}

@Override
public String toString() {
return "[T]" + super.toString();
}

@Override
public String toTxt() {
return "T | " + super.toTxt();
}
}
Loading