-
Notifications
You must be signed in to change notification settings - Fork 200
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
[Gerald Koh Kheng Guan] iP #192
base: master
Are you sure you want to change the base?
Changes from 15 commits
de8ed4d
5d5cc71
bc93fd2
7d21da9
6d10d3f
444b5c0
f7d165c
25dffa2
609c4a9
544ba64
8902eae
69d4b63
959c5a4
13f7505
8a06c04
e0c6dc9
391f7bf
77ef37e
6ba8f86
f423633
bf20902
cbc2097
c9f98f4
7831692
6df56ee
f4792f0
a7d571e
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 |
---|---|---|
|
@@ -15,3 +15,4 @@ bin/ | |
|
||
/text-ui-test/ACTUAL.TXT | ||
text-ui-test/EXPECTED-UNIX.TXT | ||
data/savedList.txt |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,10 +1,11 @@ | ||
import utility.Methods; | ||
import java.util.Scanner; | ||
public class Duke { | ||
public static void main(String[] args) { | ||
String logo = " ____ _ \n" | ||
+ "| _ \\ _ _| | _____ \n" | ||
+ "| | | | | | | |/ / _ \\\n" | ||
+ "| |_| | |_| | < __/\n" | ||
+ "|____/ \\__,_|_|\\_\\___|\n"; | ||
System.out.println("Hello from\n" + logo); | ||
DukeSession dukeSession = new DukeSession(); | ||
Methods.printGreetings(); | ||
dukeSession.setUpArrayList(); | ||
dukeSession.execute(); | ||
} | ||
} | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,92 @@ | ||
import file.methods.FileHandler; | ||
import utility.Methods; | ||
import utility.commandChecker; | ||
|
||
import tasks.Deadline; | ||
import tasks.Event; | ||
import tasks.Task; | ||
import tasks.Todo; | ||
|
||
import java.io.File; | ||
import java.util.Scanner; | ||
import java.util.ArrayList; | ||
|
||
|
||
public class DukeSession { | ||
private ArrayList<Task> actions = new ArrayList<>(); | ||
public void setUpArrayList() { | ||
FileHandler.setUpFile(actions); | ||
} | ||
public void execute() { | ||
String line; | ||
int taskNumber; | ||
String[] decisions; | ||
Task toBeAdded; | ||
String[] dates; | ||
Scanner in = new Scanner(System.in); | ||
do { | ||
line = in.nextLine(); | ||
decisions = line.split(" "); | ||
dates = line.split("/"); | ||
commandChecker currentLoop = new commandChecker(decisions, dates, actions.size()); | ||
if (currentLoop.hasErrors()) { | ||
decisions[0] = "Invalidate"; | ||
} | ||
switch (decisions[0]) { | ||
case "echo": | ||
System.out.println(Methods.findTaskDetails(line)); | ||
break; | ||
|
||
case "todo": | ||
toBeAdded = new Todo(Methods.findTaskDetails(line)); | ||
actions.add(toBeAdded); | ||
Methods.printAcknowledgement(toBeAdded, actions.size()); | ||
break; | ||
|
||
case "event": | ||
toBeAdded = new Event(Methods.findTaskDetails(dates[0]), Methods.findTaskDetails(dates[1]), Methods.findTaskDetails(dates[2])); | ||
actions.add(toBeAdded); | ||
Methods.printAcknowledgement(toBeAdded, actions.size()); | ||
break; | ||
|
||
case "deadline": | ||
toBeAdded = new Deadline(Methods.findTaskDetails(dates[0]), Methods.findTaskDetails(dates[1])); | ||
actions.add(toBeAdded); | ||
Methods.printAcknowledgement(toBeAdded, actions.size()); | ||
break; | ||
|
||
case "mark": | ||
taskNumber = Integer.parseInt(decisions[1]) - 1; | ||
actions.get(taskNumber).mark(); | ||
Methods.printDoneMarkingTasks(actions.get(taskNumber)); | ||
break; | ||
|
||
case "unmark": | ||
taskNumber = Integer.parseInt(decisions[1]) - 1; | ||
actions.get(taskNumber).unmark(); | ||
Methods.printDoneMarkingTasks(actions.get(taskNumber)); | ||
break; | ||
|
||
case "list": | ||
for (int iterator = 0; iterator < actions.size(); iterator++) { | ||
Methods.printListElement(iterator, actions.get(iterator)); | ||
} | ||
break; | ||
case "delete": | ||
taskNumber = Integer.parseInt(decisions[1]) - 1; | ||
Methods.printDeleteAcknowledgement(actions.get(taskNumber), actions.size() - 1); | ||
actions.remove(taskNumber); | ||
break; | ||
|
||
case "bye": | ||
break; | ||
|
||
default: | ||
Methods.printCurrentSupportedActions(); | ||
} | ||
} while (!decisions[0].equals("bye")); | ||
FileHandler.saveFile(actions); | ||
System.out.println("That's all from me! Goodbye!"); | ||
} | ||
|
||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
Manifest-Version: 1.0 | ||
Main-Class: Duke | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,83 @@ | ||
package file.methods; | ||
|
||
|
||
import java.io.File; | ||
import java.io.FileWriter; | ||
import java.io.IOException; | ||
import java.util.ArrayList; | ||
import java.util.Scanner; | ||
|
||
|
||
import tasks.Task; | ||
import tasks.Todo; | ||
import tasks.Event; | ||
import tasks.Deadline; | ||
|
||
public class FileHandler { | ||
|
||
public static void setUpFile(ArrayList<Task> actions) { | ||
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 function is also quite long, maybe see how you can refactor some things inside it. |
||
try { | ||
File f = new File("data/savedList.txt"); | ||
File directory = new File("data"); | ||
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 avoid the usage of magic literals. You can store the string literals as constants and use them (final static vars). See here |
||
if (!directory.exists()) { | ||
boolean success = directory.mkdir(); | ||
if (!success) { | ||
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 deep nesting to improve readability. If needed, you can refactor the inner nests to a new method. See here |
||
System.out.println("Met an error creating the directory"); | ||
} | ||
} | ||
if (!f.exists()) { | ||
f.createNewFile(); | ||
return; | ||
} | ||
String line; | ||
String[] decisions; | ||
Scanner in = new Scanner(f); | ||
Task toBeAdded; | ||
while (in.hasNext()) { | ||
line = in.nextLine(); | ||
decisions = line.split("/"); | ||
switch(decisions[0]) { | ||
case "t": | ||
toBeAdded = new Todo(decisions[2]); | ||
if (decisions[1].equals("X")) { | ||
toBeAdded.mark(); | ||
} | ||
actions.add(toBeAdded); | ||
break; | ||
|
||
case "d": | ||
toBeAdded = new Deadline(decisions[2],decisions[3]); | ||
if (decisions[1].equals("X")) { | ||
toBeAdded.mark(); | ||
} | ||
actions.add(toBeAdded); | ||
break; | ||
|
||
case "e": | ||
toBeAdded = new Event(decisions[2],decisions[3],decisions[4]); | ||
if (decisions[1].equals("X")) { | ||
toBeAdded.mark(); | ||
} | ||
actions.add(toBeAdded); | ||
break; | ||
|
||
default: | ||
System.out.println("File has been corrupted"); | ||
} | ||
} | ||
} catch (IOException e) { | ||
System.out.println("Something went wrong: " + e.getMessage()); | ||
} | ||
} | ||
public static void saveFile(ArrayList<Task> actions) { | ||
try { | ||
FileWriter fw = new FileWriter("data/savedList.txt"); | ||
for (Task i : actions) { | ||
fw.write(i.getCommand() + System.lineSeparator()); | ||
} | ||
fw.close(); | ||
} catch (IOException e) { | ||
System.out.println("Something went wrong: " + e.getMessage()); | ||
} | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
package tasks; | ||
|
||
public class Deadline extends Task { | ||
|
||
private String by; | ||
|
||
public Deadline(String description, String by) { | ||
super(description); | ||
this.by = by; | ||
} | ||
|
||
@Override | ||
public String toString() { | ||
return "[D]" + super.toString() + " (by: " + by + ")"; | ||
} | ||
|
||
|
||
public String getCommand() { | ||
return "d/" + this.getStatusIcon() + "/" + this.getDescription() + "/" + this.by; | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
package tasks; | ||
|
||
public class Event extends Task { | ||
|
||
private String from; | ||
private String to; | ||
|
||
public Event(String description, String from, String to) { | ||
super(description); | ||
this.from = from; | ||
this.to = to; | ||
} | ||
|
||
@Override | ||
public String toString() { | ||
return "[E]" + super.toString() + " (from: " + from + "to: " + to + ")"; | ||
|
||
} | ||
|
||
public String getCommand() { | ||
return "e/" + this.getStatusIcon() + "/" + this.getDescription() + "/" + this.from + "/" + this.to ; | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
package tasks; | ||
|
||
public class Task { | ||
private String description; | ||
private boolean isDone; | ||
|
||
private String command; | ||
public Task(String description) { | ||
this.description = description; | ||
this.isDone = false; | ||
} | ||
|
||
public String getStatusIcon() { | ||
return (isDone ? "X" : " "); // mark done task with X | ||
} | ||
|
||
public void mark() { | ||
this.isDone = true; | ||
} | ||
|
||
public void unmark() { | ||
this.isDone = false; | ||
} | ||
|
||
public String getDescription() { | ||
return (this.description); | ||
} | ||
|
||
public String toString() { | ||
return ("[" + this.getStatusIcon() + "] " + this.getDescription()); | ||
} | ||
|
||
public String getCommand() { | ||
return this.command; | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
package tasks; | ||
|
||
public class Todo extends Task { | ||
|
||
public Todo(String description) { | ||
super(description); | ||
} | ||
@Override | ||
public String toString() { | ||
return "[T]" + super.toString(); | ||
} | ||
|
||
|
||
public String getCommand() { | ||
return "t/" + this.getStatusIcon() + "/"+ this.getDescription(); | ||
} | ||
|
||
} | ||
|
||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
package utility; | ||
|
||
public class DukeException extends Exception { | ||
public String description; | ||
|
||
public DukeException() { | ||
this.description = "No Description"; | ||
} | ||
|
||
public DukeException(String description) { | ||
this.description = description; | ||
} | ||
|
||
public void setDescription(String description) { | ||
this.description = description; | ||
} | ||
|
||
public String getDescription() { | ||
return this.description; | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,49 @@ | ||
package utility; | ||
|
||
import tasks.Task; | ||
|
||
public class Methods { | ||
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. The name of this class does not describe what it is used for. Maybe consider naming it |
||
public static void printGreetings() { | ||
String logo = " ____ _ \n" | ||
+ "| _ \\ _ _| | _____ \n" | ||
+ "| | | | | | | |/ / _ \\\n" | ||
+ "| |_| | |_| | < __/\n" | ||
+ "|____/ \\__,_|_|\\_\\___|\n" | ||
+ "__________________________\n"; | ||
System.out.println("Hello from\n" + logo); | ||
System.out.println("Hello! Do you need anything from me?\n" | ||
+ "I have only been trained to greet, echo and list you so far.\n" | ||
+ "Once my owner is more proficient in what he does, he will give me more functions!\n" | ||
+ " 1)echo \n 2)todo\n 3)mark\n 4)unmark\n 5)deadline\n 6)event\n 7)delete\n" | ||
+ "When you wish to exit, do tell me by typing : bye"); | ||
} | ||
|
||
public static void print(String input) { | ||
System.out.println(input); | ||
} | ||
|
||
public static void printListElement(int iterator, Task action) { | ||
Methods.print(iterator + 1 | ||
+ ")" | ||
+ action.toString()); | ||
} | ||
|
||
public static void printDoneMarkingTasks(Task action) { | ||
Methods.print("Done!\n" + action.toString()); | ||
} | ||
|
||
public static String findTaskDetails(String line) { | ||
return line.substring(line.indexOf(" ") + 1); | ||
} | ||
|
||
public static void printAcknowledgement(Task action, int actionCounter) { | ||
System.out.println("Got it. I've added this task:\n" + action.toString() + System.lineSeparator() + "Now you have " + actionCounter + " tasks in the list."); | ||
} | ||
|
||
public static void printCurrentSupportedActions() { | ||
Methods.print("I am currently only able to do: \n 1)echo \n 2)todo\n 3)mark\n 4)unmark\n 5)deadline\n 6)event\n 7)delete\n"); | ||
} | ||
public static void printDeleteAcknowledgement(Task action, int actionCounter) { | ||
print("Got it. I've removed this task\n" + action.toString() + System.lineSeparator() + "Now you have " + actionCounter + " tasks in the list."); | ||
} | ||
} |
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.
This method is quite long (>30 lines). Consider refactoring the code to a new method OR see how you can combine common things from the case statements together. See here