Skip to content

Commit

Permalink
Fix bug in Deadline and reformat code
Browse files Browse the repository at this point in the history
  • Loading branch information
TrungBui32 committed Oct 21, 2024
1 parent 42416ac commit 58e96c7
Show file tree
Hide file tree
Showing 35 changed files with 191 additions and 22 deletions.
Binary file modified src/main/java/AddDeadlineCommand.class
Binary file not shown.
7 changes: 5 additions & 2 deletions src/main/java/AddDeadlineCommand.java
Original file line number Diff line number Diff line change
@@ -1,13 +1,15 @@
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.time.format.DateTimeParseException;

/**
* Command to add a deadline task.
*/
public class AddDeadlineCommand extends Command {
private String description;
private String by;
private static final DateTimeFormatter INPUT_FORMAT = DateTimeFormatter.ofPattern("yyyy-MM-dd HHmm");

/**
* Constructs an AddDeadlineCommand with the specified input.
*
Expand All @@ -18,11 +20,12 @@ public AddDeadlineCommand(String input) {
this.description = parts[0].trim().substring(9).trim();
this.by = parts[1].trim();
}

/**
* Executes the command to add the deadline task to the task list.
*
* @param tasks The task list to which the task will be added.
* @param ui The user interface for displaying messages to the user.
* @param tasks The task list to which the task will be added.
* @param ui The user interface for displaying messages to the user.
* @param storage The storage for saving tasks to a file.
*/
@Override
Expand Down
Binary file modified src/main/java/AddEventCommand.class
Binary file not shown.
20 changes: 20 additions & 0 deletions src/main/java/AddEventCommand.java
Original file line number Diff line number Diff line change
@@ -1,14 +1,34 @@
/**
* Represents a command to add a new event to the task list.
* This command creates a new Event task with a description, start time, and end time.
*/
public class AddEventCommand extends Command {
private String description;
private String from;
private String to;

/**
* Constructs a new AddEventCommand with the specified event details.
*
* @param description The description of the event.
* @param from The start time/date of the event.
* @param to The end time/date of the event.
*/
public AddEventCommand(String description, String from, String to) {
this.description = description;
this.from = from;
this.to = to;
}

/**
* Executes the command to add a new event to the task list.
* This method creates a new Event, adds it to the task list, updates the UI,
* and saves the updated task list to storage.
*
* @param tasks The TaskList to which the new event will be added.
* @param ui The Ui object used to display output to the user.
* @param storage The Storage object used to save the updated task list.
*/
@Override
public void execute(TaskList tasks, Ui ui, Storage storage) {
Task newTask = new Event(description, from, to);
Expand Down
Binary file modified src/main/java/AddTodoCommand.class
Binary file not shown.
6 changes: 4 additions & 2 deletions src/main/java/AddTodoCommand.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
*/
public class AddTodoCommand extends Command {
private String description;

/**
* Constructs an AddTodoCommand with the specified description.
*
Expand All @@ -11,11 +12,12 @@ public class AddTodoCommand extends Command {
public AddTodoCommand(String description) {
this.description = description;
}

/**
* Executes the command to add the todo task to the task list.
*
* @param tasks The task list to which the task will be added.
* @param ui The user interface for displaying messages to the user.
* @param tasks The task list to which the task will be added.
* @param ui The user interface for displaying messages to the user.
* @param storage The storage for saving tasks to a file.
*/
@Override
Expand Down
Binary file modified src/main/java/Command.class
Binary file not shown.
20 changes: 20 additions & 0 deletions src/main/java/Command.java
Original file line number Diff line number Diff line change
@@ -1,6 +1,26 @@
/**
* Represents an abstract command in the task management system.
* This class serves as a base for all concrete command implementations.
*/
public abstract class Command {
/**
* Executes the command.
* This method should be implemented by all concrete command classes to define
* their specific behavior.
*
* @param tasks The TaskList on which the command operates.
* @param ui The Ui object used to interact with the user.
* @param storage The Storage object used to save and load tasks.
* @throws PoirotException If an error occurs during command execution.
*/
public abstract void execute(TaskList tasks, Ui ui, Storage storage) throws PoirotException;

/**
* Checks if this command should exit the program.
* By default, commands do not exit the program.
*
* @return true if the program should exit after this command, false otherwise.
*/
public boolean isExit() {
return false;
}
Expand Down
Binary file modified src/main/java/Deadline.class
Binary file not shown.
20 changes: 20 additions & 0 deletions src/main/java/Deadline.java
Original file line number Diff line number Diff line change
@@ -1,21 +1,41 @@
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;

/**
* Represents a deadline task in the task management system.
* A deadline task is a task that needs to be completed by a specific date and time.
*/
public class Deadline extends Task {
private LocalDateTime by;
private static final DateTimeFormatter INPUT_FORMAT = DateTimeFormatter.ofPattern("yyyy-MM-dd HHmm");
private static final DateTimeFormatter OUTPUT_FORMAT = DateTimeFormatter.ofPattern("MMM d yyyy, hh:mm a");

/**
* Constructs a new Deadline task with the given description and deadline.
*
* @param description The description of the deadline task.
* @param by The deadline date and time in the format "yyyy-MM-dd HHmm".
*/
public Deadline(String description, String by) {
super(description);
this.by = LocalDateTime.parse(by, INPUT_FORMAT);
}

/**
* Returns a string representation of the deadline task.
*
* @return A formatted string representing the deadline task, including its status, description, and deadline.
*/
@Override
public String toString() {
return "[D][" + getStatusIcon() + "] " + description + " (by: " + by.format(OUTPUT_FORMAT) + ")";
}

/**
* Returns a string representation of the deadline task for file storage.
*
* @return A formatted string representing the deadline task for saving to a file.
*/
@Override
public String toFileString() {
return "D | " + (isDone ? "1" : "0") + " | " + description + " | " + by.format(INPUT_FORMAT);
Expand Down
Binary file modified src/main/java/DeleteCommand.class
Binary file not shown.
5 changes: 3 additions & 2 deletions src/main/java/DeleteCommand.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
*/
public class DeleteCommand extends Command {
private int index;

/**
* Constructs a DeleteCommand with the specified index.
*
Expand All @@ -15,8 +16,8 @@ public DeleteCommand(int index) {
/**
* Executes the command to delete the specified task from the task list.
*
* @param tasks The task list from which the task will be deleted.
* @param ui The user interface for displaying messages to the user.
* @param tasks The task list from which the task will be deleted.
* @param ui The user interface for displaying messages to the user.
* @param storage The storage for saving tasks to a file.
* @throws PoirotException If the index is out of bounds.
*/
Expand Down
4 changes: 2 additions & 2 deletions src/main/java/Event.java
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,8 @@ public class Event extends Task {
* Constructs an Event with the specified description, from, and to time.
*
* @param description The description of the event.
* @param from The starting time of the event in 'yyyy-MM-dd HHmm' format.
* @param to The ending time of the event in 'yyyy-MM-dd HHmm' format.
* @param from The starting time of the event in 'yyyy-MM-dd HHmm' format.
* @param to The ending time of the event in 'yyyy-MM-dd HHmm' format.
*/
public Event(String description, String from, String to) {
super(description);
Expand Down
Binary file modified src/main/java/ExitCommand.class
Binary file not shown.
5 changes: 3 additions & 2 deletions src/main/java/ExitCommand.java
Original file line number Diff line number Diff line change
Expand Up @@ -5,14 +5,15 @@ public class ExitCommand extends Command {
/**
* Executes the command to exit the application.
*
* @param tasks The task list (not used).
* @param ui The user interface for displaying messages to the user.
* @param tasks The task list (not used).
* @param ui The user interface for displaying messages to the user.
* @param storage The storage for saving tasks to a file (not used).
*/
@Override
public void execute(TaskList tasks, Ui ui, Storage storage) {
ui.showExit();
}

/**
* Indicates whether this command is an exit command.
*
Expand Down
4 changes: 2 additions & 2 deletions src/main/java/ListCommand.java
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,8 @@ public class ListCommand extends Command {
/**
* Executes the command to display the list of tasks.
*
* @param tasks The task list to display.
* @param ui The user interface for displaying messages to the user.
* @param tasks The task list to display.
* @param ui The user interface for displaying messages to the user.
* @param storage The storage for saving tasks to a file (not used).
*/
@Override
Expand Down
Binary file modified src/main/java/Parser.class
Binary file not shown.
28 changes: 25 additions & 3 deletions src/main/java/Parser.java
Original file line number Diff line number Diff line change
@@ -1,4 +1,14 @@
/**
* The Parser class is responsible for parsing user input and creating appropriate Command objects.
*/
public class Parser {
/**
* Parses the input string and returns the corresponding Command object.
*
* @param input The user input string to be parsed.
* @return A Command object based on the parsed input.
* @throws PoirotException If the input is invalid or cannot be parsed.
*/
public static Command parse(String input) throws PoirotException {
String[] list_input = input.split(" ");
switch (list_input[0]) {
Expand All @@ -24,21 +34,33 @@ public static Command parse(String input) throws PoirotException {
throw new PoirotException("Unknown command!");
}
}

/**
* Parses the deadline command input and creates an AddDeadlineCommand object.
*
* @param input The deadline command input string.
* @return An AddDeadlineCommand object.
* @throws PoirotException If the deadline format is invalid.
*/
private static Command parseDeadlineCommand(String input) throws PoirotException {
try {
String[] parts = input.split("/by");
if (parts.length < 2) {
throw new PoirotException("Invalid deadline format! Use: deadline <description> /by <yyyy-mm-dd>");
}
String description = parts[0].trim().substring(9).trim();
String description = parts[0].trim();
String by = parts[1].trim();
return new AddDeadlineCommand(description + " /by " + by);
} catch (ArrayIndexOutOfBoundsException e) {
throw new PoirotException("Invalid format for deadline command.");
}
}

/**
* Parses the event command input and creates an AddEventCommand object.
*
* @param input The event command input string.
* @return An AddEventCommand object.
* @throws PoirotException If the event format is invalid.
*/
private static Command parseEventCommand(String input) throws PoirotException {
try {
if (!input.contains("/from") || !input.contains("/to")) {
Expand Down
Binary file modified src/main/java/Poirot.class
Binary file not shown.
20 changes: 20 additions & 0 deletions src/main/java/Poirot.java
Original file line number Diff line number Diff line change
@@ -1,9 +1,19 @@
/**
* The main class for the Poirot task management application.
* This class initializes the application, loads saved tasks, and runs the main command loop.
*/
public class Poirot {

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

/**
* Constructs a new Poirot application instance.
* Initializes the UI, storage, and task list, and loads any previously saved tasks.
*
* @param filePath The file path for storing and loading tasks.
*/
public Poirot(String filePath) {
ui = new Ui();
storage = new Storage(filePath);
Expand All @@ -17,6 +27,10 @@ public Poirot(String filePath) {
}
}

/**
* Runs the main command loop of the application.
* Continuously reads user input, executes commands, and displays results until an exit command is given.
*/
public void run() {
ui.showWelcome();
boolean isExit = false;
Expand All @@ -32,6 +46,12 @@ public void run() {
}
}

/**
* The main entry point for the Poirot application.
* Creates a new Poirot instance and starts running it.
*
* @param args Command line arguments (not used).
*/
public static void main(String[] args) {
new Poirot("./data/duke.txt").run();
}
Expand Down
Binary file modified src/main/java/PoirotException.class
Binary file not shown.
13 changes: 11 additions & 2 deletions src/main/java/PoirotException.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,14 @@
public class PoirotException extends Exception{
public PoirotException(String message){
/**
* Represents a custom exception for the Poirot task management application.
* This exception is used to handle application-specific errors and provide meaningful error messages.
*/
public class PoirotException extends Exception {
/**
* Constructs a new PoirotException with the specified error message.
*
* @param message The detailed error message explaining the reason for the exception.
*/
public PoirotException(String message) {
super(message);
}
}
Binary file modified src/main/java/Storage.class
Binary file not shown.
3 changes: 3 additions & 0 deletions src/main/java/Storage.java
Original file line number Diff line number Diff line change
@@ -1,10 +1,12 @@
import java.io.*;
import java.util.Scanner;

/**
* Handles loading and saving tasks to a file.
*/
public class Storage {
private String filePath;

/**
* Constructs a Storage object with the specified file path.
*
Expand Down Expand Up @@ -62,6 +64,7 @@ public Task[] loadTasksFromFile() {

return tasks;
}

/**
* Saves the specified tasks to the file.
*
Expand Down
Binary file modified src/main/java/Task.class
Binary file not shown.
6 changes: 6 additions & 0 deletions src/main/java/Task.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
abstract class Task {
protected String description;
protected boolean isDone;

/**
* Constructs a Task with the specified description.
*
Expand All @@ -13,9 +14,11 @@ public Task(String description) {
this.description = description;
this.isDone = false;
}

public String getDescription() {
return description;
}

/**
* Returns the status icon of the task.
*
Expand All @@ -24,6 +27,7 @@ public String getDescription() {
public String getStatusIcon() {
return (isDone ? "X" : " ");
}

/**
* Marks the task as done or not done.
*
Expand All @@ -32,7 +36,9 @@ public String getStatusIcon() {
public void setDone(boolean isDone) {
this.isDone = isDone;
}

public abstract String toString();

/**
* Returns a string representation for saving the task to a file.
*
Expand Down
Binary file modified src/main/java/TaskList.class
Binary file not shown.
Loading

0 comments on commit 58e96c7

Please sign in to comment.