Skip to content

Commit

Permalink
A-MoreOOP
Browse files Browse the repository at this point in the history
  • Loading branch information
zoebelle-pang committed Feb 22, 2024
1 parent 29f9d66 commit 94df8a9
Showing 17 changed files with 270 additions and 167 deletions.
5 changes: 1 addition & 4 deletions data/duke.txt
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 modified src/main/java/Deadline.class
Binary file not shown.
15 changes: 7 additions & 8 deletions src/main/java/Deadline.java
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 modified src/main/java/Duke.class
Binary file not shown.
179 changes: 25 additions & 154 deletions src/main/java/Duke.java
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 modified src/main/java/Event.class
Binary file not shown.
2 changes: 1 addition & 1 deletion src/main/java/Event.java
Original file line number Diff line number Diff line change
@@ -19,6 +19,6 @@ public String toString() {
String formattedFrom = from.format(DateTimeFormatter.ofPattern("MMM dd yyyy hh:mma"));
String formattedTo = to.format(DateTimeFormatter.ofPattern("MMM dd yyyy hh:mma"));

return "E |" + super.toString() + " |" + " from: " + formattedFrom + " to: " + formattedTo ;
return "E " + super.toString() + " |" + " from: " + formattedFrom + " to: " + formattedTo ;
}
}
Binary file added src/main/java/Parser.class
Binary file not shown.
30 changes: 30 additions & 0 deletions src/main/java/Parser.java
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 added src/main/java/Storage.class
Binary file not shown.
44 changes: 44 additions & 0 deletions src/main/java/Storage.java
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 modified src/main/java/Task.class
Binary file not shown.
17 changes: 17 additions & 0 deletions src/main/java/Task.java
Original file line number Diff line number Diff line change
@@ -1,13 +1,29 @@
// Task.java
public class Task {
private static int nextTaskNumber = 1;

private int taskNumber;
private String taskDescription;
private boolean isDone;

public Task(String taskDescription) {
// Check if the task is newly created or loaded from storage
if (nextTaskNumber == 1) {
// Newly created task, assign the next task number
this.taskNumber = nextTaskNumber++;
} else {
// Loaded from storage, keep the existing task number
this.taskNumber = nextTaskNumber;
}

this.taskDescription = taskDescription;
this.isDone = false;
}

public static void resetNextTaskNumber() {
nextTaskNumber = 1;
}

public void markAsDone() {
this.isDone = true;
}
@@ -16,6 +32,7 @@ public void markAsUndone() {
this.isDone = false;
}

@Override
public String toString() {
return "|" + (isDone ? " 1 " : " 0 ") + "| " + taskDescription;
}
Binary file added src/main/java/TaskList.class
Binary file not shown.
Loading

0 comments on commit 94df8a9

Please sign in to comment.