-
Notifications
You must be signed in to change notification settings - Fork 362
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
base: master
Are you sure you want to change the base?
[Gibson0918] IP #375
Changes from 14 commits
556af3f
d2246c4
28b9930
75b6c9e
27b9c65
a008a30
7f54de7
f43cdd7
7e981d9
ee4a824
44bf9f2
038d559
cdbfde3
9e0df04
33c6108
6b26a8e
3893500
a8d7d89
26a98f0
c722460
dcb5baf
4dc52da
1756cea
391e222
ca66669
fc417a2
818d54e
451fc56
6a11a02
9b4f6d8
cbb24a9
cad9985
486c91d
77e726e
f94e91b
7d857d1
18c5321
e2a6d89
14367ac
5954700
b272b25
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
public enum Type { | ||
TODO, DEADLINE, EVENT | ||
} |
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 |
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"; | ||
} | ||
} |
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]) { | ||
case "bye": | ||
System.out.println("Bye. Hope to see you again soon!"); | ||
sc.close(); | ||
startDuke = false; | ||
break; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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!"); | ||
} | ||
} | ||
} |
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); | ||
} | ||
|
||
} |
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; | ||
} | ||
} |
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; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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")) + ")"; | ||
|
||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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"; | ||
} | ||
} |
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(); | ||
} |
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"; | ||
} | ||
} |
There was a problem hiding this comment.
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