-
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
[chashaobao] iP #640
base: master
Are you sure you want to change the base?
[chashaobao] iP #640
Changes from 9 commits
68c58c1
03523ec
81a9c53
60e495d
fddb9e9
48f9b91
064c6c3
97639d6
4f01857
1df1350
55989ae
957bf55
dfae243
0dea61f
cd8b26e
421974d
08746c5
ba6a7ef
91afd9e
3da8d0b
fb664fa
991b22f
6df0184
c7862f4
1ee7602
5f50efb
e73a476
b01a403
5656a9d
620f3de
20e7261
e543672
668af35
c088340
78d3987
1c02284
ac340f4
55d7ca0
cd35b95
b6f266c
a0b1e69
6bf7a93
6f02cc0
e4dc31b
dabdb0b
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 @@ | ||
{ | ||
"CurrentProjectSetting": null | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
{ | ||
"ExpandedNodes": [ | ||
"" | ||
], | ||
"PreviewInSolutionExplorer": false | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
{ | ||
"Version": 1, | ||
"WorkspaceRootPath": "C:\\Users\\65814\\OneDrive\\NUSY2S1\\CS2103T\\IP\\ip\\", | ||
"Documents": [], | ||
"DocumentGroupContainers": [ | ||
{ | ||
"Orientation": 0, | ||
"VerticalTabListWidth": 256, | ||
"DocumentGroups": [] | ||
} | ||
] | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
{ | ||
"CurrentProjectSetting": null | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
{ | ||
"ExpandedNodes": [ | ||
"" | ||
], | ||
"PreviewInSolutionExplorer": false | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
{ | ||
"Version": 1, | ||
"WorkspaceRootPath": "C:\\Users\\65814\\OneDrive\\NUSY2S1\\CS2103T\\IP\\ip\\src\\main\\java\\", | ||
"Documents": [], | ||
"DocumentGroupContainers": [ | ||
{ | ||
"Orientation": 0, | ||
"VerticalTabListWidth": 256, | ||
"DocumentGroups": [] | ||
} | ||
] | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
public class Deadline extends Task { | ||
protected String by; | ||
|
||
public Deadline(String description, String by) { | ||
super(description); | ||
this.by = by; | ||
} | ||
|
||
public Deadline(String description, boolean isDone, String by) { | ||
super(description, isDone); | ||
this.by = by; | ||
} | ||
|
||
@Override | ||
public String toString() { | ||
return "D | " + super.toString() + " | " + by; | ||
} | ||
} |
This file was deleted.
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
public class Event extends Task { | ||
protected String from; | ||
protected String to; | ||
|
||
public Event(String description, String from, String to) { | ||
super(description); | ||
this.from = from; | ||
this.to = to; | ||
} | ||
|
||
public Event(String description, boolean isDone, String from, String to) { | ||
super(description, isDone); | ||
this.from = from; | ||
this.to = to; | ||
} | ||
|
||
@Override | ||
public String toString() { | ||
return "E | " + super.toString() + " | " + this.from + " | " + this.to; | ||
} | ||
|
||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
public class InvalidCommandException extends Exception { | ||
public InvalidCommandException(String message) { | ||
super(message); | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
public class InvalidTaskFormatException extends Exception { | ||
public InvalidTaskFormatException(String message) { | ||
super(message); | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,241 @@ | ||
import java.io.BufferedReader; | ||
import java.io.InputStreamReader; | ||
import java.io.IOException; | ||
import java.io.BufferedWriter; | ||
import java.io.File; | ||
import java.io.FileReader; | ||
import java.io.FileWriter; | ||
import java.util.ArrayList; | ||
|
||
public class LittleMissHelpful { | ||
private static final String FILE_PATH = "./data/LittleMissHelpfullist.txt"; | ||
public static void main(String[] args) { | ||
/** | ||
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 you can add this comment in the header instead? (i.e. before the public class in Line 10) It may help others to understand your code purpose better! |
||
* Simulates a chatbot | ||
* Reads input and provides a reply | ||
*/ | ||
|
||
String name = "Ah Bang Mang"; | ||
String lineBreak = "---------------------------------"; | ||
String greeting = "Hello! I'm " + name + ".\nWhat you want sia?"; | ||
String exitLine = "Ok, I zao first then!"; | ||
|
||
BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); | ||
|
||
// Print greeting | ||
System.out.println(lineBreak); | ||
System.out.println(greeting); | ||
System.out.println(lineBreak); | ||
|
||
ArrayList<Task> list = new ArrayList<Task>(); | ||
|
||
// Load tasks from file | ||
loadTasks(list); | ||
|
||
int counter = list.size();; | ||
while (true) { // Continuous input | ||
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 refactor this part into the the Parse class as suggested in the project brief week 3? 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. agreed! Separating all the parsing commands into a separate parse class could make the code look neater 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. Yes, thank you! I have abstracted out the classes now |
||
try { | ||
String input = br.readLine(); | ||
|
||
// Split into command and description/arguments | ||
String[] inputList = input.split(" ", 2); | ||
String command = inputList[0]; | ||
|
||
if (inputList.length < 2) { | ||
// Check if the user command is "bye" -> exit | ||
if (command.equalsIgnoreCase("bye")) { | ||
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 you could separate the UI related code to a UI class? |
||
System.out.println(lineBreak); | ||
System.out.println(exitLine); | ||
System.out.println(lineBreak); | ||
saveTasks(list); | ||
break; // Exit the loop | ||
} | ||
|
||
// Check if the user command is "list" -> print list | ||
if (command.equalsIgnoreCase("list")) { | ||
System.out.println(lineBreak); | ||
|
||
if (counter == 0) { | ||
System.out.println("Wah shiok! No tasks at the moment leh."); | ||
} else { | ||
System.out.println("Siao liao! This your current task list leh..."); | ||
for (int i = 0; i < counter; i++) { | ||
int listNumber = i + 1; | ||
Task listItem = list.get(i); | ||
System.out.println(listNumber + ". " + listItem.toString()); | ||
} | ||
} | ||
System.out.println(lineBreak); | ||
continue; | ||
} | ||
|
||
// Invalid command --> throw exception | ||
System.out.println(lineBreak); | ||
System.out.println("Alamak! Format salah already. Please provide a valid command."); | ||
System.out.println(lineBreak); | ||
continue; | ||
} | ||
|
||
String item = inputList[1]; | ||
|
||
// Check if user command is "mark" / "unmark" -> mark / unmark task | ||
if (command.equalsIgnoreCase("mark")) { | ||
try { | ||
int listIndex = Integer.parseInt(item) - 1; | ||
if (listIndex < 0 || listIndex >= counter) { | ||
throw new TaskNotFoundException(lineBreak + "\nHai-ya, task number out of range lah.\n" + lineBreak); | ||
} | ||
Task curTask = list.get(listIndex); | ||
list.set(listIndex, curTask.markTask()); | ||
System.out.println(lineBreak); | ||
System.out.println("Wah upz! You have marked this task as done: " + curTask.toString()); | ||
System.out.println(lineBreak); | ||
} catch (NumberFormatException e) { | ||
System.out.println(lineBreak + "\nHai-yo, task number must be an integer lah.\n" + lineBreak); | ||
} catch (TaskNotFoundException e) { | ||
System.out.println(e.getMessage()); | ||
} | ||
continue; | ||
|
||
} else if (command.equalsIgnoreCase("unmark")) { | ||
try { | ||
int listIndex = Integer.parseInt(item) - 1; | ||
if (listIndex < 0 || listIndex >= counter) { | ||
throw new TaskNotFoundException(lineBreak + "Hai-ya, task number out of range lah.\n" + lineBreak); | ||
} | ||
Task curTask = list.get(listIndex); | ||
list.set(listIndex, curTask.unmarkTask()); | ||
System.out.println(lineBreak); | ||
System.out.println("Ok, I see you laze. You have marked this task as not done yet: " + curTask.toString()); | ||
System.out.println(lineBreak); | ||
} catch (NumberFormatException e) { | ||
System.out.println(lineBreak + "\nHai-yo, task number must be an integer lah.\n" + lineBreak); | ||
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 check looks the same as the check for the "mark" command, perhaps can refactor to reduce repetition? |
||
} catch (TaskNotFoundException e) { | ||
System.out.println(e.getMessage()); | ||
} | ||
continue; | ||
} | ||
|
||
//deletes task from list | ||
if (command.equalsIgnoreCase("delete")) { | ||
try { | ||
int listIndex = Integer.parseInt(item) - 1; | ||
if (listIndex < 0 || listIndex >= counter) { | ||
throw new TaskNotFoundException(lineBreak + "\nHai-ya, task number out of range lah.\n" + lineBreak); | ||
} | ||
Task curTask = list.get(listIndex); | ||
list.remove(listIndex); | ||
counter--; | ||
|
||
System.out.println(lineBreak); | ||
System.out.println("Wah shiok! This task no more liao: " + curTask.toString()); | ||
System.out.println("Now got only " + counter + " tasks left."); | ||
System.out.println(lineBreak); | ||
} catch (NumberFormatException e) { | ||
System.out.println(lineBreak + "\nHai-yo, task number must be an integer lah.\n" + lineBreak); | ||
} catch (TaskNotFoundException e) { | ||
System.out.println(e.getMessage()); | ||
} | ||
continue; | ||
} | ||
|
||
// Adds tasks to list | ||
if (command.equals("todo") || command.equals("deadline") || command.equals("event")) { | ||
Task newTask = null; | ||
|
||
try { | ||
if (command.equalsIgnoreCase("todo")) { | ||
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 switch and case statement may come in handy here. (frankly I also never use) |
||
newTask = new Todo(item); | ||
|
||
} else if (command.equalsIgnoreCase("deadline")) { | ||
String[] itemList = item.split("/by", 2); | ||
if (itemList.length < 2) { | ||
throw new InvalidTaskFormatException( | ||
lineBreak + "\nAlamak! Deadline format salah already. Use 'deadline description /by date'.\n" + lineBreak); | ||
} | ||
String desc = itemList[0].trim(); | ||
String by = itemList[1].trim(); | ||
|
||
newTask = new Deadline(desc, by); | ||
|
||
} else if (command.equalsIgnoreCase("event")) { | ||
String[] itemList = item.split("/from | /to", 3); | ||
if (itemList.length < 3) { | ||
throw new InvalidTaskFormatException( | ||
lineBreak + "\nAlamak! Event format salah already. Use 'event description /from start /to end'.\n" + lineBreak); | ||
} | ||
String desc = itemList[0].trim(); | ||
String from = itemList[1].trim(); | ||
String to = itemList[2].trim(); | ||
|
||
newTask = new Event(desc, from, to); | ||
} | ||
|
||
list.add(newTask); | ||
System.out.println(lineBreak); | ||
System.out.println("Added to list liao: " + newTask.toString()); | ||
System.out.println("Sian, now got " + (counter + 1) + " tasks in your list."); | ||
System.out.println(lineBreak); | ||
counter++; | ||
|
||
} catch (InvalidTaskFormatException e) { | ||
System.out.println(e.getMessage()); | ||
} | ||
continue; | ||
|
||
} else { | ||
// Invalid command --> throw exception | ||
System.out.println( | ||
lineBreak + "\nAlamak! Format salah already... Please provide a valid command and description.\n" + lineBreak); | ||
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 can provide some descriptions of what are valid commands |
||
} | ||
} catch (IOException e) { | ||
System.out.println("Aiyo salah! Try again lor..."); | ||
e.printStackTrace(); | ||
} | ||
} | ||
} | ||
|
||
private static void loadTasks(ArrayList<Task> list) { | ||
/** | ||
* Loads tasks from a txt file from harddrive to generate initial list | ||
* | ||
* Creates a new file with a new path if the file does not currently exist | ||
*/ | ||
File dataFile = new File(FILE_PATH); | ||
if (!dataFile.getParentFile().exists()) { | ||
dataFile.getParentFile().mkdirs(); | ||
} | ||
if (dataFile.exists()) { | ||
try (BufferedReader br = new BufferedReader(new FileReader(dataFile))) { | ||
String line; | ||
while ((line = br.readLine()) != null) { | ||
try { | ||
Task task = Task.fromStringToTask(line); | ||
list.add(task); | ||
} catch (InvalidTaskFormatException e) { | ||
System.out.println("Warning: Skipping invalid task format: " + line); | ||
} | ||
} | ||
} catch (IOException e) { | ||
System.out.println("Error reading the file: " + e.getMessage()); | ||
} | ||
} | ||
} | ||
|
||
private static void saveTasks(ArrayList<Task> list) { | ||
/** | ||
* Saves tasks to a txt file on harddrive | ||
*/ | ||
File dataFile = new File(FILE_PATH); | ||
try (BufferedWriter bw = new BufferedWriter(new FileWriter(dataFile))) { | ||
for (Task task : list) { | ||
bw.write(task.toString()); | ||
bw.newLine(); | ||
} | ||
} catch (IOException e) { | ||
System.out.println("Error writing to the file: " + e.getMessage()); | ||
} | ||
} | ||
} | ||
|
||
|
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.
Would it make sense to save the string as Java Datetime instead?