-
Notifications
You must be signed in to change notification settings - Fork 435
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
[Zoebelle Pang] iP #470
base: master
Are you sure you want to change the base?
[Zoebelle Pang] iP #470
Changes from 14 commits
55f9f9f
f837ddb
a6f7324
4abe831
9fd519f
4f9805f
f76e20a
23f0151
bf00c0d
d42cf5f
ce111a4
b957fc9
fda81d8
c3c4b8d
f61db8c
1dda06a
4a85b91
c0bd29b
4e3d1c4
29f9d66
94df8a9
cbf8106
1f309a6
235599e
341f3ef
7286517
5be57a7
b107abc
ee6642a
44bbf68
6e2f711
9ed10ba
db2313f
84c0386
020e951
92432c2
8aec68d
dcf8f47
0aa73e2
eb9f02b
a8afb71
6f8b889
7c3cbc2
3f29f12
0512cb1
9e1104f
eef2934
2360d62
9871801
218b8cc
fefb08a
337580b
555392f
1900564
5163f4a
0840c85
8330255
59064ef
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 | borrow book | ||
D || 0 | return book | June 6th | ||
E || 0 | project meeting | Mon 2-4pm | ||
T || 1 | join sports club |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,10 +1,228 @@ | ||
import java.io.File; | ||
import java.io.FileNotFoundException; | ||
import java.util.ArrayList; | ||
import java.util.Scanner; | ||
import java.io.FileWriter; | ||
import java.io.IOException; | ||
|
||
class Task { | ||
private String taskDescription; | ||
private boolean isDone; | ||
|
||
public Task(String taskDescription) { | ||
this.taskDescription = taskDescription; | ||
this.isDone = 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 noticed the same issue in other places too. |
||
} | ||
|
||
public void markAsDone() { | ||
this.isDone = 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. Should the |
||
} | ||
|
||
public void markAsUndone() { | ||
this.isDone = 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. Similar to the previous comment, should the |
||
} | ||
|
||
public String toString() { | ||
return "|" + (isDone ? " 1 " : " 0 ") + "| " + taskDescription; | ||
} | ||
} | ||
|
||
class Todo extends Task { | ||
public Todo(String description) { | ||
super(description); | ||
} | ||
|
||
@Override | ||
public String toString() { | ||
return "T |" + super.toString(); | ||
} | ||
} | ||
|
||
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 ; | ||
} | ||
} | ||
|
||
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; | ||
} | ||
|
||
@Override | ||
public String toString() { | ||
return "E |" + super.toString() + " | " + from + "-" + to ; | ||
} | ||
} | ||
|
||
public class Duke { | ||
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 change the class name to your bot's name? |
||
public static void main(String[] args) { | ||
String logo = " ____ _ \n" | ||
+ "| _ \\ _ _| | _____ \n" | ||
+ "| | | | | | | |/ / _ \\\n" | ||
+ "| |_| | |_| | < __/\n" | ||
+ "|____/ \\__,_|_|\\_\\___|\n"; | ||
System.out.println("Hello from\n" + logo); | ||
File file = new File("data/duke.txt"); | ||
try { | ||
if (!file.exists()) { | ||
file.createNewFile(); | ||
} | ||
} catch (IOException e) { | ||
System.out.println("Error creating file: " + e.getMessage()); | ||
} | ||
|
||
Scanner scanner = new Scanner(System.in); | ||
ArrayList<Task> tasks = new ArrayList<>(); | ||
|
||
// Load existing tasks from file | ||
loadTasksFromFile("data/duke.txt", tasks); | ||
|
||
System.out.println("Hello! I'm Bentley\n" + "What can I do for you?\n"); | ||
|
||
while (true) { | ||
try { | ||
String userInput = scanner.nextLine(); | ||
|
||
if (userInput.equals("Bye")) { | ||
System.out.println("Bye. Hope to see you again soon!"); | ||
break; | ||
} else if (userInput.equals("List")) { | ||
|
||
for (int i = 0; i < tasks.size(); i++) { | ||
System.out.println((i + 1) + ". " + tasks.get(i)); | ||
} | ||
|
||
} else if (userInput.startsWith("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. I have only highlighted a small portion but good job on using K&R style brackets (aka Egyptian style) throughout your code. |
||
if (userInput.length() <= 5) { | ||
throw new IllegalArgumentException("looks like something is missing (description/ Deadline)"); | ||
} | ||
String description = userInput.substring(5).trim(); | ||
if (description.isEmpty()) { | ||
throw new IllegalArgumentException("looks like the description is missing"); | ||
} | ||
tasks.add(new Todo(description)); | ||
System.out.println("Got it. I've added this task:"); | ||
System.out.println(" " + tasks.get(tasks.size() - 1)); | ||
System.out.println("Now you have " + tasks.size() + " tasks in the list."); | ||
|
||
} else if (userInput.startsWith("deadline")) { | ||
if (userInput.length() <= 9) { | ||
throw new IllegalArgumentException("looks like something is missing (description/ Deadline)"); | ||
} | ||
String[] parts = userInput.substring(9).split("/by"); | ||
String description = parts[0].trim(); | ||
String by = parts[1].trim(); | ||
if (description.isEmpty() || by.isEmpty()) { | ||
throw new IllegalArgumentException("looks like something is missing (description/ Deadline)"); | ||
} | ||
tasks.add(new Deadline(description, by)); | ||
System.out.println("Got it. I've added this task:"); | ||
System.out.println(" " + tasks.get(tasks.size() - 1)); | ||
System.out.println("Now you have " + tasks.size() + " tasks in the list."); | ||
|
||
} else if (userInput.startsWith("event")) { | ||
if (userInput.length() <= 6) { | ||
throw new IllegalArgumentException("looks like something is missing (description/ Deadline)"); | ||
} | ||
String[] parts = userInput.substring(6).split("/from"); | ||
String description = parts[0].trim(); | ||
String[] eventParts = parts[1].trim().split("/to"); | ||
String from = eventParts[0].trim(); | ||
String to = eventParts[1].trim(); | ||
if (description.isEmpty() || from.isEmpty() || to.isEmpty()) { | ||
throw new IllegalArgumentException( | ||
"looks like something is missing (description/ start date/ end date)"); | ||
} | ||
tasks.add(new Event(description, from, to)); | ||
System.out.println("Got it. I've added this task:"); | ||
System.out.println(" " + tasks.get(tasks.size() - 1)); | ||
System.out.println("Now you have " + tasks.size() + " tasks in the list."); | ||
|
||
} else if (userInput.startsWith("mark")) { | ||
System.out.println(" Nice! I've marked this task as done:"); | ||
int taskNumber = Integer.parseInt(userInput.split(" ")[1]); | ||
tasks.get(taskNumber - 1).markAsDone(); | ||
|
||
for (int i = 0; i < tasks.size(); i++) { | ||
System.out.println((i + 1) + ". " + tasks.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. I liked how you have added white spaces within the statements according to the Java Coding Standard. |
||
} | ||
|
||
} else if (userInput.startsWith("unmark")) { | ||
System.out.println(" OK, I've marked this task as not done yet:"); | ||
int taskNumber = Integer.parseInt(userInput.split(" ")[1]); | ||
tasks.get(taskNumber - 1).markAsUndone(); | ||
|
||
for (int i = 0; i < tasks.size(); i++) { | ||
System.out.println((i + 1) + ". " + tasks.get(i)); | ||
} | ||
|
||
} else if (userInput.startsWith("delete")) { | ||
int taskNumber = Integer.parseInt(userInput.split(" ")[1]); | ||
if (taskNumber > 0 && taskNumber <= tasks.size()) { | ||
Task removedTask = tasks.remove(taskNumber - 1); | ||
System.out.println("Noted. I've removed this task:"); | ||
System.out.println(" " + removedTask); | ||
System.out.println("Now you have " + tasks.size() + " tasks in the list."); | ||
} else { | ||
System.out.println("Invalid task number. Please provide a valid task number."); | ||
} | ||
|
||
} else { | ||
System.out.println("please input a valid task code"); | ||
} | ||
writeTasksToFile("data/duke.txt", tasks); | ||
} catch (IllegalArgumentException e) { | ||
System.out.println(e.getMessage()); | ||
} | ||
} | ||
|
||
try { | ||
FileWriter writer = new FileWriter("data/duke.txt"); | ||
for (Task task : tasks) { | ||
writer.write(task.toString() + "\n"); | ||
} | ||
writer.close(); | ||
System.out.println("Tasks written to file successfully!"); | ||
} catch (IOException e) { | ||
e.printStackTrace(); | ||
} | ||
|
||
scanner.close(); | ||
} | ||
// Method to load tasks from file | ||
private static void loadTasksFromFile(String fileName, ArrayList<Task> tasks) { | ||
try { | ||
File file = new File(fileName); | ||
Scanner scanner = new Scanner(file); | ||
while (scanner.hasNextLine()) { | ||
String taskDescription = scanner.nextLine(); | ||
tasks.add(new Task(taskDescription)); | ||
} | ||
scanner.close(); | ||
} catch (FileNotFoundException e) { | ||
System.out.println("File not found: " + e.getMessage()); | ||
} | ||
} | ||
|
||
// Method to write tasks to file | ||
private static void writeTasksToFile(String fileName, ArrayList<Task> tasks) { | ||
try { | ||
FileWriter writer = new FileWriter(fileName); | ||
for (Task task : tasks) { | ||
writer.write(task.toString() + "\n"); | ||
} | ||
writer.close(); | ||
System.out.println("Tasks written to file successfully!"); | ||
} catch (IOException e) { | ||
e.printStackTrace(); | ||
} | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -5,3 +5,4 @@ Hello from | |
| |_| | |_| | < __/ | ||
|____/ \__,_|_|\_\___| | ||
|
||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
diff a/text-ui-test/EXPECTED.TXT b/text-ui-test/EXPECTED.TXT (rejected hunks) | ||
@@ -1,7 +1,3 @@ | ||
-Hello from | ||
- ____ _ | ||
-| _ \ _ _| | _____ | ||
-| | | | | | | |/ / _ \ | ||
-| |_| | |_| | < __/ | ||
-|____/ \__,_|_|\_\___| | ||
+Hello! I'm Bentley | ||
+What can I do for you? | ||
|
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 it should be while the input is not equal to "bye", rather than while(true)?