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

[victorouy] iP #207

Open
wants to merge 25 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
34 changes: 14 additions & 20 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,24 +1,18 @@
# Duke project template
# Duke Program User Guide

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.
## Input commands
### Add to task
- todo [description]
- deadline [description]/[by]
- event [description]/[from]/[to]

## Setting up in Intellij
### Manipulate tasks
- mark [task number]
- delete [task number]

Prerequisites: JDK 11, update Intellij to the most recent version.
### Get tasks
- list
- find [search]

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 11** (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.
3. 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
____ _
| _ \ _ _| | _____
| | | | | | | |/ / _ \
| |_| | |_| | < __/
|____/ \__,_|_|\_\___|
```
### Exit program
- bye
3 changes: 3 additions & 0 deletions data/tasks.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
T|0|hellp
D|1|help /monday
T|0|fasdfsdaf
39 changes: 14 additions & 25 deletions docs/README.md
Original file line number Diff line number Diff line change
@@ -1,29 +1,18 @@
# User Guide
# Duke Program User Guide

## Features
## Input commands
### Add to task
- todo [description]
- deadline [description]/[by]
- event [description]/[from]/[to]

### Feature-ABC
### Manipulate tasks
- mark [task number]
- delete [task number]

Description of the feature.
### Get tasks
- list
- find [search]

### Feature-XYZ

Description of the feature.

## Usage

### `Keyword` - Describe action

Describe the action and its outcome.

Example of usage:

`keyword (optional arguments)`

Expected outcome:

Description of the outcome.

```
expected output
```
### Exit program
- bye
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.main.Duke

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

/**
* Parses the user's inputs by making sense of the user command
*/
public class Parser {
private String input;

public void setInput(String input) {
this.input = input;
}

/**
* Parse the input to find the index of task to be marked
*
* @return index to be marked
*/
public int parseMarkIndex() {
String stringListNumber = input.substring(5);
return Integer.parseInt(stringListNumber) - 1;
}

/**
* Parse the input to find the index of task to be deleted
*
* @return index to be deleted
*/
public int parseDeleteIndex() {
String stringListNumber = input.substring(7);
return Integer.parseInt(stringListNumber) - 1;
}

/**
* Parse input to get deadline date
*
* @return string of deadline date
*/
public String parseDeadlineBy() {
return input.substring(input.lastIndexOf("/") + 1);
}

public String parseEventFrom() {
String tempInput = input.substring(input.indexOf("/") + 1);
return tempInput.substring(0, tempInput.indexOf("/"));
}

public String parseEventTo() {
String tempInput = input.substring(input.indexOf("/") + 1);
return tempInput.substring(tempInput.lastIndexOf("/") + 1);
}

public boolean validateEventInput() {
return input.matches(".*/.*/.*");
}
}
122 changes: 122 additions & 0 deletions src/main/java/duke/entity/Storage.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,122 @@
package duke.entity;

import duke.exceptions.DukeException;
import duke.task.Deadline;
import duke.task.Event;
import duke.task.Task;
import duke.task.Todo;

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

/**
* Class used to deal with loading tasks from the file and saving tasks in the file
*/
public class Storage {
private String filePath;

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

/**
* Load or creates the data file into an array list of task
*
* @return loaded array list of task
* @throws DukeException if data will not be saved
*/
public ArrayList<Task> load() throws DukeException {
String[] filePathArr = filePath.split("/");
ArrayList<Task> tasks = new ArrayList<Task>();
String tempPath = filePathArr[0];

for (int i = 0; i < filePathArr.length; i++) {
if (i != 0) {
tempPath = tempPath + "/" + filePathArr[i];
}
java.nio.file.Path filePath = getPath(tempPath);
boolean pathExists = java.nio.file.Files.exists(filePath);
File pathFile = new File(filePath.toString());

try {
if (i == (filePathArr.length - 1) && pathExists) {
readDukeFile(tasks, pathFile);
} else if (!pathExists) {
if (i == (filePathArr.length - 1)) {
pathFile.createNewFile();
} else {
pathFile.mkdir();
}
}
} catch (IOException e) {
throw new DukeException("Note: data will not be saved (Duke.txt unable to be created)");
}
}
return tasks;
}

public void updateDukeFile(TaskList taskList) {
writeDukeFile(taskList.getTask(0), false);
for (int i = 1; i < taskList.taskSize(); i++) {
writeDukeFile(taskList.getTask(i), true);
}
}

public void writeDukeFile(Task task, boolean appendFile) {
String line = (task.getIsDone() ? "1" : "0") + "|" + task.getDescription();
if (task instanceof Todo) {
line = "T|" + line;
} else if (task instanceof Deadline) {
line = "D|" + line;
} else if (task instanceof Event) {
line = "E|" + line;
}
try {
File dukeFile = new File(getPath(this.filePath).toString());
FileWriter fw = new FileWriter(dukeFile, appendFile);
if (appendFile) {
fw.write("\n" + line);
} else {
fw.write(line);
}
fw.close();
} catch (IOException ex) {}
}

private void readDukeFile(ArrayList<Task> tasks, File dukeFile) throws FileNotFoundException {
Scanner s = new Scanner(dukeFile);
while (s.hasNextLine()) {
String task = s.nextLine();
if (task.length() > 4) {
String taskType = task.substring(0, 1);
boolean isMarked = task.substring(2, 3).equals("0") ? false : true;
String taskDesc = task.substring(4);

if (taskType.equals("T")) {
Task tempTask = new Todo(taskDesc, isMarked);
tasks.add(tempTask);
} else if (taskType.equals("D")) {
Task tempTask = new Deadline(taskDesc, taskDesc.substring(taskDesc.lastIndexOf("/") + 1), isMarked);
tasks.add(tempTask);
} else if (taskType.equals("E")) {
String tempInput = taskDesc.substring(taskDesc.indexOf("/") + 1);
String fromString = tempInput.substring(0, tempInput.indexOf("/"));
String toString = tempInput.substring(tempInput.lastIndexOf("/") + 1);

Task tempTask = new Event(taskDesc, fromString, toString, isMarked);
tasks.add(tempTask);
}
}
}
}

private java.nio.file.Path getPath(String path) {
File f = new File("");
String home = f.getAbsolutePath();
return java.nio.file.Paths.get(home, path);
}
}
53 changes: 53 additions & 0 deletions src/main/java/duke/entity/TaskList.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
package duke.entity;

import duke.task.Task;
import java.util.ArrayList;

/**
* Contains the task list with operations to manipulate tasks in the list
*/
public class TaskList {
private ArrayList<Task> taskArrayList;

public TaskList() {}

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

public ArrayList<Task> getTaskArrayList() {
return taskArrayList;
}

public Task getTask(int index) {
return this.taskArrayList.get(index);
}

public void addTask(Task task) {
taskArrayList.add(task);
}

public int taskSize() {
return taskArrayList.size();
}

public void removeTask(int index) {
this.taskArrayList.remove(index);
}

/**
* Searches for tasks in task list that matches a given keyword
*
* @param keyword string to search for match
* @return contains the found/matched tasks
*/
public ArrayList<Task> findTaskArrayList(String keyword) {
ArrayList<Task> tempTaskArrayList = new ArrayList<Task>();
for (Task task : taskArrayList) {
if (task.getDescription().matches("(.*)" + keyword + "(.*)")) {
tempTaskArrayList.add(task);
}
}
return tempTaskArrayList;
}
}
Loading