-
Notifications
You must be signed in to change notification settings - Fork 590
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
[T1duS] ip #680
base: master
Are you sure you want to change the base?
[T1duS] ip #680
Changes from 1 commit
68c58c1
03523ec
81a9c53
6aa0869
9aa95e9
8d7ef61
26a4f78
742b278
8921f9d
41419f3
791ceff
eb2be1d
02d84a8
7689960
2fe4354
f3dff1a
1bfdf9e
98329b9
7f11c22
4b4e23e
be97f49
5d93574
ad05bc5
27a30a9
29a1931
b479ae6
97b281f
39871e8
fe1682a
0e3f9d3
cc1bbc6
2359427
825a679
d0c45b5
c221350
09d31e7
4007c2c
10d6d49
616bbc5
1d1452e
38ee534
e8af505
3669b82
daebd82
38ae099
e89275f
b96c5e1
9c8fa68
98b29a4
e732e25
4661936
cb3b0a4
a40616a
f57ad05
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 |
---|---|---|
@@ -1,13 +1,18 @@ | ||
public class Deadline extends Task { | ||
protected String by; | ||
private String by; | ||
|
||
public Deadline(String description, String by) { | ||
super(description); | ||
this.by = by; | ||
} | ||
|
||
@Override | ||
public String toSaveFormat() { | ||
return "D|" + (isDone ? "1" : "0") + "|" + description + "|" + by; | ||
} | ||
|
||
@Override | ||
public String toString() { | ||
return "[D]" + super.toString() + " (by: " + by + ")"; | ||
return "[D]" + (isDone ? "[X] " : "[ ] ") + description + " (by: " + by + ")"; | ||
} | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,15 +1,20 @@ | ||
public class Event extends Task { | ||
protected String to; | ||
protected String from; | ||
private String from; | ||
private String to; | ||
|
||
public Event(String description, String to, String from) { | ||
public Event(String description, String from, String to) { | ||
super(description); | ||
this.to = to; | ||
this.from = from; | ||
this.to = to; | ||
} | ||
|
||
@Override | ||
public String toSaveFormat() { | ||
return "E|" + (isDone ? "1" : "0") + "|" + description + "|" + from + "|" + to; | ||
} | ||
|
||
@Override | ||
public String toString() { | ||
return "[E]" + super.toString() + " (from: " + from + " to: " + to + ")"; | ||
return "[E]" + (isDone ? "[X] " : "[ ] ") + description + " (from: " + from + " to: " + to + ")"; | ||
} | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,70 +1,76 @@ | ||
import java.util.Scanner; | ||
|
||
public class Opus { | ||
public static void main(String[] args) { | ||
|
||
private Storage storage; | ||
private TaskList taskList; | ||
// private Ui ui; | ||
|
||
public Opus(String filePath) { | ||
// ui = new Ui(); | ||
storage = new Storage(filePath); | ||
taskList = new TaskList(storage.load()); | ||
} | ||
|
||
public void run() { | ||
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. Could this method be refactored into smaller methods to handle the different scenarios more effectively? |
||
Scanner scanner = new Scanner(System.in); | ||
|
||
System.out.println(" Hello! I'm Opus"); | ||
System.out.println(" What can I do for you?"); | ||
|
||
Task[] tasks = new Task[101]; | ||
int taskCount = 0; | ||
|
||
while(true) { | ||
while (true) { | ||
String s = scanner.nextLine(); | ||
String[] words = s.split(" "); | ||
|
||
try{ | ||
try { | ||
if (s.equals("bye")) { | ||
storage.save(taskList.getTasks()); | ||
break; | ||
} else if (s.equals("list")) { | ||
for (int i = 1; i <= taskCount; i++) { | ||
System.out.println(i + ". " + tasks[i]); | ||
} | ||
} | ||
else if (words[0].equals("mark")) { | ||
int i = Integer.parseInt(words[1]); | ||
tasks[i].markAsDone(); | ||
taskList.listTasks(); | ||
} else if (words[0].equals("mark")) { | ||
int i = Integer.parseInt(words[1]) - 1; | ||
taskList.getTask(i).markAsDone(); | ||
System.out.println("Nice! I've marked this task as done:"); | ||
System.out.println(tasks[i]); | ||
} | ||
else if (words[0].equals("delete")) { | ||
int i = Integer.parseInt(words[1]); | ||
System.out.println(taskList.getTask(i)); | ||
} else if (words[0].equals("delete")) { | ||
int i = Integer.parseInt(words[1]) - 1; | ||
System.out.println("Noted. I've removed this task:"); | ||
System.out.println(" " + tasks[i]); | ||
for (int j = i; j < taskCount; j++) { | ||
tasks[j] = tasks[j + 1]; | ||
} | ||
taskCount--; | ||
System.out.println(" Now you have " + taskCount + " tasks in the list."); | ||
} | ||
else { | ||
System.out.println(" " + taskList.getTask(i)); | ||
taskList.removeTask(i); | ||
System.out.println(" Now you have " + taskList.getSize() + " tasks in the list."); | ||
} else { | ||
if (words[0].equals("todo")) { | ||
if (words.length <= 1) { | ||
throw new OpusEmptyDescriptionException("The description of a todo cannot be empty."); | ||
} | ||
tasks[++taskCount] = new ToDo(s.substring(5)); | ||
Task todo = new ToDo(s.substring(5)); | ||
taskList.addTask(todo); | ||
} else if (words[0].equals("deadline")) { | ||
String[] parts = s.substring(9).split(" /by "); | ||
tasks[++taskCount] = new Deadline(parts[0], parts[1]); | ||
Task deadline = new Deadline(parts[0], parts[1]); | ||
taskList.addTask(deadline); | ||
} else if (words[0].equals("event")) { | ||
String[] parts = s.substring(6).split(" /from "); | ||
String[] parts2 = parts[1].split(" /to "); | ||
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? |
||
tasks[++taskCount] = new Event(parts[0], parts2[1], parts2[0]); | ||
} | ||
else{ | ||
Task event = new Event(parts[0], parts2[0], parts2[1]); | ||
taskList.addTask(event); | ||
} else { | ||
throw new OpusUnknownCommandException("I'm sorry, but I don't know what that means."); | ||
} | ||
System.out.println(" Got it. I've added this task:"); | ||
System.out.println(" " + tasks[taskCount]); | ||
System.out.println(" Now you have " + taskCount + " tasks in the list."); | ||
System.out.println(" " + taskList.getTask(taskList.getSize() - 1)); | ||
System.out.println(" Now you have " + taskList.getSize() + " tasks in the list."); | ||
} | ||
} | ||
catch (OpusException e) { | ||
} catch (OpusException e) { | ||
System.out.println(e.getMessage()); | ||
} | ||
} | ||
System.out.println(" Bye. Hope to see you again soon!"); | ||
scanner.close(); | ||
} | ||
|
||
public static void main(String[] args) { | ||
new Opus("data/tasks.txt").run(); | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
public class Parser{ | ||
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 is nitpicking but don't forget the space to follow coding standards. In the future can check through. |
||
|
||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,67 @@ | ||
import java.io.File; | ||
import java.io.FileWriter; | ||
import java.io.IOException; | ||
import java.util.ArrayList; | ||
import java.util.Scanner; | ||
|
||
public class Storage { | ||
private String filePath; | ||
|
||
public Storage(String filePath) { | ||
this.filePath = filePath; | ||
} | ||
|
||
public ArrayList<Task> load(){ | ||
ArrayList<Task> tasks = new ArrayList<>(); | ||
File file = new File(filePath); | ||
|
||
try{ | ||
Scanner scanner = new Scanner(file); | ||
while (scanner.hasNextLine()) { | ||
String line = scanner.nextLine(); | ||
String[] parts = line.split("\\|"); | ||
String taskType = parts[0]; | ||
boolean isDone = parts[1].equals("1"); | ||
switch (taskType) { | ||
case "T": | ||
tasks.add(new ToDo(parts[2])); | ||
break; | ||
case "D": | ||
tasks.add(new Deadline(parts[2], parts[3])); | ||
break; | ||
case "E": | ||
tasks.add(new Event(parts[2], parts[3], parts[4])); | ||
break; | ||
} | ||
if (isDone) { | ||
tasks.get(tasks.size() - 1).markAsDone(); | ||
} | ||
} | ||
scanner.close(); | ||
return tasks; | ||
} | ||
catch (IOException e) { | ||
return tasks; | ||
} | ||
} | ||
|
||
public void save(ArrayList<Task> tasks){ | ||
File file = new File(filePath); | ||
|
||
File parentDir = file.getParentFile(); | ||
if (parentDir != null && !parentDir.exists()) { | ||
parentDir.mkdirs(); | ||
} | ||
|
||
try{ | ||
FileWriter writer = new FileWriter(filePath); | ||
for (Task task : tasks) { | ||
writer.write(task.toSaveFormat() + "\n"); | ||
} | ||
writer.close(); | ||
} | ||
catch (IOException e){ | ||
return; | ||
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 logging or printing an error message could be helpful for diagnosing the exceptions here? |
||
} | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
import java.util.ArrayList; | ||
|
||
public class TaskList { | ||
private ArrayList<Task> tasks; | ||
|
||
public TaskList() { | ||
this.tasks = new ArrayList<>(); | ||
} | ||
|
||
public TaskList(ArrayList<Task> tasks) { | ||
this.tasks = tasks; | ||
} | ||
|
||
public void addTask(Task task) { | ||
tasks.add(task); | ||
} | ||
|
||
public Task getTask(int index) { | ||
return tasks.get(index); | ||
} | ||
|
||
public Task removeTask(int index) { | ||
return tasks.remove(index); | ||
} | ||
|
||
public int getSize() { | ||
return tasks.size(); | ||
} | ||
|
||
public ArrayList<Task> getTasks() { | ||
return tasks; | ||
} | ||
|
||
public void listTasks() { | ||
for (int i = 0; i < tasks.size(); i++) { | ||
System.out.println((i + 1) + ". " + tasks.get(i)); | ||
} | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -3,8 +3,13 @@ public ToDo(String description) { | |
super(description); | ||
} | ||
|
||
@Override | ||
public String toSaveFormat() { | ||
return "T|" + (isDone ? "1" : "0") + "|" + description; | ||
} | ||
|
||
@Override | ||
public String toString() { | ||
return "[T]" + super.toString(); | ||
return "[T]" + (isDone ? "[X] " : "[ ] ") + description; | ||
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 could add a method to Task class that returns a string with (isDone ? "[X] " : "[ ] "). Then you can call super.thisMethod for easier coding? |
||
} | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
public class Ui { | ||
public void showTaskAdded(Task task, int taskCount) { | ||
System.out.println("Got it. I've added this task:"); | ||
System.out.println(" " + task); | ||
System.out.println("Now you have " + taskCount + " tasks in the list."); | ||
} | ||
|
||
public void showTaskDeleted(Task task, int taskCount) { | ||
System.out.println("Noted. I've removed this task:"); | ||
System.out.println(" " + task); | ||
System.out.println("Now you have " + taskCount + " 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.
Perhaps can give the deadline date a clearer name? Right now the name "by" may not be clear to other programmers.