-
Notifications
You must be signed in to change notification settings - Fork 461
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
[Agamjyot] IP #508
base: master
Are you sure you want to change the base?
[Agamjyot] IP #508
Changes from 15 commits
57e2341
2b23dc8
c55c8ed
0660f25
9c8a970
f257b8b
0ccc628
bf66b62
4fe9d41
3818960
57d85ac
f53dcee
3afdcfa
384d581
8d6ddd7
c1bf358
accb5a3
d9f7d38
4523685
5df069b
9dd15c1
7bce990
49e2710
88b482d
4384133
442ea6c
056eb0a
42706f7
9c28a7c
51b2288
4f3d6e9
c0827d8
7b9bab4
dbf16c7
59b8e93
b3af198
8a18dfb
4cea6b7
95a650f
bf8695a
e848cc2
cbde808
e3fb39c
7f9547b
df90777
ee8334f
e837e13
67177e2
62bc3da
b27f4b2
eb69c56
27889fa
83b6f98
5caa3a7
c1b87ed
ad0488a
5e973d5
6a8c444
11569a4
71215d2
bb70659
fc79cf8
f5d2536
0832875
40cd274
741e01c
64c3727
9c7adf9
dc7926c
3ca54c8
9b697d6
8bffba6
761dc56
d8654c7
6648332
7062d1d
810b80e
b083f5b
21d9bfa
3acb5cd
0aefd9e
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,4 @@ | ||
T | 1 | read book | ||
D | 0 | return book | June 6th | ||
E | 0 | project meeting | Aug 6th 2-4pm | ||
T | 1 | join sports club |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,83 @@ | ||
import java.util.ArrayList; | ||
|
||
class ChatBot { | ||
|
||
private String name; | ||
private final String line = "------------------------------" + | ||
"----------------------------------"; | ||
private ArrayList<Task> tasks; | ||
|
||
ChatBot(String name) { | ||
|
||
this.name = name; | ||
this.tasks = new ArrayList<Task>(); | ||
|
||
} | ||
|
||
public void greet() { | ||
|
||
System.out.println(line + "\n\t Hello I'm " + name + "!!\n" + | ||
"What do you wanna chat about today?\n" + line); | ||
} | ||
|
||
public void addTask(Task task) { | ||
|
||
this.tasks.add(task); | ||
System.out.println(line + "\n\tGot it. I just added the " + | ||
"task:\n\t\t" + task + "\n\tNow you have " + | ||
"" + this.tasks.size() + " tasks in the list\n" + line); | ||
} | ||
|
||
public void printTasks() { | ||
|
||
System.out.println(line); | ||
|
||
if(this.tasks.size() == 0) { | ||
|
||
System.out.println("You currently have no tasks"); | ||
} else { | ||
|
||
for (int i = 0; i < this.tasks.size(); i++) { | ||
|
||
System.out.println("\t" + (i + 1) + ". " + this.tasks.get(i)); | ||
} | ||
} | ||
System.out.println(line); | ||
} | ||
|
||
public void markDone(int index) { | ||
|
||
this.tasks.get(index).done(true); | ||
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. Since you have a relatively long chain of code to run, it might be better to omit the |
||
System.out.println(line + "\n\tExcellent! I have marked " + | ||
"the task as done:\n\t" + this.tasks.get(index) + "\n" + line); | ||
} | ||
|
||
public void markUndone(int index) { | ||
|
||
this.tasks.get(index).done(false); | ||
System.out.println(line + "\n\tNoted! I have marked " + | ||
"the task as not done yet:\n\t" | ||
+ this.tasks.get(index) + "\n" + line); | ||
} | ||
|
||
public void delete(int index) { | ||
|
||
System.out.println(line + "\n\tNoted. I've remove this task:" + | ||
this.tasks.get(index)); | ||
this.tasks.remove(index); | ||
System.out.println("\t Now you have " + this.tasks.size() + " " + | ||
"tasks in the list.\n" + line); | ||
} | ||
|
||
public void echo(String input) { | ||
|
||
System.out.println(line + "\n\t" + input + "\n" + line); | ||
} | ||
|
||
public void bye() { | ||
|
||
System.out.println(line + "\n\t Bye. Looking forward to chating " + | ||
"with you soon again!\n" + line); | ||
} | ||
|
||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
class ChatBotException extends Exception { | ||
|
||
ChatBotException(String message) { | ||
|
||
super(message); | ||
} | ||
|
||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
import java.time.LocalDateTime; | ||
import java.time.format.DateTimeFormatter; | ||
|
||
|
||
class Deadline extends Task { | ||
|
||
private LocalDateTime completeBy; | ||
|
||
Deadline(String description, String completeBy) { | ||
super(description, false); | ||
this.completeBy = LocalDateTime.parse(completeBy, | ||
DateTimeFormatter.ofPattern("[d/M/y HHmm]"));; | ||
} | ||
|
||
Deadline(String description, boolean isDone, String completeBy) { | ||
|
||
super(description, isDone); | ||
|
||
this.completeBy = LocalDateTime.parse(completeBy, | ||
DateTimeFormatter.ofPattern("[d/M/y HHmm]"));; | ||
|
||
} | ||
|
||
@Override | ||
public String toFileString() { | ||
|
||
return "D | " + (this.isDone ? 1 : 0) + " | " + | ||
this.description + " | " + this.completeBy; | ||
} | ||
|
||
@Override | ||
public String toString() { | ||
|
||
return "[D]" + super.toString() + " (by: " + | ||
completeBy.format(DateTimeFormatter.ofPattern("MMM d yyyy")) | ||
+ ")"; | ||
} | ||
|
||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,10 +1,97 @@ | ||
public class Duke { | ||
public static void main(String[] args) { | ||
String logo = " ____ _ \n" | ||
+ "| _ \\ _ _| | _____ \n" | ||
+ "| | | | | | | |/ / _ \\\n" | ||
+ "| |_| | |_| | < __/\n" | ||
+ "|____/ \\__,_|_|\\_\\___|\n"; | ||
System.out.println("Hello from\n" + logo); | ||
} | ||
} | ||
import java.io.FileNotFoundException; | ||
import java.io.IOException; | ||
import java.util.Scanner; | ||
import java.util.ArrayList; | ||
|
||
public class Duke { | ||
|
||
public static void main(String[] args) { | ||
|
||
Scanner sc = new Scanner(System.in); | ||
|
||
ChatBot chatBot = new ChatBot("duke"); | ||
|
||
Storage taskStorage = new Storage(); | ||
|
||
chatBot.greet(); | ||
|
||
boolean quit = false; | ||
String input; | ||
String[] command; | ||
String time; // for the deadline or time of the event | ||
String taskName; | ||
|
||
try { | ||
ArrayList<Task> tasks = taskStorage.convertToTaskList(); | ||
while (!quit) { | ||
|
||
try { | ||
input = sc.next(); | ||
|
||
switch (input) { | ||
|
||
case "bye": | ||
quit = true; | ||
chatBot.bye(); | ||
try { | ||
taskStorage.saveTaskList(tasks); | ||
} catch (IOException io) { | ||
chatBot.echo(io.getMessage() + "\nUnable to save data. " + | ||
"All the data will be lost!"); | ||
} | ||
break; | ||
case "list": | ||
chatBot.printTasks(); | ||
break; | ||
case "mark": | ||
chatBot.markDone(sc.nextInt() - 1); | ||
sc.nextLine(); | ||
break; | ||
case "unmark": | ||
chatBot.markUndone(sc.nextInt() - 1); | ||
sc.nextLine(); | ||
break; | ||
case "deadline": | ||
command = sc.nextLine().split(" /by "); | ||
chatBot.addTask(new Deadline(command[0], command[1])); | ||
break; | ||
case "event": | ||
command = sc.nextLine().split(" /at "); | ||
chatBot.addTask(new Events(command[0], command[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. Perhaps it would be better to abstract out
before you pass it into your Class Constructors for greater clarity. 😄 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 for pointing that out, will make the changes in future commits. |
||
break; | ||
case "todo": | ||
taskName = sc.nextLine(); | ||
if (taskName.isEmpty()) { | ||
throw new ChatBotException("The description of todo cannot be empty " + | ||
"please use the format:\n\t" + | ||
"todo <description>"); | ||
} | ||
chatBot.addTask(new ToDo(taskName)); | ||
break; | ||
case "delete": | ||
chatBot.delete(sc.nextInt() - 1); | ||
sc.nextLine(); | ||
break; | ||
default: | ||
sc.nextLine(); | ||
throw new ChatBotException("I am sorry, but I don't " + | ||
"understand this command"); | ||
} | ||
|
||
} catch (ChatBotException e) { | ||
|
||
chatBot.echo(e.getMessage()); | ||
} | ||
|
||
} | ||
|
||
} catch(FileNotFoundException e) { | ||
chatBot.echo(e.getMessage() + "\n Creating new file"); | ||
try { | ||
taskStorage.createDataFile(); | ||
} catch (IOException ioe) { | ||
chatBot.echo(e.getMessage() + "Couldn't create new file"); | ||
} | ||
} | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
import java.time.LocalDateTime; | ||
import java.time.format.DateTimeFormatter; | ||
|
||
|
||
class Events extends Task { | ||
|
||
private LocalDateTime eventTime; | ||
|
||
Events(String description, String eventTime) { | ||
|
||
super(description, false); | ||
this.eventTime = LocalDateTime.parse(eventTime, | ||
DateTimeFormatter.ofPattern("[d/M/y HHmm]"));; | ||
} | ||
|
||
Events(String description, boolean isDone, String eventTime) { | ||
|
||
super(description, isDone); | ||
this.eventTime = LocalDateTime.parse(eventTime, | ||
DateTimeFormatter.ofPattern("[d/M/y HHmm]"));; | ||
|
||
} | ||
|
||
@Override | ||
public String toFileString() { | ||
|
||
return "E | " + (this.isDone ? 1 : 0) + " | " + | ||
this.description + " | " + this.eventTime; | ||
} | ||
|
||
@Override | ||
public String toString() { | ||
|
||
return "[E]" + super.toString() + " (at: " + | ||
eventTime.format(DateTimeFormatter.ofPattern("MMM d yyyy")) | ||
+ ")"; | ||
} | ||
|
||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,87 @@ | ||
import java.io.File; | ||
import java.io.FileNotFoundException; | ||
import java.io.FileWriter; | ||
import java.io.IOException; | ||
import java.util.ArrayList; | ||
import java.util.Date; | ||
import java.util.Scanner; | ||
|
||
class Storage { | ||
|
||
private final String DATA_FILEPATH = "ip/src/main/data/duke.txt"; | ||
|
||
|
||
public void createDataFile() throws IOException { | ||
|
||
String[] dataPathList = DATA_FILEPATH.split("/"); | ||
String currentDirectory = ""; | ||
|
||
for(int i = 0; i < dataPathList.length - 1; i++) { | ||
|
||
currentDirectory += dataPathList[i]; | ||
File directory = new File(currentDirectory); | ||
|
||
if (!directory.exists()) { | ||
directory.mkdir(); | ||
} | ||
|
||
currentDirectory += "/"; | ||
|
||
} | ||
|
||
File newFile = new File(DATA_FILEPATH); | ||
newFile.createNewFile(); | ||
} | ||
|
||
public ArrayList<Task> convertToTaskList() throws FileNotFoundException { | ||
|
||
ArrayList<Task> taskList = new ArrayList<Task>(); | ||
File file = new File(DATA_FILEPATH); | ||
Scanner sc = new Scanner(file); | ||
|
||
while(sc.hasNextLine()) { | ||
|
||
String task = sc.nextLine(); | ||
char type = task.charAt(0); | ||
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.
|
||
boolean isDone = task.charAt(1) == '1'; | ||
String[] description = task.substring(2).split("\\|"); | ||
|
||
switch(type){ | ||
|
||
case 'T': | ||
taskList.add(new ToDo(description[0], isDone)); | ||
break; | ||
case 'D': | ||
taskList.add(new Deadline(description[0], isDone, description[1])); | ||
break; | ||
case 'E': | ||
taskList.add(new Events(description[0], isDone, description[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. Similarly, you could abstract out
before passing them into the respective Constructors. |
||
break; | ||
default: | ||
break; | ||
} | ||
|
||
} | ||
|
||
return taskList; | ||
|
||
} | ||
|
||
public void saveTaskList(ArrayList<Task> taskList) throws IOException { | ||
|
||
FileWriter fw = new FileWriter(DATA_FILEPATH); | ||
|
||
for(Task t : taskList) { | ||
|
||
fw.write(t.toFileString() + System.lineSeparator()); | ||
|
||
} | ||
|
||
fw.close(); | ||
|
||
} | ||
|
||
|
||
|
||
|
||
} |
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.
Nothing major, but I feel like naming it
botName
would be clearer to understand, since later on there will be many possible instances of "names of tasks" and such.