forked from nus-cs2103-AY2324S2/ip
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
29f9d66
commit 94df8a9
Showing
17 changed files
with
270 additions
and
167 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1 @@ | ||
| 0 | T || 1 | read book | ||
| 0 | D || 0 | return book | Sep 15 2019 | ||
| 0 | E || 0 | project meeting | from: Sep 15 2019 06:00PM to: Sep 15 2019 07:00PM | ||
| 0 | T || 0 | join sport club | ||
T || 0 | homework |
Binary file not shown.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,20 +1,19 @@ | ||
// Deadline.java | ||
import java.time.LocalDate; | ||
import java.time.LocalDateTime; | ||
import java.time.format.DateTimeFormatter; | ||
|
||
public class Deadline extends Task { | ||
protected LocalDate by; | ||
|
||
protected LocalDateTime by; | ||
|
||
public Deadline(String description, String by) { | ||
super(description); | ||
|
||
this.by = LocalDate.parse(by, DateTimeFormatter.ofPattern("yyyy-MM-dd")); | ||
this.by = LocalDateTime.parse(by, DateTimeFormatter.ofPattern("yyyy-MM-dd HHmm")); | ||
} | ||
|
||
@Override | ||
public String toString() { | ||
|
||
String formattedDate = by.format(DateTimeFormatter.ofPattern("MMM dd yyyy")); | ||
return "D |" + super.toString() + " | " + formattedDate; | ||
return "D" + super.toString() + " by: " + by.format( | ||
DateTimeFormatter.ofPattern("MMM dd yyyy hh:mma") | ||
); | ||
} | ||
} |
Binary file not shown.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,172 +1,43 @@ | ||
import java.io.File; | ||
import java.io.FileNotFoundException; | ||
import java.time.LocalDate; | ||
import java.time.LocalDateTime; | ||
import java.time.format.DateTimeFormatter; | ||
import java.time.format.DateTimeParseException; | ||
import java.util.Locale; | ||
import java.util.ArrayList; | ||
import java.util.Scanner; | ||
import java.io.FileWriter; | ||
import java.io.IOException; | ||
|
||
|
||
public class Duke { | ||
public static void main(String[] args) { | ||
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"); | ||
private static Storage storage; | ||
private static TaskList taskList; | ||
private static Ui ui; | ||
|
||
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")) { | ||
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."); | ||
public static void main(String[] args) { | ||
|
||
} 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."); | ||
storage = new Storage("data/duke.txt"); | ||
taskList = new TaskList(); | ||
ui = new Ui(); | ||
|
||
} 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(); | ||
Task.resetNextTaskNumber(); | ||
|
||
for (int i = 0; i < tasks.size(); i++) { | ||
System.out.println((i + 1) + ". " + tasks.get(i)); | ||
} | ||
// Load existing tasks from file | ||
storage.loadTasks(taskList.getTasks()); | ||
|
||
} 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(); | ||
ui.showWelcomeMessage(); | ||
|
||
for (int i = 0; i < tasks.size(); i++) { | ||
System.out.println((i + 1) + ". " + tasks.get(i)); | ||
} | ||
Scanner scanner = new Scanner(System.in); | ||
|
||
} 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."); | ||
} | ||
try { | ||
while (true) { | ||
String userInput = scanner.nextLine(); | ||
|
||
if (userInput.equals("Bye")) { | ||
ui.showByeMessage(); | ||
storage.writeTasks(taskList.getTasks()); | ||
break; | ||
} else { | ||
System.out.println("please input a valid task code"); | ||
Parser.parseCommand(userInput, taskList, ui, storage); | ||
storage.writeTasks(taskList.getTasks()); | ||
} | ||
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(); | ||
} catch (IllegalArgumentException e) { | ||
System.out.println(e.getMessage()); | ||
} finally { | ||
scanner.close(); | ||
} | ||
} | ||
} |
Binary file not shown.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Binary file not shown.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
public class Parser { | ||
|
||
public static void parseCommand(String userInput, TaskList taskList, Ui ui, Storage storage) { | ||
try { | ||
if (userInput.equals("Bye")) { | ||
ui.showByeMessage(); | ||
storage.writeTasks(taskList.getTasks()); | ||
System.exit(0); | ||
} else if (userInput.equals("List")) { | ||
taskList.listTasks(); | ||
} else if (userInput.startsWith("todo")) { | ||
taskList.addTodoTask(userInput); | ||
} else if (userInput.startsWith("deadline")) { | ||
taskList.addDeadlineTask(userInput); | ||
} else if (userInput.startsWith("event")) { | ||
taskList.addEventTask(userInput); | ||
} else if (userInput.startsWith("mark")) { | ||
taskList.markAsDone(userInput); | ||
} else if (userInput.startsWith("unmark")) { | ||
taskList.markAsUndone(userInput); | ||
} else if (userInput.startsWith("delete")) { | ||
taskList.deleteTask(userInput); | ||
} else { | ||
System.out.println("Invalid command. Please input a valid command."); | ||
} | ||
} catch (IllegalArgumentException e) { | ||
System.out.println(e.getMessage()); | ||
} | ||
} | ||
} |
Binary file not shown.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
import java.io.File; | ||
import java.io.FileNotFoundException; | ||
import java.io.FileWriter; | ||
import java.io.IOException; | ||
import java.util.ArrayList; | ||
import java.util.Scanner; | ||
|
||
public class Storage { | ||
|
||
private String fileName; | ||
|
||
public Storage(String fileName) { | ||
this.fileName = fileName; | ||
} | ||
|
||
// Method to load tasks from file | ||
public void loadTasks(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 | ||
public void writeTasks(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(); | ||
} | ||
} | ||
} |
Binary file not shown.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Binary file not shown.
Oops, something went wrong.