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

[LYHTHADDEUS] iP #316

Open
wants to merge 15 commits into
base: master
Choose a base branch
from
Binary file added Daiyan.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
35 changes: 13 additions & 22 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,26 +1,17 @@
# Duke project template
# CS2103T Individual Project
This is a project by NUS school of computing for the mod CS2103T. I named the main file after Daiyan from Girl's Frontline (An mobile RPG game i adore). The project is currently unfinished and will take the course of this semester to complete.
```
____ ____ _ __ __ ____ __ _
| _) \ / () \ | |\ \/ // () \ | \| |
|____//__/\__\|_| |__|/__\/__\|_|\__|
```
__________________________________________
![Daiyan](https://github.com/lyhthaddeus/ip/blob/master/Daiyan.png)

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.
Hello I'm Daiyan
What can I do for you?
__________________________________________

## Setting up in Intellij
[More information on Daiyan](https://iopwiki.com/wiki/Daiyan)

Prerequisites: JDK 17, update Intellij to the most recent version.

1. Open Intellij (if you are not in the welcome screen, click `File` > `Close Project` to close the existing project first)
1. Open the project into Intellij as follows:
1. Click `Open`.
1. Select the project directory, and click `OK`.
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:
```
Hello from
____ _
| _ \ _ _| | _____
| | | | | | | |/ / _ \
| |_| | |_| | < __/
|____/ \__,_|_|\_\___|
```

**Warning:** Keep the `src\main\java` folder as the root folder for Java files (i.e., don't rename those folders or move Java files to another folder outside of this folder path), as this is the default location some tools (e.g., Gradle) expect to find Java files.
2 changes: 2 additions & 0 deletions data/storage.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
T | false | task 1
E | false | task 1 | 1 | 2
100 changes: 100 additions & 0 deletions src/main/java/Controller/Parser.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,100 @@
package Controller;

import java.io.IOException;
import java.util.Scanner;
import DataStructure.TaskList;
import TaskObjects.*;

import Enums.CommandTypes;
import Exception.InvalidInputException;
import Exception.SyntaxException;

public class Parser {

private TaskList<Task> taskList;
private static final String LINE_BREAK = "\n__________________________________________\n";

public Parser() {
}

public void start() {
Scanner scanner = new Scanner(System.in);
String input;
System.out.print("How may I assist you commander?" + LINE_BREAK);
this.taskList = Storage.load();

while(true) {
input = scanner.nextLine().trim();
String[] parsed = input.split(" ", 2);
CommandTypes commandTypes = CommandTypes.fromString(parsed[0]);

try {
switch (commandTypes) {
case BYE, Q:
System.out.println("Bye, hope to see you again Commander." + LINE_BREAK);
return;
case LIST, LS:
taskList.getList();
break;
case MARK:
if (parsed.length < 2) {
throw new SyntaxException("mark", "mark <index of item>");
}
taskList.mark(parsed[1]);
break;
case UNMARK:
if (parsed.length < 2) {
throw new SyntaxException("unmark", "unmark <index of item>");
}
taskList.unmarked(parsed[1]);
break;
case DELETE, DEL:
if (parsed.length < 2) {
throw new SyntaxException("delete", "delete <index of item>");
}
taskList.delete(parsed[1]);
break;
case TODO:
if (parsed.length < 2) {
throw new SyntaxException("Todo", "todo <Task>");
}
taskList.add(new Todo(parsed[1], false));
break;
case DEADLINE:
if (parsed.length < 2) {
throw new SyntaxException("Deadline", "deadline <Task> /by <Time>");
}
String[] deadlinePartition = parsed[1].split("/by");
if (deadlinePartition.length < 2) {
throw new SyntaxException("Deadline", "deadline <Task> /by <Time>");
}
String deadlineDescription = deadlinePartition[0].trim();
String deadlineBy = deadlinePartition[1].trim();
taskList.add(new Deadline(deadlineDescription, false,deadlineBy));
break;
case EVENT:
if (parsed.length < 2) {
throw new SyntaxException("Event", "event <Task> /from <Time> /to <Time>");
}
String[] eventPartition1 = parsed[1].split("/from");
if (eventPartition1.length < 2) {
throw new SyntaxException("Event", "event <Task> /from <Time> /to <Time>");
}
String eventDescription = eventPartition1[0].trim();
String[] eventPartition2 = eventPartition1[1].split("/to");
if (eventPartition2.length < 2) {
throw new SyntaxException("Event", "event <Task> /from <Time> /to <Time>");
}
String eventFrom = eventPartition2[0].trim();
String eventTo = eventPartition2[1].trim();
taskList.add(new Event(eventDescription, false, eventFrom, eventTo));
break;
default:
throw new InvalidInputException("Sorry Commander, but I do not understand your orders.");
}
} catch (InvalidInputException e) {
System.out.println(e.getMessage() + LINE_BREAK);
}
}
}
}
88 changes: 88 additions & 0 deletions src/main/java/Controller/Storage.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,88 @@
package Controller;

import DataStructure.TaskList;
import TaskObjects.*;
import java.io.*;
import java.util.ArrayList;
import java.util.List;
import Exception.InvalidInputException;
import Exception.StorageSyntaxException;

public class Storage {
private static final String PATH = "./data/storage.txt";

public static TaskList<Task> load() {
TaskList<Task> taskList = new TaskList<>();
File file = new File(PATH);

if (!file.isFile()) {
return taskList;
}

try (BufferedReader reader = new BufferedReader(new FileReader(file))) {
String readerPointer = reader.readLine();
while (readerPointer != null) {
Task task = parser(readerPointer);
if (task != null) {
taskList.load(task);
}
readerPointer = reader.readLine();
}
} catch (IOException e) {
System.out.println("Sorry Commander, there appears to be an error fetching previous task: " + e.getMessage());
}

return taskList;
}

public static void save(List<? extends Task> taskList) {
File file = new File(PATH);
file.getParentFile().mkdirs();

try {
if (!file.isFile()) {
file.createNewFile();
}

try (BufferedWriter writer = new BufferedWriter(new FileWriter(file))){
for (Task task : taskList) {
writer.write(task.toFileFormat());
writer.newLine();
}
}
} catch (IOException e) {
System.out.println("Sorry Commander, there is an error with saving tasks: " + e.getMessage());
}
}

private static Task parser(String input) {
String[] split = input.split(" \\| ");

String type = split[0].trim();
boolean isCompleted = split[1].trim().equals("1");
String description = split[2].trim();

Task returnTask;
try {
switch (type) {
case "T":
returnTask = new Todo(description, isCompleted);
break;
case "D":
returnTask = new Deadline(description, isCompleted, split[3].trim());
break;
case "E":
returnTask = new Event(description, isCompleted, split[3].trim(), split[4].trim());
break;
default:
throw new StorageSyntaxException();
}
} catch (InvalidInputException e) {
System.out.println(e.getMessage());
return null;
}

return returnTask;
}

}
14 changes: 14 additions & 0 deletions src/main/java/Daiyan.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
import Controller.Parser;

public class Daiyan {

public static void main(String[] args) {
String asciiArt = " ____ ____ _ __ __ ____ __ _ \n" +
"| _) \\ / () \\ | |\\ \\/ // () \\ | \\| |\n" +
"|____//__/\\__\\|_| |__|/__\\/__\\|_|\\__|";
System.out.println(asciiArt);
System.out.println("__________________________________________\nHello I'm Daiyan\nWhat can I do for you?\n__________________________________________\n");
Parser parser = new Parser();
parser.start();
}
}
85 changes: 85 additions & 0 deletions src/main/java/DataStructure/TaskList.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,85 @@
package DataStructure;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

import Controller.Storage;
import TaskObjects.Task;

public class TaskList<T extends Task> {

private List<T> TaskList;

public TaskList() {
this.TaskList = new ArrayList<>();
}

private static final String LINE_BREAK = "\n__________________________________________\n";


public void load(T task) {
this.TaskList.add(task);
}

public void add(T task) {
this.TaskList.add(task);
Storage.save(this.TaskList);

System.out.println("Task has been successfully added");
System.out.println(task.toString());
this.count();
}

public void delete(String ids) {
int id = Integer.parseInt(ids);
T deleted = this.TaskList.remove(id - 1);
Storage.save(this.TaskList);

System.out.println("Commander, the task has been successfully deleted");
System.out.println(deleted.toString());
this.count();
}

public void mark(String ids) {
int id = Integer.parseInt(ids);
this.TaskList.get(id - 1).markDone();
Storage.save(this.TaskList);

System.out.println("Commander, task " + ids + " has been marked completed");
System.out.println(TaskList.get(id - 1).toString() + LINE_BREAK);
}

public void unmarked(String ids) {
int id = Integer.parseInt(ids);
this.TaskList.get(id - 1).markUndone();
Storage.save(this.TaskList);

System.out.println("Commander, taks " + ids + " has been mark incomplete");
System.out.println(TaskList.get(id - 1).toString() + LINE_BREAK);
}

public void count() {
int count = this.TaskList.size();
System.out.println("Commander, you currently have " + count + " tasks" + LINE_BREAK);
}

public void getList() {
System.out.println(this.toString() + LINE_BREAK);
}

public String toString() {
if (TaskList.isEmpty()) {
return "Commander, currently you have no outstanding task";
}
StringBuilder result = new StringBuilder();
int count = 0;
for (T command: TaskList) {
count++;
result.append(count).append(". ").append(command).append("\n");
}

return result.toString();
}

}
10 changes: 0 additions & 10 deletions src/main/java/Duke.java

This file was deleted.

25 changes: 25 additions & 0 deletions src/main/java/Enums/CommandTypes.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
package Enums;

public enum CommandTypes {
TODO,
DEADLINE,
EVENT,
MARK,
UNMARK,
LIST,
LS,
BYE,
Q,
DELETE,
DEL,
INVALID;

// Static method to map a command string to the corresponding enum
public static CommandTypes fromString(String command) {
try {
return CommandTypes.valueOf(command.toUpperCase());
} catch (IllegalArgumentException e) {
return CommandTypes.INVALID;
}
}
}
7 changes: 7 additions & 0 deletions src/main/java/Exception/InvalidInputException.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
package Exception;

public class InvalidInputException extends Exception {
public InvalidInputException(String msg) {
super(msg);
}
}
8 changes: 8 additions & 0 deletions src/main/java/Exception/StorageSyntaxException.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
package Exception;

public class StorageSyntaxException extends InvalidInputException{

public StorageSyntaxException() {
super("Apologies Commander, there appear to be an issue with the storage file");
}
}
7 changes: 7 additions & 0 deletions src/main/java/Exception/SyntaxException.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
package Exception;

public class SyntaxException extends InvalidInputException {
public SyntaxException(String type, String style) {
super("Apologies Commander, invalid " + type + " format. Please use (" + style + ").");
}
}
Loading