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

[Teo Ziyi Ivy] iP #182

Open
wants to merge 27 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
153 changes: 141 additions & 12 deletions docs/README.md
Original file line number Diff line number Diff line change
@@ -1,29 +1,158 @@
# User Guide

## Features
## Command list

### Feature-ABC
### `todo` - adds todo task
### `deadline` - adds deadline task
### `event` - adds event task
### `list` - lists all task
### `find` - find matching tasks
### `done` - completes task
### `delete` - deletes task
### `bye` - exits program

Description of the feature.
## Usage

### Feature-XYZ
### `todo` - adds todo task

Description of the feature.
Adds todo task to the list

## Usage
Example of usage:

### `Keyword` - Describe action
`todo Complete CS2113 lecture quiz`

Describe the action and its outcome.
Expected outcome:

Example of usage:
Displays successfully addition of todo task to the list and the total number of tasks in the list.

```
Kao got it! ( 。•̀ᴗ-)✧ Kao has added this task:
Complete CS2113 lecture quiz
Now you have 1 tasks in the list.
```

### `deadline` - adds deadline task

Adds deadline task to the list

Example of usage:

`deadline Complete iP increment /by Thursday 2359`

Expected outcome:

Displays successfully addition of deadline task to the list and the total number of tasks in the list.

```
Kao got it! ( 。•̀ᴗ-)✧ Kao has added this task:
Complete iP increment
Now you have 2 tasks in the list.
```

### `event` - adds event task

Adds event task to the list

Example of usage:

`event Attend CS2113 lecture /at Friday 1400`

Expected outcome:

Displays successfully addition of event task to the list and the total number of tasks in the list.

```
Kao got it! ( 。•̀ᴗ-)✧ Kao has added this task:
Attend CS2113 lecture
Now you have 3 tasks in the list.
```

### `list` - lists all task

Lists the entire task list

Example of usage:

`list`

Expected outcome:

Displays the entire task list

`keyword (optional arguments)`
```
Right away! ε=ε=┌( >_<)┘
Kao retrieved the tasks in your list:
1. [T][ ] Complete CS2113 lecture quiz
2. [D][ ] Complete iP increment (by: Thursday 2359)
3. [E][ ] Attend CS2113 lecture (at: Friday 1400)
```

### `find` - find matching tasks

Finds matching tasks in the list

Example of usage:

`find CS2113`

Expected outcome:

Display tasks in the task list with the matching keyword

```
Kao got it! ε=ε=┌( `ー´)┘
Kao retrieved the matching tasks in your list:
1. [T][ ] Complete CS2113 lecture quiz
3. [E][ ] Attend CS2113 lecture (at: Friday 1400)
```

### `done` - completes task

Marks task as complete

Example of usage:

`done 1`

Expected outcome:

Description of the outcome.
Displays successful completion of the corresponding task

```
expected output
Nice job! ( >ω<)☆ Kao marked this task as done:
Complete CS2113 lecture quiz
```

### `delete` - deletes task

Deletes task from the list

Example of usage:

`delete 1`

Expected outcome:

Displays successful deletion of the corresponding task

```
Alright! ( ・∀・ )ノ Kao removed this task:
Complete CS2113 lecture quiz
Now you have 2 tasks in the list.
```

### `bye` - exits program

Exits and closes the program

Example of usage:

`bye`

Expected outcome:

Shows successful termination of the program

```
Bye bye! Kao will be waiting for you 。:゚( ´°ω°` )゚:。
```
10 changes: 0 additions & 10 deletions src/main/java/Duke.java

This file was deleted.

3 changes: 3 additions & 0 deletions src/main/java/META-INF/MANIFEST.MF
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
Manifest-Version: 1.0
Main-Class: duke.Duke

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

import duke.commands.Command;
import duke.commands.CommandOutput;
import duke.tasks.*;

import java.util.Scanner;

public class Duke {

private Storage storage;
private TaskList tasks;
private Ui ui;
private Parser parser;

public Duke(String filePath) {
ui = new Ui();
storage = new Storage(filePath);
parser = new Parser();
TaskListResponse response = storage.initialiseTasks();
tasks = response.taskList;
Ui.printMessage(response.response);
}

//attempts to execute the user command
public CommandOutput executeCommand(Command command) {
try {
return command.execute();
} catch (Exception e){
return new CommandOutput(e.getMessage(), false, null);
}
}

//runs the program
public void run() {
ui.printGreeting();
String line;
Scanner in = new Scanner(System.in);
ui.printPrompt();
line = in.nextLine();
while(!line.equals(parser.BYE)) {
Command command = parser.parseCommand(tasks, line);
CommandOutput commandOutput = executeCommand(command);
if (commandOutput.isUpdated()) {
tasks = commandOutput.getTaskList();
String errorMessage = storage.write(tasks);
ui.printMessage(errorMessage);
}
ui.printMessage(commandOutput.getResponse());
ui.printPrompt();
line = in.nextLine();
}
ui.printExit();
}

public static void main(String[] args) {
new Duke("data/duke.txt").run();
}
}
46 changes: 46 additions & 0 deletions src/main/java/duke/Parser.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
package duke;

import duke.commands.Command;
import duke.commands.ListCommand;
import duke.commands.AddCommand;
import duke.commands.FindCommand;
import duke.commands.NoCommand;
import duke.commands.DoneCommand;
import duke.commands.DeleteCommand;
import duke.tasks.TaskList;
import duke.tasks.TaskType;

public class Parser {

public final String LIST = "list";
public final String TODO = "todo";
public final String DEADLINE = "deadline";
public final String EVENT = "event";
public final String FIND = "find";
public final String DONE = "done";
public final String DELETE = "delete";
public final String BYE = "bye";

//executes program and updates task list according to user command
public Command parseCommand(TaskList taskList, String line) {
final String[] splitLine = line.split(" ", 2);
final String command = splitLine[0];
final String userCommand = line.replaceAll("^" + command + " ", "");
if (command.equals(LIST)) {
return new ListCommand(taskList);
} else if (command.equals(TODO)) {
return new AddCommand(taskList, userCommand, TaskType.TODO);
} else if (command.equals(DEADLINE)) {
return new AddCommand(taskList, userCommand, TaskType.DEADLINE);
} else if (command.equals(EVENT)) {
return new AddCommand(taskList, userCommand, TaskType.EVENT);
} else if (command.equals(FIND)) {
return new FindCommand(taskList, userCommand);
} else if (command.equals(DONE)) {
return new DoneCommand(taskList, userCommand);
} else if (command.equals(DELETE)) {
return new DeleteCommand(taskList, userCommand);
}
return new NoCommand(taskList);
}
}
100 changes: 100 additions & 0 deletions src/main/java/duke/Storage.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,100 @@
package duke;

import duke.exceptions.FileFormatException;
import duke.tasks.Deadline;
import duke.tasks.Event;
import duke.tasks.Task;
import duke.tasks.Todo;
import duke.tasks.TaskList;
import duke.tasks.TaskListResponse;

import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileWriter;
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.Scanner;

public class Storage {

private final String filePath;
private final File f;

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

//load tasks from save file if save file exists
public void loadTasks(TaskList taskList) throws FileNotFoundException, FileFormatException {
Scanner s = new Scanner(f); // create a Scanner using the File as the source
while (s.hasNext()) {
String task = s.nextLine();
if (!task.matches("T\\s\\|\\s[01]\\s\\|.+")
&& !task.matches("[ED]\\\\s\\\\|\\\\s[01]\\\\s\\\\|.+\\\\|.+")) {
throw new FileFormatException();
}
String[] taskSplit= task.split(" \\| ");
String taskType = taskSplit[0];
boolean isDone = taskSplit[1].equals("1");
String description = taskSplit[2];
String preposition = taskSplit.length >= 4 ? taskSplit[3] : "";
if (taskType.equals("E")){
taskList.tasks.add(new Event(description, isDone, preposition));
} else if (taskType.equals("D")){
taskList.tasks.add(new Deadline(description, isDone, preposition));
} else {
taskList.tasks.add(new Todo(description, isDone));
}
}
}

//creates a save file
public void createFile() throws IOException {
Path pathToFile = Paths.get("data/duke.txt");
Files.createDirectories(pathToFile.getParent());
f.createNewFile();
}

//program output for save file
public TaskListResponse initialiseTasks() {
TaskList taskList = new TaskList();
String response = "";
try {
loadTasks(taskList);
} catch (FileNotFoundException e0){
response = response + "Kao cannot find the file data/duke.txt ( ;ŏ﹏ŏ ) ";
try {
createFile();
} catch (IOException e1) {
response = response + "Kao has encountered an error creating a new file ( °ㅂ°╬ )";
}
response = response + "Kao will create a new file data/duke.txt for you!";
} catch (FileFormatException e2) {
response = response + "Kao is facing a formatting error with the save file ヽ( `д´ )ノ";
taskList.clearList();
}
return new TaskListResponse(taskList, response);
}

//writing to save file
private void writeToFile(TaskList taskList) throws IOException {
FileWriter fw = new FileWriter("data/duke.txt");
for (Task list : taskList.tasks) {
fw.write(list.saveTask() + "\n");
}
fw.close();
}

//program output for writing to save file
public String write(TaskList taskList) {
try {
writeToFile(taskList);
} catch (IOException e) {
return "Kao has faced an error writing to file ( ; ω ; )";
}
return "";
}
}
Loading