-
Notifications
You must be signed in to change notification settings - Fork 228
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
Michael Ong iP #235
base: master
Are you sure you want to change the base?
Michael Ong iP #235
Changes from 6 commits
3b19ba1
a75fcee
4498a6b
3ba23a5
1332167
bf447d2
dd958f6
a4a2685
7079a47
6988915
8c6a077
8572393
9dd0f7e
b2618e0
2b500b2
8cb2983
ee2df78
fbb0793
d984a52
94ecea4
80cb4ab
be0283c
57b5d4e
e178e21
0080b9c
ec0b360
b6558dc
197fc2a
80b1fea
7cf7620
53e6063
9720822
669c7dc
687d765
27d8d64
2b332ac
37032d1
45b9fb5
3993d29
78260bb
fdd22b6
8cbcf22
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,3 @@ | ||
[T][✘] dknslsald | ||
[E][✘] lfdknsdf (at: lfdsnfl) | ||
[D][✓] saldna (by: kshdflad) |
Original file line number | Diff line number | Diff line change | ||||
---|---|---|---|---|---|---|
@@ -0,0 +1,47 @@ | ||||||
import java.io.File; | ||||||
import java.io.FileNotFoundException; | ||||||
import java.io.FileWriter; | ||||||
import java.io.IOException; | ||||||
import java.util.ArrayList; | ||||||
import java.util.List; | ||||||
import java.util.Scanner; | ||||||
|
||||||
public class Database { | ||||||
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. Consider adding a couple of class-level comments describing the class. |
||||||
|
||||||
String name; | ||||||
|
||||||
|
||||||
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 might have inadvertently added an extra blank line here:
Suggested change
|
||||||
public Database(String name){ | ||||||
this.name = name; | ||||||
} | ||||||
|
||||||
public ArrayList<String> readFile() throws FileNotFoundException { | ||||||
try { | ||||||
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 your indentations in this class! |
||||||
ArrayList<String> tasksStringName = new ArrayList<>(); | ||||||
File f = new File(name); | ||||||
Scanner s = new Scanner(f); | ||||||
while (s.hasNext()) { | ||||||
tasksStringName.add(s.nextLine()); | ||||||
} | ||||||
return tasksStringName; | ||||||
} catch (FileNotFoundException e) { | ||||||
throw new FileNotFoundException("File not found"); | ||||||
} | ||||||
} | ||||||
|
||||||
public void writeTaskToFile(List<Task> tasks){ | ||||||
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. Be careful of the spaces 😉
Suggested change
|
||||||
String string = ""; | ||||||
for (Task task : tasks) { | ||||||
string = string + task.toString() + "\n"; | ||||||
} | ||||||
try { | ||||||
FileWriter fileWriter = new FileWriter(name); | ||||||
fileWriter.write(string); | ||||||
fileWriter.close(); | ||||||
} catch (IOException e) { | ||||||
System.out.println(e.getMessage()); | ||||||
} | ||||||
|
||||||
} | ||||||
} | ||||||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
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: " + this.by + ")"; | ||
} | ||
} |
Original file line number | Diff line number | Diff line change | ||||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
@@ -1,10 +1,261 @@ | ||||||||||||||
import java.io.FileNotFoundException; | ||||||||||||||
import java.util.ArrayList; | ||||||||||||||
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); | ||||||||||||||
|
||||||||||||||
public static void main(String[] args) throws DukeException, FileNotFoundException { | ||||||||||||||
Database database = new Database( "data.txt"); | ||||||||||||||
ArrayList<String> listOfTasks; | ||||||||||||||
ArrayList<Task> myList; | ||||||||||||||
try { | ||||||||||||||
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 indentation is a little off here: should be in line with the block.
Suggested change
|
||||||||||||||
listOfTasks = database.readFile(); | ||||||||||||||
myList = readInput(listOfTasks); | ||||||||||||||
} | ||||||||||||||
catch (FileNotFoundException e){ | ||||||||||||||
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
Suggested change
|
||||||||||||||
throw new FileNotFoundException("No File Detected"); | ||||||||||||||
} | ||||||||||||||
System.out.println("____________________________________"); | ||||||||||||||
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. Would it be better to have this line format as a constant since it is repeated several times in the class? |
||||||||||||||
System.out.println("Hello! I'm Duke\nWhat can I do for you?"); | ||||||||||||||
System.out.println("____________________________________"); | ||||||||||||||
|
||||||||||||||
Scanner input = new Scanner(System.in); | ||||||||||||||
while(input.hasNextLine()) { | ||||||||||||||
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. Maybe it would be better if you avoid deep nesting to improve code quality 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. Another space missed 😉
Suggested change
|
||||||||||||||
String s = input.next(); | ||||||||||||||
switch (s){ | ||||||||||||||
case "todo": | ||||||||||||||
System.out.println("____________________________________"); | ||||||||||||||
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
Suggested change
|
||||||||||||||
try { | ||||||||||||||
String s1 = input.nextLine(); | ||||||||||||||
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 might have missed this: variable names could do with a more specific name, especially since the scope isn't small. Perhaps |
||||||||||||||
if (s1.equals("")) { | ||||||||||||||
throw new DukeException("Enter a valid todo"); | ||||||||||||||
} else { | ||||||||||||||
char[] chars = s1.toCharArray(); | ||||||||||||||
if(chars[1] == ' '){ | ||||||||||||||
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. Another missing space 😉 |
||||||||||||||
throw new DukeException("Enter valid todo"); | ||||||||||||||
} else { | ||||||||||||||
try{ | ||||||||||||||
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. And another 😉 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. Applies to a couple other lines below. |
||||||||||||||
System.out.println("Got it. I've added this todo:"); | ||||||||||||||
String desc = s1.substring(1); | ||||||||||||||
Todo newTodo = new Todo(desc); | ||||||||||||||
System.out.println(" " + newTodo.toString()); | ||||||||||||||
myList.add(newTodo); | ||||||||||||||
System.out.println("Now you have " + myList.size() + " tasks in the list."); | ||||||||||||||
database.writeTaskToFile(myList); | ||||||||||||||
} catch (Exception e){ | ||||||||||||||
System.out.println("Enter valid todo"); | ||||||||||||||
} | ||||||||||||||
|
||||||||||||||
} | ||||||||||||||
} | ||||||||||||||
} catch (DukeException e){ | ||||||||||||||
System.out.println(e.getMessage()); | ||||||||||||||
} | ||||||||||||||
System.out.println("____________________________________"); | ||||||||||||||
break; | ||||||||||||||
|
||||||||||||||
case "deadline": | ||||||||||||||
|
||||||||||||||
System.out.println("____________________________________"); | ||||||||||||||
try{ | ||||||||||||||
String s1 = input.nextLine(); | ||||||||||||||
if(s1.equals("")){ | ||||||||||||||
throw new DukeException("Enter valid deadline task."); | ||||||||||||||
} else { | ||||||||||||||
try { | ||||||||||||||
String wholeString = s1.substring(1); | ||||||||||||||
String[] parts = wholeString.split(" /by "); | ||||||||||||||
String deadlineDesc = parts[0]; | ||||||||||||||
if(parts.length == 1){ | ||||||||||||||
throw new DukeException("Please adhere to convention:\n(task /by deadline timing)"); | ||||||||||||||
} else { | ||||||||||||||
String deadline = parts[1]; | ||||||||||||||
System.out.println("Got it. I've added this deadline:"); | ||||||||||||||
Deadline newDeadline = new Deadline(deadlineDesc, deadline); | ||||||||||||||
System.out.println(" " + newDeadline.toString()); | ||||||||||||||
myList.add(newDeadline); | ||||||||||||||
System.out.println("Now you have " + myList.size() + " tasks in the list."); | ||||||||||||||
database.writeTaskToFile(myList); | ||||||||||||||
|
||||||||||||||
} | ||||||||||||||
} catch(DukeException e) { | ||||||||||||||
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. Not a coding standard, but figured I could chip in. Since the |
||||||||||||||
System.out.println(e.getMessage()); | ||||||||||||||
} | ||||||||||||||
} | ||||||||||||||
|
||||||||||||||
} catch (DukeException e){ | ||||||||||||||
System.out.println(e.getMessage()); | ||||||||||||||
} | ||||||||||||||
System.out.println("____________________________________"); | ||||||||||||||
break; | ||||||||||||||
|
||||||||||||||
case "event": | ||||||||||||||
|
||||||||||||||
System.out.println("____________________________________"); | ||||||||||||||
try{ | ||||||||||||||
String s1 = input.nextLine(); | ||||||||||||||
if(s1.equals("")){ | ||||||||||||||
throw new DukeException("Enter valid Event."); | ||||||||||||||
} else { | ||||||||||||||
try { | ||||||||||||||
String wholeString = s1.substring(1); | ||||||||||||||
String[] parts = wholeString.split(" /at "); | ||||||||||||||
String eventDesc = parts[0]; | ||||||||||||||
if(parts.length == 1){ | ||||||||||||||
throw new DukeException("Please adhere to convention:\n(Event /at event details)"); | ||||||||||||||
} else { | ||||||||||||||
String eventDetails = parts[1]; | ||||||||||||||
System.out.println("Got it. I've added this Event:"); | ||||||||||||||
Event newEvent = new Event(eventDesc, eventDetails); | ||||||||||||||
System.out.println(" " + newEvent.toString()); | ||||||||||||||
myList.add(newEvent); | ||||||||||||||
System.out.println("Now you have " + myList.size() + " tasks in the list."); | ||||||||||||||
database.writeTaskToFile(myList); | ||||||||||||||
} | ||||||||||||||
} catch(DukeException e) { | ||||||||||||||
System.out.println(e.getMessage()); | ||||||||||||||
} | ||||||||||||||
} | ||||||||||||||
|
||||||||||||||
} catch (DukeException e){ | ||||||||||||||
System.out.println(e.getMessage()); | ||||||||||||||
} | ||||||||||||||
System.out.println("____________________________________"); | ||||||||||||||
break; | ||||||||||||||
|
||||||||||||||
case "list": | ||||||||||||||
System.out.println("____________________________________"); | ||||||||||||||
System.out.println("Here are the tasks in your list:"); | ||||||||||||||
for(int i=1; i< myList.size()+1; i++){ | ||||||||||||||
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. Note the spaces here as well:
Suggested change
|
||||||||||||||
System.out.println(i +". " + myList.get(i-1).toString()); | ||||||||||||||
} | ||||||||||||||
System.out.println("____________________________________"); | ||||||||||||||
|
||||||||||||||
break; | ||||||||||||||
|
||||||||||||||
case "bye": | ||||||||||||||
System.out.println("____________________________________"); | ||||||||||||||
System.out.println(" Bye. Hope to see you again soon!"); | ||||||||||||||
System.out.println("____________________________________"); | ||||||||||||||
System.exit(0); | ||||||||||||||
|
||||||||||||||
break; | ||||||||||||||
|
||||||||||||||
case "done": | ||||||||||||||
|
||||||||||||||
System.out.println("____________________________________"); | ||||||||||||||
try{ | ||||||||||||||
String s1 = input.nextLine(); | ||||||||||||||
if(s1.equals("")){ | ||||||||||||||
throw new DukeException("Please specify what task is done"); | ||||||||||||||
} else { | ||||||||||||||
String parts[] = s1.split(" "); | ||||||||||||||
if(parts.length> 2){ | ||||||||||||||
throw new DukeException("Please insert valid index to mark as done"); | ||||||||||||||
} else { | ||||||||||||||
try { | ||||||||||||||
String indexString = parts[1]; | ||||||||||||||
int index =Integer.parseInt(indexString); | ||||||||||||||
Task t = myList.get(index - 1); | ||||||||||||||
t.markAsDone(); | ||||||||||||||
System.out.println("Nice! I've marked this task as done"); | ||||||||||||||
System.out.println(t.toString()); | ||||||||||||||
database.writeTaskToFile(myList); | ||||||||||||||
} catch (Exception e){ | ||||||||||||||
System.out.println("Please enter a valid index"); | ||||||||||||||
} | ||||||||||||||
} | ||||||||||||||
} | ||||||||||||||
|
||||||||||||||
} catch(DukeException e){ | ||||||||||||||
System.out.println(e.getMessage()); | ||||||||||||||
} | ||||||||||||||
System.out.println("____________________________________"); | ||||||||||||||
|
||||||||||||||
break; | ||||||||||||||
|
||||||||||||||
case "delete": | ||||||||||||||
// | ||||||||||||||
System.out.println("____________________________________"); | ||||||||||||||
try{ | ||||||||||||||
String s1 = input.nextLine(); | ||||||||||||||
if(s1.equals("")){ | ||||||||||||||
throw new DukeException("Please specify which task to delete"); | ||||||||||||||
} else { | ||||||||||||||
String parts[] = s1.split(" "); | ||||||||||||||
if(parts.length> 2){ | ||||||||||||||
throw new DukeException("Please insert valid index to delete"); | ||||||||||||||
} else { | ||||||||||||||
try { | ||||||||||||||
String indexString = parts[1]; | ||||||||||||||
int index =Integer.parseInt(indexString); | ||||||||||||||
Task t = myList.get(index-1); | ||||||||||||||
System.out.println("Noted i have removed this task"); | ||||||||||||||
System.out.println(myList.get(index-1)); | ||||||||||||||
myList.remove(index-1); | ||||||||||||||
System.out.println("Now you have " + myList.size() + " tasks in the list"); | ||||||||||||||
database.writeTaskToFile(myList); | ||||||||||||||
|
||||||||||||||
} catch (Exception e){ | ||||||||||||||
System.out.println("Please enter a valid index"); | ||||||||||||||
} | ||||||||||||||
} | ||||||||||||||
} | ||||||||||||||
|
||||||||||||||
} catch(DukeException e){ | ||||||||||||||
System.out.println(e.getMessage()); | ||||||||||||||
} | ||||||||||||||
System.out.println("____________________________________"); | ||||||||||||||
|
||||||||||||||
|
||||||||||||||
break; | ||||||||||||||
|
||||||||||||||
default: | ||||||||||||||
//clear the buffer | ||||||||||||||
input.nextLine(); | ||||||||||||||
System.out.println("____________________________________"); | ||||||||||||||
System.out.println("Please insert a valid command"); | ||||||||||||||
System.out.println("____________________________________"); | ||||||||||||||
|
||||||||||||||
break; | ||||||||||||||
|
||||||||||||||
|
||||||||||||||
} | ||||||||||||||
|
||||||||||||||
} | ||||||||||||||
} | ||||||||||||||
|
||||||||||||||
private static ArrayList<Task> readInput(ArrayList<String> strings) { | ||||||||||||||
ArrayList<Task> tasks = new ArrayList<>(); | ||||||||||||||
for(String str: strings) { | ||||||||||||||
char identifier = str.charAt(1); | ||||||||||||||
switch(identifier) { | ||||||||||||||
case 'D': | ||||||||||||||
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. Maybe it would be better to leave out the indentation for case clauses |
||||||||||||||
String subString = str.substring(7); | ||||||||||||||
String[] inputs = subString.split("by: "); | ||||||||||||||
String name = inputs[0].substring(0, inputs[0].length()-2); | ||||||||||||||
String deadline = inputs[1].substring(0, inputs[1].length()-1); | ||||||||||||||
Deadline deadline1 = new Deadline(name, deadline); | ||||||||||||||
tasks.add(deadline1); | ||||||||||||||
break; | ||||||||||||||
case 'T': | ||||||||||||||
String subString1 = str.substring(7); | ||||||||||||||
Todo todo1 = new Todo(subString1); | ||||||||||||||
tasks.add(todo1); | ||||||||||||||
break; | ||||||||||||||
case 'E': | ||||||||||||||
String subString2 = str.substring(7); | ||||||||||||||
String[] inputs1 = subString2.split("at: "); | ||||||||||||||
String desc = inputs1[0].substring(0, inputs1[0].length()-2); | ||||||||||||||
String at = inputs1[1].substring(0, inputs1[1].length()-1); | ||||||||||||||
Event event1 = new Event(desc, at); | ||||||||||||||
tasks.add(event1); | ||||||||||||||
|
||||||||||||||
break; | ||||||||||||||
} | ||||||||||||||
} | ||||||||||||||
return tasks; | ||||||||||||||
} | ||||||||||||||
|
||||||||||||||
|
||||||||||||||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
public class DukeException extends Exception { | ||
|
||
public DukeException(String errorMessage) { | ||
super(errorMessage); | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
public class Event extends Task { | ||
|
||
private String at; | ||
|
||
public Event(String description, String at) { | ||
super(description); | ||
this.at = at; | ||
} | ||
|
||
@Override | ||
public String toString(){ | ||
return "[E]" + super.toString() + " (at: " + this.at + ")"; | ||
|
||
} | ||
|
||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
public class Task { | ||
protected String description; | ||
protected boolean isDone; | ||
|
||
//constructor takes in a String that describes the task | ||
public Task(String description) { | ||
this.description = description; | ||
this.isDone = false; | ||
} | ||
|
||
public String getStatusIcon() { | ||
return (isDone ? "\u2713" : "\u2718"); //return tick or X symbols | ||
} | ||
|
||
public void markAsDone() { | ||
this.isDone = true; | ||
} | ||
|
||
public String toString() { | ||
return "[" +this.getStatusIcon() + "] " + this.description; | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
public class Todo extends Task { | ||
public Todo(String description) { | ||
super(description); | ||
} | ||
|
||
@Override | ||
public String toString() { | ||
return "[T]" + super.toString(); | ||
} | ||
|
||
} |
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.
Your
import
statements are very nicely formatted 🎉