-
Notifications
You must be signed in to change notification settings - Fork 482
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
[Tyrus Lye]iP #557
base: master
Are you sure you want to change the base?
[Tyrus Lye]iP #557
Changes from 11 commits
28ad2b8
ed6d4d2
8664014
892b284
3f22749
bfe5fea
12b399e
9a3f089
3f9bc73
c8920e9
937f61c
f0cc53b
5310a44
1579d0c
6ff52fa
22c309f
bc05937
918f5c8
47a247e
ccd2127
724286a
d9e7d80
25866f1
904d88d
56b0750
0b4d08c
7273363
2da97b5
4f32000
54290c6
c028425
e73f390
4d45b43
3c69f79
0efd428
70cbc5f
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 |
---|---|---|
@@ -1,10 +1,272 @@ | ||
import java.time.LocalDate; | ||
import java.time.LocalDateTime; | ||
import java.time.format.DateTimeFormatter; | ||
import java.util.Scanner; | ||
import java.util.ArrayList; | ||
import java.io.*; | ||
|
||
public class Duke { | ||
private static final String FILE_PATH = "src/main/java/tasks.txt"; | ||
public static void main(String[] args) { | ||
String logo = " ____ _ \n" | ||
+ "| _ \\ _ _| | _____ \n" | ||
+ "| | | | | | | |/ / _ \\\n" | ||
+ "| |_| | |_| | < __/\n" | ||
+ "|____/ \\__,_|_|\\_\\___|\n"; | ||
Scanner scanner = new Scanner(System.in); | ||
ArrayList<Task> tasks = new ArrayList<>(100); | ||
loadTasksFromFile(tasks); | ||
String logo = "██╗░░░██╗██████╗░██████╗░░█████╗░██╗\n" | ||
+ "██║░░░██║██╔══██╗██╔══██╗██╔══██╗██║\n" | ||
+ "██║░░░██║██████╔╝██████╦╝██║░░██║██║\n" | ||
+ "██║░░░██║██╔══██╗██╔══██╗██║░░██║██║\n" | ||
+ "╚██████╔╝██║░░██║██████╦╝╚█████╔╝██║\n" | ||
+ "░╚═════╝░╚═╝░░╚═╝╚═════╝░░╚════╝░╚═╝\n"; | ||
System.out.println("Hello from\n" + logo); | ||
System.out.println("What can I do for you?"); | ||
System.out.println("____________________________________________________________"); | ||
|
||
while (true) { | ||
String command = scanner.nextLine(); | ||
System.out.println("____________________________________________________________"); | ||
try{ | ||
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. would it be better to use enumerates for the commands? |
||
System.out.println("Bye. Hope to see you again soon!"); | ||
break; | ||
} else if (command.equalsIgnoreCase("list")) { | ||
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 it will be better if this part is separated into different functions like addToDoTask, etc. to improve readability, what do you think? |
||
System.out.println("Here are the tasks in your list:"); | ||
for (int i = 0; i < tasks.size(); i++) { | ||
System.out.println((i + 1) + ". " + tasks.get(i)); | ||
} | ||
} else if (command.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. would it be better to package some of these if, else if else steps into a function that handles everything within the outer most if case? for eg. a function to handle creating todo here. |
||
String description = command.substring(5).trim(); | ||
if (description.isEmpty()) { | ||
throw new DukeException("The description of a todo cannot be empty."); | ||
} | ||
tasks.add(new Todo(description)); | ||
System.out.println("Got it. I've added this task:\n " + tasks.get(tasks.size() - 1)); | ||
System.out.println("Now you have " + tasks.size() + " tasks in the list."); | ||
} else if (command.startsWith("deadline")) { | ||
// Parse the date and time in the format d/M/yyyy HHmm | ||
String[] parts = command.split(" /by "); | ||
if (parts.length < 2) { | ||
throw new DukeException("Deadline command must include a date."); | ||
} | ||
String description = parts[0].substring(9).trim(); | ||
LocalDateTime dateTime = LocalDateTime.parse(parts[1], DateTimeFormatter.ofPattern("d/M/yyyy HHmm")); | ||
|
||
tasks.add(new Deadline(description, dateTime)); | ||
System.out.println("Got it. I've added this task:\n " + tasks.get(tasks.size() - 1)); | ||
System.out.println("Now you have " + tasks.size() + " tasks in the list."); | ||
} else if (command.startsWith("event")) { | ||
String description = command.substring(6, command.indexOf("/from")).trim(); | ||
String from = command.substring(command.indexOf("/from") + 6, command.indexOf("/to")).trim(); | ||
String to = command.substring(command.indexOf("/to") + 4).trim(); | ||
tasks.add(new Event(description, from, to)); | ||
System.out.println("Got it. I've added this task:\n " + tasks.get(tasks.size() - 1)); | ||
System.out.println("Now you have " + tasks.size() + " tasks in the list."); | ||
} else if (command.startsWith("mark")) { | ||
int index = Integer.parseInt(command.split(" ")[1]) - 1; | ||
if (index >= 0 && index < tasks.size()) { | ||
tasks.get(index).markDone(); | ||
System.out.println("Nice! I've marked this task as done:\n " + tasks.get(index)); | ||
} else { | ||
System.out.println("Invalid task 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. would it be useful if instead of printing invalid task index, the size of the tasklist is also returned in this else case to let user know that for example there is only 3 tasks in the tasklist thats why their input 4 is invalid. Something like "Invalid task index, the task list has " + tasklist.size() + "tasks. " |
||
} | ||
} else if (command.startsWith("unmark")) { | ||
int index = Integer.parseInt(command.split(" ")[1]) - 1; | ||
if (index >= 0 && index < tasks.size()) { | ||
tasks.get(index).markNotDone(); | ||
System.out.println("OK, I've marked this task as not done yet:\n " + tasks.get(index)); | ||
} else { | ||
System.out.println("Invalid task index."); | ||
} | ||
} else if (command.startsWith("delete")) { | ||
int index = Integer.parseInt(command.split(" ")[1]) - 1; | ||
if (index >= 0 && index < tasks.size()) { | ||
Task removedTask = tasks.remove(index); | ||
System.out.println("Noted. I've removed this task:\n " + removedTask); | ||
System.out.println("Now you have " + tasks.size() + " tasks in the list."); | ||
} else { | ||
System.out.println("Invalid task index."); | ||
} | ||
} else { | ||
throw new DukeException("I'm sorry, but I don't know what that means :-("); | ||
} | ||
saveTasksToFile(tasks); | ||
} | ||
catch (DukeException e) { | ||
System.out.println("☹ OOPS!!! " + e.getMessage()); | ||
} | ||
|
||
System.out.println("____________________________________________________________"); | ||
} | ||
|
||
|
||
scanner.close(); | ||
} | ||
private static String formatDate(LocalDate date) { | ||
return date.format(DateTimeFormatter.ofPattern("MMM dd yyyy")); | ||
} | ||
private static void loadTasksFromFile(ArrayList<Task> tasks) { | ||
try { | ||
File file = new File(FILE_PATH); | ||
if (file.exists()) { | ||
Scanner fileScanner = new Scanner(file); | ||
while (fileScanner.hasNextLine()) { | ||
String line = fileScanner.nextLine(); | ||
Task task = createTaskFromLine(line); | ||
if (task != null) { | ||
tasks.add(task); | ||
} | ||
} | ||
fileScanner.close(); | ||
} | ||
} catch (FileNotFoundException e) { | ||
// Handle file not found exception | ||
System.out.println("File not found: " + FILE_PATH); | ||
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 that the handling of this exception not only tells that user that the file cannot be found but also returns the filepath |
||
} | ||
} | ||
|
||
private static Task createTaskFromLine(String line) { | ||
String[] parts = line.split(" \\| "); | ||
if (parts.length < 3) { | ||
return null; | ||
} | ||
|
||
String type = parts[0]; | ||
String status = parts[1]; | ||
String description = parts[2]; | ||
|
||
Task task = null; | ||
switch (type) { | ||
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. For switch statements, I think there shouldn't be indentation on the next line. On the mod website: |
||
case "T": | ||
task = new Todo(description); | ||
break; | ||
case "D": | ||
if (parts.length >= 4) { | ||
String by = parts[3]; | ||
task = new Deadline(description, LocalDateTime.parse(by, DateTimeFormatter.ofPattern("d/M/yyyy HHmm"))); | ||
} | ||
break; | ||
case "E": | ||
if (parts.length >= 5) { | ||
String from = parts[3]; | ||
String to = parts[4]; | ||
task = new Event(description, from, to); | ||
} | ||
break; | ||
} | ||
|
||
if (task != null) { | ||
if (status.equals("1")) { | ||
task.markDone(); | ||
} else { | ||
task.markNotDone(); | ||
} | ||
} | ||
|
||
return task; | ||
} | ||
|
||
|
||
private static void saveTasksToFile(ArrayList<Task> tasks) { | ||
try { | ||
FileWriter fileWriter = new FileWriter(FILE_PATH); | ||
for (Task task : tasks) { | ||
fileWriter.write(task.toFileString() + System.lineSeparator()); | ||
} | ||
fileWriter.close(); | ||
} catch (IOException e) { | ||
// Handle IO exception | ||
System.out.println("Error saving tasks to file: " + e.getMessage()); | ||
} | ||
} | ||
} | ||
|
||
class Task { | ||
protected String description; | ||
protected boolean isDone; | ||
|
||
protected LocalDateTime date; | ||
|
||
public String toFileString() { | ||
return ""; | ||
} | ||
public String formatDate() { | ||
return date.format(DateTimeFormatter.ofPattern("MMM dd yyyy")); | ||
} | ||
public Task(String description, LocalDateTime date) { | ||
this.description = description; | ||
this.isDone = false; | ||
this.date = date; | ||
} | ||
public String getStatusIcon() { | ||
return (isDone ? "[X]" : "[ ]"); // Return a tick or cross symbol cuz im lazy like that, or its easier. idk | ||
} | ||
public boolean isDone() { | ||
return isDone; | ||
} | ||
public void markDone() { | ||
isDone = true; | ||
} | ||
public void markNotDone() { | ||
isDone = false; | ||
} | ||
@Override | ||
public String toString() { | ||
return "[" + (isDone ? "X" : " ") + "] " + description; | ||
} | ||
} | ||
|
||
class Todo extends Task { | ||
public Todo(String description) { | ||
super(description,null); | ||
} | ||
@Override | ||
public String toFileString() { | ||
return "T | " + (isDone ? "1" : "0") + " | " + description; | ||
} | ||
|
||
@Override | ||
public String toString() { | ||
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. Clean and concise, good job! |
||
return "[T]" + super.toString(); | ||
} | ||
} | ||
|
||
class Deadline extends Task { | ||
protected String by; | ||
|
||
public Deadline(String description, LocalDateTime date) { | ||
super(description, date); | ||
} | ||
@Override | ||
public String toFileString() { | ||
return "D | " + (isDone ? "1" : "0") + " | " + description + " | " + date; | ||
} | ||
@Override | ||
public String toString() { | ||
return "[D]" + super.toString() + " (by: " + date + ")"; | ||
} | ||
} | ||
|
||
|
||
class Event extends Task { | ||
protected String from; | ||
protected String to; | ||
|
||
public Event(String description, String from, String to) { | ||
super(description,null); | ||
this.from = from; | ||
this.to = to; | ||
} | ||
@Override | ||
public String toFileString() { | ||
return "E | " + (isDone ? "1" : "0") + " | " + description + " | " + from + " | " + to; | ||
} | ||
@Override | ||
public String toString() { | ||
return "[E]" + super.toString() + " (from: " + from + " to: " + to + ")"; | ||
} | ||
} | ||
|
||
class DukeException extends Exception { | ||
public DukeException(String message) { | ||
super(message); | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
T | 0 | thing | ||
E | 0 | thing2 | now | later | ||
D | 0 | return book | 2019-12-02T18:00 |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,7 +1,5 @@ | ||
Hello from | ||
____ _ | ||
| _ \ _ _| | _____ | ||
| | | | | | | |/ / _ \ | ||
| |_| | |_| | < __/ | ||
|____/ \__,_|_|\_\___| | ||
|
||
____________________________________________________________ | ||
Got it. I've added this task: | ||
[D][ ] do homework (by: no idea :-p) | ||
Now you have 6 tasks in the list. | ||
____________________________________________________________ |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
deadline do homework /by no idea :-p |
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.
This is cool!!