Skip to content

Commit

Permalink
Added UI,Parser, Storage and TaskList
Browse files Browse the repository at this point in the history
  • Loading branch information
qwertybox123 committed Sep 10, 2023
1 parent f56c1a7 commit 96f087e
Show file tree
Hide file tree
Showing 10 changed files with 295 additions and 127 deletions.
13 changes: 13 additions & 0 deletions src/main/java/Command.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
public enum Command {
TODO,
DEADLINE,
EVENT,
LIST,
DONE,
DELETE,
FIND,
EXIT,
INVALID,
UNMARK,
MARK
}
245 changes: 130 additions & 115 deletions src/main/java/Duke.java
Original file line number Diff line number Diff line change
Expand Up @@ -7,141 +7,156 @@

public class Duke {

//private Storage storage;
//private TaskList tasks;
//private Ui ui;



public static void main(String[] args) {
String name = "Johnnythesnake";
System.out.println("Hello I'm " + name + "\n" + "What can I do for you? Aside from completing your CS2103 project for you");
Scanner scanner = new Scanner(System.in);
String filename = "tasks.txt";
// Create a File object with the filename
File file = new File(filename);

TaskList tasks = new TaskList();
if (file.exists()) {
tasks = TaskReader.readTasksFromFile(filename);
System.out.println(tasks);
private final Storage storage;
private TaskList tasks;
private final Ui ui;

public Duke(String filePath) {
ui = new Ui();
storage = new Storage(filePath);
try {
tasks = storage.load();
} catch (DukeException e) {
ui.showLoadingError();
tasks = new TaskList();
}
while (true) {
System.out.println("Enter a command: ");
String command = scanner.nextLine();
if (command.equalsIgnoreCase("bye")) { // bye exits the code
Exit exit = new Exit();
System.out.println(exit.exitMessage());
break;
} else if (command.equalsIgnoreCase("list")) { //list shows the task list
tasks.listOfTasks();
} else if (command.startsWith("unmark")) { // unmark the task in question
int taskNumber = Integer.parseInt(command.substring(7)) - 1;
if (taskNumber < tasks.size()) {
tasks.unmarkTask(taskNumber);;
}
} else if (command.startsWith("mark")) { // mark the task in question
int taskNumber = Integer.parseInt(command.substring(5)) - 1;
if (taskNumber < tasks.size()) {
tasks.markTaskAsDone(taskNumber);
}


} else if (command.startsWith("todo")) {
String description = command.substring(4).trim(); // Trim any leading/trailing spaces

try {
if (description.isEmpty()) {
throw new EmptyTodoException();
}
}

Todo todo = new Todo(description, false);
tasks.addTask(todo);

} catch (EmptyTodoException e) {
System.out.println(e.getMessage());
}
} else if (command.startsWith("deadline")) {
// Split the input
String descriptionDeadline = command.substring(8).trim(); // Remove "deadline" and leading spaces
public void run() {
ui.showWelcomeMessage();

if (descriptionDeadline.isEmpty()) {
while (true) {
String userInput = ui.getUserInput();
Command command = Parser.parseCommand(userInput);
String description = Parser.parseDescription(userInput);
switch (command) {
case EXIT:
storage.save(tasks, "tasks.txt");
ui.closeScanner();
ui.showExitMessage();
return;
case LIST:
ui.showTaskList(tasks);
break;
case UNMARK:
try {
throw new EmptyDeadlineException();
} catch (EmptyDeadlineException e) {
System.out.println(e.getMessage());
int taskNumber = Integer.parseInt(description) - 1;
if (taskNumber >= 0 && taskNumber < tasks.size()) {
tasks.unmarkTask(taskNumber);
} else {
ui.showError("Task number out of range.");
}
} catch (NumberFormatException e) {
ui.showError("Invalid task number. Please provide a valid integer.");
}
} else {
// Find the index of the deadline separator "/"
int separatorIndex = descriptionDeadline.indexOf('/');

if (separatorIndex != -1) { // Ensure the separator exists in the input
// Extract the task description and deadline

String description = descriptionDeadline.substring(0, separatorIndex).trim();
String deadline = descriptionDeadline.substring(separatorIndex + 4).trim();
String pattern = "\\d{4}/\\d{2}/\\d{2}";
Pattern datePattern = Pattern.compile(pattern);
Matcher matcher = datePattern.matcher(deadline);
if (matcher.find()) {
DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy/MM/dd");
LocalDate localDateDeadline = LocalDate.parse(deadline, formatter);
Deadline deadlineTask = new Deadline(description,false, localDateDeadline);
tasks.addTask(deadlineTask);

break;
case MARK:
try {
int taskNumber = Integer.parseInt(description) - 1;
if (taskNumber >= 0 && taskNumber < tasks.size()) {
tasks.markTaskAsDone(taskNumber);
} else {
System.out.println("Please input your deadline in YYYY/MM/DD format");
ui.showError("Task number out of range.");
}
} else {
System.out.println("Invalid input format for deadline. Please input in the following format: <deadline> <description> /by <YYYY/MM/DD> ");
} catch (NumberFormatException e) {
ui.showError("Invalid task number. Please provide a valid integer.");
}
}
} else if (command.startsWith("event")) {
// split the input
String descriptionStartEndTime = command.substring(5).trim(); // Remove "event" and leading spaces
if (descriptionStartEndTime.isEmpty()) {
break;
case TODO:
try {
throw new EmptyEventException();
} catch (EmptyEventException e) {
if (description.isEmpty()) {
throw new EmptyTodoException();
}
Todo todo = new Todo(description, false);
tasks.addTask(todo);

} catch (EmptyTodoException e) {
System.out.println(e.getMessage());
}
} else {
// Find the indices of the time separators
int fromIndex = descriptionStartEndTime.indexOf("/from");
int toIndex = descriptionStartEndTime.indexOf("/to");

if (fromIndex != -1 && toIndex != -1) {
// Extract the task description, startTime, and endTime
String description = descriptionStartEndTime.substring(0, fromIndex).trim();
String startTime = descriptionStartEndTime.substring(fromIndex + 5, toIndex).trim();
String endTime = descriptionStartEndTime.substring(toIndex + 3).trim();

// Create a new Event object
Event eventTask = new Event(description, false, startTime, endTime);
tasks.addTask(eventTask);
break;
case DEADLINE:

if (description.isEmpty()) {
try {
throw new EmptyDeadlineException();
} catch (EmptyDeadlineException e) {
System.out.println(e.getMessage());
}
} else {
System.out.println("Invalid input format for event command.");
// Find the index of the deadline separator "/"
int separatorIndex = description.indexOf('/');

if (separatorIndex != -1) { // Ensure the separator exists in the input
// Extract the task description and deadline

String descriptionString = description.substring(0, separatorIndex).trim();
String deadline = description.substring(separatorIndex + 4).trim();
String pattern = "\\d{4}/\\d{2}/\\d{2}";
Pattern datePattern = Pattern.compile(pattern);
Matcher matcher = datePattern.matcher(deadline);
if (matcher.find()) {
DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy/MM/dd");
LocalDate localDateDeadline = LocalDate.parse(deadline, formatter);
Deadline deadlineTask = new Deadline(descriptionString, false, localDateDeadline);
tasks.addTask(deadlineTask);

} else {
System.out.println("Please input your deadline in YYYY/MM/DD format");
}
} else {
System.out.println("Invalid input format for deadline. Please input in the following format: <deadline> <description> /by <YYYY/MM/DD> ");
}
}
}
} else if (command.startsWith("delete")) {
int taskNumber = Integer.parseInt(command.substring(7)) - 1;
if (taskNumber < tasks.size()) {
tasks.deleteTask(taskNumber);
break;
case EVENT:
if (description.isEmpty()) {
try {
throw new EmptyEventException();
} catch (EmptyEventException e) {
System.out.println(e.getMessage());
}
} else {
// Find the indices of the time separators
int fromIndex = description.indexOf("/from");
int toIndex = description.indexOf("/to");

}
if (fromIndex != -1 && toIndex != -1) {
// Extract the task description, startTime, and endTime
String descriptionString = description.substring(0, fromIndex).trim();
String startTime = description.substring(fromIndex + 5, toIndex).trim();
String endTime = description.substring(toIndex + 3).trim();

// Create a new Event object
Event eventTask = new Event(descriptionString, false, startTime, endTime);
tasks.addTask(eventTask);

} else {
try {
throw new UnknownInputException();
} catch (UnknownInputException e) {
System.out.println(e.getMessage());
}
} else {
System.out.println("Invalid input format for event command.");
}
}
break;
case DELETE:
try {
int taskNumber = Integer.parseInt(description) - 1;
if (taskNumber >= 0 && taskNumber < tasks.size()) {
tasks.deleteTask(taskNumber);
} else {
ui.showError("Task number out of range.");
}
} catch (NumberFormatException e) {
ui.showError("Invalid task number. Please provide a valid integer.");
}
break;
case INVALID:
ui.showError("Invalid command. Please try again.");
break;
}

}
TaskWriter.writeTasksToFile(tasks, "tasks.txt");
}

public static void main(String[] args) {
new Duke("tasks.txt").run();
}
}

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





43 changes: 43 additions & 0 deletions src/main/java/Parser.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
public class Parser {
public static Command parseCommand(String userInput) {
String[] inputParts = userInput.split(" ", 2); // Split input into two parts: command and arguments
String command = inputParts[0].toUpperCase(); // Convert command to uppercase for case-insensitivity


switch (command) {
case "TODO":
return Command.TODO;
case "DEADLINE":
return Command.DEADLINE;
case "EVENT":
return Command.EVENT;
case "LIST":
return Command.LIST;
case "DONE":
return Command.DONE;
case "DELETE":
return Command.DELETE;
case "FIND":
return Command.FIND;
case "BYE":
return Command.EXIT;
case "UNMARK":
return Command.UNMARK;
case "MARK":
return Command.MARK;
default:
return Command.INVALID;
}


}

public static String parseDescription(String userInput) {
String[] parts = userInput.split(" ", 2); // Split input into command and arguments
if (parts.length > 1) {
return parts[1];
}
return "";
}

}
32 changes: 32 additions & 0 deletions src/main/java/Storage.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

public class Storage {
public String filePath;

public Storage(String filePath) {

this.filePath = filePath;
}

public TaskList load() throws DukeException {
TaskList tasks = new TaskList();
File file = new File(filePath);
if (file.exists()) {
tasks = TaskReader.readTasksFromFile(filePath);
System.out.println(tasks);
}

return tasks;
}

public void save(TaskList tasks, String filePath) {
TaskWriter.writeTasksToFile(tasks, filePath);
}
}
1 change: 1 addition & 0 deletions src/main/java/Task.java
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ public String getName() {
}

public void setName(String name) {

this.name = name;
}

Expand Down
4 changes: 4 additions & 0 deletions src/main/java/TaskList.java
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,10 @@ public void addTask(Task task) {
System.out.println("Now you have " + tasks.size() + " tasks in the list.");
}

public void addTaskFromStorage(Task task) {
tasks.add(task);
}

public boolean isEmpty() {
return tasks.isEmpty();
}
Expand Down
Loading

0 comments on commit 96f087e

Please sign in to comment.