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

[Ng Yong Jie] iP #74

Open
wants to merge 17 commits into
base: master
Choose a base branch
from
115 changes: 102 additions & 13 deletions docs/README.md
Original file line number Diff line number Diff line change
@@ -1,29 +1,118 @@
# User Guide

## Features
## Quick Start

### Feature-ABC
1. Copy the file "2113.iP.jar" from this repository into your folder of choice
2. Open terminal and change the directory to that folder.
3. Run the jar file by entering `java -jar "2113.iP.jar"` into the terminal.

Description of the feature.
You should be greeted by Duke as follows:
```
Hello from
____ _
| _ \ _ _| | _____
| | | | | | | |/ / _ \
| |_| | |_| | < __/
|____/ \__,_|_|\_\___|

Hello! I'm Duke
What can I do for you?
```
4. Input the commands below and press Enter to execute the command.
5. Exit Duke using the input "bye".

### Feature-XYZ
## Features
List of commands that Duke supports

Description of the feature.
### list
Displays all tasks in your task list.
```
list
_____________________________________________
Here are the tasks in your list:
1.[T][ ] Accounting Homework
2.[D][ ] individual project (by: Friday 2359)
3.[E][ ] Date with my girlfriend (at: next week sometime I haven't decide)
```
### todo
Adds a todo to your task list.

## Usage
Format: todo `taskName`
```
todo Accounting Homework
_____________________________________________
Got it. I've added this task:
[T][ ] Accounting Homework
Now there are 1 tasks in the list.
```
### deadline
Adds a deadline to your task list.

### `Keyword` - Describe action
Format: deadline `taskName` / `taskDateTime`
```
deadline individual project /Friday 2359
_____________________________________________
Got it. I've added this task:
[D][ ] individual project (by: Friday 2359)
Now there are 2 tasks in the list.
```
### event
Adds an event to your task list

Describe the action and its outcome.
Format: event `taskName` / `taskDateTime`
```
event Date with my girlfriend /next week sometime I haven't decide
_____________________________________________
Got it. I've added this task:
[E][ ] Date with my girlfriend (at: next week sometime I haven't decide)
Now there are 3 tasks in the list.
```

Example of usage:
### mark
Mark a task in your list as done.

`keyword (optional arguments)`
Format: mark `taskNumber`
```
mark 1
_____________________________________________
Nice! I've marked this task as done:
[T][X] Accounting Homework
```
### unmark
Mark a task in your list as not done.

Expected outcome:
Format: mark `taskNumber`
```
unmark 2
_____________________________________________
OK, I've marked this task as not done yet:
[D][ ] individual project (by: Friday 2359)
```
### delete
Delete a task from your task list.

Description of the outcome.
Format: delete `taskNumber`
```
delete 1
_____________________________________________
Noted. I've removed this task:
[T][X] Accounting Homework
```
### find
Find relevant tasks based on a keyword.

Format: find `keyword`
```
find girlfriend
_____________________________________________
Here are the matching tasks in your list:
1.[E][ ] Date with my girlfriend (at: next week sometime I haven't decide)
```
expected output
### bye
Exits the application.
```
bye
_____________________________________________

Bye! See you :)
```
72 changes: 72 additions & 0 deletions src/main/java/Command.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@

/**
* Command takes care of the command input by user
*/
public class Command {

String commandAction;

public Command(String commandAction) {
this.commandAction = commandAction;
}

/**
* Checks which command is being called and executes it
*/
public boolean executeCommand(TaskList tasks, String fullCommand) throws Exception {
switch (commandAction.toLowerCase()) {
case "bye":
return true;
case "list":
//print the task list
tasks.printList();
break;
case "find":
if(commandAction.matches(fullCommand)){
System.out.println("OOPS! The description of todo cannot be empty");
} else {
tasks.findMatching(fullCommand);
}
break;
case "todo":
//add todo to task list, no date
if(commandAction.matches(fullCommand)){
// throw new DukeException();
System.out.println("OOPS! The description of todo cannot be empty");
} else {
tasks.addTodo(commandAction, fullCommand);
}
break;
case "deadline":
//add deadline to task list, got date
if(commandAction.matches(fullCommand)){
// throw new DukeException();
System.out.println("OOPS! The description of deadline cannot be empty");
} else {
tasks.addDeadline(commandAction, fullCommand);
}
break;
case "event":
//add event to task list, got date
if(commandAction.matches(fullCommand)){
// throw new DukeException();
System.out.println("OOPS! The description of event cannot be empty");
} else {
tasks.addEvent(commandAction, fullCommand);
}
break;
case "mark":
tasks.markDone(fullCommand);
break;
case "unmark":
tasks.markUndone(fullCommand);
break;
case "delete":
tasks.deleteTask(fullCommand);
break;
default:
System.out.println("OOPS! I'm sorry, I don't know what that means :(");
}
return false;
}
}
59 changes: 53 additions & 6 deletions src/main/java/Duke.java
Original file line number Diff line number Diff line change
@@ -1,10 +1,57 @@
import java.io.FileNotFoundException;

/**
* Duke is a task tracking application that can read user inputs and execute the commands
*/
public class Duke {
private Storage storage;
private TaskList tasks;
private Ui ui;

/**
* Reads from local txt file for existing TaskList, and creates a new file if it doesn't exist
*/
public Duke(String filePath) {
ui = new Ui();
storage = new Storage(filePath);
try {
tasks = new TaskList();
Storage.getFileContents(filePath, tasks);
} catch (FileNotFoundException e) {
Ui.showFileWriteError();
tasks = new TaskList();
}
}

/**
* Runs the application till user ends it by the input "bye"
*/
private void run() {
ui.welcomeMessage();
boolean isExit = false;
while (!isExit) {
try {
String commandInput = ui.readCommand();
ui.showDivider();
Command command = new Command(Parser.parse(commandInput));
isExit = command.executeCommand(tasks, commandInput);
} catch (Exception e) {
ui.showError("Invalid command! Try again :)");
} finally {
System.out.println();
}
}
ui.goodbyeMessage();
}

/**
* Entry point for Duke
*/
public static void main(String[] args) {
String logo = " ____ _ \n"
+ "| _ \\ _ _| | _____ \n"
+ "| | | | | | | |/ / _ \\\n"
+ "| |_| | |_| | < __/\n"
+ "|____/ \\__,_|_|\\_\\___|\n";
System.out.println("Hello from\n" + logo);
new Duke("data/tasks.txt").run();
}




}
2 changes: 2 additions & 0 deletions src/main/java/DukeException.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
public class DukeException extends Throwable {
}
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

23 changes: 23 additions & 0 deletions src/main/java/Parser.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
/**
* Parser parses user input to split up commands and its descriptions
*/
public class Parser {

public static String parse(String command) throws Exception{
String[] items = command.split(" ");
return items[0];
}

public static String parseDescription(String command) throws Exception{
return command.substring(command.indexOf(' ') + 1);
}

public static String parseDeadline(String command) throws Exception{
return command.substring(0, command.indexOf('/')) + "(by: " +command.substring(command.indexOf('/') + 1) + ")";
}

public static String parseEvent(String command) throws Exception{
return command.substring(0, command.indexOf('/')) + "(at: " +command.substring(command.indexOf('/') + 1) + ")";
}

}
72 changes: 72 additions & 0 deletions src/main/java/Storage.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@

import java.io.*;
import java.util.ArrayList;
import java.util.Scanner;

/**
* Storage deals with loading tasks from the file and saving tasks in the file.
*/
public class Storage {
private static String filePath;
public Storage(String filePath) {
this.filePath = filePath;
}

/**
* Saves the given task list to a specified local file
*/
public static void writeToFile(String filePath, ArrayList<Task> tasks) {
try {
File storedFile = new File(filePath);
FileWriter writeFile = new FileWriter(storedFile);
for (Task task : tasks) {
Boolean isDone = task.isDone;
writeFile.write(task.type + "|" + isDone + "|" + task.description + System.lineSeparator());
}
writeFile.close();
} catch (IOException e) {
Ui.showFileWriteError();
}
}

/**
* Extracts the from the local file and puts the information into a task list
*/
public static void getFileContents(String filePath, TaskList tasks) throws FileNotFoundException {
File storedFile = new File(filePath);
if (!storedFile.getParentFile().exists()) {
storedFile.getParentFile().mkdirs();
}
try {
if (!storedFile.exists()) {
storedFile.createNewFile();
}
} catch (IOException e) {
Ui.showFileWriteError();
}
Scanner s = new Scanner(new FileInputStream((filePath)));
while (s.hasNext()) {
String command = s.nextLine();
String inputCommands[] = command.split("\\|", 3);
switch (inputCommands[0]) {
case "todo":
Task todo = new Task("todo", inputCommands[2]);
TaskList.checkMarked(todo,inputCommands[1]);
tasks.add(todo);
break;
case "deadline":
Task deadline = new Task("deadline", inputCommands[2]);
TaskList.checkMarked(deadline,inputCommands[1]);
tasks.add(deadline);
break;
case "event":
Task event = new Task("event", inputCommands[2]);
TaskList.checkMarked(event,inputCommands[1]);
tasks.add(event);
break;
default:
throw new FileNotFoundException();
}
}
}
}
Loading