Skip to content

Commit

Permalink
Merge pull request #4 from nicknamenic/branch-Level-7
Browse files Browse the repository at this point in the history
Increment to Level 7
  • Loading branch information
nicknamenic authored Mar 8, 2024
2 parents 215cd8e + 445a861 commit 47e5965
Show file tree
Hide file tree
Showing 10 changed files with 202 additions and 43 deletions.
3 changes: 3 additions & 0 deletions data/nocturne.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
[E][ ] delimit (from: 2pm to: 4pm)
[D][X] finish cs2113t (by: 2359)
[T][X] help
10 changes: 5 additions & 5 deletions src/main/java/Nocturne.java
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
import exceptions.NocturneException;
import util.Parser;
import util.TaskList;
import util.Ui;

public class Nocturne {
public static void main(String[] args) throws NocturneException {
public static void main(String[] args) {
TaskList list = new TaskList();
System.out.println("Good evening. I'm Nocturne.");
System.out.println("What ails you on this fine day?");
Ui.greetingMessage();
Parser.getInput(list);
System.out.println("Farewell, and may the fortunes be ever in your favour.");
Ui.farewellMessage();
}
}
}
9 changes: 8 additions & 1 deletion src/main/java/tasks/Deadline.java
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,15 @@ public Deadline(String description, String by) {
this.by = by;
}

public Deadline(String description, String by, boolean isDone) {
super(description);
this.by = by;
this.isDone = isDone;
}

@Override
public String toString() {
return "[D]" + "[" + super.getStatusIcon() + "] " + this.description + " (by: " + by + ")";
}
}
}

13 changes: 10 additions & 3 deletions src/main/java/tasks/Event.java
Original file line number Diff line number Diff line change
@@ -1,15 +1,22 @@
package tasks;

public class Event extends Task{
public class Event extends Task {
protected String from;
protected String to;

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

public Event(String description, String from, String to, boolean isDone) {
super(description);
this.from = from;
this.to = to;
this.isDone = isDone;
}

@Override
public String toString(){
return "[E]" + "[" + super.getStatusIcon() + "] " + this.description + " (from: " + from + " " + "to: " + to + ")";
Expand Down
3 changes: 2 additions & 1 deletion src/main/java/tasks/Task.java
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,8 @@ public void markAsDone() {
public void markAsUndone() {
this.isDone = false;
}

public String getDescription() {
return this.description;
}
}
}
12 changes: 10 additions & 2 deletions src/main/java/tasks/Todo.java
Original file line number Diff line number Diff line change
@@ -1,13 +1,21 @@
package tasks;

public class Todo extends Task {
public Todo(String description){

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

public Todo(String description, boolean isDone) {
super(description);
isDone = false;
this.isDone = isDone;
}


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

}
}
12 changes: 8 additions & 4 deletions src/main/java/util/Parser.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
import tasks.Deadline;
import tasks.Event;
import tasks.Todo;

import java.util.Scanner;

public class Parser {
Expand Down Expand Up @@ -44,16 +43,21 @@ private static void getResponse(String input, TaskList tasks) {
eventSplit[1].substring(5).trim(),
eventSplit[2].substring(3).trim()));
break;
case "find":
tasks.findTask(task);
break;
case "delete":
tasks.deleteTask(Integer.parseInt(task));
break;
default:
System.out.println("Your command is invalid, invalid. Try again.");
}
} catch (IndexOutOfBoundsException e) {
System.out.println("You are missing a /. Do not let this happen again.");
}

Ui.missingSlashMessage();
} catch (NumberFormatException e) {
System.out.println("Ensure that you include a number after your delete/find/mark command.");
}
}

public static void getInput(TaskList tasks) {
Expand All @@ -64,4 +68,4 @@ public static void getInput(TaskList tasks) {
line = in.nextLine();
}
}
}
}
72 changes: 72 additions & 0 deletions src/main/java/util/Storage.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
package util;

import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileWriter;
import java.io.IOException;
import java.util.Scanner;
import tasks.Deadline;
import tasks.Event;
import tasks.Task;
import tasks.Todo;

public class Storage {
private final String PATH = "./data/nocturne.txt";
private File f;

public Storage() {
File dir = new File("./data");
if (!dir.exists()) {
dir.mkdir();
}
this.f = new File(PATH);
}

public void saveToFile(TaskList taskList) {
try {
FileWriter fileWriter = new FileWriter(this.f);
fileWriter.write("");
for (Task task : taskList.tasks) {
fileWriter.append(task.toString()).append("\n");
}
fileWriter.close();
} catch (IOException e) {
throw new RuntimeException(e);
}
}

public void readFromFile(TaskList tasks) {
try {
Scanner sc = new Scanner(this.f);

while(sc.hasNextLine()) {
String data = sc.nextLine();
String task = data.substring(6);
task = task.replace('(', ' ');
task = task.replace(')', ' ');
boolean isDone = data.charAt(4) == 'X';
switch (data.charAt(1)) {
case 'D':
String[] deadlineSplit = task.split("by:");
tasks.addTaskStealth(new Deadline(deadlineSplit[0].trim(),
deadlineSplit[1].trim(),
isDone));
break;
case 'E':
String[] eventSplit = task.split(":");
tasks.addTaskStealth(new Event(eventSplit[0].substring(0,
eventSplit[0].length() - 4).trim(),
eventSplit[1].substring(0, eventSplit[1].length() - 2).trim(),
eventSplit[2].trim(),
isDone));
break;
case 'T':
tasks.addTaskStealth(new Todo(task.trim(), isDone));
}
}
sc.close();
} catch (IndexOutOfBoundsException | FileNotFoundException e) {
throw new RuntimeException(e);
}
}
}
80 changes: 53 additions & 27 deletions src/main/java/util/TaskList.java
Original file line number Diff line number Diff line change
@@ -1,62 +1,88 @@
package util;

import tasks.Task;

import java.util.ArrayList;
import tasks.Task;

public class TaskList {
protected ArrayList<Task> tasks = new ArrayList<>();
protected Storage storage = new Storage();

public TaskList() {
this.storage.readFromFile(this);
}

public void addTask (Task task) {
public void addTask(Task task) {
System.out.println("A task I see. I have added it.");
System.out.println(" " + task);
tasks.add(task);
Ui.printTask(task);
this.tasks.add(task);
this.storage.saveToFile(this);
}

public void addTaskStealth(Task task) {
this.tasks.add(task);
this.storage.saveToFile(this);
}

public void deleteTask(int index) {
try {
System.out.println("Should all acquaintance be forgot..." );
System.out.println(" " + tasks.get(index - 1));
tasks.remove(index - 1);
System.out.println(tasks.size() + " task(s) remain.");
System.out.println("Should all acquaintance be forgot...");
Ui.printTask(this.tasks.get(index - 1));
this.tasks.remove(index - 1);
System.out.println(this.tasks.size() + " task(s) remain.");
} catch (IndexOutOfBoundsException e) {
System.out.println("You are trying to access forbidden territory. Tread carefully.");
Ui.indexOutOfBoundsMessage();
}

this.storage.saveToFile(this);
}

public void listTasks () {
if (tasks.isEmpty()) {
System.out.println("Your list is empty. Try again, when you have become more productive.");
public void listTasks() {
if (this.tasks.isEmpty()) {
Ui.emptyListMessage();
}
for (int i = 0; i < tasks.size(); i++) {
System.out.print((i + 1) + ".");
System.out.println(tasks.get(i));

for(int i = 0; i < this.tasks.size(); ++i) {
System.out.print(i + 1 + ".");
System.out.println(this.tasks.get(i));
}

}

public void markTask (int index) {
public void markTask(int index) {
try {
tasks.get(index - 1).markAsDone();
(this.tasks.get(index - 1)).markAsDone();
System.out.println("Congratulations. I have marked this task as finished:");
System.out.println(" " + tasks.get(index - 1));
} catch (IndexOutOfBoundsException e) {
System.out.println("You are trying to access forbidden territory. Tread carefully.");
Ui.printTask(this.tasks.get(index - 1));
} catch (IndexOutOfBoundsException var3) {
Ui.indexOutOfBoundsMessage();
}

this.storage.saveToFile(this);
}

public void unmarkTask (int index) {
public void unmarkTask(int index) {
try {
tasks.get(index - 1).markAsUndone();
(this.tasks.get(index - 1)).markAsUndone();
System.out.println("Do not neglect your duties. I have marked this task as unfinished:");
System.out.println(" " + tasks.get(index - 1));
Ui.printTask(this.tasks.get(index - 1));
} catch (IndexOutOfBoundsException e) {
System.out.println("You are trying to access forbidden territory. Tread carefully.");
Ui.indexOutOfBoundsMessage();
}

this.storage.saveToFile(this);
}

public void findTask(String keyFind) {
try {
System.out.println("Here is what you are looking for: ");
int count = 1;
for (Task task : this.tasks) {
if (task.getDescription().contains(keyFind)) {
System.out.println(count + ". " + task);
++count;
}
}
} catch (IndexOutOfBoundsException e) {
Ui.indexOutOfBoundsMessage();
}
}
}
}
31 changes: 31 additions & 0 deletions src/main/java/util/Ui.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
package util;

import tasks.Task;

public class Ui {

public static void greetingMessage() {
System.out.println("Good evening. I'm Nocturne.");
System.out.println("What ails you on this fine day?");
}

public static void farewellMessage() {
System.out.println("Farewell. And may the fortunes be ever in your favour.");
}

public static void indexOutOfBoundsMessage() {
System.out.println("You are trying to access forbidden territory. Tread carefully.");
}

public static void emptyListMessage() {
System.out.println("Your list is empty. Try again, when you have become more productive.");
}

public static void missingSlashMessage() {
System.out.println("You are missing a /. Do not let this happen again.");
}

public static void printTask(Task task) {
System.out.println(" " + task);
}
}

0 comments on commit 47e5965

Please sign in to comment.