-
Notifications
You must be signed in to change notification settings - Fork 191
iP #188
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
base: master
Are you sure you want to change the base?
iP #188
Changes from 30 commits
0acc03a
e5a6c76
73e933e
9aa6701
9d224de
d10b249
431e205
423e4b6
dd62bad
0fd82f9
69645da
f2d4394
0a70a2e
8235c1d
199fc4c
e893cef
f0a5864
75c9f16
f192e85
b88ba29
da1e49e
23fdf30
4643e21
d87aae0
9609e5f
47dd64e
95d1f4f
692352e
d31772b
db8bd73
57a79f0
8e7fd03
21c0704
a1a550c
32656a1
7306402
c50db4d
a1ce7a1
52c2770
b32f9ed
a332d1a
7c7da8e
ebf987f
2f8ce8d
5dce913
4561808
a3b5c81
f16c13f
99cffd9
55f4c5b
6310239
4e96b08
5a77160
a1236a4
e6f1fd6
4f230f3
56fdebc
a279691
6022a07
d89f151
abf55fd
553b120
9d2ea67
7154720
effe27d
3b0adcf
55bc19a
d2019f2
3bc240c
56bb844
ffa2789
8cb4736
4466d03
b5a9282
b883ff9
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 @@ | ||
| event 6 /at 6pm | 0 |
This file was deleted.
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,3 @@ | ||
| Manifest-Version: 1.0 | ||
| Main-Class: duke.Duke | ||
|
|
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,16 @@ | ||
| package duke; | ||
|
|
||
| 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][" + getStatusIcon() + "] " + super.toString() + " (by: " + by + ")"; | ||
| } | ||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,291 @@ | ||
| package duke; | ||
|
|
||
| import java.util.Scanner; | ||
| import java.util.ArrayList; | ||
| import java.io.File; | ||
| import java.io.FileNotFoundException; | ||
| import java.io.FileWriter; | ||
| import java.io.IOException; | ||
| import java.nio.file.Files; | ||
| import java.nio.file.Paths; | ||
| import java.nio.file.StandardOpenOption; | ||
|
|
||
| public class Duke { | ||
|
|
||
| private static int byeFlag = 0; | ||
| private static int loadFlag = 0; | ||
| private static int positionCheck = 0; | ||
|
|
||
| //private static final String filePath = "./data/Tasks.txt"; | ||
|
|
||
| private static ArrayList<Task> commands = new ArrayList<>(); | ||
|
|
||
| private static void sendCommand() { | ||
| String line; | ||
| Scanner in = new Scanner(System.in); | ||
| while (byeFlag != 1) { | ||
| try { | ||
| System.out.println(Response.LINE); | ||
| System.out.print(Response.USER_PROMPT_MESSAGE); | ||
| line = in.nextLine(); | ||
| System.out.println(Response.LINE); | ||
| checkCommand(line); | ||
| } catch (NumberFormatException e) { | ||
| System.out.println(Response.NUMBER_FORMAT_EXCEPTION); | ||
| } catch (DukeException e) { | ||
| System.out.print(""); | ||
| } catch (IOException e) { | ||
| System.out.print(""); | ||
| } | ||
| } | ||
| } | ||
|
|
||
| private static void checkCommand(String line) throws DukeException, IOException { | ||
| String[] input = line.split(" "); | ||
| String firstWord = input[0]; | ||
| if (input.length == 1) { | ||
| switch (line) { | ||
| case "bye": | ||
| byeFlag = 1; | ||
| break; | ||
| case "list": | ||
| if (positionCheck == 0) { | ||
| throw new DukeException(Response.EMPTY); | ||
| } else { | ||
| printList(); | ||
| break; | ||
| } | ||
| case "done": | ||
| throw new DukeException(Response.UNSPECIFIED_DONE); | ||
| case "delete": | ||
| throw new DukeException(Response.UNSPECIFIED_DELETE); | ||
| default: | ||
| throw new DukeException(Response.UNSPECIFIED_TASK); | ||
| } | ||
| } else { | ||
| switch (firstWord) { | ||
| case "done": | ||
| case "delete": | ||
| int taskNumber = Integer.parseInt(input[1]); | ||
| int taskIndex = taskNumber - 1; | ||
| if (positionCheck <= 0) { | ||
| throw new DukeException(Response.EMPTY); | ||
| } else if ( (taskNumber > positionCheck ) || ( taskNumber <= 0) ) { | ||
| throw new DukeException(Response.INVALID); | ||
| } else if (firstWord.equals("done")) { | ||
| markDone(taskIndex); | ||
| break; | ||
| } else { | ||
| deleteTask(taskIndex); | ||
| break; | ||
| } | ||
| default: | ||
| checkTypeOfTask(line); | ||
| break; | ||
| } | ||
| } | ||
| } | ||
|
|
||
| public static void sayBye() { | ||
| System.out.println(Response.ENDING_MESSAGE + Response.LINE); | ||
| } | ||
|
|
||
| public static void addDeadline(String[] input, int length) throws DukeException, IOException { | ||
| String description; | ||
| String by; | ||
| for (int i = 1 ; i < length ; i++) { | ||
| if ((input[i].equals("/by")) && (i != 1) && (i != (length-1))) { | ||
| description = input[1]; | ||
| by = input[i+1]; | ||
| for (int j = 2 ; j < i ; j++) { | ||
| description += (" " + input[j]); | ||
| } | ||
| for (int k = i+2 ; k < length ; k++) { | ||
| by += (" " + input[k]); | ||
| } | ||
| commands.add( new Deadline(description,by) ); | ||
| if (loadFlag == 1) { | ||
| System.out.println("Added to Galactic database:" ); | ||
| System.out.println(commands.get(positionCheck)); | ||
| saveNewTask(input); | ||
| } | ||
| positionCheck += 1; | ||
| return; | ||
| } | ||
| } | ||
| throw new DukeException(Response.DEADLINE_ERROR); | ||
| } | ||
|
|
||
| public static void addEvent(String[] input, int length) throws DukeException, IOException { | ||
| String description; | ||
| String at; | ||
| for (int i = 1 ; i < length ; i++) { | ||
| if ((input[i].equals("/at")) && (i != 1) && (i != (length-1))) { | ||
| description = input[1]; | ||
| at = input[i+1]; | ||
| for (int j = 2 ; j < i ; j++) { | ||
| description += (" " + input[j]); | ||
| } | ||
| for (int k = i+2 ; k < length ; k++) { | ||
| at += (" " + input[k]); | ||
| } | ||
| commands.add( new Event(description,at) ); | ||
| if (loadFlag == 1) { | ||
| System.out.println("Added to Galactic database:"); | ||
| System.out.println(commands.get(positionCheck)); | ||
| saveNewTask(input); | ||
| } | ||
| positionCheck += 1; | ||
| return; | ||
| } | ||
| } | ||
| throw new DukeException(Response.EVENT_ERROR); | ||
| } | ||
|
|
||
| public static void addTodo(String[] input, int length) throws DukeException, IOException { | ||
| if (length == 1) { | ||
| throw new DukeException(Response.TODO_ERROR); | ||
| } else { | ||
| String description = input[1]; | ||
| for (int i = 2 ; i < length ; i++) { | ||
| description += (" " + input[i]); | ||
| } | ||
| commands.add( new Todo(description) ); | ||
| if (loadFlag == 1) { | ||
| System.out.println("Added to Galactic database:"); | ||
| System.out.println(commands.get(positionCheck)); | ||
| saveNewTask(input); | ||
| } | ||
| positionCheck += 1; | ||
| } | ||
| } | ||
|
|
||
| public static void checkTypeOfTask(String line) throws DukeException, IOException { | ||
| String[] input = line.split(" "); | ||
| int length = input.length; | ||
| String firstWord = input[0]; | ||
| switch (firstWord) { | ||
| case "deadline": | ||
| addDeadline(input,length); | ||
| break; | ||
| case "event": | ||
| addEvent(input,length); | ||
| break; | ||
| case "todo": | ||
| addTodo(input,length); | ||
| break; | ||
| default: | ||
| throw new DukeException(Response.UNSPECIFIED_TASK); | ||
| } | ||
| } | ||
|
|
||
| private static void printList() { | ||
| System.out.println("Accessing archives..."); | ||
| int i = 1; | ||
| for (Task num : commands) { | ||
| System.out.println(i + ". " + num); | ||
| i += 1; | ||
| } | ||
| } | ||
|
|
||
| private static void markDone(int doneTaskNumber) throws IOException { | ||
| (commands.get(doneTaskNumber)).markAsDone(); | ||
| if (loadFlag == 1) { | ||
| System.out.println("The following task has been marked as done Master!"); | ||
| System.out.println((doneTaskNumber + 1) + ". " + commands.get(doneTaskNumber)); | ||
| saveAllTasks(); | ||
| } | ||
| } | ||
|
|
||
| private static void deleteTask(int doneTaskNumber) throws IOException { | ||
| System.out.println("Taking one last look Master, at this Task. Removing the following from my memory"); | ||
| System.out.println((doneTaskNumber+1) + ". " + commands.get(doneTaskNumber)); | ||
| commands.remove(commands.get(doneTaskNumber)); | ||
| positionCheck -= 1; | ||
| System.out.println("Goodbye Task, may the force be with you. You have " + positionCheck + " task(s) left Master"); | ||
| saveAllTasks(); | ||
| } | ||
|
|
||
| public static void greetUser() { | ||
| System.out.println(Response.GREETINGS); | ||
| } | ||
|
|
||
| public static void saveNewTask(String[] input) throws IOException { | ||
| String filePath = new File("Tasks.txt").getAbsolutePath(); | ||
| FileWriter fw = new FileWriter(filePath, true); | ||
| String fullTaskAsString = ""; | ||
| for (String individualString : input) { | ||
| fullTaskAsString += individualString + " "; | ||
| } | ||
| fullTaskAsString += "| 0"; | ||
| fw.write(fullTaskAsString + "\n"); | ||
| fw.close(); | ||
| } | ||
|
|
||
| public static void saveAllTasks() throws IOException { | ||
| String filePath = new File("Tasks.txt").getAbsolutePath(); | ||
| FileWriter fw = new FileWriter(filePath, false); | ||
| String taskInFile; | ||
| String done; | ||
| for (Task individualTask : commands) { | ||
| if (individualTask instanceof Deadline) { | ||
| taskInFile = "deadline " + individualTask.description + " /by " + ((Deadline) individualTask).by; | ||
| } else if (individualTask instanceof Event) { | ||
| taskInFile = "event " + individualTask.description + " /at " + ((Event) individualTask).at; | ||
| } else { | ||
| taskInFile = "todo " + individualTask.description; | ||
| } | ||
| if (individualTask.isDone) { | ||
| done = "1"; | ||
| } else { | ||
| done = "0"; | ||
| } | ||
|
||
| String fullTaskAsString = taskInFile + " | " + done + "\n"; | ||
| Files.write(Paths.get(filePath), fullTaskAsString.getBytes(), StandardOpenOption.APPEND); | ||
| } | ||
| } | ||
|
|
||
| public static void loadTasks() throws DukeException, IOException { | ||
| String filePath = new File("Tasks.txt").getAbsolutePath(); | ||
| File f = new File(filePath); | ||
| Scanner s = new Scanner(f); | ||
| int taskNumber = 0; | ||
| String textFromFile; | ||
| String[] taskFromFile; | ||
| String[] taskInput; | ||
| while (s.hasNext()) { | ||
| textFromFile = s.nextLine(); | ||
| taskFromFile = textFromFile.split(" \\| "); | ||
| taskInput = (taskFromFile[0]).split(" "); | ||
| switch(taskInput[0]) { | ||
| case "deadline": | ||
| addDeadline(taskInput, taskInput.length); | ||
| break; | ||
| case "event": | ||
| addEvent(taskInput, taskInput.length); | ||
| break; | ||
| case "todo": | ||
| addTodo(taskInput, taskInput.length); | ||
|
||
| default: | ||
| } | ||
| if ( taskFromFile[1].equals("1")) { | ||
| markDone(taskNumber); | ||
| } | ||
| taskNumber += 1; | ||
| } | ||
| System.out.println(Response.STARTING_MESSAGE); | ||
| loadFlag = 1; | ||
| } | ||
|
|
||
| public static void main(String[] args) throws DukeException, IOException { | ||
| try { | ||
| loadTasks(); | ||
| } catch (FileNotFoundException e) { | ||
| System.out.println("File not found. Automatic text file creation initiated master!"); | ||
| loadFlag = 1; | ||
| } | ||
| greetUser(); | ||
| sendCommand(); | ||
| sayBye(); | ||
| } | ||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,8 @@ | ||
| package duke; | ||
|
|
||
| public class DukeException extends Exception { | ||
| public DukeException(String errorMessage){ | ||
| super(errorMessage); | ||
| System.out.println(errorMessage); | ||
| } | ||
| } | ||
|
Comment on lines
+3
to
+16
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. Great use of super, with this overwriting of error messages are done easily |
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,16 @@ | ||
| package duke; | ||
|
|
||
| public class Event extends Task{ | ||
|
|
||
| protected String at; | ||
|
|
||
| public Event(String description, String at) { | ||
| super(description); | ||
| this.at = at; | ||
| } | ||
|
|
||
| @Override | ||
| public String toString() { | ||
| return "[E][" + getStatusIcon() + "] " + super.toString() + " (at: " + at + ")"; | ||
| } | ||
| } |
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.
Great to see that you organise your types (e.g., classes) into a package for easier management!