-
Notifications
You must be signed in to change notification settings - Fork 270
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
[Ria Khaitan] iP #328
base: master
Are you sure you want to change the base?
[Ria Khaitan] iP #328
Changes from 11 commits
d839859
151461d
47b8641
b17138e
9c7b04f
753720c
9b13c8d
83929b9
23ce597
6ed0f74
641333d
04c29b8
b1955ad
e290e05
08f9af6
0d9e8c2
7e9f3b7
7752dbf
2b8b28d
ce15d66
73df732
5cec4bd
9a52a54
e6d592a
e4b8c5a
2bda280
f0b93f4
34a78ab
a18e31b
1640332
8d134fe
2b126c7
48d87fc
9bae6a7
0583ae0
8281bb2
b3ed67a
31e1a12
d97ad83
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 | ||
*.class |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,10 +1,209 @@ | ||
import exceptions.DukeExceptions; | ||
import exceptions.DukeInvalidInput; | ||
import exceptions.DukeInvalidTodo; | ||
|
||
import java.io.*; | ||
import java.util.Objects; | ||
import java.time.LocalDate; | ||
import java.util.Scanner; | ||
import java.util.ArrayList; | ||
|
||
public class Duke { | ||
public static void main(String[] args) { | ||
String logo = " ____ _ \n" | ||
+ "| _ \\ _ _| | _____ \n" | ||
+ "| | | | | | | |/ / _ \\\n" | ||
+ "| |_| | |_| | < __/\n" | ||
+ "|____/ \\__,_|_|\\_\\___|\n"; | ||
System.out.println("Hello from\n" + logo); | ||
public static void lineOne() { | ||
System.out.println("*************************************************************************"); | ||
} | ||
|
||
public static void lineTwo() { | ||
System.out.println("|-|-|-|-|-|-|-|-|-|-|-|-|-|-|-|-|-|-|-|-|-|-|-|-|-|-|-|-|-|-|-|-|-|-|-|"); | ||
} | ||
|
||
public static String makeDesc(String[] text, int len) { | ||
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 a more intuitive variable name here? |
||
String newText = ""; | ||
for (int i = 1; i < len; i++) { | ||
newText = newText + text[i] + " "; | ||
} | ||
return newText.trim(); | ||
} | ||
|
||
public static void writeToFile(ArrayList<Task> list) throws IOException { | ||
try { | ||
File f = new File("/Users/riakhaitan/iP/ip/data/duke.txt"); | ||
f.createNewFile(); | ||
|
||
boolean directory = f.getParentFile().mkdirs(); | ||
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. Just a suggestion, perhaps you could use a name for this variable that is more boolean-sounding such as isDirectory? Good use of mkdir() here, creating a directory to store data |
||
FileWriter writer = new FileWriter(f, false); | ||
for (Task task : list) { | ||
String fileInput; | ||
Task ele = task; | ||
if (ele instanceof ToDo) { | ||
fileInput = "[T][" + ele.getStatusIcon() + "]/" + ele.description; | ||
} else if (ele instanceof Deadline) { | ||
Deadline deadL = (Deadline) ele; | ||
fileInput = "[D][" + deadL.getStatusIcon() + "]/" + deadL.description + "/" + deadL.when; | ||
} else { | ||
Event eve = (Event) ele; | ||
fileInput = "[E][" + eve.getStatusIcon() + "]/" + eve.description + "/" + eve.at; | ||
} | ||
writer.write(fileInput + "\n"); | ||
writer.flush(); | ||
} | ||
} catch (IOException ex) { | ||
ex.printStackTrace(); | ||
} | ||
} | ||
|
||
public static void loadFromFile(ArrayList<Task> list) throws IOException { | ||
File f = new File("/Users/riakhaitan/iP/ip/data/duke.txt"); | ||
f.createNewFile(); | ||
String input, desc; | ||
Task t; | ||
Scanner in = new Scanner(f); | ||
while(in.hasNextLine()) { | ||
input = in.nextLine(); | ||
String[] tSplit = input.split("/"); | ||
String[] splitT = tSplit[0].split("]"); | ||
switch(splitT[0]) { | ||
case "[T": | ||
t = new ToDo(tSplit[1]); | ||
break; | ||
case "[E": | ||
t = new Event(tSplit[1], tSplit[2]); | ||
break; | ||
case "[D": | ||
t = new Deadline(tSplit[1], LocalDate.parse(tSplit[2])); | ||
break; | ||
default: | ||
t = new Task("eee"); | ||
break; | ||
} | ||
if(splitT[1].equals("[X")) { | ||
t.done(); | ||
list.add(t); | ||
} | ||
else { | ||
list.add(t); | ||
} | ||
} | ||
printList(list, list.size()); | ||
in.close(); | ||
} | ||
|
||
public static int exitHalloumi() { | ||
lineOne(); | ||
System.out.println("See you soon! Have a good day ^_^"); | ||
lineOne(); | ||
return 1; | ||
} | ||
|
||
public static void printList(ArrayList<Task> list, int size) { | ||
lineOne(); | ||
System.out.println("List:"); | ||
if(list.isEmpty()) { | ||
System.out.println("No tasks to complete! ^_^"); | ||
return; | ||
} | ||
for (int i = 0; i < size; i++) { | ||
System.out.println(i + 1 + ". " + list.get(i)); | ||
} | ||
lineOne(); | ||
} | ||
|
||
public static void main(String[] args) throws DukeExceptions, IOException { | ||
Scanner sc = new Scanner(System.in); | ||
int num = 0; | ||
ArrayList<Task> lists = new ArrayList<Task>(); | ||
loadFromFile(lists); | ||
lineOne(); | ||
System.out.println("Hi! I'm Halloumi ^_^"); | ||
System.out.println("What do you need help with today?"); | ||
lineOne(); | ||
do { | ||
String text = sc.nextLine(); | ||
String[] textSplitOne = text.split("/"); //by and at | ||
String[] textSplit = textSplitOne[0].split(" "); | ||
String fullDesc = makeDesc(textSplit, textSplit.length); | ||
try { | ||
switch (textSplit[0]) { | ||
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. For case clauses, there is no indentation |
||
case "bye": | ||
num = exitHalloumi(); | ||
break; | ||
case "list": | ||
printList(lists, lists.size()); | ||
break; | ||
case "mark": | ||
lineOne(); | ||
System.out.println("Good Job! ^_^"); | ||
System.out.println("Task number " + textSplit[1] + " has been marked as done!"); | ||
int tNum = Integer.parseInt(textSplit[1]); | ||
lists.get(tNum - 1).done(); | ||
System.out.println(lists.get(tNum - 1)); | ||
lineOne(); | ||
break; | ||
case "unmark": | ||
lineOne(); | ||
System.out.println("I've unmarked task number " + textSplit[1]); | ||
System.out.println("Complete it soon! ^_^"); | ||
int tNo = Integer.parseInt(textSplit[1]); | ||
lists.get(tNo - 1).undo(); | ||
System.out.println(lists.get(tNo - 1)); | ||
lineOne(); | ||
break; | ||
case "delete" : | ||
lineOne(); | ||
System.out.println("Noted. I've removed this task:"); | ||
//System.out.println("Complete it soon! ^_^"); | ||
int t2No = Integer.parseInt(textSplit[1]); | ||
System.out.println(lists.get(t2No - 1)); | ||
lists.remove(t2No - 1); | ||
lineOne(); | ||
break; | ||
case "todo": | ||
try { | ||
if(fullDesc.equals(" ") || fullDesc.equals("")) { | ||
throw new DukeInvalidTodo(); | ||
} | ||
} | ||
catch(DukeInvalidTodo e) { | ||
System.err.println(e.getMessage()); | ||
continue; | ||
} | ||
lineTwo(); | ||
System.out.println("New task added:"); | ||
Task t = new ToDo(fullDesc); | ||
lists.add(t); | ||
System.out.println(t); | ||
System.out.println("You have " + lists.size() + " tasks left now! ^_^"); | ||
lineTwo(); | ||
break; | ||
case "event": | ||
lineTwo(); | ||
System.out.println("New task added:"); | ||
Task t1 = new Event(fullDesc, textSplitOne[1]); | ||
lists.add(t1); | ||
System.out.println(t1); | ||
System.out.println("You have " + lists.size() + " tasks left now! ^_^"); | ||
lineTwo(); | ||
break; | ||
case "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. Logical units within a block should be separated by a line, perhaps a space after each case would be ideal! |
||
lineTwo(); | ||
System.out.println("New task added:"); | ||
String[] date = textSplitOne[1].split(" "); | ||
Task t2 = new Deadline(fullDesc, LocalDate.parse(date[1])); | ||
lists.add(t2); | ||
System.out.println(t2); | ||
System.out.println("You have " + lists.size() + " tasks left now! ^_^"); | ||
lineTwo(); | ||
break; | ||
default: | ||
throw new DukeInvalidInput(); | ||
|
||
} | ||
} catch (DukeInvalidInput e) { | ||
System.err.println(e.getMessage()); | ||
} | ||
} | ||
while (num == 0) ; | ||
writeToFile(lists); | ||
} | ||
} | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,62 @@ | ||
import java.time.LocalDate; | ||
import java.time.format.DateTimeFormatter; | ||
|
||
public class Task { | ||
String description; | ||
boolean isDone; | ||
|
||
public Task(String desc) { | ||
description = desc; | ||
isDone = false; | ||
} | ||
public String getStatusIcon() { | ||
return (isDone ? "X" : " "); // mark done task with X | ||
} | ||
|
||
public String toString() { | ||
return String.format("[%s] %s", getStatusIcon(), description); | ||
} | ||
|
||
public void done() { | ||
isDone = true; | ||
} | ||
|
||
public void undo() { | ||
isDone = false; | ||
} | ||
} | ||
|
||
class ToDo extends Task { | ||
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. I like how the code has been indented |
||
public ToDo(String desc) { | ||
super(desc); | ||
} | ||
|
||
public String toString() { | ||
return "[T]" + super.toString(); | ||
} | ||
} | ||
|
||
class Event extends Task { | ||
String at; | ||
|
||
public Event(String desc, String b) { | ||
super(desc); | ||
at = b; | ||
} | ||
public String toString() { | ||
return "[E]" + super.toString() + " (: " + at + ")"; | ||
} | ||
} | ||
|
||
class Deadline extends Task { | ||
LocalDate when; | ||
|
||
public Deadline(String desc, LocalDate date) { | ||
super(desc); | ||
when = date; | ||
} | ||
public String toString() { | ||
return "[D]" + super.toString() + " (: " + | ||
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. A good practice would be to place a line break before an operator! Good use of line break to improve readability |
||
when.format(DateTimeFormatter.ofPattern("MMM d yyyy")) + ")"; | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
package exceptions; | ||
|
||
public class DukeExceptions extends Exception { | ||
public DukeExceptions(String output) { | ||
super(output); | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
package exceptions; | ||
|
||
public class DukeInvalidInput extends DukeExceptions { | ||
public DukeInvalidInput() { | ||
super("ENTER A VALID INPUT! ^_^"); | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
package exceptions; | ||
|
||
public class DukeInvalidTodo extends DukeExceptions { | ||
public DukeInvalidTodo() { | ||
super("ENTER A DESCRIPTION! ^_^"); | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,7 +1,24 @@ | ||
Hello from | ||
____ _ | ||
| _ \ _ _| | _____ | ||
| | | | | | | |/ / _ \ | ||
| |_| | |_| | < __/ | ||
|____/ \__,_|_|\_\___| | ||
|
||
************************************************************************* | ||
Hi! I'm Halloumi ^_^ | ||
What do you need help with today? | ||
************************************************************************* | ||
|-|-|-|-|-|-|-|-|-|-|-|-|-|-|-|-|-|-|-|-|-|-|-|-|-|-|-|-|-|-|-|-|-|-|-| | ||
New task added: | ||
[T][ ] borrow book | ||
You have 1 tasks in the list now! ^_^ | ||
|-|-|-|-|-|-|-|-|-|-|-|-|-|-|-|-|-|-|-|-|-|-|-|-|-|-|-|-|-|-|-|-|-|-|-| | ||
|-|-|-|-|-|-|-|-|-|-|-|-|-|-|-|-|-|-|-|-|-|-|-|-|-|-|-|-|-|-|-|-|-|-|-| | ||
New task added: | ||
[E][ ] wake up (: at 8am) | ||
You have 2 tasks in the list now! ^_^ | ||
|-|-|-|-|-|-|-|-|-|-|-|-|-|-|-|-|-|-|-|-|-|-|-|-|-|-|-|-|-|-|-|-|-|-|-| | ||
|-|-|-|-|-|-|-|-|-|-|-|-|-|-|-|-|-|-|-|-|-|-|-|-|-|-|-|-|-|-|-|-|-|-|-| | ||
New task added: | ||
[D][ ] submit assignment (: by Sunday) | ||
You have 3 tasks in the list now! ^_^ | ||
|-|-|-|-|-|-|-|-|-|-|-|-|-|-|-|-|-|-|-|-|-|-|-|-|-|-|-|-|-|-|-|-|-|-|-| | ||
1. [T][ ] borrow book | ||
2. [E][ ] wake up (: at 8am) | ||
3. [D][ ] submit assignment (: by Sunday) | ||
See you soon! Have a good day ^_^ | ||
************************************************************************* |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
todo borrow book | ||
event wake up /at 8am | ||
deadline submit assignment /by Sunday | ||
list | ||
bye |
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.
Should the import statements be ordered differently?