-
Notifications
You must be signed in to change notification settings - Fork 193
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
[Wang Zhihuang] iP #181
base: master
Are you sure you want to change the base?
[Wang Zhihuang] iP #181
Changes from 23 commits
60a88c0
8747f62
ab4602c
b43adfa
68d69b3
c3cd436
6b63884
9089498
5aab948
5d11a58
aa34d61
753e0a8
eea7087
b6f0c99
326a6f2
04badc3
30ba786
22b68f0
3a8a6f4
793418f
44695ee
5cf8dd0
6b52c9e
3d92406
054a4d6
770747e
99938ba
0b0382a
258e217
25f9be6
433daa2
c89f1a9
86057c2
60db3bb
b89640a
8fbb849
1e3f1a9
e99c7f8
ab2da15
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 @@ | ||
D | 0 | return book | Sunday |
This file was deleted.
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
package duke; | ||
|
||
public class DukeException extends Exception { | ||
|
||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
package duke.command; | ||
|
||
import duke.task.TaskManager; | ||
import java.io.IOException; | ||
import java.util.Scanner; | ||
|
||
|
||
public class Duke { | ||
public static void main(String[] args) throws IOException { | ||
TaskManager taskManager = new TaskManager(); | ||
Scanner scanner = new Scanner(System.in); | ||
Ui ui = new Ui(taskManager, scanner); | ||
ui.start(); | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,116 @@ | ||
package duke.command; | ||
|
||
import duke.data.Storage; | ||
import duke.task.TaskManager; | ||
import duke.DukeException; | ||
|
||
import java.io.FileNotFoundException; | ||
import java.io.IOException; | ||
import java.nio.file.Files; | ||
import java.nio.file.Path; | ||
import java.nio.file.Paths; | ||
import java.util.Scanner; | ||
|
||
public class Ui { | ||
private TaskManager taskManager; | ||
private Scanner scanner; | ||
|
||
private static final String LINE = " ____________________________________________________________\n"; | ||
private static final String LOGO = "\n" + | ||
" \n" + | ||
" ,--. ,------. ,--. ,--. ,--. \n" + | ||
",-' '-.,---.| .-. \\ ,---.| | `--',---,-' '-. \n" + | ||
"'-. .-| .-. | | \\ | .-. | | ,--( .-'-. .-' \n" + | ||
" | | ' '-' | '--' ' '-' | '--| .-' `)| | \n" + | ||
" `--' `---'`-------' `---'`-----`--`----' `--' \n" + | ||
" \n"; | ||
private static final String GREETINGS = LINE | ||
+ LOGO | ||
+ " Welcome to the toDoList Chatbot\n" | ||
+ " What would you like to do today?\n" | ||
+ LINE; | ||
private static final String FAREWELL = " Bye. Hope to see you again soon!"; | ||
|
||
private static final String TO_DO = "todo"; | ||
private static final String DEADLINE = "deadline"; | ||
private static final String EVENT = "event"; | ||
private static final String BYE = "bye"; | ||
private static final String LIST = "list"; | ||
private static final String DONE = "done"; | ||
private static final String DELETE = "delete"; | ||
private static final String ADD_SUCCESS = " Nice! I've marked this task as done: "; | ||
private static final String DELETE_SUCCESS = " Noted. I've removed this task:"; | ||
private static final String PATH_NAME = "data/output.txt"; | ||
|
||
public Ui(TaskManager taskManager, Scanner scanner) { | ||
this.taskManager = taskManager; | ||
this.scanner = scanner; | ||
} | ||
|
||
private static void saveData() { | ||
try { | ||
String pathName = PATH_NAME; | ||
//create folder with file if absent initially | ||
Path path = Paths.get(pathName); | ||
Files.createDirectories(path.getParent()); | ||
Storage.writeToFile(pathName); | ||
} catch (IOException e) { | ||
e.printStackTrace(); | ||
} | ||
} | ||
|
||
private static void loadData() throws IOException { | ||
try { | ||
Storage.load(PATH_NAME); | ||
} catch (FileNotFoundException e) { | ||
e.printStackTrace(); | ||
} | ||
} | ||
|
||
public void start() throws IOException { | ||
loadData(); | ||
System.out.println(GREETINGS); | ||
boolean isExit = false; | ||
while (!isExit) { | ||
String input = scanner.nextLine(); | ||
String[] command = input.split(" "); | ||
String firstWord = command[0]; | ||
|
||
System.out.print(LINE); | ||
try { | ||
switch (firstWord) { | ||
case BYE: | ||
System.out.println(FAREWELL); | ||
isExit = true; | ||
break; | ||
case LIST: | ||
taskManager.list(); | ||
break; | ||
case DONE: | ||
int taskNumber = Integer.parseInt(command[1]); | ||
System.out.println(ADD_SUCCESS); | ||
taskManager.checkDone(command); | ||
System.out.println(" " + taskManager.getName(taskNumber)); | ||
break; | ||
case TO_DO: | ||
case DEADLINE: | ||
case EVENT: | ||
taskManager.add(input); | ||
break; | ||
case DELETE: | ||
System.out.println(DELETE_SUCCESS); | ||
taskManager.deleteTask(command); | ||
break; | ||
default: | ||
System.out.println(" ☹ OOPS!!! I'm sorry, but I don't know what that means :-("); | ||
} | ||
} catch (DukeException e) { | ||
System.out.println(" ☹ OOPS!!! The description of a " + firstWord + " cannot be empty."); | ||
} catch (NumberFormatException e) { | ||
System.out.println(" ☹ OOPS!!! The task's index should be an integer."); | ||
} | ||
System.out.print(LINE); | ||
} | ||
saveData(); | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,66 @@ | ||
package duke.data; | ||
|
||
import duke.task.Deadline; | ||
import duke.task.Event; | ||
import duke.task.Task; | ||
import duke.task.TaskManager; | ||
|
||
import java.io.File; | ||
import java.io.FileWriter; | ||
import java.io.IOException; | ||
import java.util.Scanner; | ||
|
||
public class Storage { | ||
|
||
public static void load(String filePath) throws IOException { | ||
File newFile = new File(filePath); | ||
Scanner scanner = new Scanner(newFile); | ||
|
||
while (scanner.hasNext()) { | ||
String line = scanner.nextLine(); | ||
String[] array = line.split(" | "); | ||
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. Might want to improve on the name for "array" to make it more meaningful. |
||
switch (array[0]) { | ||
case "T": | ||
TaskManager.loadToDoFromFile(array[2]); | ||
break; | ||
case "D": | ||
TaskManager.loadDeadlineFromFile(array[2], array[3]); | ||
break; | ||
case "E": | ||
TaskManager.loadEventFromFile(array[2], array[3]); | ||
break; | ||
} | ||
} | ||
} | ||
|
||
|
||
public static void appendToFile(String filePath, String textToAppend) throws IOException { | ||
FileWriter fw = new FileWriter(filePath, true); // create a FileWriter in append mode | ||
fw.write(textToAppend); | ||
fw.close(); | ||
} | ||
|
||
public static void writeToFile(String filePath) throws IOException { | ||
File file = new File(filePath); | ||
if (file.createNewFile()) { | ||
System.out.println("File created"); | ||
} | ||
String textToAppend; | ||
for (Task task: TaskManager.taskList) { | ||
String taskType = task.getIcon(); | ||
String status = task.getStatus(); | ||
String description = task.getDescription(); | ||
String timing = task.getTime(); | ||
|
||
textToAppend = taskType + " | " + status + " | " + description; | ||
if (task instanceof Event || task instanceof Deadline) { | ||
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. Avoid complicated expressions, is there any way you can extract it out or make this more streamlined? (hint: use booleans) |
||
textToAppend += " | " + timing; | ||
} | ||
textToAppend += "\n"; | ||
appendToFile(filePath, textToAppend); | ||
} | ||
} | ||
|
||
|
||
|
||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
package duke.task; | ||
|
||
public class Deadline extends Task { | ||
|
||
protected String timing; | ||
|
||
public Deadline(String description, String by) { | ||
super(description); | ||
this.timing = by; | ||
} | ||
|
||
public String getIcon() { | ||
return "D"; | ||
} | ||
|
||
public String getTiming() { | ||
return "(by:" + timing + ")"; | ||
} | ||
|
||
public String getTime() { | ||
return timing; | ||
} | ||
|
||
@Override | ||
public String toString() { | ||
return "[" + getIcon() + "]" + super.toString() + getTiming(); | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
package duke.task; | ||
|
||
public class Event extends Task { | ||
protected String timing; | ||
|
||
public Event(String description, String timing) { | ||
super(description); | ||
this.timing = timing; | ||
} | ||
|
||
public String getIcon() { | ||
return "E"; | ||
} | ||
|
||
public String getTiming() { | ||
return "(at:" + timing + ")"; | ||
} | ||
|
||
public String getTime() { | ||
return timing; | ||
} | ||
@Override | ||
public String toString() { | ||
return "[" + getIcon() + "]" + super.toString() + getTiming(); | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
package duke.task; | ||
|
||
public class Task { | ||
protected String description; | ||
protected boolean isDone; | ||
|
||
public Task(String description) { | ||
this.description = description; | ||
this.isDone = false; | ||
} | ||
|
||
public void taskDone() { | ||
this.isDone = true; | ||
} | ||
|
||
public String getStatusIcon() { | ||
return (isDone ? "[X]" : "[ ]"); | ||
} | ||
|
||
public String getStatus() { | ||
return (isDone ? "1" : "0"); | ||
} | ||
|
||
public String getIcon() { | ||
return ""; | ||
} | ||
|
||
public String getTiming() { | ||
return ""; | ||
} | ||
|
||
public String getDescription() { | ||
return description; | ||
} | ||
|
||
public String getTime() { | ||
return ""; | ||
} | ||
|
||
public String toString() { | ||
return getStatusIcon() + " " + this.description ; | ||
} | ||
|
||
|
||
} |
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.
Consider adding a line spacing before a comment for improved readability.