-
Notifications
You must be signed in to change notification settings - Fork 116
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
[ZiliaAJY] iP #26
base: master
Are you sure you want to change the base?
[ZiliaAJY] iP #26
Changes from 24 commits
1e4f606
572029a
c55d4cc
13abd01
c66bb95
b039dc1
b211e72
533b98c
b2210b3
f6fab3f
3d681c7
5ef469c
aed0ccc
b0c3f13
7fc6aa2
78f99e1
f1916e6
7106d51
b43d56d
209eed1
a9541f7
8348d78
980a0bc
b9935e3
6cad730
fd417a5
791c952
263e667
14f52ce
ff9aaa2
afe81f6
67f9aff
ed72fa4
9aa2d97
b5b403f
aee9480
19d3c30
146b65e
ad59a07
ea67181
bc9fe9d
ac0e563
4dfee35
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
T | ✓ | run | ||
E | ✗ | sleep | from 3am to 10pm | ||
D | ✗ | slides | by mon | ||
D | ✓ | eat | by 8pm | ||
E | ✗ | cs2113 lecture | from Fri 4pm to 6pm |
This file was deleted.
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
Manifest-Version: 1.0 | ||
Main-Class: conglo.Conglo | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,253 @@ | ||
package conglo; | ||
|
||
import conglo.manual.*; | ||
import conglo.task.*; | ||
import conglo.exception.*; | ||
import conglo.storage.*; | ||
|
||
import java.util.ArrayList; | ||
import java.util.Scanner; | ||
|
||
/** | ||
* Conglo is a command-line task management application that allows users to | ||
* add tasks, mark tasks as done or not done, and list tasks. The tasks can | ||
* include ToDos, Events, and Deadlines. | ||
*/ | ||
public class Conglo { | ||
|
||
// Constants | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. redundant comment |
||
protected static final String LINE_SEPARATOR = "-------------------------------------"; | ||
|
||
// Class variables | ||
protected static String command; | ||
private static final ArrayList<Task> taskList = TaskStorage.loadTasks(); | ||
protected static boolean isDelete = false; | ||
|
||
/** | ||
* The main method that starts the Conglo application. It handles user input | ||
* and processes commands until the user types "bye". | ||
* | ||
* @param args Command-line arguments (not used). | ||
*/ | ||
public static void main(String[] args) { | ||
greetUser(); | ||
Scanner scanner = new Scanner(System.in); | ||
command = scanner.nextLine(); | ||
while (!command.equals("bye")) { | ||
try { | ||
processCommand(command); | ||
} catch (CongloException e) { | ||
System.out.println(e.getMessage()); | ||
} | ||
printLineSeparator(); | ||
command = scanner.nextLine(); | ||
} | ||
scanner.close(); | ||
sayGoodbye(); | ||
} | ||
|
||
/** | ||
* Prints a line separator to the console. | ||
*/ | ||
public static void printLineSeparator() { | ||
System.out.println(LINE_SEPARATOR); | ||
} | ||
|
||
private static void saveTasks() { | ||
TaskStorage.saveTasks(taskList); | ||
} | ||
|
||
/** | ||
* Greets the user when the application starts. | ||
*/ | ||
public static void greetUser() { | ||
printLineSeparator(); | ||
System.out.println("Hola! I'm Conglo, the friendly task manager."); | ||
printLineSeparator(); | ||
QuickManual.printManual(); | ||
printLineSeparator(); | ||
listTasks(); | ||
printLineSeparator(); | ||
} | ||
|
||
/** | ||
* Echoes the details of the newly added task and the current task count. | ||
* | ||
* @param task The task to be echoed. | ||
*/ | ||
public static void echoTask(Task task) { | ||
int size = taskList.size(); | ||
if (isDelete) { | ||
System.out.println("Okie! Task is removed from list:"); | ||
size--; | ||
} else { | ||
System.out.println("All done! Task added to list:"); | ||
} | ||
String taskSuffix = size > 1 ? " tasks" : " task"; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. could consider making this "easy to read" by using explicit |
||
System.out.println(" " + task.toFileFormat()); | ||
System.out.println("The list has " + size + taskSuffix + " now."); | ||
} | ||
|
||
/** | ||
* Lists all the tasks currently in the task list. | ||
*/ | ||
public static void listTasks() { | ||
if (taskList.isEmpty()) { | ||
System.out.println("The list is empty!"); | ||
} | ||
for (int i = 0; i < taskList.size(); i++) { | ||
System.out.println((i + 1) + ". " + taskList.get(i).toFileFormat()); | ||
} | ||
} | ||
|
||
/** | ||
* Marks a task as done or not done based on the user command. | ||
* | ||
* @param words The split command words where the first word is "mark" or "unmark". | ||
* @throws CongloException.InvalidTaskNumber If the task number is invalid or out of range. | ||
*/ | ||
public static void markTask(String[] words) throws CongloException.InvalidTaskNumber { | ||
int i; | ||
try { | ||
i = Integer.parseInt(words[1].substring(0, 1)) - 1; | ||
} catch (NumberFormatException e) { | ||
System.out.println("Invalid format! Please provide a task number >.<"); | ||
return; | ||
} | ||
|
||
if (i >= taskList.size() || i < 0) { | ||
throw new CongloException.InvalidTaskNumber(); | ||
} | ||
if (words[0].equals("mark")) { | ||
taskList.get(i).markAsDone(); | ||
System.out.println("Nice! I've marked this task as done:"); | ||
} else { | ||
taskList.get(i).markAsNotDone(); | ||
System.out.println("OK, I've marked this task as not done yet:"); | ||
} | ||
System.out.println(taskList.get(i).toFileFormat()); | ||
} | ||
|
||
/** | ||
* Adds a todo task to the task list. | ||
* | ||
* @param sentence The command containing the task description. | ||
*/ | ||
public static void addTodo(String sentence) { | ||
Todo todo = new Todo(sentence); | ||
taskList.add(todo); | ||
echoTask(todo); | ||
saveTasks(); | ||
} | ||
|
||
/** | ||
* Adds a deadline task to the task list. | ||
* | ||
* @param sentence The command containing the task description and deadline. | ||
* @throws CongloException.InvalidFormat If the deadline format is incorrect. | ||
*/ | ||
public static void addDeadline(String sentence) throws CongloException.InvalidFormat { | ||
if (!sentence.contains(" /by ")) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. can try improving SLAP in such cases |
||
throw new CongloException.InvalidFormat("deadline"); | ||
} | ||
String[] words = sentence.split(" /by "); | ||
Deadline deadline = new Deadline(words[0], words[1]); | ||
taskList.add(deadline); | ||
echoTask(deadline); | ||
saveTasks(); | ||
} | ||
|
||
/** | ||
* Adds an event task to the task list. | ||
* | ||
* @param sentence The command containing the task description, start, and end times. | ||
* @throws CongloException.InvalidFormat If the event format is incorrect. | ||
*/ | ||
public static void addEvent(String sentence) throws CongloException.InvalidFormat { | ||
if (!sentence.contains(" /from ") || !sentence.contains(" /to ")) { | ||
throw new CongloException.InvalidFormat("event"); | ||
} | ||
String[] words = sentence.split(" /from | /to "); | ||
Event event = new Event(words[0], words[1], words[2]); | ||
taskList.add(event); | ||
echoTask(event); | ||
saveTasks(); | ||
} | ||
|
||
public static void deleteTask(String word) throws CongloException.InvalidTaskNumber { | ||
int i; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. use a more descriptive variable name. |
||
try { | ||
i = Integer.parseInt(word) - 1; | ||
} catch (NumberFormatException e) { | ||
System.out.println("Invalid format! Please provide a task number >.<"); | ||
return; | ||
} | ||
if (i >= taskList.size() || i < 0) { | ||
throw new CongloException.InvalidTaskNumber(); | ||
} | ||
isDelete = true; | ||
echoTask(taskList.get(i)); | ||
taskList.remove(i); | ||
isDelete = false; | ||
saveTasks(); | ||
} | ||
|
||
/** | ||
* Processes the user's command and invokes the corresponding action. | ||
* | ||
* @param command The full command entered by the user. | ||
* @throws CongloException If the command is invalid or an error occurs while processing. | ||
*/ | ||
public static void processCommand(String command) throws CongloException { | ||
String[] words = command.split(" ", 2); | ||
switch(words[0]) { | ||
case "list": | ||
if (words.length > 1) { | ||
throw new CongloException.InvalidFormat("list"); | ||
} | ||
listTasks(); | ||
break; | ||
case "unmark": | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. fall through comment is needed |
||
case "mark": | ||
if (words.length == 1 || words[1].isEmpty()) { | ||
throw new CongloException.MissingTaskNumber(words[0]); | ||
} | ||
markTask(words); | ||
break; | ||
case "delete": | ||
if (words.length == 1 || words[1].isEmpty()) { | ||
throw new CongloException.MissingTaskNumber(words[0]); | ||
} | ||
deleteTask(words[1]); | ||
break; | ||
case "todo": | ||
if (words.length == 1 || words[1].isEmpty()) { | ||
throw new CongloException.MissingDescription("Todo"); | ||
} | ||
addTodo(words[1]); | ||
break; | ||
case "deadline": | ||
if (words.length == 1 || words[1].isEmpty()) { | ||
throw new CongloException.MissingDescription("deadline"); | ||
} | ||
addDeadline(words[1]); | ||
break; | ||
case "event": | ||
if (words.length == 1 || words[1].isEmpty()) { | ||
throw new CongloException.MissingDescription("event"); | ||
} | ||
addEvent(words[1]); | ||
break; | ||
default: | ||
throw new CongloException.UnknownCommand(); | ||
} | ||
} | ||
|
||
/** | ||
* Prints a goodbye message when the user exits the application. | ||
*/ | ||
public static void sayGoodbye() { | ||
System.out.println("Goodbye. See you next time!"); | ||
printLineSeparator(); | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,59 @@ | ||
package conglo.exception; | ||
|
||
public class CongloException extends Exception { | ||
|
||
public CongloException(String message) { | ||
super(message); | ||
} | ||
|
||
// Custom exception for unknown commands | ||
public static class UnknownCommand extends CongloException { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. consider making these separate classes rather than nested classes |
||
public UnknownCommand() { | ||
super("Sorry, I'm not smart enough to understand what you mean."); | ||
} | ||
} | ||
|
||
// Custom exception for missing description | ||
public static class MissingDescription extends CongloException { | ||
public MissingDescription(String type) { | ||
super("Oopsies, please add a description of " + type + "."); | ||
} | ||
} | ||
|
||
// Custom exception for invalid task number | ||
public static class InvalidTaskNumber extends CongloException { | ||
public InvalidTaskNumber() { | ||
super("Invalid task number! Please try again."); | ||
} | ||
} | ||
|
||
// Custom exception for missing task number | ||
public static class MissingTaskNumber extends CongloException { | ||
public MissingTaskNumber(String action) { | ||
super("Please provide task number to " + action + "."); | ||
} | ||
} | ||
|
||
// Custom exception for invalid input format | ||
public static class InvalidFormat extends CongloException { | ||
public InvalidFormat(String type) { | ||
super(generateMessage(type)); | ||
} | ||
|
||
private static String generateMessage(String type) { | ||
switch (type) { | ||
case "list": | ||
return "Do you mean 'list'?"; | ||
case "deadline": | ||
return "Oh dear, your deadline command is a bit off." | ||
+ System.lineSeparator() + "Please use: deadline [description] /by [deadline]."; | ||
case "event": | ||
return "Oh no! Your event command needs a bit of tweaking." | ||
+ System.lineSeparator() + "Try: event [description] /from [start time] /to [end time]."; | ||
default: | ||
return "Oops! Looks like there's a hiccup in your command." | ||
+ System.lineSeparator() + "Please follow the correct format."; | ||
} | ||
} | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
package conglo.manual; | ||
|
||
public class QuickManual { | ||
|
||
/** | ||
* Prints a quick manual of instructions for the Conglo application. | ||
*/ | ||
public static void printManual() { | ||
System.out.println("Here's a quick manual to get you started:"); | ||
System.out.println("1. Add a todo: todo [description]"); | ||
System.out.println("2. Add a deadline: deadline [description] /by [deadline]"); | ||
System.out.println("3. Add an event: event [description] /from [start time] /to [end time]"); | ||
System.out.println("4. Mark a task as done: mark [task number]"); | ||
System.out.println("5. Unmark a task: unmark [task number]"); | ||
System.out.println("6. Delete a task: delete [task number]"); | ||
System.out.println("7. List all tasks: list"); | ||
System.out.println("8. Exit the application: bye"); | ||
} | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
don't use wildcard imports