Skip to content
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

[niha81] iP #317

Open
wants to merge 13 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
# Duke project template
# Woogie project template

This is a project template for a greenfield Java project. It's named after the Java mascot _Duke_. Given below are instructions on how to use it.

Expand All @@ -13,7 +13,7 @@ Prerequisites: JDK 17, update Intellij to the most recent version.
1. If there are any further prompts, accept the defaults.
1. Configure the project to use **JDK 17** (not other versions) as explained in [here](https://www.jetbrains.com/help/idea/sdk.html#set-up-jdk).<br>
In the same dialog, set the **Project language level** field to the `SDK default` option.
1. After that, locate the `src/main/java/Duke.java` file, right-click it, and choose `Run Duke.main()` (if the code editor is showing compile errors, try restarting the IDE). If the setup is correct, you should see something like the below as the output:
1. After that, locate the `src/main/java/Woogie.java` file, right-click it, and choose `Run Woogie.main()` (if the code editor is showing compile errors, try restarting the IDE). If the setup is correct, you should see something like the below as the output:
```
Hello from
____ _
Expand Down
3 changes: 3 additions & 0 deletions data/woogie.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
T | 0 | a
D | 1 | b | 2025-02-28 1400
E | 0 | c | 2024-12-03 1300 | 2025-02-03 1500
2 changes: 1 addition & 1 deletion docs/README.md
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
# Duke User Guide
# Woogie User Guide

// Update the title above to match the actual product name

Expand Down
23 changes: 23 additions & 0 deletions src/main/java/Deadline.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;

public class Deadline extends Task {
private LocalDateTime by;
private static final DateTimeFormatter INPUT_FORMATTER = DateTimeFormatter.ofPattern("yyyy-MM-dd HHmm");
private static final DateTimeFormatter OUTPUT_FORMATTER = DateTimeFormatter.ofPattern("MMM d yyyy, h:mm a");

public Deadline(String description, String by) {
super(description);
this.by = LocalDateTime.parse(by, INPUT_FORMATTER);
}

@Override
public String toFileFormat() {
return "D | " + (isDone ? "1" : "0") + " | " + description + " | " + by.format(INPUT_FORMATTER);
}

@Override
public String toString() {
return "[D]" + super.toString() + " (by: " + by.format(OUTPUT_FORMATTER) + ")";
}
}
10 changes: 0 additions & 10 deletions src/main/java/Duke.java

This file was deleted.

27 changes: 27 additions & 0 deletions src/main/java/Event.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;

public class Event extends Task {
private LocalDateTime from;
private LocalDateTime to;
private static final DateTimeFormatter INPUT_FORMATTER = DateTimeFormatter.ofPattern("yyyy-MM-dd HHmm");
private static final DateTimeFormatter OUTPUT_FORMATTER = DateTimeFormatter.ofPattern("MMM d yyyy, h:mm a");

public Event(String description, String from, String to) {
super(description);
this.from = LocalDateTime.parse(from, INPUT_FORMATTER);
this.to = LocalDateTime.parse(to, INPUT_FORMATTER);
}

@Override
public String toFileFormat() {
return "E | " + (isDone ? "1" : "0") + " | " + description + " | "
+ from.format(INPUT_FORMATTER) + " | " + to.format(INPUT_FORMATTER);
}

@Override
public String toString() {
return "[E]" + super.toString() + " (from: " + from.format(OUTPUT_FORMATTER)
+ " to: " + to.format(OUTPUT_FORMATTER) + ")";
}
}
53 changes: 53 additions & 0 deletions src/main/java/Storage.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
import java.io.File;
import java.io.FileWriter;
import java.io.FileReader;
import java.io.BufferedReader;
import java.io.IOException;
import java.util.ArrayList;


public class Storage {
private final String filePath;

public Storage(String filePath) {
this.filePath = filePath;
}

public void saveTasks(ArrayList<Task> tasks) {
try {
File file = new File(filePath);
file.getParentFile().mkdirs();
FileWriter writer = new FileWriter(file);

for (Task task : tasks) {
writer.write(task.toFileFormat() + "\n");
}
writer.close();
} catch (IOException e) {
System.out.println("Error saving tasks: " + e.getMessage());
}
}

public ArrayList<Task> loadTasks() {
ArrayList<Task> tasks = new ArrayList<>();
File file = new File(filePath);

if (!file.exists()) {
return tasks; // Return empty list if file does not exist
}

try (BufferedReader reader = new BufferedReader(new FileReader(file))) {
String line;
while ((line = reader.readLine()) != null) {
Task task = Task.fromFileFormat(line);
if (task != null) {
tasks.add(task);
}
}
} catch (IOException e) {
System.out.println("Error loading tasks: " + e.getMessage());
}

return tasks;
}
}
53 changes: 53 additions & 0 deletions src/main/java/Task.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
public abstract class Task {
protected String description;
protected boolean isDone;

public Task(String description) {
this.description = description;
this.isDone = false;
}

public void markDone() {
this.isDone = true;
}

public void markUndone() {
this.isDone = false;
}

public abstract String toFileFormat();

public static Task fromFileFormat(String line) {
String[] parts = line.split(" \\| ");
if (parts.length < 3) return null;

String type = parts[0];
boolean isDone = parts[1].equals("1");
String description = parts[2];

switch (type) {
case "T":
ToDo todo = new ToDo(description);
if (isDone) todo.markDone();
return todo;
case "D":
if (parts.length < 4) return null;
Deadline deadline = new Deadline(description, parts[3]);
if (isDone) deadline.markDone();
return deadline;
case "E":
if (parts.length < 5) return null;
Event event = new Event(description, parts[3], parts[4]);
if (isDone) event.markDone();
return event;
default:
return null;
}
}

@Override
public String toString() {
String status = isDone ? "X" : " ";
return "[" + status + "] " + description;
}
}
15 changes: 15 additions & 0 deletions src/main/java/ToDo.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
public class ToDo extends Task {
public ToDo(String description) {
super(description);
}

@Override
public String toFileFormat() {
return "T | " + (isDone ? "1" : "0") + " | " + description;
}

@Override
public String toString() {
return "[T]" + super.toString();
}
}
Loading