-
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
[Ernest Yu] iP #619
base: master
Are you sure you want to change the base?
[Ernest Yu] iP #619
Changes from 34 commits
68c58c1
03523ec
81a9c53
c9700d5
3a37ec7
7d54463
8d1d88e
2a18dae
833133a
38f9bec
1b521ad
a190e1e
72e666e
cbeeef2
6c942c6
cbd56fd
e89bede
356ec8c
63cec7f
e53a47f
bc2746b
c9b71c5
987c3b1
9683515
89ba645
1e125c2
885696a
ffd1bc3
fd4fc4c
7da5dcd
ef6ce97
105be6c
7ce1c85
c69296f
75bceb8
7d741f1
20200b9
40d18a4
8b5c1b3
4833381
a41eda4
454e46d
f79e64f
424cf74
ff4ac5e
54a5a8c
2ee5da5
9c2135a
dee2a80
c2aa27f
9f50bcb
91ec795
f6c4932
edf23bd
7770545
e764925
aa1b7ee
4ec63b2
f9ae1ad
5d96c88
25e0a01
149b7dc
0489e23
ca812de
f14bd71
303a829
70d268e
f617801
3611a22
b8a2cdf
352806d
d25df0f
2c5fa98
a3acccc
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 @@ | ||
todo xyz | ||
event bcd /from today /to tmr | ||
deadline me /by fri |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
TaskHandler.java | ||
This file was deleted.
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,95 @@ | ||
package chatBot.bot; | ||
|
||
import chatBot.command.Command; | ||
|
||
/** Bword is a chatbot that helps with planning tasks | ||
* | ||
*/ | ||
|
||
public class Bword { | ||
private static final String FILELOCATION = "./data/bword.txt"; | ||
//public static final String HLINE = "____________________________________________________________\n"; | ||
|
||
private Storage storage; | ||
private TaskList taskList; | ||
private Ui ui; | ||
|
||
Bword(String s) { | ||
this.ui = new Ui(); | ||
this.storage = new Storage(s); | ||
try { | ||
this.taskList = new TaskList(this.storage.load()); | ||
} catch (Exception e) { | ||
ui.showLoadingError(); | ||
this.taskList = new TaskList(); | ||
} | ||
} | ||
|
||
void run() { | ||
this.ui.showWelcome(); | ||
boolean isExit = false; | ||
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 you name your boolean here with prefix 'is'. 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. thanks |
||
|
||
while (!isExit) { | ||
try { | ||
String fullCommand = this.ui.readCommand(); | ||
this.ui.showLine(); // show the divider line ("_______") | ||
Command c = Parser.parse(fullCommand); | ||
c.execute(this.taskList, this.ui, this.storage); | ||
isExit = c.isExit(); | ||
} catch (Exception e) { | ||
this.ui.showError(e.getMessage()); | ||
} finally { | ||
this.ui.showLine(); | ||
} | ||
} | ||
} | ||
|
||
public static void main(String[] args) { | ||
Bword bot = new Bword(FILELOCATION); | ||
bot.run(); | ||
|
||
/* | ||
TaskHandler th = new TaskHandler(); | ||
File file; | ||
try { | ||
file = new File(FILELOCATION); | ||
Scanner sc = new Scanner(file); | ||
while (sc.hasNextLine()) { | ||
String s = sc.nextLine(); | ||
// System.out.println(s); | ||
th.addPastTask(s); | ||
} | ||
sc.close(); | ||
} catch (FileNotFoundException e) { | ||
System.out.println("File not found"); | ||
e.printStackTrace(); | ||
return; | ||
} | ||
*/ | ||
// Scanner sc = new Scanner(System.in); | ||
|
||
/* | ||
enum States {to_loop, to_exit, to_list} | ||
States currentState = States.to_loop; | ||
|
||
while (currentState != States.to_exit) { | ||
String command = sc.next(); | ||
String s = sc.nextLine(); | ||
System.out.print(HLINE); | ||
String tmp = s.strip(); | ||
if (command.equals("bye")) { | ||
break; | ||
} | ||
th.handleCommand(command, s); | ||
System.out.print(HLINE); | ||
} | ||
|
||
file.delete(); | ||
th.writeToFile(FILELOCATION); | ||
|
||
System.out.println( | ||
" Bye. Hope to see you again soon!\n" + | ||
HLINE); | ||
*/ | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,113 @@ | ||
package chatBot.bot; | ||
|
||
import chatBot.command.*; | ||
import chatBot.exception.EmptyDescException; | ||
import chatBot.exception.InvalidCommandException; | ||
import chatBot.task.Deadline; | ||
import chatBot.task.Event; | ||
import chatBot.task.Task; | ||
import chatBot.task.ToDoTask; | ||
|
||
import java.time.format.DateTimeParseException; | ||
|
||
public class Parser { | ||
static Task parseTask(String s) { | ||
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. Should this String and the Task variable later be named more descriptively, as you are using them throughout the method? |
||
String command = s.split(" ")[0]; | ||
String desc = s.substring(s.indexOf(" ")); | ||
|
||
desc = desc.stripLeading(); | ||
|
||
Task t = null; | ||
|
||
if (command.equals("todo")) { | ||
try { | ||
t = new ToDoTask(desc); | ||
} catch (Exception e) { | ||
Ui.printAnything(e.getMessage()); | ||
} | ||
} else if (command.equals("deadline")) { | ||
try { | ||
String[] arr = desc.split("/by"); | ||
try { | ||
t = new Deadline(arr[0].strip(), arr[1].strip()); | ||
} catch (ArrayIndexOutOfBoundsException e) { | ||
Ui.printAnything("missing /by"); | ||
} catch (DateTimeParseException ex) { | ||
Ui.printAnything("Incorrect date format stored: " + arr[1].strip()); | ||
} | ||
} catch (EmptyDescException e) { | ||
Ui.printAnything(e.getMessage()); | ||
} | ||
} else if (command.equals("event")) { | ||
String[] arr = desc.split("/from"); | ||
String[] arr2 = new String[0]; | ||
try { | ||
arr2 = arr[1].split("/to"); | ||
} catch (ArrayIndexOutOfBoundsException e) { | ||
System.out.println("missing /from"); | ||
} | ||
t = null; | ||
try { | ||
try { | ||
t = new Event(arr[0].strip(), arr2[0].strip(), arr2[1].strip()); | ||
} catch (ArrayIndexOutOfBoundsException ex) { | ||
System.out.println("missing /to"); | ||
} catch (DateTimeParseException ex) { | ||
System.out.println("Incorrect date format stored: " + arr[1].strip()); | ||
} | ||
} catch (Exception e) { | ||
Ui.printAnything(e.getMessage()); | ||
} | ||
} else { | ||
try { | ||
throw new InvalidCommandException(); | ||
} catch (InvalidCommandException e) { | ||
Ui.printAnything(e.getMessage()); | ||
} | ||
//System.out.println("Unknown command: " + command); | ||
} | ||
|
||
if (t == null) { | ||
System.out.println("null tasked returned back to Storage.load()"); | ||
} | ||
|
||
return t; | ||
} | ||
|
||
static Command parse(String fullCommand) { | ||
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. Should these methods have header comments to make the parameters and return values more clear? |
||
String action = fullCommand.split(" ")[0]; | ||
String desc = fullCommand.substring(fullCommand.indexOf(" ") + 1); | ||
if (fullCommand.indexOf(" ") == -1) { | ||
desc = ""; | ||
} | ||
Command c = null; | ||
if (action.equals("mark")) { | ||
c = new MarkCommand(Integer.parseInt(desc) - 1); | ||
// Task t = this.tasks.get(Integer.parseInt(desc) - 1).markAsDone(); | ||
// System.out.println("I've marked as done:\n" + t); | ||
} else if (action.equals("unmark")) { | ||
c = new UnmarkCommand(Integer.parseInt(desc) - 1); | ||
// Task t = this.tasks.get(Integer.parseInt(desc) - 1).markAsNotDone(); | ||
// System.out.println("I've marked as not done:\n" + t); | ||
} else if (action.equals("list")) { | ||
c = new ListCommand(); | ||
// System.out.println(this.getTasksString()); | ||
} else if (action.equals("todo") || action.equals("deadline") || action.equals("event")) { | ||
//System.out.println("action is: " + action + " and desc is: " + desc + "."); | ||
c = new AddCommand(action, desc); | ||
} else if (action.equals("delete")) { | ||
c = new DeleteCommand(Integer.parseInt(desc)); | ||
// this.deleteTask(Integer.parseInt(desc)); | ||
} else if (action.equals("bye")) { | ||
c = new ExitCommand(); | ||
} else { | ||
try { | ||
throw new InvalidCommandException(); | ||
} catch (InvalidCommandException e) { | ||
System.out.println(e); | ||
} | ||
//System.out.println("Unknown command: " + command); | ||
} | ||
return c; | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,62 @@ | ||
package chatBot.bot; | ||
|
||
import chatBot.task.Task; | ||
|
||
import java.io.File; // Import the File class | ||
import java.io.FileNotFoundException; // Import this class to handle errors | ||
import java.io.IOException; | ||
import java.util.ArrayList; | ||
import java.util.Scanner; | ||
import java.io.FileWriter; | ||
|
||
public class Storage { | ||
private File file; | ||
private final String filePath; | ||
|
||
Storage(String filePath) { | ||
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 an access modifier could be added for the constructor here for the sake of completeness :) |
||
this.filePath = filePath; | ||
try { | ||
this.file = new File(filePath); | ||
} catch (NullPointerException e) { | ||
System.out.println("pathname unrecognised"); | ||
} catch (Exception e) { | ||
System.out.println("Exception occured: " + e.getMessage()); | ||
} | ||
} | ||
|
||
ArrayList<Task> load() { | ||
ArrayList<Task> tasks = new ArrayList<>(); | ||
Scanner sc = null; | ||
try { | ||
sc = new Scanner(file); | ||
while (sc.hasNextLine()) { | ||
String s = sc.nextLine(); | ||
Task t = Parser.parseTask(s); | ||
if (t == null) { | ||
continue; | ||
} | ||
tasks.add(t); | ||
} | ||
} catch (FileNotFoundException e) { | ||
throw new RuntimeException(e); | ||
} | ||
sc.close(); | ||
file.delete(); | ||
return tasks; | ||
} | ||
|
||
public void writeToFile(String s) { | ||
try { | ||
FileWriter myWriter = new FileWriter(filePath); | ||
myWriter.write(s); | ||
myWriter.close(); | ||
// myWriter.write("Files in Java might be tricky, but it is fun enough!"); | ||
// myWriter.close(); | ||
// System.out.println("Successfully wrote to the file."); | ||
} catch (IOException e) { | ||
System.out.println("An error occurred."); | ||
e.printStackTrace(); | ||
} | ||
} | ||
|
||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,57 @@ | ||
package chatBot.bot; | ||
|
||
import chatBot.task.Task; | ||
|
||
import java.util.ArrayList; | ||
|
||
/** TaskList serves the purpose of where all the Task is being stored | ||
* @param tasks - there are previous tasks stored in ./data and is retreived. | ||
*/ | ||
public class TaskList { | ||
private ArrayList<Task> tasks; | ||
|
||
TaskList(ArrayList<Task> tasks) { | ||
this.tasks = tasks; | ||
} | ||
|
||
TaskList() { | ||
this.tasks = new ArrayList<Task>(); | ||
} | ||
|
||
public void addTask(Task task) { | ||
this.tasks.add(task); | ||
} | ||
|
||
/** Create String object for storing back into file in Storage class */ | ||
public String getTaskCommands() { | ||
String s = ""; | ||
for (Task t : this.tasks) { | ||
s += t.getOriginalCommand() + "\n"; | ||
} | ||
return s; | ||
} | ||
|
||
public int size() { | ||
return this.tasks.size(); | ||
} | ||
|
||
public Task getTask(int index) { | ||
return this.tasks.get(index); | ||
} | ||
|
||
public void listTasks() { | ||
String s = ""; | ||
for (int i = 0; i < this.tasks.size(); i++) { | ||
s += String.format("%d.", i + 1) + this.tasks.get(i) + "\n"; | ||
} | ||
System.out.println(s.stripTrailing()); | ||
} | ||
|
||
public String getTaskToString(int index) { | ||
return this.tasks.get(index).toString(); | ||
} | ||
|
||
public void removeTask(int index) { | ||
this.tasks.remove(index); | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
package chatBot.bot; | ||
|
||
import java.util.Scanner; | ||
|
||
public class Ui { | ||
private Scanner scanner; | ||
public static final String HLINE = "____________________________________________________________\n"; | ||
|
||
Ui() { | ||
this.scanner = new Scanner(System.in); | ||
} | ||
|
||
void showWelcome() { | ||
System.out.print(HLINE + | ||
" Hello! I'm 'B word'\n" + | ||
" What can I do for you?\n" + | ||
HLINE); | ||
} | ||
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 consider proper access modifiers for most methods in this class |
||
|
||
public void showGoodbye() { | ||
System.out.println("Bye. Hope to see you again soon!"); | ||
} | ||
|
||
void showLine() { | ||
System.out.println(HLINE); | ||
} | ||
|
||
String readCommand() { | ||
return this.scanner.nextLine(); | ||
} | ||
|
||
void showError(String error) { | ||
System.out.println(error); | ||
} | ||
|
||
void showLoadingError() { | ||
System.out.println("Something went wrong when loading file"); | ||
} | ||
|
||
static void printAnything(String s) { | ||
System.out.println(s); | ||
} | ||
} |
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.
How come you have a TaskHandler.java class which is not used?
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.
To be removed in future updates.