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
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
# Duke project template
# 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 @@ -15,7 +15,7 @@ Prerequisites: JDK 11, update Intellij to the most recent version.
1. Click `Open or Import`.
1. Select the project directory, and click `OK`
1. If there are any further prompts, accept the defaults.
1. After the importing is complete, locate the `src/main/java/Duke.java` file, right-click it, and choose `Run Duke.main()`. If the setup is correct, you should see something like the below:
1. After the importing is complete, locate the `src/main/java/duke.java` file, right-click it, and choose `Run duke.main()`. If the setup is correct, you should see something like the below:
```
Hello from
____ _
Expand Down
3 changes: 3 additions & 0 deletions duke.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
T|false|read book
T|false|return book
T|false|borrow book
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

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

import java.io.IOException;

public class Duke {
private static Storage storage;
private static TaskList tasks;
private static Ui ui;
private static final String FILE_PATH="duke.txt";
public static int thingsCounted = 0;

public Duke(String filePath) throws IOException {
ui = new Ui();
storage = new Storage(filePath);
tasks = new TaskList();
}

public void run() throws DukeException {
thingsCounted = storage.readFromFile(tasks);
ui.start();
ui.count(thingsCounted);
ui.doTasks(tasks);
ui.end();
storage.writeToFile(tasks);
}

public static void main(String[] args) throws DukeException, IOException {
new Duke(FILE_PATH).run();
}
}
5 changes: 5 additions & 0 deletions src/main/java/duke/DukeException.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
package duke;

public class DukeException extends Exception {

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

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

public class Parser {
public static String getCommand(String command) {
try{
if (command.equals("list")) {
return "list";
} else if (command.equals("bye")) {
return "bye";
} else if ( command.equals("todo") || command.equals("deadline")
|| command.equals("event") || command.equals("done")
|| command.equals("delete")) {
throw new DukeException();
} else if (command.contains("todo")) {
return "todo";
} else if (command.contains("event")) {
return "event";
} else if (command.contains("deadline")) {
return "deadline";
} else if (command.contains("delete")) {
return "delete";
} else if (command.contains("done")) {
return "done";
} else if (command.contains("find")) {
return "find";
} else {
throw new DukeException();
}
} catch (DukeException e) {
Ui.dealWithException(command);
return "fail";
}
}

public static String getDateFormat(String datetime) {
LocalDate date;
String dateForm;
try {
date = LocalDate.parse(datetime);
dateForm = date.format(DateTimeFormatter.ofPattern("MMM dd yyyy"));
} catch (Exception e) {
dateForm = datetime;
}
return dateForm;
}
}
64 changes: 64 additions & 0 deletions src/main/java/duke/Storage.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
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.ArrayList;
import java.util.Scanner;

public class Storage {
public static ArrayList<Task> taskArrayList;
private static File f;
private static String filePath;
public static int fileTasksCounted = 0;

public static void writeToFile(TaskList tasks){
try {
FileWriter fw = new FileWriter(filePath);
taskArrayList = tasks.getTasksList();
for(Task task: taskArrayList) {
fw.write(task.printIntoFile() + "\n");
}
fw.close();
} catch (IOException e) {
System.out.println("Error writing file");
}
}

public static int readFromFile(TaskList tasks){
try {
Scanner sc = new Scanner(f);
Task task;
while(sc.hasNext()){
String[] taskInFile = sc.nextLine().split("\\|");
if(taskInFile[0].equals("T")){
task = new Todo(taskInFile[2]);
}else if(taskInFile[0].equals("D")){
task = new Deadline(taskInFile[2],taskInFile[3]);
}else{
task = new Event(taskInFile[2], taskInFile[3]);
}
fileTasksCounted++;
if(taskInFile[1].equals("true")){
task.taskDone();
}
tasks.add(task);
}
} catch (FileNotFoundException e) {
System.out.println("Error reading file");
}
return fileTasksCounted;
}

public Storage(String filePath) throws IOException {
f = new File(filePath);
f.createNewFile();
this.filePath=filePath;
}
}
119 changes: 119 additions & 0 deletions src/main/java/duke/TaskList.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,119 @@
package duke;

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

import java.util.ArrayList;
import static java.util.stream.Collectors.toList;

public class TaskList {
public static ArrayList<Task> tasksList;

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

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

public void add(Task task){
tasksList.add(task);
}

public Task remove(int index){
Task removeTask = tasksList.remove(index);
return removeTask;
}

public Task get(int index){
Task getTask=tasksList.get(index);
return getTask;
}

public ArrayList<Task> getTasksList(){
return tasksList;
}

public static void listTasks() {
int count = 1;
for(Task task : tasksList){
System.out.print(count + ". ");
System.out.println(task);
count++;
}
}

public static void findTasks(String command) {
String findText = command.substring(4);
ArrayList<Task> findTasks = (ArrayList<Task>) tasksList.stream()
.filter((t) -> t.getDescription().contains(findText))
.collect(toList());
System.out.println("Here are the matching tasks in your list:");
int count = 1;
for(Task task : findTasks){
System.out.print(count + ". ");
System.out.println(task);
count++;
}
if(count==1){
System.out.println("no matching result");
}
}

public static void addTodoTask(String command) throws DukeException{
String todoDescription;
todoDescription = command.substring(4);
Task task = new Todo(todoDescription);
tasksList.add(task);
Ui.printTask(task);
}

public static void addDeadlineTask(String command) throws DukeException{
String deadlineDescription;
String deadlineDate;
int index;
index = command.indexOf("/");
deadlineDescription = command.substring(9,index-1);
deadlineDate = command.substring(index+4);
Task task = new Deadline(deadlineDescription, deadlineDate);
tasksList.add(task);
Ui.printTask(task);

}

public static void addEventTask(String command) throws DukeException{
String eventDescription;
String eventDate;
int index;
index = command.indexOf("/");
eventDescription = command.substring(6,index-1);
eventDate = command.substring(index+4);
Task task = new Event(eventDescription, eventDate);
tasksList.add(task);
Ui.printTask(task);
}

public static void markTaskAsDone(String command, int countTasks) throws DukeException{
int index = Integer.parseInt(command.substring(4));
if(index > countTasks){
throw new DukeException();
}
tasksList.get(index-1).taskDone();
System.out.println("Nice! I've marked this task as done:\n" + tasksList.get(index-1));
}

public static void delete(String command, int tasksCounted) throws DukeException {
int index = Integer.parseInt(command.substring(6));
if(index > tasksCounted){
throw new DukeException();
}
Task task = tasksList.remove(index - 1);
tasksCounted--;
System.out.println("Noted. I've removed this task: ");
System.out.println(task);
System.out.println("Now you have " + tasksCounted + " tasks in the list.");
}
}
Loading