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

[Gibson0918] IP #375

Open
wants to merge 41 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 14 commits
Commits
Show all changes
41 commits
Select commit Hold shift + click to select a range
556af3f
Add Gradle support
May 24, 2020
d2246c4
Add Level 1. Greet, Echo, Exit feature
Gibson0918 Jan 31, 2023
28b9930
Level 2 Completed
Gibson0918 Jan 31, 2023
75b6c9e
Level 3 done
Gibson0918 Jan 31, 2023
27b9c65
level 4 completed
Gibson0918 Feb 1, 2023
a008a30
Update Duke.java
Gibson0918 Feb 1, 2023
7f54de7
Completed Duke's Automated Text UI testing
Gibson0918 Feb 1, 2023
f43cdd7
Level 5 Completed.
Gibson0918 Feb 1, 2023
7e981d9
Level 6 Completed
Gibson0918 Feb 1, 2023
ee4a824
Added Enumerations
Gibson0918 Feb 1, 2023
44bf9f2
Update Duke.java
Gibson0918 Feb 1, 2023
038d559
Delete unnecessary files
Gibson0918 Feb 1, 2023
cdbfde3
Level 7 Completed
Gibson0918 Feb 1, 2023
9e0df04
Level 8 Completed
Gibson0918 Feb 1, 2023
33c6108
Merge branch 'branch-Level-8'
Gibson0918 Feb 1, 2023
6b26a8e
Refactor Duke to use more OOP
Gibson0918 Feb 2, 2023
3893500
Merge remote-tracking branch 'upstream/add-gradle-support'
Gibson0918 Feb 2, 2023
a8d7d89
Merge remote-tracking branch 'origin/add-gradle-support'
Gibson0918 Feb 3, 2023
26a98f0
Fixed bugs related to datetime and organize duke into packages
Gibson0918 Feb 4, 2023
c722460
add Gradle to Duke
Gibson0918 Feb 4, 2023
dcb5baf
Added JUnit tests for Duke
Gibson0918 Feb 5, 2023
4dc52da
Merge branch 'master' of https://github.com/Gibson0918/ip
Gibson0918 Feb 6, 2023
1756cea
Release duke to the wild
Gibson0918 Feb 6, 2023
391e222
Added Java Docs to Duke
Gibson0918 Feb 7, 2023
ca66669
Added StringBuilder class to Duke
Gibson0918 Feb 7, 2023
fc417a2
Completed Level 9
Gibson0918 Feb 7, 2023
818d54e
Tweak Duke's code
Gibson0918 Feb 7, 2023
451fc56
Level 10 Completed
Gibson0918 Feb 8, 2023
6a11a02
Remove commented lines
Gibson0918 Feb 8, 2023
9b4f6d8
Added more Java Docs
Gibson0918 Feb 9, 2023
cbb24a9
Improve Duke's code quality
Gibson0918 Feb 10, 2023
cad9985
Update README.md
Gibson0918 Feb 13, 2023
486c91d
Add Update feature to Duke
Gibson0918 Feb 14, 2023
77e726e
Updated GUI and added set method to tasklist
Gibson0918 Feb 14, 2023
f94e91b
Added Assertions to Duke
Gibson0918 Feb 14, 2023
7d857d1
Merge pull request #2 from Gibson0918/branch-A-Assertions
Gibson0918 Feb 14, 2023
18c5321
Update README.md
Gibson0918 Feb 15, 2023
e2a6d89
Fixed some bugs relating to DateTime format and Duke exit command
Gibson0918 Feb 15, 2023
14367ac
Merge branch 'master' of https://github.com/Gibson0918/ip
Gibson0918 Feb 15, 2023
5954700
Added Ui image for docs
Gibson0918 Feb 15, 2023
b272b25
Clean up code for checkstyle
Gibson0918 Feb 17, 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
3 changes: 3 additions & 0 deletions Type.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
public enum Type {
TODO, DEADLINE, EVENT
}
3 changes: 3 additions & 0 deletions duke.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
T | 0 | borrow book
D | 1 | return book | Sunday
E | 0 | project meeting | from Mon 2pm | to 4pm
19 changes: 19 additions & 0 deletions src/main/java/Deadline.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
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: " + by + ")";
}

@Override
public String formatForFile() {
return "D | " + (this.isDone ? 1 : 0) + " | " + description + " | " + this.by + "\n";
}
}
191 changes: 191 additions & 0 deletions src/main/java/Duke.java
Original file line number Diff line number Diff line change
@@ -1,10 +1,201 @@
import java.util.Scanner;
import java.util.ArrayList;
import java.io.File;
import java.io.FileNotFoundException;



public class Duke {

enum Type {
TODO, DEADLINE, EVENT
}

private static ArrayList<Task> taskList = new ArrayList<>();
private static boolean startDuke = true;

public static void main(String[] args) {
String logo = " ____ _ \n"
+ "| _ \\ _ _| | _____ \n"
+ "| | | | | | | |/ / _ \\\n"
+ "| |_| | |_| | < __/\n"
+ "|____/ \\__,_|_|\\_\\___|\n";
System.out.println("Hello from\n" + logo);

Scanner sc = new Scanner(System.in);

loadFile();

while (startDuke) {
initDuke(sc);
}
}

public static void loadFile() {
try {
taskList = DukeFile.loadFile();
for (Task t : taskList) {
System.out.println(t);
}
} catch (FileNotFoundException e) {
System.out.println(e);
}
}

public static void saveFile(ArrayList<Task> taskList) {
DukeFile.saveData(taskList);
}

public static void initDuke(Scanner sc) {
String[] userInput = sc.nextLine().split(" ", 2);

try {
switch (userInput[0]) {

Choose a reason for hiding this comment

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

Could potentially abstract out userInput[0] using enum

case "bye":
System.out.println("Bye. Hope to see you again soon!");
sc.close();
startDuke = false;
break;

Choose a reason for hiding this comment

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

Switch-case should not be indented. Could edit coding style in Intellij to reduce having to manually check.


case "list":
if (Task.taskCount == 0) {
System.out.println("You have no tasks");
}
else {
System.out.println("Here are the tasks in your list");
for (int i = 0; i < Task.taskCount; i++) {
System.out.printf("%d. %s \n", i + 1, taskList.get(i));
}
}
break;

case "mark":
markTask(Integer.valueOf(userInput[1]) - 1);
break;

case "unmark":
unmarkTask(Integer.valueOf(userInput[1]) - 1);
break;

case "todo":
if (userInput.length < 2) {
throw new DukeException("OOPS!!! The description of a todo cannot be empty.");
} else {
addTaskToList(Type.TODO, userInput[1]);
}
break;

case "deadline":
if (userInput.length < 2) {
throw new DukeException("OOPS!!! The description of a deadline cannot be empty.");
} else {
addTaskToList(Type.DEADLINE, userInput[1]);
}
break;

case "event":
if (userInput.length < 2) {
throw new DukeException("OOPS!!! The description of a event cannot be empty.");
} else {
addTaskToList(Type.EVENT, userInput[1]);
}
break;

case "delete":
if (userInput.length < 2) {
throw new DukeException("Missing taskID!");
} else {
deleteTask(Integer.valueOf(userInput[1]) - 1);
}
break;

default:
throw new DukeException("OOPS!!! I'm sorry, but I don't know what that means :-(");
}
} catch (DukeException e) {
System.out.println(e);
}
}

public static void addTaskToList(Type type, String userInput) throws DukeException {
switch (type) {
case TODO:
Task newToDo = new Todo(userInput);
taskList.add(newToDo);
saveFile(taskList);
Task.incrementTaskCount();
System.out.println("Got it. I've added this task:");
System.out.println(newToDo);
break;

case DEADLINE:
String[] deadlineFormatter = userInput.split(" /by ");
if (deadlineFormatter.length < 2 ) {
throw new DukeException("Either the description or deadline of the task is missing");
}
else {
Task newDeadLineTask = new Deadline(deadlineFormatter[0], deadlineFormatter[1]);
taskList.add(newDeadLineTask);
saveFile(taskList);
Task.incrementTaskCount();
System.out.println("Got it. I've added this task:");
System.out.println(newDeadLineTask);
}
break;

case EVENT:
String[] eventFormatter = userInput.split("/");
if (eventFormatter.length < 3 ) {
throw new DukeException("Either the description or dates (from/to) of the task is missing");
}
else {
Task newEventTask = new Event(eventFormatter[0], eventFormatter[1], eventFormatter[2]);
taskList.add(newEventTask);
saveFile(taskList);
Task.incrementTaskCount();
System.out.println("Got it. I've added this task:");
System.out.println(newEventTask);
}
break;
}
System.out.printf("Now you have %d tasks in the list.\n", Task.taskCount);
}

public static void markTask(int taskID) {
if (Task.taskCount > taskID && Task.taskCount > 0) {
System.out.println("Nice! I've marked this task as done:");
Task currentTask = taskList.get(taskID);
currentTask.mark();
saveFile(taskList);
System.out.println(currentTask);
} else {
System.out.println("Invalid taskID entered!");
}
}

public static void unmarkTask(int taskID) {
if (Task.taskCount > taskID && Task.taskCount > 0) {
System.out.println("OK, I've marked this task as not done yet:");
Task currentTask = taskList.get(taskID);
currentTask.unmark();
saveFile(taskList);
System.out.println(currentTask);
} else {
System.out.println("Invalid taskID entered!");
}
}

public static void deleteTask(int taskID) {
if (Task.taskCount > taskID && Task.taskCount > 0) {
System.out.println("Noted. I've removed this task:");
Task currentTask = taskList.get(taskID);
taskList.remove(taskID);
Task.decrementTaskCount();
saveFile(taskList);
System.out.println(currentTask);
System.out.printf("Now you have %d tasks in the list.\n", Task.taskCount);
} else {
System.out.println("Invalid taskID entered!");
}
}
}
6 changes: 6 additions & 0 deletions src/main/java/DukeException.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
public class DukeException extends Exception {
public DukeException(String message) {
super(message);
}

}
66 changes: 66 additions & 0 deletions src/main/java/DukeFile.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
import java.io.File;
import java.io.FileWriter;
import java.util.ArrayList;
import java.util.Scanner;
import java.io.IOException;
import java.io.FileNotFoundException;

public class DukeFile {


private static void checkFile() {
File file = new File("duke.txt");
if (!file.isFile()) {
try {
file.createNewFile();
} catch (IOException e) {
System.out.println(e);
}

}
}

public static void saveData(ArrayList<Task> taskList) {
try {
FileWriter fileWriter = new FileWriter("duke.txt");
for (Task t : taskList) {
fileWriter.write(t.formatForFile());
}
fileWriter.close();
} catch (IOException e) {
System.out.println(e);
}
}

public static ArrayList<Task> loadFile() throws FileNotFoundException {
ArrayList<Task> taskList = new ArrayList<>();
File file = new File("duke.txt");
if (file.isFile()) {
Scanner sc = new Scanner(file);
while (sc.hasNext()) {
String[] data = sc.nextLine().split(" \\| ");
Task task = null;
switch (data[0]) {
case "T":
task = new Todo(data[2]);
break;
case "D":
task = new Deadline(data[2], data[3]);
break;
case "E":
task = new Event(data[2], data[3], data[4]);
break;
}

if (data[1].equals("1")) {
task.mark();
}

taskList.add(task);
Task.incrementTaskCount();
}
}

return taskList;
}
}
23 changes: 23 additions & 0 deletions src/main/java/Event.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;

public class Event extends Task {
protected LocalDateTime from;
protected LocalDateTime to;

Choose a reason for hiding this comment

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

perhaps startDate and endDate would be better names

public Event(String description, String from, String to) {
super(description);
this.from = LocalDateTime.parse(from, DateTimeFormatter.ofPattern("yyyy-MM-dd HHmm"));
this.to = LocalDateTime.parse(to, DateTimeFormatter.ofPattern("yyyy-MM-dd HHmm"));
}

@Override
public String toString() {
return "[E]" + super.toString() + " (from: " + from.format(DateTimeFormatter.ofPattern("MMM dd yyyy HH:mm")) + " to: " + to.format(DateTimeFormatter.ofPattern("MMM dd yyyy HH:mm")) + ")";

Choose a reason for hiding this comment

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

this return statement could be indented better

}

@Override
public String formatForFile() {
return "E | " + (this.isDone ? 1 : 0) + " | " + description + " | " + this.from + " | " + this.to + "\n";
}
}
37 changes: 37 additions & 0 deletions src/main/java/Task.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
public abstract class Task {
protected String description;
protected boolean isDone;
protected static int taskCount = 0;

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

public static void incrementTaskCount() {
taskCount++;
}

public static void decrementTaskCount() {
taskCount--;
}

public String getStatusIcon() {
return (isDone ? "X" : " ");
}

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

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

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

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

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

@Override
public String formatForFile() {
return "T | " + (this.isDone ? 1 : 0) + " | " + this.description + "\n";
}
}
Loading