-
Notifications
You must be signed in to change notification settings - Fork 270
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
[KristopherPTaslim] iP #287
base: master
Are you sure you want to change the base?
Changes from 11 commits
d839859
5406049
0dd972e
1812262
277c43f
73b1d1c
d619a83
318b3aa
32bb23a
bc9608c
e754bcb
b37f27b
83bb0f3
27fc406
589b161
e8b6b2a
28a85db
7bbe7be
7b43639
bbc3057
6ea1b8c
3d551ba
452a916
aa60d80
6b75a3c
725a458
fd68d86
80a985b
4524860
adc7846
c3d28a2
97d12b1
1dfa71f
c96d62b
36ccfcc
99300b0
7e5a187
cc38eb8
1a329c8
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,12 @@ | ||
class ChatBot { | ||
private final String input; | ||
|
||
ChatBot(String input) { | ||
this.input = input; | ||
} | ||
|
||
public String toString() { | ||
return "added: " + this.input; | ||
} | ||
|
||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
class Deadline extends Task { | ||
|
||
protected String by; | ||
|
||
public Deadline(String description, String by) { | ||
super(description); | ||
this.by = by; | ||
} | ||
|
||
@Override | ||
public String toString() { | ||
return "[D]" + super.toString() + "(by: " + by + ")"; | ||
} | ||
|
||
@Override | ||
public String getInitial() { | ||
return "[D]"; | ||
} | ||
} |
Original file line number | Diff line number | Diff line change | ||||
---|---|---|---|---|---|---|
@@ -1,10 +1,166 @@ | ||||||
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.util.ArrayList; | ||||||
import java.util.Scanner; | ||||||
import java.io.IOException; | ||||||
import java.time.LocalDateTime; | ||||||
import java.time.format.DateTimeFormatter; | ||||||
|
||||||
class Duke { | ||||||
|
||||||
public static void main(String[] args) throws DukeException{ | ||||||
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. You might need to leave a space after DukeException
Suggested change
|
||||||
Scanner sc = new Scanner(System.in); | ||||||
FileClass fc = new FileClass(); //file class | ||||||
String home = System.getProperty("user.home"); //home directory | ||||||
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 make comments behind the lines! It makes the logic clear 😄 |
||||||
|
||||||
String filePath = home + "/Desktop/ip/duke.txt"; | ||||||
System.out.println(filePath); | ||||||
fc.createFile(filePath); | ||||||
|
||||||
ArrayList<Task> taskArray = new ArrayList<Task>(); | ||||||
ArrayList<String> stringArray = new ArrayList<String>(); | ||||||
|
||||||
String beginning = "Hello! I'm Duke\n" + | ||||||
"What can I do for you?"; | ||||||
System.out.println(beginning); | ||||||
|
||||||
String input = sc.nextLine(); | ||||||
|
||||||
while (!input.equals("bye")) { | ||||||
String[] checkType = input.split(" "); | ||||||
String messageTask = "Got it. I've added this task: \n"; | ||||||
|
||||||
try { | ||||||
if (input.equals("list")) { | ||||||
|
||||||
if (taskArray.isEmpty()) { | ||||||
System.out.println("The list is empty"); | ||||||
} else { | ||||||
System.out.println("Here are the tasks in your list:"); | ||||||
for (int i = 0; i < taskArray.size(); i++) { | ||||||
String index = String.valueOf(i + 1); | ||||||
System.out.println(index + "." + taskArray.get(i)); | ||||||
} | ||||||
} | ||||||
} else if (checkType[0].equals("mark") || checkType[0].equals("unmark")) { //this is to check whether it goes through marking | ||||||
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 think this line has more than the 120 character line limit allowed by the coding standard. Maybe you could add a line break to improve readability |
||||||
|
||||||
try { | ||||||
int index = Integer.parseInt(checkType[1]) - 1; | ||||||
Task tasks = taskArray.get(index); | ||||||
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 change the name of this variable to |
||||||
System.out.println(tasks.marking(checkType[0])); | ||||||
taskArray.set(index, tasks); | ||||||
} catch (ArrayIndexOutOfBoundsException e) { | ||||||
System.out.println("The input is not valid :("); | ||||||
} | ||||||
|
||||||
|
||||||
} else if (checkType[0].equals("todo")) { //check todo | ||||||
|
||||||
String toDoCondition = "todo "; | ||||||
|
||||||
try { | ||||||
int indexOfToDo = toDoCondition.length(); //to find todo | ||||||
String stringSliced = input.substring(indexOfToDo,input.length()); | ||||||
stringArray.add(stringSliced); | ||||||
Todo todoTask = new Todo(stringSliced); | ||||||
taskArray.add(todoTask); | ||||||
String noOfTask = String.valueOf(taskArray.size()); | ||||||
System.out.println(messageTask + todoTask.toString() + "\n" | ||||||
+ "Now you have " + noOfTask + " tasks in the list."); | ||||||
|
||||||
} catch (StringIndexOutOfBoundsException e) { | ||||||
System.out.println("☹ OOPS!!! The description of a todo cannot be empty."); | ||||||
} | ||||||
|
||||||
|
||||||
} else if (checkType[0].equals("deadline")) { //check deadline | ||||||
|
||||||
String deadlineCondition = "/by "; | ||||||
|
||||||
try { | ||||||
int indexOfTime = input.indexOf(deadlineCondition); //to find / | ||||||
String dateTime = input.substring(indexOfTime + deadlineCondition.length(), input.length()); // the date and time for by | ||||||
//convert to the correct one | ||||||
LocalDateTime deadlineTime = LocalDateTime.parse(dateTime, DateTimeFormatter.ofPattern("d/M/y Hmm")); | ||||||
String convertedTime = deadlineTime.format(DateTimeFormatter.ofPattern("MMM d yyyy hh:mm a")); | ||||||
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 variables :), it is very clear what they all do 👍 |
||||||
String stringSliced = input.substring(9, indexOfTime); // after deadline | ||||||
stringArray.add(stringSliced); | ||||||
Deadline deadlineTask = new Deadline(stringSliced, convertedTime); | ||||||
taskArray.add(deadlineTask); | ||||||
String noOfTask = String.valueOf(taskArray.size()); | ||||||
System.out.println(messageTask + deadlineTask.toString() + "\n" | ||||||
+ "Now you have " + noOfTask + " tasks in the list."); | ||||||
} catch (StringIndexOutOfBoundsException e) { | ||||||
System.out.println("☹ OOPS!!! The description of a deadline cannot be empty."); | ||||||
} | ||||||
|
||||||
} else if (checkType[0].equals("event")) { // check event | ||||||
|
||||||
String eventCondition = "/at "; | ||||||
try { | ||||||
int indexOfTime = input.indexOf(eventCondition); //to find / | ||||||
String dateTime = input.substring(indexOfTime + eventCondition.length(), input.length()); // the date and time for by | ||||||
//convert to the correct one | ||||||
LocalDateTime eventTime = LocalDateTime.parse(dateTime, DateTimeFormatter.ofPattern("d/M/y Hmm")); | ||||||
String convertedTime = eventTime.format(DateTimeFormatter.ofPattern("MMM d yyyy hh:mm a")); | ||||||
String stringSliced = input.substring(6, indexOfTime); // after deadline | ||||||
stringArray.add(stringSliced); | ||||||
Event eventTask = new Event(stringSliced, convertedTime); | ||||||
taskArray.add(eventTask); | ||||||
String noOfTask = String.valueOf(taskArray.size()); | ||||||
System.out.println(messageTask + eventTask.toString() + "\n" | ||||||
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. Great use of comments to clarify what each line is doing! Maybe you could also use a few blank lines to separate the code block into logical sections (such as formatting, processing and output.) The coding standard recommends that "Logical units within a block should be separated by one blank line." |
||||||
+ "Now you have " + noOfTask + " tasks in the list."); | ||||||
} catch (StringIndexOutOfBoundsException e) { | ||||||
System.out.println("☹ OOPS!!! The description of a event cannot be empty."); | ||||||
} | ||||||
} else if (checkType[0].equals("delete")) { | ||||||
|
||||||
try { | ||||||
int index = Integer.parseInt(checkType[1]) - 1; | ||||||
String toBeRemoved = taskArray.get(index).toString(); | ||||||
taskArray.remove(index); | ||||||
String noOfTask = String.valueOf(taskArray.size()); | ||||||
String messageDeleted = "Noted. I've removed this task: \n" + | ||||||
toBeRemoved + "\n" + | ||||||
"Now you have " + noOfTask + " tasks in the list."; | ||||||
System.out.println(messageDeleted); | ||||||
|
||||||
} catch (ArrayIndexOutOfBoundsException e) { | ||||||
System.out.println("There are no tasks to be deleted!"); | ||||||
} | ||||||
} else { | ||||||
throw new DukeException("☹ OOPS!!! I'm sorry, but I don't know what that means :-("); | ||||||
} | ||||||
} catch (DukeException e) { | ||||||
System.out.println("☹ OOPS!!! I'm sorry, but I don't know what that means :-("); | ||||||
} | ||||||
|
||||||
input = sc.nextLine(); | ||||||
|
||||||
} | ||||||
|
||||||
// Recording everything in the duke.txt | ||||||
for (int i = 0; i < taskArray.size(); i++ ) { | ||||||
Task tasks = taskArray.get(i); | ||||||
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 it is referring to just 1 task, will it be better to name this variable as 'task' instead of 'tasks'? |
||||||
try { | ||||||
String firstInitial = tasks.getInitial() ; //first initial character | ||||||
String textToAdd = firstInitial + " | " + tasks.getStatusIcon() + " | " | ||||||
+ stringArray.get(i); | ||||||
fc.writeFile(filePath, textToAdd); | ||||||
} catch (IOException e) { | ||||||
System.out.println("File not found"); | ||||||
} | ||||||
|
||||||
} | ||||||
|
||||||
String ending = "Bye. Hope to see you again soon!"; | ||||||
|
||||||
System.out.println(ending); | ||||||
sc.close(); | ||||||
} | ||||||
} | ||||||
|
||||||
|
||||||
|
||||||
|
||||||
|
||||||
|
||||||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
class DukeException extends RuntimeException { | ||
|
||
public DukeException(String Message) { | ||
super(Message); | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
class Event extends Task { | ||
|
||
protected String at; | ||
|
||
public Event(String description, String at) { | ||
super(description); | ||
this.at = at; | ||
} | ||
|
||
@Override | ||
public String toString() { | ||
return "[E]" + super.toString() + "(at: " + at + ")"; | ||
} | ||
|
||
@Override | ||
public String getInitial() { | ||
return "[E]"; | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
import java.io.File; | ||
import java.io.IOException; | ||
import java.io.FileWriter; | ||
|
||
class FileClass { | ||
|
||
public void createFile(String filePath) { | ||
File f = new File(filePath); //initialise the file | ||
if (!f.exists()) { //meaning f doesnt exist | ||
try { | ||
f.createNewFile(); | ||
} catch (IOException e) { | ||
System.out.println("Path Directory is invalid!"); | ||
} | ||
} | ||
} | ||
//taken from W3.3c File Access | ||
public void writeFile(String filePath, String textToAdd) throws IOException { | ||
FileWriter fw = new FileWriter(filePath, true); // initialise the filewriter | ||
fw.write(textToAdd + "\r\n"); | ||
fw.close(); | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
import java.util.ArrayList; | ||
|
||
public class Task { | ||
protected String description; | ||
protected ArrayList<Boolean> isDone; | ||
|
||
public Task(String description) { | ||
this.description = description; | ||
this.isDone = new ArrayList<Boolean>(); | ||
isDone.add(false); | ||
} | ||
|
||
public String getStatusIcon() { | ||
return (isDone.get(0) ? "[X]" : "[ ]"); // mark done task with X // if done is "X" then " " | ||
} | ||
|
||
public void setAsDone() { | ||
isDone.set(0, true); | ||
} | ||
|
||
public void setAsUndone() { | ||
isDone.set(0, false); | ||
} | ||
|
||
public String marking(String mark) { | ||
if (mark.equals("mark")) { | ||
setAsDone(); | ||
String messageMarked = "Nice! I've marked this task as done: \n" + | ||
this.toString(); | ||
return messageMarked; | ||
} else { | ||
setAsUndone(); | ||
String messageUnmarked = "OK, I've marked this task as not done yet: \n" + | ||
this.toString(); | ||
return messageUnmarked; | ||
|
||
} | ||
} | ||
|
||
public String toString() { | ||
return getStatusIcon() + " " + this.description; | ||
} | ||
|
||
public String getInitial(){ | ||
return "Task"; | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
class Todo extends Task { | ||
|
||
public Todo(String description) { | ||
super(description); | ||
} | ||
|
||
@Override | ||
public String toString() { | ||
return "[T]" + super.toString(); | ||
} | ||
|
||
@Override | ||
public String getInitial() { | ||
return "[T]"; | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,7 +1,16 @@ | ||
Hello from | ||
____ _ | ||
| _ \ _ _| | _____ | ||
| | | | | | | |/ / _ \ | ||
| |_| | |_| | < __/ | ||
|____/ \__,_|_|\_\___| | ||
|
||
Hello! I'm Duke | ||
What can I do for you? | ||
Got it. I've added this task: | ||
[T][ ] read book | ||
Now you have 1 tasks in the list. | ||
Got it. I've added this task: | ||
[T][ ] buy bread | ||
Now you have 2 tasks in the list. | ||
Got it. I've added this task: | ||
[D][ ] do homework (by: 3rd Jan) | ||
Now you have 3 tasks in the list. | ||
Here are the tasks in your list: | ||
1.[T][ ] read book | ||
2.[T][ ] buy bread | ||
3.[D][ ] do homework (by: 3rd Jan) | ||
Bye. Hope to see you again soon! |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
todo read book | ||
todo buy bread | ||
deadline do homework /by 3rd Jan | ||
list | ||
bye |
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.
Following the coding standards, you might need to arrange the import statements into alphabetical order! :)
Same for some of the other classes that includes import statements.