Skip to content

Commit 92d0237

Browse files
committed
Merge branch 'A-MoreOOP'
2 parents 6656f9e + 96f087e commit 92d0237

File tree

14 files changed

+414
-160
lines changed

14 files changed

+414
-160
lines changed

src/main/java/Command.java

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
public enum Command {
2+
TODO,
3+
DEADLINE,
4+
EVENT,
5+
LIST,
6+
DONE,
7+
DELETE,
8+
FIND,
9+
EXIT,
10+
INVALID,
11+
UNMARK,
12+
MARK
13+
}

src/main/java/DateConverter.java

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
import java.text.ParseException;
2+
import java.text.SimpleDateFormat;
3+
import java.util.Date;
4+
5+
public class DateConverter {
6+
public static String convertDate(String inputDateStr) {
7+
try {
8+
// Define a SimpleDateFormat for parsing the input date
9+
SimpleDateFormat inputDateFormat = new SimpleDateFormat("MMM dd yyyy");
10+
11+
// Parse the input date string to obtain a Date object
12+
Date inputDate = inputDateFormat.parse(inputDateStr);
13+
14+
// Convert the Date to a LocalDate if needed
15+
// For this example, we'll convert it to a string in "yyyy/MM/dd" format
16+
SimpleDateFormat outputDateFormat = new SimpleDateFormat("yyyy/MM/dd");
17+
18+
// Format the Date as a string in "yyyy/MM/dd" format
19+
return outputDateFormat.format(inputDate);
20+
} catch (ParseException e) {
21+
// Handle any parsing errors
22+
return "Invalid date format: " + e.getMessage();
23+
}
24+
}
25+
26+
}

src/main/java/Deadline.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import java.time.LocalDate;
22
import java.time.format.DateTimeFormatter;
3-
public class Deadline extends Tasks {
3+
public class Deadline extends Task {
44
private LocalDate deadline;
55

66

src/main/java/Duke.java

Lines changed: 134 additions & 144 deletions
Original file line numberDiff line numberDiff line change
@@ -1,172 +1,162 @@
11
import java.util.Scanner;
2-
import java.util.ArrayList;
32
import java.io.File;
43
import java.util.regex.Pattern;
54
import java.util.regex.Matcher;
65
import java.time.LocalDate;
76
import java.time.format.DateTimeFormatter;
8-
import java.io.BufferedReader;
9-
import java.io.FileReader;
10-
import java.io.IOException;
11-
import java.io.FileWriter;
12-
import java.io.BufferedWriter;
13-
import java.io.IOException;
147

158
public class Duke {
169

17-
18-
public static void main(String[] args) {
19-
String name = "Johnnythesnake";
20-
System.out.println("Hello I'm " + name + "\n" + "What can I do for you? Aside from completing your CS2103 project for you");
21-
Scanner scanner = new Scanner(System.in);
22-
String filename = "tasks.txt";
23-
// Create a File object with the filename
24-
File file = new File(filename);
25-
26-
ArrayList<Tasks> tasksList = new ArrayList<>();
27-
if (file.exists()) {
28-
tasksList = TaskReader.readTasksFromFile(filename);
29-
System.out.println(tasksList);
10+
private final Storage storage;
11+
private TaskList tasks;
12+
private final Ui ui;
13+
14+
public Duke(String filePath) {
15+
ui = new Ui();
16+
storage = new Storage(filePath);
17+
try {
18+
tasks = storage.load();
19+
} catch (DukeException e) {
20+
ui.showLoadingError();
21+
tasks = new TaskList();
3022
}
31-
while (true) {
32-
System.out.println("Enter a command: ");
33-
String command = scanner.nextLine();
34-
if (command.equalsIgnoreCase("bye")) { // bye exits the code
35-
Exit exit = new Exit();
36-
System.out.println(exit.exitMessage());
37-
break;
38-
} else if (command.equalsIgnoreCase("list")) { //list shows the task list
39-
System.out.println("Here are the tasks in your list: ");
40-
if (!tasksList.isEmpty()) {
41-
for (int i = 1; i <= tasksList.size(); i++) {
42-
System.out.println(i + "." + tasksList.get(i - 1));
43-
}
44-
}
45-
} else if (command.startsWith("unmark")) { // unmark the task in question
46-
int taskNumber = Integer.parseInt(command.substring(7)) - 1;
47-
if (taskNumber < tasksList.size()) {
48-
Tasks task = tasksList.get(taskNumber);
49-
task.setMarked(false);
50-
tasksList.set(taskNumber, task);
51-
System.out.println("OK, I've marked this task as not done yet:\n" + " " + tasksList.get(taskNumber));
52-
}
53-
} else if (command.startsWith("mark")) { // mark the task in question
54-
int taskNumber = Integer.parseInt(command.substring(5)) - 1;
55-
if (taskNumber < tasksList.size()) {
56-
Tasks task = tasksList.get(taskNumber);
57-
task.setMarked(true);
58-
tasksList.set(taskNumber, task);
59-
System.out.println("Nice! I've marked this task as done:\n" + " " + tasksList.get(taskNumber));
60-
}
61-
62-
63-
} else if (command.startsWith("todo")) {
64-
String description = command.substring(4).trim(); // Trim any leading/trailing spaces
65-
66-
try {
67-
if (description.isEmpty()) {
68-
throw new EmptyTodoException();
69-
}
23+
}
7024

71-
Todo todo = new Todo(description, false);
72-
tasksList.add(todo);
7325

74-
System.out.println("Got it. I've added this task:");
75-
System.out.println(" " + todo);
76-
System.out.println("Now you have " + tasksList.size() + " tasks in the list.");
77-
} catch (EmptyTodoException e) {
78-
System.out.println(e.getMessage());
79-
}
80-
} else if (command.startsWith("deadline")) {
81-
// Split the input
82-
String descriptionDeadline = command.substring(8).trim(); // Remove "deadline" and leading spaces
26+
public void run() {
27+
ui.showWelcomeMessage();
8328

84-
if (descriptionDeadline.isEmpty()) {
29+
while (true) {
30+
String userInput = ui.getUserInput();
31+
Command command = Parser.parseCommand(userInput);
32+
String description = Parser.parseDescription(userInput);
33+
switch (command) {
34+
case EXIT:
35+
storage.save(tasks, "tasks.txt");
36+
ui.closeScanner();
37+
ui.showExitMessage();
38+
return;
39+
case LIST:
40+
ui.showTaskList(tasks);
41+
break;
42+
case UNMARK:
8543
try {
86-
throw new EmptyDeadlineException();
87-
} catch (EmptyDeadlineException e) {
88-
System.out.println(e.getMessage());
44+
int taskNumber = Integer.parseInt(description) - 1;
45+
if (taskNumber >= 0 && taskNumber < tasks.size()) {
46+
tasks.unmarkTask(taskNumber);
47+
} else {
48+
ui.showError("Task number out of range.");
49+
}
50+
} catch (NumberFormatException e) {
51+
ui.showError("Invalid task number. Please provide a valid integer.");
8952
}
90-
} else {
91-
// Find the index of the deadline separator "/"
92-
int separatorIndex = descriptionDeadline.indexOf('/');
93-
94-
if (separatorIndex != -1) { // Ensure the separator exists in the input
95-
// Extract the task description and deadline
96-
97-
String description = descriptionDeadline.substring(0, separatorIndex).trim();
98-
String deadline = descriptionDeadline.substring(separatorIndex + 4).trim();
99-
String pattern = "\\d{4}/\\d{2}/\\d{2}";
100-
Pattern datePattern = Pattern.compile(pattern);
101-
Matcher matcher = datePattern.matcher(deadline);
102-
if (matcher.find()) {
103-
DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy/MM/dd");
104-
LocalDate localDateDeadline = LocalDate.parse(deadline, formatter);
105-
Deadline deadlineTask = new Deadline(description,false, localDateDeadline);
106-
tasksList.add(deadlineTask);
107-
System.out.println("Got it. I've added this deadline:");
108-
System.out.println(" " + deadlineTask);
109-
System.out.println("Now you have " + tasksList.size() + " tasks in the list.");
53+
break;
54+
case MARK:
55+
try {
56+
int taskNumber = Integer.parseInt(description) - 1;
57+
if (taskNumber >= 0 && taskNumber < tasks.size()) {
58+
tasks.markTaskAsDone(taskNumber);
11059
} else {
111-
System.out.println("Please input your deadline in YYYY/MM/DD format");
60+
ui.showError("Task number out of range.");
11261
}
113-
} else {
114-
System.out.println("Invalid input format for deadline. Please input in the following format: <deadline> <description> /by <YYYY/MM/DD> ");
62+
} catch (NumberFormatException e) {
63+
ui.showError("Invalid task number. Please provide a valid integer.");
11564
}
116-
}
117-
} else if (command.startsWith("event")) {
118-
// split the input
119-
String descriptionStartEndTime = command.substring(5).trim(); // Remove "event" and leading spaces
120-
if (descriptionStartEndTime.isEmpty()) {
65+
break;
66+
case TODO:
12167
try {
122-
throw new EmptyEventException();
123-
} catch (EmptyEventException e) {
68+
if (description.isEmpty()) {
69+
throw new EmptyTodoException();
70+
}
71+
Todo todo = new Todo(description, false);
72+
tasks.addTask(todo);
73+
74+
} catch (EmptyTodoException e) {
12475
System.out.println(e.getMessage());
12576
}
126-
} else {
127-
// Find the indices of the time separators
128-
int fromIndex = descriptionStartEndTime.indexOf("/from");
129-
int toIndex = descriptionStartEndTime.indexOf("/to");
130-
131-
if (fromIndex != -1 && toIndex != -1) {
132-
// Extract the task description, startTime, and endTime
133-
String description = descriptionStartEndTime.substring(0, fromIndex).trim();
134-
String startTime = descriptionStartEndTime.substring(fromIndex + 5, toIndex).trim();
135-
String endTime = descriptionStartEndTime.substring(toIndex + 3).trim();
136-
137-
// Create a new Event object
138-
Event eventTask = new Event(description, false, startTime, endTime);
139-
tasksList.add(eventTask);
140-
141-
// Print confirmation message
142-
System.out.println("Got it. I've added this task:");
143-
System.out.println(" " + eventTask);
144-
System.out.println("Now you have " + tasksList.size() + " tasks in the list.");
77+
break;
78+
case DEADLINE:
79+
80+
if (description.isEmpty()) {
81+
try {
82+
throw new EmptyDeadlineException();
83+
} catch (EmptyDeadlineException e) {
84+
System.out.println(e.getMessage());
85+
}
14586
} else {
146-
System.out.println("Invalid input format for event command.");
87+
// Find the index of the deadline separator "/"
88+
int separatorIndex = description.indexOf('/');
89+
90+
if (separatorIndex != -1) { // Ensure the separator exists in the input
91+
// Extract the task description and deadline
92+
93+
String descriptionString = description.substring(0, separatorIndex).trim();
94+
String deadline = description.substring(separatorIndex + 4).trim();
95+
String pattern = "\\d{4}/\\d{2}/\\d{2}";
96+
Pattern datePattern = Pattern.compile(pattern);
97+
Matcher matcher = datePattern.matcher(deadline);
98+
if (matcher.find()) {
99+
DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy/MM/dd");
100+
LocalDate localDateDeadline = LocalDate.parse(deadline, formatter);
101+
Deadline deadlineTask = new Deadline(descriptionString, false, localDateDeadline);
102+
tasks.addTask(deadlineTask);
103+
104+
} else {
105+
System.out.println("Please input your deadline in YYYY/MM/DD format");
106+
}
107+
} else {
108+
System.out.println("Invalid input format for deadline. Please input in the following format: <deadline> <description> /by <YYYY/MM/DD> ");
109+
}
147110
}
148-
}
149-
} else if (command.startsWith("delete")) {
150-
int taskNumber = Integer.parseInt(command.substring(7)) - 1;
151-
if (taskNumber < tasksList.size()) {
152-
Tasks task = tasksList.get(taskNumber);
153-
tasksList.remove(taskNumber);
154-
155-
System.out.println("Noted. I've removed this task: \n" + " " + task);
156-
System.out.println("Now you have " + tasksList.size() + " tasks in the list.");
157-
}
158-
159-
160-
} else {
161-
try {
162-
throw new UnknownInputException();
163-
} catch (UnknownInputException e) {
164-
System.out.println(e.getMessage());
165-
}
166-
}
111+
break;
112+
case EVENT:
113+
if (description.isEmpty()) {
114+
try {
115+
throw new EmptyEventException();
116+
} catch (EmptyEventException e) {
117+
System.out.println(e.getMessage());
118+
}
119+
} else {
120+
// Find the indices of the time separators
121+
int fromIndex = description.indexOf("/from");
122+
int toIndex = description.indexOf("/to");
167123

124+
if (fromIndex != -1 && toIndex != -1) {
125+
// Extract the task description, startTime, and endTime
126+
String descriptionString = description.substring(0, fromIndex).trim();
127+
String startTime = description.substring(fromIndex + 5, toIndex).trim();
128+
String endTime = description.substring(toIndex + 3).trim();
129+
130+
// Create a new Event object
131+
Event eventTask = new Event(descriptionString, false, startTime, endTime);
132+
tasks.addTask(eventTask);
133+
134+
} else {
135+
System.out.println("Invalid input format for event command.");
136+
}
137+
}
138+
break;
139+
case DELETE:
140+
try {
141+
int taskNumber = Integer.parseInt(description) - 1;
142+
if (taskNumber >= 0 && taskNumber < tasks.size()) {
143+
tasks.deleteTask(taskNumber);
144+
} else {
145+
ui.showError("Task number out of range.");
146+
}
147+
} catch (NumberFormatException e) {
148+
ui.showError("Invalid task number. Please provide a valid integer.");
149+
}
150+
break;
151+
case INVALID:
152+
ui.showError("Invalid command. Please try again.");
153+
break;
154+
}
168155
}
169-
TaskWriter.writeTasksToFile(tasksList, "tasks.txt");
156+
}
157+
158+
public static void main(String[] args) {
159+
new Duke("tasks.txt").run();
170160
}
171161
}
172162

src/main/java/DukeException.java

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
public class DukeException extends Exception {
2+
public DukeException(String message) {
3+
super(message);
4+
}
5+
}
6+
7+
8+
9+
10+

src/main/java/Event.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
public class Event extends Tasks {
1+
public class Event extends Task {
22
private String startTime;
33
private String endTime;
44

0 commit comments

Comments
 (0)