Skip to content
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
5 changes: 3 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
# Duke project template
# duke.Duke project template

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.

Expand All @@ -13,7 +13,7 @@ Prerequisites: JDK 11, update Intellij to the most recent version.
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:
3. After that, locate the `src/main/java/duke.Duke.java` file, right-click it, and choose `Run duke.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
____ _
Expand All @@ -22,3 +22,4 @@ Prerequisites: JDK 11, update Intellij to the most recent version.
| |_| | |_| | < __/
|____/ \__,_|_|\_\___|
```
4. Duke is part of https://se-education.org
4 changes: 4 additions & 0 deletions data/duke.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
E | 1 | read book | 2021-10-11 - 2021-10-13
D | 0 | return book | 2021-10-14
T | 0 | sporting
D | 0 | sporting | 2021-11-13
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

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

import java.time.LocalDate;
import java.time.format.DateTimeFormatter;
import java.util.Locale;

public class DateFun {

public static LocalDate stringToLocalDate(String timeStr){
DateTimeFormatter fmt = DateTimeFormatter.ofPattern("yyyy-MM-dd");
LocalDate date = LocalDate.parse(timeStr, fmt);
return date;
}

/**
* date convert to string
* @param date: a LocalDate variable
* @return a string which shows date
*/
public static String LocalDateToString(LocalDate date){
DateTimeFormatter fmt = DateTimeFormatter.ofPattern("MMM dd yyyy");
return date.format(fmt);
}

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

import duke.command.Command;
import duke.exception.InvalidException;
import duke.exception.MissReqException;
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.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.Scanner;

public class Duke {

private TaskList tasks;
private Storage storage;
private Ui ui;
private boolean isExit;

public Duke(){isExit = false;}

public Duke(String path){
ui = new Ui();
storage = new Storage(path);
isExit = false;
try{
tasks = storage.load();
}catch (FileNotFoundException e){
tasks = new TaskList();
ui.showLoadException();
}

}



public void dukeGreet(){
String logo = " ____ _ \n"
+ "| _ \\ _ _| | _____ \n"
+ "| | | | | | | |/ / _ \\\n"
+ "| |_| | |_| | < __/\n"
+ "|____/ \\__,_|_|\\_\\___|\n";
System.out.println("Hello from\n" + logo);
ui.showLine();
System.out.println(" Hello! I'm duke.Duke");
System.out.println(" What can I do for you?\n");
ui.showLine();
}


public void dukeEcho(){
while(!isExit){
String req = ui.readReq();
try{
Command command = Parser.parse(req);
command.execute(tasks, ui, storage);
isExit = command.isExit();
}catch (IOException e){
ui.showStoreException();
}catch (Exception e){
ui.showException(e, req);
}
}

}

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

import duke.command.*;
import duke.exception.InvalidException;
import duke.exception.MissReqException;
import duke.task.Deadline;
import duke.task.Event;
import duke.task.Task;
import duke.task.Todo;

public class Parser {
/**
*
* @param req Extract task from command
* @return a task object
* @throws MissReqException
* @throws InvalidException
*/
public static Task getTask(String req) throws MissReqException, InvalidException {
String[] reqs = req.trim().split(" ");
String type = reqs[0];
String description;
assert reqs.length > 1 : "invalid input";
if(type.equals("todo")){
if(reqs.length == 1){
throw new MissReqException("todo");
}
description = req.replaceFirst("todo", "").trim();
return new Todo(description);
} else if(type.equals("deadline")){
if(reqs.length == 1){
throw new MissReqException("deadline");
}
String[] descriptions = req.replaceFirst("deadline", "").trim().split("/by ");
assert descriptions.length == 2: "invalid input";
description = descriptions[0].trim();
String by = descriptions[1];
return new Deadline(description, by);
} else if(type.equals("event")){
if(reqs.length == 1){
throw new MissReqException("event");
}
String[] descriptions = req.replaceFirst("event", "").trim().split("/at ");
assert descriptions.length == 2: "invalid input";
description = descriptions[0].trim();
String at = descriptions[1];
return new Event(description, at);
} else{
throw new InvalidException();
}
}

/**
* Extract command
* @param req the command input of user
* @return Variable of Command
* @throws MissReqException
* @throws InvalidException
*/
public static Command parse(String req) throws MissReqException, InvalidException {
String[] reqs = req.split(" ");
if(req.equals("bye")){
return new ExitCommand(req);
}
if(reqs[0].equals("list")) {
assert reqs.length == 1: "invalid input";
return new ListCommand(req);
}
if(reqs[0].equals("done")) {
assert reqs.length == 2: "invalid input";
int idx = Integer.parseInt(req.substring(4).trim()) - 1;
return new DoneCommand(req, idx);
}
if(reqs[0].equals("delete")) {
assert reqs.length == 2: "invalid input";
int idx = Integer.parseInt(reqs[1]) - 1;
return new DeleteCommand(req, idx);
}
if(reqs[0].equals("find")){
assert reqs.length > 1: "invalid input";
String search = reqs[1];
return new FindCommand(req, search);
}
Task task = getTask(req);
return new AddCommand(req, task);
}


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

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.Scanner;

public class Storage {
public String filepath;

/**
* storage constructor
* @param filepath the address of file path
*/
public Storage(String filepath){
this.filepath = filepath;

}

/**
* store the task list to selected file
* @param tasks
* @throws IOException
*/

public void store(TaskList tasks) throws IOException {
File file = new File(filepath);
File dir = file.getParentFile();
if(!file.exists()){
if(!dir.exists()){
dir.mkdirs();
}
file.createNewFile();
}
FileWriter fw = new FileWriter(file);
for(int i = 0; i < tasks.size(); i++){
fw.write(tasks.taskList.get(i).toStoreString());
}
fw.close();
}


/**
* load task list from selected file
* @return task List from selected file
* @throws FileNotFoundException
*/
public TaskList load() throws FileNotFoundException {
TaskList tasks = new TaskList();
File file = new File(filepath);
Scanner sc = new Scanner(file);
while(sc.hasNext()){
String line = sc.nextLine();
String[] data = line.split(" \\| ");
boolean isDone = data[1].equals("1") ? true : false;
Task task;
if(data[0].equals("T")){
task = new Todo(isDone, data[2]);
}
else if(data[0].equals("D")){
task = new Deadline(isDone, data[2], data[3]);
}
else{
task = new Event(isDone, data[2], data[3]);
}
tasks.addTask(task);
}
return tasks;
}

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

import duke.task.Task;

import java.util.ArrayList;

public class TaskList {
public ArrayList<Task> taskList = new ArrayList<>();

public void addTask(Task t){
taskList.add(t);
}

public Task deleteTask(int idx){
Task t = taskList.get(idx);
taskList.remove(idx);
return t;
}

public Task doneTask(int idx){
taskList.get(idx).doTask();
return taskList.get(idx);
}

public int size(){
return taskList.size();
}

public TaskList find(String search){
TaskList tl = new TaskList();
for(Task task: taskList){
if(task.containInfo(search)){
tl.addTask(task);
}
}
return tl;
}
}
Loading