-
Notifications
You must be signed in to change notification settings - Fork 362
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
[randallnhr] iP #389
base: master
Are you sure you want to change the base?
[randallnhr] iP #389
Changes from 14 commits
556af3f
d931af6
34904a0
4e12dad
510ffff
927000e
7315848
149e1d0
357e5f7
c2c6903
231ec2e
4614698
dbb7cbf
df38bca
5c0e7b1
674f297
c894cbf
2e673f7
359765a
224fa30
24d5209
74266a2
56f1dc5
25c6740
1318085
31a8483
c0d137a
caacaec
0af8473
47ec878
8794559
c21cf25
8539e7e
d962643
47632b4
0f35873
1c91d33
919e432
9a5d484
a70e173
0c185b1
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,5 @@ bin/ | |
|
||
/text-ui-test/ACTUAL.TXT | ||
text-ui-test/EXPECTED-UNIX.TXT | ||
|
||
*.class |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
import java.time.LocalDate; | ||
|
||
class Deadline extends Task { | ||
private LocalDate deadline; | ||
public Deadline(String name, String start) { | ||
super(name); | ||
this.deadline = LocalDate.parse(start); | ||
} | ||
|
||
@Override | ||
public String getFileDesc() { | ||
return this.isDone | ||
? "D|1|" + this.name + "|" + convertFileDate(this.deadline) | ||
: "D|0|" + this.name + "|" + convertFileDate(this.deadline); | ||
} | ||
|
||
@Override | ||
public String toString() { | ||
return this.isDone | ||
? "[D][X] " + this.name + " (by: " + getDate(this.deadline) + ")" | ||
: "[D][ ] " + this.name + " (by: " + getDate(this.deadline) + ")"; | ||
} | ||
} | ||
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,10 +1,150 @@ | ||
import java.io.IOException; | ||
|
||
public class Duke { | ||
public static void main(String[] args) { | ||
String logo = " ____ _ \n" | ||
+ "| _ \\ _ _| | _____ \n" | ||
+ "| | | | | | | |/ / _ \\\n" | ||
+ "| |_| | |_| | < __/\n" | ||
+ "|____/ \\__,_|_|\\_\\___|\n"; | ||
System.out.println("Hello from\n" + logo); | ||
|
||
private static final String SAVE_PATH = "./taskList.txt"; | ||
private Storage storage; | ||
private TaskList taskList; | ||
private Ui ui = new Ui(); | ||
private Parser parser = new Parser(); | ||
|
||
public Duke() { | ||
try { | ||
this.storage = new Storage(SAVE_PATH); | ||
this.taskList = storage.loadData(); | ||
} catch (DukeException e) { | ||
System.out.println(e.getMessage()); | ||
} | ||
} | ||
|
||
public void run() { | ||
try { | ||
ui.start(); | ||
String[] input = ui.readLine(); | ||
while (!input[0].equals("bye")) { | ||
ui.displayLine(); | ||
parser.readInput(input, taskList); | ||
ui.displayLine(); | ||
input = ui.readLine(); | ||
} | ||
ui.goodbye(); | ||
storage.storeData(this.taskList.getTasks()); | ||
} catch (DukeException e) { | ||
System.out.println(e.getMessage()); | ||
} | ||
} | ||
|
||
public static void main(String[] args) throws IOException, DukeException { | ||
new Duke().run(); | ||
} | ||
} | ||
// System.out.println("Hello! I'm Duke\n" + | ||
// "What can I do for you?"); | ||
|
||
// BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); | ||
// try { | ||
// ArrayList<Task> lst = loadData(SAVE_PATH); | ||
// int count = 0 + lst.size(); | ||
// String[] word = br.readLine().strip().split(" ",2); | ||
|
||
// while (!word[0].equals("bye")) { | ||
// try { | ||
// if (word[0].equals("list")) { | ||
// int curr = 1; | ||
// Iterator<Task> iter = lst.iterator(); | ||
// while (iter.hasNext()) { | ||
// System.out.println(curr + " " + iter.next()); | ||
// curr++; | ||
// } | ||
// word = br.readLine().strip().split(" ",2); | ||
// } else if (word[0].equals("mark")) { | ||
// if (word.length == 1) { | ||
// throw new DukeException("Mark needs a number."); | ||
// } | ||
// if (Integer.parseInt(word[1]) > count) { | ||
// throw new DukeException("Invalid task."); | ||
// } | ||
// Task t = lst.get(Integer.parseInt(word[1]) - 1); | ||
// t.isDone = true; | ||
// System.out.println("Task has been marked as done:\n " + t); | ||
// word = br.readLine().split(" ",2); | ||
// } else if (word[0].equals("unmark")) { | ||
// if (word.length == 1) { | ||
// throw new DukeException("Unmark needs a number."); | ||
// } | ||
// if (Integer.parseInt(word[1]) > count) { | ||
// throw new DukeException("Invalid task."); | ||
// } | ||
// Task t = lst.get(Integer.parseInt(word[1]) - 1); | ||
// t.isDone = false; | ||
// System.out.println("Task has been marked as not done:\n " + t); | ||
// word = br.readLine().split(" ",2); | ||
// } else if (word[0].equals("todo")) { | ||
// if (word.length == 1) { | ||
// throw new DukeException("todo needs a description"); | ||
// } | ||
// Task t = new Todo(word[1].strip()); | ||
// lst.add(t); | ||
// count++; | ||
// System.out.println("Added new todo:\n " + t + "\nNumber of tasks: " + count); | ||
// word = br.readLine().strip().split(" ",2); | ||
// } else if (word[0].equals("deadline")) { | ||
// if (word.length == 1 || !word[1].contains("/by")) { | ||
// throw new DukeException("Deadline needs a /by."); | ||
// } | ||
// String[] tempWord = word[1].strip().split("/by "); | ||
// if (tempWord.length == 1) { | ||
// throw new DukeException("/by needs a date/time."); | ||
// } | ||
// try { | ||
// Task t = new Deadline(tempWord[0].strip(), tempWord[1].strip()); | ||
// lst.add(t); | ||
// count++; | ||
// System.out.println("Added new deadline:\n " + t + "\nNumber of tasks: " + count); | ||
// word = br.readLine().strip().split(" ",2); | ||
// } catch (DateTimeParseException e) { | ||
// throw new DukeException("Date after /by needs to be in format yyyy-mm-dd"); | ||
// } | ||
// } else if (word[0].equals("event")) { | ||
// if (word.length == 1 || !word[1].contains("/from") || !word[1].contains("/to") ) { | ||
// throw new DukeException("Event needs a /from and /to."); | ||
// } | ||
// String[] tempWord = word[1].split("/"); | ||
// String[] from = tempWord[1].split(" ",2); | ||
// String[] to = tempWord[2].split(" ",2); | ||
// if (from.length == 1 || to.length == 1) { | ||
// throw new DukeException("/from and /to needs a date/time."); | ||
// } | ||
// try { | ||
// Task t = new Event(tempWord[0].strip(), from[1].strip(), to[1].strip()); | ||
// lst.add(t); | ||
// count++; | ||
// System.out.println("Added new event:\n " + t + "\nNumber of tasks: " + count); | ||
// word = br.readLine().strip().split(" ",2); | ||
// } catch (DateTimeParseException e) { | ||
// throw new DukeException("Date after /from and /to needs to be in format yyyy-mm-dd"); | ||
// } | ||
// } else if (word[0].equals("delete")) { | ||
// if (word.length == 1) { | ||
// throw new DukeException("Delete needs a number."); | ||
// } | ||
// if (Integer.parseInt(word[1]) > count) { | ||
// throw new DukeException("Invalid task."); | ||
// } | ||
// Task t = lst.remove(Integer.parseInt(word[1]) - 1); | ||
// count--; | ||
// System.out.println("Deleted task:\n " + t + "\nNumber of tasks: " + count); | ||
// word = br.readLine().strip().split(" ",2); | ||
// } else { | ||
// throw new DukeException("Sorry I do not understand the command"); | ||
// } | ||
// } catch (DukeException e) { | ||
// System.out.println(e.getMessage()); | ||
// word = br.readLine().strip().split(" ",2); | ||
// } | ||
// } | ||
// storeData(lst); | ||
// System.out.println("Duke: Goodbye"); | ||
// } catch (IOException e) { | ||
// System.out.println(e.getMessage()); | ||
// } | ||
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 suggest you to just delete all these unnecessary commented code. That is mainly the point of a version control system like git which is to undo any changes that you might not like in the future. Commenting them out and leaving them in code makes your code unnecssarily long and confusing. |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
public class DukeException extends Exception{ | ||
public DukeException(String m) { | ||
super(m); | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
import java.time.LocalDate; | ||
|
||
class Event extends Task { | ||
private LocalDate from; | ||
private LocalDate to; | ||
public Event(String name, String start, String end) throws DukeException { | ||
super(name); | ||
this.from = LocalDate.parse(start); | ||
this.to = LocalDate.parse(end); | ||
if (this.from.isAfter(this.to)) { | ||
throw new DukeException("/to date is before /from date"); | ||
} | ||
} | ||
|
||
@Override | ||
public String getFileDesc() { | ||
return this.isDone | ||
? "E|1|" + this.name + "|" + convertFileDate(this.from) + "|" + convertFileDate(this.to) | ||
: "E|0|" + this.name + "|" + convertFileDate(this.from) + "|" + convertFileDate(this.to); | ||
} | ||
|
||
@Override | ||
public String toString() { | ||
return this.isDone | ||
? "[E][X] " + this.name + " (from: " + getDate(this.from) + " to: " + getDate(this.to) + ")" | ||
: "[E][ ] " + this.name + " (from: " + getDate(this.from) + " to: " + getDate(this.to) + ")"; | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,109 @@ | ||
import java.time.format.DateTimeParseException; | ||
|
||
public class Parser { | ||
public Parser() {}; | ||
|
||
public void readInput(String[] input, TaskList taskList) throws DukeException { | ||
switch (input[0]) { | ||
case "list": | ||
taskList.list(); | ||
break; | ||
case "mark": | ||
mark(input, taskList); | ||
break; | ||
case "unmark": | ||
unmark(input, taskList); | ||
break; | ||
case "todo": | ||
todo(input, taskList); | ||
break; | ||
case "deadline": | ||
deadline(input, taskList); | ||
break; | ||
case "event": | ||
event(input, taskList); | ||
break; | ||
case "delete": | ||
delete(input, taskList); | ||
break; | ||
default: | ||
throw new DukeException("Sorry I do not understand the command"); | ||
} | ||
} | ||
|
||
public void mark(String[] input, TaskList taskList) throws DukeException { | ||
if (input.length == 1) { | ||
throw new DukeException("Mark needs a number."); | ||
} | ||
if (Integer.parseInt(input[1]) > taskList.size()) { | ||
throw new DukeException("Invalid task."); | ||
} | ||
taskList.markTask(Integer.parseInt(input[1]) - 1); | ||
} | ||
|
||
public void unmark(String[] input, TaskList taskList) throws DukeException { | ||
if (input.length == 1) { | ||
throw new DukeException("Unmark needs a number."); | ||
} | ||
if (Integer.parseInt(input[1]) > taskList.size()) { | ||
throw new DukeException("Invalid task."); | ||
} | ||
taskList.unmarkTask(Integer.parseInt(input[1]) - 1); | ||
} | ||
|
||
public void todo(String[] input, TaskList taskList) throws DukeException { | ||
if (input.length == 1) { | ||
throw new DukeException("todo needs a description"); | ||
} | ||
Task t = new Todo(input[1].strip()); | ||
taskList.addTask(t); | ||
System.out.println("Added new todo:\n " + t + "\nNumber of tasks: " + taskList.size()); | ||
} | ||
|
||
public void deadline(String[] input, TaskList taskList) throws DukeException { | ||
if (input.length == 1 || !input[1].contains("/by")) { | ||
throw new DukeException("Deadline needs a /by."); | ||
} | ||
String[] tempInput = input[1].strip().split("/by "); | ||
if (tempInput.length == 1) { | ||
throw new DukeException("/by needs a date/time."); | ||
} | ||
try { | ||
Task t = new Deadline(tempInput[0].strip(), tempInput[1].strip()); | ||
taskList.addTask(t); | ||
System.out.println("Added new deadline:\n " + t + "\nNumber of tasks: " + taskList.size()); | ||
} catch (DateTimeParseException e) { | ||
throw new DukeException("Date after /by needs to be in format yyyy-mm-dd"); | ||
} | ||
} | ||
|
||
public void event(String[] input, TaskList taskList) throws DukeException { | ||
if (input.length == 1 || !input[1].contains("/from") || !input[1].contains("/to") ) { | ||
throw new DukeException("Event needs a /from and /to."); | ||
} | ||
String[] tempInput = input[1].split("/"); | ||
String[] from = tempInput[1].split(" ",2); | ||
String[] to = tempInput[2].split(" ",2); | ||
if (from.length == 1 || to.length == 1) { | ||
throw new DukeException("/from and /to needs a date/time."); | ||
} | ||
try { | ||
Task t = new Event(tempInput[0].strip(), from[1].strip(), to[1].strip()); | ||
taskList.addTask(t); | ||
System.out.println("Added new event:\n " + t + "\nNumber of tasks: " + taskList.size()); | ||
} catch (DateTimeParseException e) { | ||
throw new DukeException("Date after /from and /to needs to be in format yyyy-mm-dd"); | ||
} | ||
} | ||
|
||
public void delete(String[] input, TaskList taskList) throws DukeException { | ||
if (input.length == 1) { | ||
throw new DukeException("Delete needs a number."); | ||
} | ||
int index = Integer.parseInt(input[1]); | ||
if (index > taskList.size()) { | ||
throw new DukeException("Invalid task."); | ||
} | ||
taskList.deleteTask(index - 1); | ||
} | ||
} |
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, instead of leaving the isDone logic in this toString() function, you could leave it in the Task class instead so that you dont have to retype so much code if you need to create a new Task type in the future.