Skip to content

Commit

Permalink
Add Dude Level 7
Browse files Browse the repository at this point in the history
  • Loading branch information
aaronxujiachen committed Sep 21, 2023
1 parent 317108a commit 898968d
Show file tree
Hide file tree
Showing 5 changed files with 139 additions and 26 deletions.
Empty file added data/duke.txt
Empty file.
21 changes: 17 additions & 4 deletions src/dude/Deadline.java
Original file line number Diff line number Diff line change
@@ -1,17 +1,30 @@
package dude;

public class Deadline extends Task {
String by;
private String by;

// Constructor to initialize a deadline with a description and a due date
public Deadline(String description, String by) {
super(description);
this.by = by;
this.type = "[D]"; // Type [D] indicates this is a dude.Deadline
this.type = "[D]";
}

@Override
public String toFileFormat() {
return super.toFileFormat() + " | " + by;
}

public static Deadline fromFileFormat(String fileString) {
String[] parts = fileString.split("\\s\\|\\s");
Deadline deadline = new Deadline(parts[2], parts[3]);
if (parts[1].equals("1")) {
deadline.markAsDone();
}
return deadline;
}

@Override
public String toString() {
return getType() + getStatusIcon() + " " + description + " (by: " + by + ")";
return super.toString() + " (by: " + by + ")";
}
}
81 changes: 77 additions & 4 deletions src/dude/Dude.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,16 @@

import java.util.ArrayList;
import java.util.Scanner;
import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import java.io.FileNotFoundException;

public class Dude {

private static final String DIRECTORY_PATH = "./data";
private static final String FILE_PATH = DIRECTORY_PATH + "/duke.txt";

// Method to draw horizontal lines
public static void drawLine() {
for (int i = 0; i < 30; i++) {
Expand Down Expand Up @@ -32,7 +39,8 @@ public static void hiDude() {
// Method to handle the storage of tasks
public static void storeDude() {
Scanner scan = new Scanner(System.in);
ArrayList<Task> tasks = new ArrayList<>();
setupFile();
ArrayList<Task> tasks = loadFromFile();

String input = scan.nextLine();
while (!input.isEmpty()) {
Expand Down Expand Up @@ -67,7 +75,6 @@ public static void storeDude() {
scan.close();
}


private static void listTasks(ArrayList<Task> tasks) {
System.out.println("Here are the tasks in your list:");
for (int i = 0; i < tasks.size(); i++) {
Expand All @@ -86,9 +93,9 @@ private static void addTodoTask(ArrayList<Task> tasks, String input) throws Dude
}
tasks.add(new Task(taskDescription));
printAddedTask(tasks);
saveToFile(tasks);
}


private static void addDeadlineTask(ArrayList<Task> tasks, String input) throws DudeException {
int byIndex = input.indexOf("/by");
if (byIndex == -1) {
Expand All @@ -107,6 +114,7 @@ private static void addDeadlineTask(ArrayList<Task> tasks, String input) throws

tasks.add(new Deadline(taskDescription, by));
printAddedTask(tasks);
saveToFile(tasks);
}

private static void addEventTask(ArrayList<Task> tasks, String input) throws DudeException {
Expand Down Expand Up @@ -134,6 +142,7 @@ private static void addEventTask(ArrayList<Task> tasks, String input) throws Dud

tasks.add(new Event(taskDescription, from, to));
printAddedTask(tasks);
saveToFile(tasks);
}

private static void markOrUnmarkTask(ArrayList<Task> tasks, String input) {
Expand All @@ -158,6 +167,8 @@ private static void markOrUnmarkTask(ArrayList<Task> tasks, String input) {
} catch (NumberFormatException e) {
System.out.println("Invalid task index format.");
}

saveToFile(tasks);
}

private static void printAddedTask(ArrayList<Task> tasks) {
Expand Down Expand Up @@ -187,8 +198,71 @@ private static void deleteTask(ArrayList<Task> tasks, String input) {
} catch (NumberFormatException e) {
System.out.println("Invalid task index format.");
}

saveToFile(tasks);
}

private static void setupFile() {
// Check if the directory exists, if not, create it
File directory = new File(DIRECTORY_PATH);
if (!directory.exists()) {
directory.mkdir();
}

// Check if the file exists, if not, create it
File file = new File(FILE_PATH);
if (!file.exists()) {
try {
file.createNewFile();
} catch (IOException e) {
System.out.println("Error creating file: " + e.getMessage());
}
}
}

private static ArrayList<Task> loadFromFile() {
ArrayList<Task> tasks = new ArrayList<>();
try {
File file = new File(FILE_PATH);
Scanner fileReader = new Scanner(file);

while (fileReader.hasNextLine()) {
String line = fileReader.nextLine();
// Assuming the first char denotes the type of task
char taskType = line.charAt(0);
switch (taskType) {
case 'T':
tasks.add(Task.fromFileFormat(line));
break;
case 'D':
tasks.add(Deadline.fromFileFormat(line));
break;
case 'E':
tasks.add(Event.fromFileFormat(line));
break;
}
}

fileReader.close();
} catch (FileNotFoundException e) {
System.out.println("Error reading from file: " + e.getMessage());
}
return tasks;
}

private static void saveToFile(ArrayList<Task> tasks) {
try {
FileWriter fileWriter = new FileWriter(FILE_PATH);
for (Task task : tasks) {
fileWriter.write(task.toFileFormat() + "\n");
}
fileWriter.close();
} catch (IOException e) {
System.out.println("Error writing to file: " + e.getMessage());
}
}


// Method to print the goodbye message
public static void byeDude() {
System.out.println("Bye. Hope to see you again soon!");
Expand All @@ -201,4 +275,3 @@ public static void main(String[] args) {
storeDude();
}
}

29 changes: 21 additions & 8 deletions src/dude/Event.java
Original file line number Diff line number Diff line change
@@ -1,19 +1,32 @@
package dude;

public class Event extends Task {
String startDate;
String endDate;
private String from;
private String to;

// Constructor to initialize an event with a description, start date, and end date
public Event(String description, String startDate, String endDate) {
public Event(String description, String from, String to) {
super(description);
this.startDate = startDate;
this.endDate = endDate;
this.type = "[E]"; // Type [E] indicates this is an dude.Event
this.from = from;
this.to = to;
this.type = "[E]";
}

@Override
public String toFileFormat() {
return super.toFileFormat() + " | " + from + " | " + to;
}

public static Event fromFileFormat(String fileString) {
String[] parts = fileString.split("\\s\\|\\s");
Event event = new Event(parts[2], parts[3], parts[4]);
if (parts[1].equals("1")) {
event.markAsDone();
}
return event;
}

@Override
public String toString() {
return getType() + getStatusIcon() + " " + description + " (from: " + startDate + " to: " + endDate + ")";
return super.toString() + " (from: " + from + " to: " + to + ")";
}
}
34 changes: 24 additions & 10 deletions src/dude/Task.java
Original file line number Diff line number Diff line change
@@ -1,29 +1,43 @@
package dude;

public class Task {
// Instance variables to store task description and status
protected String description;
protected boolean isDone;
protected String type;

// Constructor to initialize a task with a description
public Task(String description) {
this.description = description;
this.isDone = false;
this.type = "[T]"; // Default type is [T]
this.type = "[T]"; // Represents a generic task.
}

public String getType() {
return type;
}

// Method to get the status icon based on whether the task is done or not
public String getStatusIcon() {
return (isDone ? "[X]" : "[ ]");
}

public String getDescription() {
return description;
}

public void markAsDone() {
isDone = true;
}

public String toFileFormat() {
return type.charAt(1) + " | " + (isDone ? "1" : "0") + " | " + description;
}

public static Task fromFileFormat(String fileString) {
String[] parts = fileString.split("\\s\\|\\s");
Task task = new Task(parts[2]);
if (parts[1].equals("1")) {
task.markAsDone();
}
return task;
}

@Override
public String toString () {
return getType() + getStatusIcon() + " " + description;
public String toString() {
return type + getStatusIcon() + " " + description;
}
}

0 comments on commit 898968d

Please sign in to comment.