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.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 Down
10 changes: 0 additions & 10 deletions src/main/java/Duke.java

This file was deleted.

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

import Duke.ExceptionList.*;
import Duke.Storage.Storage;
import Duke.TaskList.*;
import Duke.Ui.Ui;

import java.util.Scanner;
import java.io.IOException; // Import the IOException class to handle errors
import java.util.regex.Pattern;

public class Duke {

private Storage storage;
private TaskList tl;
private Ui ui;

public Duke(){
run();
}

private Pattern pattern = Pattern.compile("-?\\d+(\\.\\d+)?");

public boolean isNumeric(String strNum) {
if (strNum == null) {
return false;
}
return pattern.matcher(strNum).matches();
}

public void run() {
ui = new Ui();
ui.showStartMessage();
ui.printBreak();
storage = new Storage();
try {
tl = new TaskList(storage.load());
}
catch (DukeException | IOException e) {
ui.showLoadingError();
tl = new TaskList();
}
String line, desc, desc2, s1, s2;
while (true) {
Scanner in = new Scanner(System.in);
line = in.nextLine();
try {
if (!line.isEmpty()) {
desc2 = "";
if (line.contains(" ")) {

String[] input = line.split(" ", 2);
desc = input[0];
desc2 = input[1];
} else {
desc = line;
}
if (line.equalsIgnoreCase("bye")) {
storage.saveTask(tl);
break;
}
if (line.equalsIgnoreCase("list")) {
ui.printBreak();
tl.printTaskList();
ui.printBreak();
} else if (desc.equalsIgnoreCase("done")) {
if (desc2.isEmpty() || !isNumeric(desc2)){
throw new CommandNotFoundException("Please enter a number.");
}else if (Integer.parseInt(desc2) > tl.getSize() || Integer.parseInt(desc2) <= 0){
throw new CommandNotFoundException("Sorry, Please enter a legit number.");
}
ui.printBreak();
int x = Integer.parseInt(desc2);
tl.setDoneStatus(x);
ui.printBreak();
} else if (desc.equalsIgnoreCase("delete")) {
if (tl.getSize()==0){
throw new NoListFoundException("Sorry, I am unable to delete any task because no task is found.");
}
if (desc2.isEmpty()|| !isNumeric(desc2)){
throw new CommandNotFoundException("Please enter a number.");
}else if (Integer.parseInt(desc2) > tl.getSize() || Integer.parseInt(desc2) <= 0){
throw new CommandNotFoundException("Sorry, Please enter a legit number.");
}
ui.printBreak();
int x = Integer.parseInt(desc2);
System.out.println("I've deleted this task entered: ");
tl.deleteTask(x);
ui.printBreak();
} else if (desc.equalsIgnoreCase("deadline")) {
if (desc2.isEmpty()){
throw new deadlineNotFoundException("Sorry, there cannot be any empty deadline task.");
}
ui.printBreak();
tl.addTask(desc,desc2,false);
System.out.println("Got it. I've added this task: ");
System.out.println(tl.getLastAddedTask());
ui.printTotalTask(tl.getSize());
ui.printBreak();
} else if (desc.equalsIgnoreCase("event")) {
if (desc2.isEmpty()){
throw new eventNotFoundException("Sorry, there cannot be any empty event task.");
}
ui.printBreak();
tl.addTask(desc,desc2,false);
System.out.println("Got it. I've added this task: ");
System.out.println(tl.getLastAddedTask());
ui.printTotalTask(tl.getSize());
ui.printBreak();
} else if (desc.equalsIgnoreCase("todo")) {
if (desc2.isEmpty()){
throw new toDoNotFoundException("Sorry, there cannot be any empty todo task.");
}
ui.printBreak();
tl.addTask(desc,desc2,false);
System.out.println("Got it. I've added this task: ");
System.out.println(tl.getLastAddedTask());
ui.printTotalTask(tl.getSize());
ui.printBreak();
} else {
throw new CommandNotFoundException("Sorry, I am unable to handle your request.");
}
}
} catch (toDoNotFoundException | CommandNotFoundException | NoListFoundException | deadlineNotFoundException | eventNotFoundException e ) {
ui.printBreak();
System.out.println(e.getMessage());
ui.printBreak();
continue;
}
}
ui.printBreak();
ui.printBye();
ui.printBreak();
}

public static void main(String[] args) {
new Duke();
}
}
11 changes: 11 additions & 0 deletions src/main/java/Duke/ExceptionList/CommandNotFoundException.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
package Duke.ExceptionList;

public class CommandNotFoundException extends Exception{
//no other code needed
public CommandNotFoundException(String message) {
super(message);
}
public CommandNotFoundException(){
super();
}
}
10 changes: 10 additions & 0 deletions src/main/java/Duke/ExceptionList/DukeException.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
package Duke.ExceptionList;

public class DukeException extends Exception {
public DukeException(String message) {
super(message);
}
public DukeException(){
super();
}
}
12 changes: 12 additions & 0 deletions src/main/java/Duke/ExceptionList/NoListFoundException.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
package Duke.ExceptionList;

public class NoListFoundException extends Exception {
//no other code needed
public NoListFoundException(String message) {
super(message);
}

public NoListFoundException() {
super();
}
}
11 changes: 11 additions & 0 deletions src/main/java/Duke/ExceptionList/deadlineNotFoundException.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
package Duke.ExceptionList;

public class deadlineNotFoundException extends Exception{
//no other code needed
public deadlineNotFoundException(String message) {
super(message);
}
public deadlineNotFoundException(){
super();
}
}
10 changes: 10 additions & 0 deletions src/main/java/Duke/ExceptionList/eventNotFoundException.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
package Duke.ExceptionList;

public class eventNotFoundException extends Exception{
public eventNotFoundException(String message) {
super(message);
}
public eventNotFoundException(){
super();
}
}
11 changes: 11 additions & 0 deletions src/main/java/Duke/ExceptionList/toDoNotFoundException.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
package Duke.ExceptionList;

public class toDoNotFoundException extends Exception{
//no other code needed
public toDoNotFoundException(String message) {
super(message);
}
public toDoNotFoundException(){
super();
}
}
79 changes: 79 additions & 0 deletions src/main/java/Duke/Storage/Storage.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
package Duke.Storage;

import java.io.FileWriter;
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;

import Duke.TaskList.*;
import java.util.ArrayList;

public class Storage {
protected final String DEFAULT_STORAGE_FILEPATH = "duke.txt";
protected final String home = System.getProperty("user.home");
private String filePath = DEFAULT_STORAGE_FILEPATH;
protected Path path;

public Storage(){
filePath = DEFAULT_STORAGE_FILEPATH;
path = Paths.get(home, filePath);
}
public Storage(String filepath) throws IOException {
this.filePath = filePath;
path = Paths.get(home, filePath);
if (!isValidPath(path)) {}
//check if file exist
isPathExist();
}

private static boolean isValidPath(Path filePath) {
return filePath.toString().endsWith(".txt");
}

private void isPathExist() throws IOException {
Path path = Paths.get(home, filePath);
// check if the directory and file exists, else create
if (!Files.exists(path)) {
Files.createDirectory(Paths.get(home,"data"));
Files.createFile(path);
}
}
/**
* This method save all the tasks in the ArrayList of TaskLists.
*
* @param tasks ArrayList of Task in TaskLists.
* */
public void saveTask(TaskList tasks){
path = Paths.get(home, "data",DEFAULT_STORAGE_FILEPATH);

// start saving task
String content = "";
ArrayList<Task> save = tasks.getTaskList();
for (Task task : save)
content += task.saveTask() + System.lineSeparator();
//System.out.println(content);
try{
writeToFile(path+"",content);
}catch(IOException e){
e.printStackTrace();
}
}
/**
* This method writes all the text into the file.
*
* @param filePath Path of the file to be written.
* @param text String of the text to be stored in the file.
* */
private void writeToFile(String filePath, String text) throws IOException {
FileWriter fw = new FileWriter(filePath);
fw.write(text);
fw.close();
}

public String load() throws IOException {
String path = home+"\\data\\"+filePath;
//return (File) Files.readAllLines(Path.of(path));
return path; // create a File for the given file path
}
}
18 changes: 18 additions & 0 deletions src/main/java/Duke/TaskList/Deadline.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
package Duke.TaskList;

public class Deadline extends Task {
protected String by;

public Deadline(String description, String by) {
super(description);
this.by = by;
}

@Override
public String toString() {
return "[D]" + super.toString() + " (by: " + by + ")";
}

@Override
public String saveTask(){ return "D|" + super.saveTask() + "|" + by;}
}
19 changes: 19 additions & 0 deletions src/main/java/Duke/TaskList/Event.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
package Duke.TaskList;

public class Event extends Task{
protected String at;

public Event(String description, String at) {
super(description);
this.at = at;
}

@Override
public String toString() {
return "[E]" + super.toString() + " (at: " + at + ")";
}

@Override
public String saveTask(){ return "E|" + super.saveTask() + "|" + at;}

}
Loading