-
Notifications
You must be signed in to change notification settings - Fork 267
[KristopherPTaslim] iP #287
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?
Changes from 13 commits
d839859
5406049
0dd972e
1812262
277c43f
73b1d1c
d619a83
318b3aa
32bb23a
bc9608c
e754bcb
b37f27b
83bb0f3
27fc406
589b161
e8b6b2a
28a85db
7bbe7be
7b43639
bbc3057
6ea1b8c
3d551ba
452a916
aa60d80
6b75a3c
725a458
fd68d86
80a985b
4524860
adc7846
c3d28a2
97d12b1
1dfa71f
c96d62b
36ccfcc
99300b0
7e5a187
cc38eb8
1a329c8
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
This file was deleted.
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,21 @@ | ||
| package duke; | ||
|
|
||
| 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 getInitial() { | ||
| return "[D]"; | ||
| } | ||
| } | ||
| Original file line number | Diff line number | Diff line change | ||||
|---|---|---|---|---|---|---|
| @@ -0,0 +1,47 @@ | ||||||
| package duke; | ||||||
|
|
||||||
| import java.io.IOException; | ||||||
| import java.util.Scanner; | ||||||
|
|
||||||
| public class Duke { | ||||||
|
|
||||||
| private Storage storage; | ||||||
| private TaskList taskList; | ||||||
| private Ui ui; | ||||||
|
|
||||||
| public Duke(String filePath) { | ||||||
| this.ui = new Ui(); | ||||||
| this.storage = new Storage(filePath); | ||||||
| this.taskList = new TaskList(storage); | ||||||
| } | ||||||
|
|
||||||
| public void run() { | ||||||
| ui.showWelcomeMessage(); | ||||||
| try { | ||||||
| Scanner sc = new Scanner(storage.load()); | ||||||
| String input = sc.nextLine(); | ||||||
| Parser parser = new Parser(taskList); | ||||||
|
|
||||||
| while (sc.hasNextLine()) { | ||||||
| parser.execute(input); | ||||||
|
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 you can change the name of the method since a parser is parsing an input
Suggested change
|
||||||
| if (input.toLowerCase().equals("bye")) { | ||||||
| break; | ||||||
| } | ||||||
| input = sc.nextLine(); | ||||||
| } | ||||||
|
|
||||||
| } catch (IOException e) { | ||||||
| System.out.println("File is not found :(!"); | ||||||
| } | ||||||
|
|
||||||
| storage.writeFile(); | ||||||
| } | ||||||
|
|
||||||
| public static void main(String[] args) { | ||||||
| Duke duke = new Duke("/data/tasks.txt"); | ||||||
| duke.run(); | ||||||
| } | ||||||
|
|
||||||
| } | ||||||
|
|
||||||
|
|
||||||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,8 @@ | ||
| package duke; | ||
|
|
||
| class DukeException extends RuntimeException { | ||
|
|
||
| public DukeException(String Message) { | ||
| super(Message); | ||
| } | ||
|
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. You could add a more descriptive message to be stored within the DukeException class |
||
| } | ||
| Original file line number | Diff line number | Diff line change | ||||||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
| @@ -0,0 +1,21 @@ | ||||||||||||||||||
| package duke; | ||||||||||||||||||
|
|
||||||||||||||||||
| class Event extends Task { | ||||||||||||||||||
|
|
||||||||||||||||||
| protected String at; | ||||||||||||||||||
|
|
||||||||||||||||||
| public Event(String description, String at) { | ||||||||||||||||||
| super(description); | ||||||||||||||||||
| this.at = at; | ||||||||||||||||||
| } | ||||||||||||||||||
|
Comment on lines
+7
to
+10
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. Same thing with the deadline task
Suggested change
|
||||||||||||||||||
|
|
||||||||||||||||||
| @Override | ||||||||||||||||||
| public String toString() { | ||||||||||||||||||
| return "[E]" + super.toString() + "(at: " + at + ")"; | ||||||||||||||||||
| } | ||||||||||||||||||
|
|
||||||||||||||||||
| @Override | ||||||||||||||||||
| public String getInitial() { | ||||||||||||||||||
| return "[E]"; | ||||||||||||||||||
| } | ||||||||||||||||||
| } | ||||||||||||||||||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,42 @@ | ||
| package duke; | ||
|
|
||
| import java.io.File; | ||
| import java.io.IOException; | ||
| import java.nio.file.Paths; | ||
| import java.io.FileWriter; | ||
| import java.nio.file.Files; | ||
| import java.nio.file.Path; | ||
|
|
||
| class FileClass { | ||
|
|
||
| public void createFile(String filePath) { | ||
| File f = new File(filePath); //initialise the file | ||
| if (!f.exists()) { //meaning f doesnt exist | ||
| try { | ||
| f.createNewFile(); | ||
| } catch (IOException e) { | ||
| System.out.println("Path Directory is invalid!"); | ||
| } | ||
| } | ||
| } | ||
| //taken from W3.3c File Access | ||
| public void writeFile(String filePath, String textToAdd) throws IOException { | ||
| FileWriter fw = new FileWriter(filePath, true); // initialise the filewriter | ||
| fw.write(textToAdd + "\r\n"); | ||
| fw.close(); | ||
| } | ||
|
|
||
| public void createDirectory(String filePath) { | ||
| File f = new File(filePath); //initialise the file | ||
| Path path = Paths.get(filePath); | ||
| if (!f.exists()) { //meaning f doesnt exist | ||
| try { | ||
| Files.createDirectory(path); | ||
| } catch (IOException e) { | ||
| System.out.println("Path Directory cannot be created!"); | ||
| } | ||
| } | ||
|
|
||
|
|
||
| } | ||
| } |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,131 @@ | ||
| package duke; | ||
|
|
||
| import java.time.LocalDateTime; | ||
| import java.time.format.DateTimeFormatter; | ||
|
|
||
| class Parser { | ||
| public TaskList taskList; | ||
| private Ui ui; | ||
|
|
||
| Parser(TaskList taskList) { | ||
| this.taskList = taskList; | ||
| this.ui = new Ui(); | ||
| } | ||
|
|
||
| public void execute(String input) { | ||
| String[] checkCase = input.split(" "); | ||
| switch (checkCase[0].toLowerCase()) { | ||
|
|
||
| case ("list"): | ||
| ui.showListMessage(taskList); | ||
| break; | ||
|
|
||
| case ("mark"): | ||
| try { | ||
| int index = Integer.parseInt(checkCase[1]) - 1; | ||
| Task tasks = taskList.getTaskArray().get(index); | ||
| System.out.println(tasks.marking(checkCase[0].toLowerCase())); | ||
| taskList.getTaskArray().set(index, tasks); | ||
| } catch (ArrayIndexOutOfBoundsException e) { | ||
| ui.showInvalidInput(); | ||
| } | ||
| break; | ||
|
|
||
| case ("unmark"): | ||
| try { | ||
| int index = Integer.parseInt(checkCase[1]) - 1; | ||
| Task tasks = taskList.getTaskArray().get(index); | ||
| System.out.println(tasks.marking(checkCase[0].toLowerCase())); | ||
| taskList.getTaskArray().set(index, tasks); | ||
| } catch (ArrayIndexOutOfBoundsException e) { | ||
| ui.showInvalidInput(); | ||
| } | ||
| break; | ||
|
|
||
| case ("todo"): | ||
|
|
||
| try { | ||
| String toDoCondition = "todo "; | ||
| int indexOfToDo = toDoCondition.length(); //to find todo | ||
| String stringSliced = input.substring(indexOfToDo,input.length()); | ||
| Todo todoTask = new Todo(stringSliced); | ||
| taskList.addTask(todoTask); | ||
| String noOfTask = String.valueOf(taskList.getTaskArray().size()); | ||
| ui.showAddedMessage(todoTask, noOfTask); | ||
| } catch (StringIndexOutOfBoundsException e) { | ||
| ui.showTodoError(); | ||
| } | ||
| break; | ||
|
|
||
| case ("deadline"): | ||
|
|
||
| try { | ||
| String deadlineCondition = "/by "; | ||
| int indexOfTime = input.indexOf(deadlineCondition); //to find / | ||
| String dateTime = input.substring(indexOfTime + deadlineCondition.length(), input.length()); // the date and time for by | ||
| //convert to the correct one | ||
| LocalDateTime deadlineTime = LocalDateTime.parse(dateTime, DateTimeFormatter.ofPattern("d/M/y Hmm")); | ||
| String convertedTime = deadlineTime.format(DateTimeFormatter.ofPattern("MMM d yyyy hh:mm a")); | ||
| String stringSliced = input.substring(9, indexOfTime); // after deadline | ||
| Deadline deadlineTask = new Deadline(stringSliced, convertedTime); | ||
| taskList.addTask(deadlineTask); | ||
| String noOfTask = String.valueOf(taskList.getTaskArray().size()); | ||
| ui.showAddedMessage(deadlineTask, noOfTask); | ||
| } catch (StringIndexOutOfBoundsException e) { | ||
| ui.showDeadlineError(); | ||
| } | ||
| break; | ||
|
|
||
| case ("event"): | ||
|
|
||
| try { | ||
| String eventCondition = "/at "; | ||
| int indexOfTime = input.indexOf(eventCondition); //to find / | ||
| String dateTime = input.substring(indexOfTime + eventCondition.length(), input.length()); // the date and time for at | ||
| //convert to the correct one | ||
| LocalDateTime eventTime = LocalDateTime.parse(dateTime, DateTimeFormatter.ofPattern("d/M/y Hmm")); | ||
| String convertedTime = eventTime.format(DateTimeFormatter.ofPattern("MMM d yyyy hh:mm a")); | ||
| String stringSliced = input.substring(6, indexOfTime); // after deadline | ||
| Event eventTask = new Event(stringSliced, convertedTime); | ||
| taskList.addTask(eventTask); | ||
| String noOfTask = String.valueOf(taskList.getTaskArray().size()); | ||
| ui.showAddedMessage(eventTask, noOfTask); | ||
|
|
||
| } catch (StringIndexOutOfBoundsException e) { | ||
| ui.showEventError(); | ||
| } | ||
| break; | ||
|
|
||
| case("delete"): | ||
|
|
||
| try { | ||
| int index = Integer.parseInt(checkCase[1]) - 1; | ||
| Task task = taskList.getTaskArray().get(index); | ||
| taskList.deleteTask(index); | ||
| String noOfTask = String.valueOf(taskList.getTaskArray().size()); | ||
| ui.showDeletedMessage(task, noOfTask); | ||
| } catch (ArrayIndexOutOfBoundsException e) { | ||
| ui.showDeleteError(); | ||
| } | ||
| break; | ||
|
|
||
| case("bye"): | ||
| ui.showGoodbyeMessage(); | ||
| break; | ||
|
|
||
| default: | ||
| ui.showDefaultMessage(); | ||
| break; | ||
|
|
||
|
|
||
|
|
||
| } | ||
|
|
||
|
|
||
| } | ||
|
Comment on lines
+17
to
+157
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. Apart from the coding standard violations, perhaps you can break up each case instruction into separate methods for increased readability? For example: |
||
|
|
||
|
|
||
|
|
||
|
|
||
|
|
||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,53 @@ | ||
| package duke; | ||
|
|
||
| import java.util.ArrayList; | ||
| import java.io.File; | ||
| import java.io.IOException; | ||
|
|
||
| class Storage { | ||
| private ArrayList<Task> taskArray; | ||
| private String path; | ||
| private static String HOME = System.getProperty("user.home"); | ||
|
|
||
| Storage(String path) { | ||
| this.taskArray = new ArrayList<Task>(); | ||
| this.path = path; | ||
| } | ||
|
|
||
| public File load() { | ||
| FileClass fc = new FileClass(); //file class | ||
| String homePath = HOME + "/data"; | ||
| path = HOME + path; | ||
| fc.createDirectory(homePath); //create directory first | ||
| fc.createFile(path); //create a file in the /home/data/tasks.txt | ||
| File file = new File(path); | ||
| return file; | ||
| } | ||
|
|
||
| public void writeFile() { | ||
| FileClass fc = new FileClass(); | ||
| String filePath = HOME + "/data/stored.txt"; | ||
| fc.createFile(filePath); | ||
| for (int i = 0; i < taskArray.size(); i++) { | ||
| Task tasks = taskArray.get(i); | ||
| try { | ||
| String firstInitial = tasks.getInitial() ; //first initial character | ||
| String textToAdd = firstInitial + " | " + tasks.getStatusIcon() + " | " | ||
| + taskArray.get(i).getDescription(); | ||
| fc.writeFile(filePath, textToAdd); | ||
| } catch (IOException e) { | ||
| System.out.println("File is not found :(!"); | ||
| } | ||
|
|
||
| } | ||
|
|
||
| } | ||
|
|
||
| public ArrayList<Task> getList() { | ||
| return this.taskArray; | ||
| } | ||
|
|
||
|
|
||
|
|
||
|
|
||
| } |
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.
Perhaps you could rename "by" to something more descriptive.