Skip to content

Commit

Permalink
Refactor code
Browse files Browse the repository at this point in the history
  • Loading branch information
chunweii committed Sep 14, 2021
1 parent 66de53f commit cb9ee25
Show file tree
Hide file tree
Showing 6 changed files with 31 additions and 29 deletions.
14 changes: 6 additions & 8 deletions src/main/java/duke/logic/CommandParser.java
Original file line number Diff line number Diff line change
Expand Up @@ -17,11 +17,9 @@ public class CommandParser {
private static final String EMPTY_INPUT_MESSAGE = "Input is empty. Type \"help\" for more information.";
private static final String TOO_LITTLE_ARGUMENTS_MESSAGE = "Too little arguments. Type \"help\" "
+ "followed by the command for more information.";
private static final String FULL_TASKLIST_MESSAGE = "Unable to add task. List is full. Consider deleting"
+ " some tasks";
private static final String INVALID_NUMBER_MESSAGE = "Please input a valid task number after the command.";
private final String output;
private boolean willExit;
private boolean isExiting;

/**
* Creates a new command parser for the input.
Expand All @@ -39,12 +37,12 @@ public CommandParser(String input, TaskList taskList, Storage storage, Ui ui) {
CommandsEnum commandEnum;
try {
commandEnum = CommandsEnum.valueOf(inputArr[0].toUpperCase());
this.willExit = false;
this.isExiting = false;
switch (commandEnum) {
// Single word commands
case BYE:
output = ui.getGoodByeMessage();
willExit = true;
isExiting = true;
return;
case LIST:
output = ui.getAllTasksMessage(taskList.getAllTasks(), taskList.size());
Expand All @@ -60,7 +58,7 @@ public CommandParser(String input, TaskList taskList, Storage storage, Ui ui) {
if (inputArr.length < 2) {
throw new InvalidCommandException(TOO_LITTLE_ARGUMENTS_MESSAGE);
}
output = parseMultiword(inputArr[1], commandEnum, taskList, ui);
output = parseMultiWord(inputArr[1], commandEnum, taskList, ui);
}
} catch (IllegalArgumentException e) {
throw new InvalidCommandException(INVALID_COMMAND);
Expand All @@ -81,7 +79,7 @@ public CommandParser(String input, TaskList taskList, Storage storage, Ui ui) {
* @param ui The user interface generating the output messages.
* @return The output string from ui.
*/
private static String parseMultiword(String inputWords, CommandsEnum commandsEnum, TaskList taskList, Ui ui) {
private static String parseMultiWord(String inputWords, CommandsEnum commandsEnum, TaskList taskList, Ui ui) {
switch (commandsEnum) {
case FIND:
return ui.getTasksWithPatternMessage(inputWords,
Expand Down Expand Up @@ -145,7 +143,7 @@ public String getOutput() {
* @return true if and only if the command is to exit
*/
public boolean willExit() {
return willExit;
return isExiting;
}

}
16 changes: 8 additions & 8 deletions src/main/java/duke/logic/DateTimeParser.java
Original file line number Diff line number Diff line change
Expand Up @@ -56,14 +56,6 @@ public DateTimeParser(String dateTime, LocalDate relativeStartDate, LocalTime re
}
}

private LocalDate parseDate(String date) {
return LocalDate.parse(date, DateTimeFormatter.ofPattern("d/M/yyyy"));
}

private LocalTime parseTime(String time) {
return LocalTime.parse(time, DateTimeFormatter.ofPattern("H:m"));
}

/**
* Returns the LocalDateTime object associated with the date and time represented in the data string.
*
Expand All @@ -74,6 +66,14 @@ public static LocalDateTime getDateTimeFromDataString(String data) {
return LocalDateTime.parse(data, DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm"));
}

private LocalDate parseDate(String date) {
return LocalDate.parse(date, DateTimeFormatter.ofPattern("d/M/yyyy"));
}

private LocalTime parseTime(String time) {
return LocalTime.parse(time, DateTimeFormatter.ofPattern("H:m"));
}

/**
* Gets the date of this datetime.
*
Expand Down
11 changes: 6 additions & 5 deletions src/main/java/duke/ui/TextCliUi.java
Original file line number Diff line number Diff line change
Expand Up @@ -13,15 +13,16 @@
public class TextCliUi {
private final Scanner sc;
private final Ui ui;
private boolean willExit;
private boolean isExiting;

/**
* Creates a new instance of a user interface by creating a new scanner and querying for the user's name.
* Creates a new instance of a user interface by creating a new scanner and
* querying for the user's name.
*/
public TextCliUi() {
sc = new Scanner(System.in);
String name = "";
willExit = false;
isExiting = false;
String logo = " ____ _ \n"
+ "| _ \\ _ _| | _____ \n"
+ "| | | | | | | |/ / _ \\\n"
Expand All @@ -39,7 +40,7 @@ public TextCliUi() {
}

public boolean willExit() {
return willExit;
return isExiting;
}

/**
Expand All @@ -52,7 +53,7 @@ public void checkInput(TaskList taskList, Storage storage) {
String userInput = sc.nextLine();
try {
CommandParser cmdParser = new CommandParser(userInput, taskList, storage, ui);
willExit = cmdParser.willExit();
isExiting = cmdParser.willExit();
System.out.println(cmdParser.getOutput());
} catch (DukeException e) {
System.out.println(e.getMessage());
Expand Down
12 changes: 7 additions & 5 deletions src/main/java/duke/ui/Ui.java
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@
*/
public class Ui {
private final String name;
private boolean willExit = false;
private boolean isExiting = false;

/**
* Creates a new instance of a user interface by creating a new scanner and querying for the user's name.
Expand All @@ -30,7 +30,7 @@ public Ui(String name) {
}

public boolean willExit() {
return willExit;
return isExiting;
}

/**
Expand All @@ -45,7 +45,7 @@ public String checkInput(String userInput, TaskList taskList, Storage storage) {
CommandParser cmdParser;
try {
cmdParser = new CommandParser(userInput, taskList, storage, this);
willExit = cmdParser.willExit();
isExiting = cmdParser.willExit();
output += cmdParser.getOutput() + "\n";
} catch (DukeException e) {
output += e.getMessage() + "\n";
Expand All @@ -59,15 +59,16 @@ public String checkInput(String userInput, Duke duke) {
}

/**
* Called when the user wants to exit the program.
* Returns the goodbye message. Called when the user wants to exit the program.
*
* @return the output string
*/
public String getGoodByeMessage() {
return ("Bye, " + name + "! Hope to see you again soon.");
return "Bye, " + name + "! Hope to see you again soon.";
}

/**
* Returns the successful add task message.
* Called when the user successfully adds the task to tasklist.
*
* @param task the task that is added
Expand All @@ -82,6 +83,7 @@ public String addTaskMessage(Task task, int sizeOfList) {
}

/**
* Returns the successful remove task message.
* Called when the user removes a task from the task list.
*
* @param task the task that is removed or deleted
Expand Down
4 changes: 2 additions & 2 deletions src/main/resources/view/DialogBox.fxml
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@

<?import javafx.geometry.Insets?>
<?import javafx.scene.control.Label?>
<?import javafx.scene.layout.HBox?>
<?import javafx.scene.image.ImageView?>
<?import javafx.scene.layout.HBox?>
<fx:root xmlns:fx="http://javafx.com/fxml/1" alignment="TOP_RIGHT" maxHeight="1.7976931348623157E308"
maxWidth="1.7976931348623157E308"
prefWidth="400.0" type="javafx.scene.layout.HBox" xmlns="http://javafx.com/javafx/8.0.171"
Expand All @@ -18,6 +18,6 @@
-fx-background-insets: 0,1;
-fx-font-family: 'Consolas', 'monospace';"
textFill="#e1e0e0"
ellipsisString=""/>
ellipsisString=""/>
<ImageView fx:id="circleDisplayPicture"/>
</fx:root>
3 changes: 2 additions & 1 deletion src/main/resources/view/MainWindow.fxml
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,8 @@
<?import javafx.scene.control.TextField?>
<?import javafx.scene.layout.AnchorPane?>
<?import javafx.scene.layout.VBox?>
<AnchorPane xmlns:fx="http://javafx.com/fxml/1" maxHeight="-Infinity" maxWidth="-Infinity" minHeight="-Infinity" minWidth="-Infinity"
<AnchorPane xmlns:fx="http://javafx.com/fxml/1" maxHeight="-Infinity" maxWidth="-Infinity" minHeight="-Infinity"
minWidth="-Infinity"
prefHeight="600.0" prefWidth="500.0" xmlns="http://javafx.com/javafx/11.0.2"
fx:controller="duke.ui.MainWindow">
<TextField fx:id="userInput" layoutY="558.0" onAction="#handleUserInput" prefHeight="42.0"
Expand Down

0 comments on commit cb9ee25

Please sign in to comment.