Skip to content

Commit

Permalink
Merge branch 'A-MoreOOP'
Browse files Browse the repository at this point in the history
  • Loading branch information
qwertybox123 committed Sep 10, 2023
2 parents 6656f9e + 96f087e commit 92d0237
Show file tree
Hide file tree
Showing 14 changed files with 414 additions and 160 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
}
26 changes: 26 additions & 0 deletions src/main/java/DateConverter.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;

public class DateConverter {
public static String convertDate(String inputDateStr) {
try {
// Define a SimpleDateFormat for parsing the input date
SimpleDateFormat inputDateFormat = new SimpleDateFormat("MMM dd yyyy");

// Parse the input date string to obtain a Date object
Date inputDate = inputDateFormat.parse(inputDateStr);

// Convert the Date to a LocalDate if needed
// For this example, we'll convert it to a string in "yyyy/MM/dd" format
SimpleDateFormat outputDateFormat = new SimpleDateFormat("yyyy/MM/dd");

// Format the Date as a string in "yyyy/MM/dd" format
return outputDateFormat.format(inputDate);
} catch (ParseException e) {
// Handle any parsing errors
return "Invalid date format: " + e.getMessage();
}
}

}
2 changes: 1 addition & 1 deletion src/main/java/Deadline.java
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import java.time.LocalDate;
import java.time.format.DateTimeFormatter;
public class Deadline extends Tasks {
public class Deadline extends Task {
private LocalDate deadline;


Expand Down
278 changes: 134 additions & 144 deletions src/main/java/Duke.java
Original file line number Diff line number Diff line change
@@ -1,172 +1,162 @@
import java.util.Scanner;
import java.util.ArrayList;
import java.io.File;
import java.util.regex.Pattern;
import java.util.regex.Matcher;
import java.time.LocalDate;
import java.time.format.DateTimeFormatter;
import java.io.BufferedReader;
import java.io.FileReader;
import java.io.IOException;
import java.io.FileWriter;
import java.io.BufferedWriter;
import java.io.IOException;

public class Duke {


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);

ArrayList<Tasks> tasksList = new ArrayList<>();
if (file.exists()) {
tasksList = TaskReader.readTasksFromFile(filename);
System.out.println(tasksList);
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
System.out.println("Here are the tasks in your list: ");
if (!tasksList.isEmpty()) {
for (int i = 1; i <= tasksList.size(); i++) {
System.out.println(i + "." + tasksList.get(i - 1));
}
}
} else if (command.startsWith("unmark")) { // unmark the task in question
int taskNumber = Integer.parseInt(command.substring(7)) - 1;
if (taskNumber < tasksList.size()) {
Tasks task = tasksList.get(taskNumber);
task.setMarked(false);
tasksList.set(taskNumber, task);
System.out.println("OK, I've marked this task as not done yet:\n" + " " + tasksList.get(taskNumber));
}
} else if (command.startsWith("mark")) { // mark the task in question
int taskNumber = Integer.parseInt(command.substring(5)) - 1;
if (taskNumber < tasksList.size()) {
Tasks task = tasksList.get(taskNumber);
task.setMarked(true);
tasksList.set(taskNumber, task);
System.out.println("Nice! I've marked this task as done:\n" + " " + tasksList.get(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);
tasksList.add(todo);

System.out.println("Got it. I've added this task:");
System.out.println(" " + todo);
System.out.println("Now you have " + tasksList.size() + " tasks in the list.");
} 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);
tasksList.add(deadlineTask);
System.out.println("Got it. I've added this deadline:");
System.out.println(" " + deadlineTask);
System.out.println("Now you have " + tasksList.size() + " tasks in the list.");
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);
tasksList.add(eventTask);

// Print confirmation message
System.out.println("Got it. I've added this task:");
System.out.println(" " + eventTask);
System.out.println("Now you have " + tasksList.size() + " tasks in the list.");
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 < tasksList.size()) {
Tasks task = tasksList.get(taskNumber);
tasksList.remove(taskNumber);

System.out.println("Noted. I've removed this task: \n" + " " + task);
System.out.println("Now you have " + tasksList.size() + " tasks in the list.");
}


} else {
try {
throw new UnknownInputException();
} catch (UnknownInputException e) {
System.out.println(e.getMessage());
}
}
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 {
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(tasksList, "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);
}
}





2 changes: 1 addition & 1 deletion src/main/java/Event.java
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
public class Event extends Tasks {
public class Event extends Task {
private String startTime;
private String endTime;

Expand Down
Loading

0 comments on commit 92d0237

Please sign in to comment.