From e5bb7f3bb51a8b820405436850002f226c9f3b44 Mon Sep 17 00:00:00 2001 From: talesrune Date: Wed, 16 Oct 2019 21:43:34 +0800 Subject: [PATCH] Local changes: Partially replaced magic numbers into constants. --- src/main/java/duke/AddWindow.java | 4 +- src/main/java/duke/Duke.java | 3 +- src/main/java/duke/MainWindow.java | 23 ++- src/main/java/duke/parser/Parser.java | 192 +++++++++--------- src/main/java/duke/storage/BudgetStorage.java | 13 +- .../java/duke/storage/ContactStorage.java | 15 +- .../java/duke/storage/PriorityStorage.java | 3 +- src/main/java/duke/storage/Storage.java | 41 ++-- src/main/java/duke/task/BudgetList.java | 11 +- src/main/java/duke/task/ContactList.java | 6 +- src/main/java/duke/task/Deadline.java | 49 +++-- src/main/java/duke/task/DetectDuplicate.java | 3 +- src/main/java/duke/task/Event.java | 49 +++-- src/main/java/duke/task/PriorityList.java | 24 ++- src/main/java/duke/task/Repeat.java | 50 +++-- src/main/java/duke/task/Task.java | 4 +- src/main/java/duke/task/TaskList.java | 15 +- src/main/java/duke/ui/Ui.java | 38 ++-- 18 files changed, 313 insertions(+), 230 deletions(-) diff --git a/src/main/java/duke/AddWindow.java b/src/main/java/duke/AddWindow.java index 30162b391c..acfa2a1c99 100644 --- a/src/main/java/duke/AddWindow.java +++ b/src/main/java/duke/AddWindow.java @@ -17,6 +17,8 @@ */ public class AddWindow extends AnchorPane { private Duke duke; + private static final int ZERO = 0; + private MainWindow mainWindow; @FXML @@ -62,7 +64,7 @@ public void setAddWindow(Duke d, MainWindow mainWindow) { "Do After" ); TaskList items = d.getTaskList(); - for (int i = 0; i < items.size(); i++) { + for (int i = ZERO; i < items.size(); i++) { cbExistingTask.getItems().add(items.get(i).getDescription()); } } diff --git a/src/main/java/duke/Duke.java b/src/main/java/duke/Duke.java index 7adb815b22..6c46b25eb1 100644 --- a/src/main/java/duke/Duke.java +++ b/src/main/java/duke/Duke.java @@ -40,6 +40,7 @@ public class Duke { private BudgetStorage budgetStorage; private BudgetList budgetList; + private static final int ZERO = 0; /** * Creates a duke to initialize storage, task list, and ui. @@ -82,7 +83,7 @@ public Duke(String filePath1, String filePath2, String filePathForBudget, String } catch (IOException e) { ui.showLoadingError(); budgetList = new BudgetList(); - budgetList.addToBudget(0); + budgetList.addToBudget(ZERO); } } diff --git a/src/main/java/duke/MainWindow.java b/src/main/java/duke/MainWindow.java index a879f07726..32d78a89b7 100644 --- a/src/main/java/duke/MainWindow.java +++ b/src/main/java/duke/MainWindow.java @@ -64,6 +64,11 @@ public class MainWindow extends AnchorPane { private Duke duke; + private static final int ZERO = 0; + private static final int ONE = 1; + private static final int TIMER_DELAY = 500; + private static final int VBOX_WIDTH = 200; + private Image userImage = new Image(this.getClass().getResourceAsStream("/images/myUser.png")); private Image dukeImage = new Image(this.getClass().getResourceAsStream("/images/myBot.png")); @@ -90,7 +95,7 @@ public void setDuke(Duke d) { Timer timer = new Timer(); TimerTask exitDuke = new TimerTask() { public void run() { - System.exit(0); + System.exit(ZERO); } }; @@ -113,7 +118,7 @@ private void handleUserInput() { dialogContainer.getChildren().add( DialogBox.getDukeDialog(response, dukeImage) ); - timer.schedule(exitDuke, new Date(System.currentTimeMillis() + 500)); + timer.schedule(exitDuke, new Date(System.currentTimeMillis() + TIMER_DELAY)); } else { response = duke.executeCommand(cmd); dialogContainer.getChildren().add( @@ -186,7 +191,7 @@ private void onMouseClickDone() { //System.out.println("CURRENTLY on " + listT.getSelectionModel().getSelectedItem()); Task taskObj = listT.getSelectionModel().getSelectedItem(); TaskList items = duke.getTaskList(); - int itemNumber = items.getIndex(taskObj) + 1; + int itemNumber = items.getIndex(taskObj) + ONE; handleUserEvent("done " + itemNumber); updateGui(); } @@ -195,7 +200,7 @@ private void onMouseClickDone() { private void onMouseClickDelete() { Task taskObj = listT.getSelectionModel().getSelectedItem(); TaskList items = duke.getTaskList(); - int itemNumber = items.getIndex(taskObj) + 1; + int itemNumber = items.getIndex(taskObj) + ONE; handleUserEvent("delete " + itemNumber); updateGui(); } @@ -225,7 +230,7 @@ private void onMouseClickUpdate() { private void onMouseClickOK() { Task taskObj = listT.getSelectionModel().getSelectedItem(); TaskList items = duke.getTaskList(); - int itemNumber = items.getIndex(taskObj) + 1; + int itemNumber = items.getIndex(taskObj) + ONE; if (cbupdateType.getSelectionModel().getSelectedItem().equals("Description")) { handleUserEvent("update " + itemNumber + " /desc " + tfnewDesc.getText().trim()); } else if (cbupdateType.getSelectionModel().getSelectedItem().equals("Date/Time")) { @@ -269,10 +274,10 @@ private void cleanUp() { @FXML private void setVboxWidth(boolean isEnabled) { if (isEnabled) { - vboxUpdate.setPrefWidth(200); + vboxUpdate.setPrefWidth(VBOX_WIDTH); vboxUpdate.setVisible(true); } else { - vboxUpdate.setPrefWidth(0); + vboxUpdate.setPrefWidth(ZERO); vboxUpdate.setVisible(false); } } @@ -306,7 +311,7 @@ private void updateGui() { protected void listViewRefresh() { listT.getItems().clear(); TaskList items = duke.getTaskList(); - for (int i = 0; i < items.size(); i++) { + for (int i = ZERO; i < items.size(); i++) { listT.getItems().add(items.get(i)); } } @@ -332,7 +337,7 @@ private void exitProgram() { dialogContainer.getChildren().add( DialogBox.getDukeDialog(response, dukeImage) ); - timer.schedule(exitDuke, new Date(System.currentTimeMillis() + 500)); + timer.schedule(exitDuke, new Date(System.currentTimeMillis() + TIMER_DELAY)); } catch (DukeException e) { response = Ui.showErrorMsgGui(e.getMessage()); dialogContainer.getChildren().add( diff --git a/src/main/java/duke/parser/Parser.java b/src/main/java/duke/parser/Parser.java index 6a6be6e2ea..853960cbba 100644 --- a/src/main/java/duke/parser/Parser.java +++ b/src/main/java/duke/parser/Parser.java @@ -40,11 +40,19 @@ * Represents a parser that breaks down user input into commands. */ public class Parser { + private static final int ZERO = 0; + private static final int ONE = 1; + private static final int MINUS_ONE = -1; + private static final int TWO = 2; + private static final int THREE = 3; + private static final int FOUR = 4; + /** * Generates a command based on the user input. * * @param sentence User input. * @param items The task list that contains a list of tasks. + * @param budgetList The list that contains a list of budget. * @return Command to be executed afterwards. * @throws Exception If there is an error interpreting the user input. */ @@ -57,19 +65,20 @@ public static Command parse(String sentence, TaskList items, BudgetList budgetLi return new ListCommand(); } else if (sentence.equals("priority")) { return new ListPriorityCommand(); - } else if (arr.length > 0 && (arr[0].equals("done") || arr[0].equals("delete") || arr[0].equals("del"))) { - if (arr.length == 1) { + } else if (arr.length > ZERO && (arr[ZERO].equals("done") + || arr[ZERO].equals("delete") || arr[ZERO].equals("del"))) { + if (arr.length == ONE) { throw new DukeException(" (>_<) OOPS!!! The task number cannot be empty."); } else { - int tasknum = Integer.parseInt(arr[1]) - 1; - if (tasknum < 0 || tasknum >= items.size()) { + int tasknum = Integer.parseInt(arr[ONE]) - ONE; + if (tasknum < ZERO || tasknum >= items.size()) { throw new DukeException(" (>_<) OOPS!!! Invalid task number."); } else { - if (arr[0].equals("done")) { + if (arr[ZERO].equals("done")) { if (items.get(tasknum).toString().contains("[A]")) { String tempString = items.get(tasknum).toString(); - tempString = tempString.split(": ", 2)[1]; - tempString = tempString.split("\\)")[0]; + tempString = tempString.split(": ", TWO)[ONE]; + tempString = tempString.split("\\)")[ZERO]; if (!items.isTaskDone(tempString)) { throw new DukeException(" (>_<) OOPS!! Task requirements has yet to be completed!" @@ -82,23 +91,23 @@ public static Command parse(String sentence, TaskList items, BudgetList budgetLi } } } - } else if (arr.length > 0 && arr[0].equals("find")) { - if (arr.length == 1) { + } else if (arr.length > ZERO && arr[ZERO].equals("find")) { + if (arr.length == ONE) { throw new DukeException(" (>_<) OOPS!!! The keyword cannot be empty."); } else { - if (arr[1].trim().isEmpty()) { + if (arr[ONE].trim().isEmpty()) { throw new DukeException(" (>_<) OOPS!!! The keyword cannot be empty."); } else { - return new FindCommand(arr[1]); + return new FindCommand(arr[ONE]); } } - } else if (arr.length > 0 && arr[0].equals("todo")) { - String[] getDescription = sentence.split(" ", 2); + } else if (arr.length > ZERO && arr[ZERO].equals("todo")) { + String[] getDescription = sentence.split(" ", TWO); DetectDuplicate detectDuplicate = new DetectDuplicate(items); - if (detectDuplicate.isDuplicate(getDescription[0], getDescription[1])) { + if (detectDuplicate.isDuplicate(getDescription[ZERO], getDescription[ONE])) { return new DuplicateFoundCommand(); } else { - for (int i = 1; i < arr.length; i++) { + for (int i = ONE; i < arr.length; i++) { taskDesc += arr[i] + " "; } taskDesc = taskDesc.trim(); @@ -109,9 +118,10 @@ public static Command parse(String sentence, TaskList items, BudgetList budgetLi return new AddCommand(taskObj); } } - } else if (arr.length > 0 && (arr[0].equals("deadline") || arr[0].equals("dl") || arr[0].equals("event"))) { - for (int i = 1; i < arr.length; i++) { - if ((arr[i].trim().isEmpty() || !arr[i].substring(0, 1).equals("/")) && !getDate) { + } else if (arr.length > ZERO && (arr[ZERO].equals("deadline") + || arr[ZERO].equals("dl") || arr[ZERO].equals("event"))) { + for (int i = ONE; i < arr.length; i++) { + if ((arr[i].trim().isEmpty() || !arr[i].substring(ZERO, ONE).equals("/")) && !getDate) { taskDesc += arr[i] + " "; } else { if (!getDate) { //detect "/" @@ -124,33 +134,33 @@ public static Command parse(String sentence, TaskList items, BudgetList budgetLi taskDesc = taskDesc.trim(); dateDesc = dateDesc.trim(); if (taskDesc.isEmpty()) { - throw new DukeException(" (>_<) OOPS!!! The description of a " + arr[0] + " cannot be empty."); + throw new DukeException(" (>_<) OOPS!!! The description of a " + arr[ZERO] + " cannot be empty."); } else if (dateDesc.isEmpty()) { throw new DukeException(" (>_<) OOPS!!! The description of date/time for " - + arr[0] + " cannot be empty."); + + arr[ZERO] + " cannot be empty."); } else { Task taskObj; - if (arr[0].equals("deadline") || arr[0].equals("dl")) { + if (arr[ZERO].equals("deadline") || arr[ZERO].equals("dl")) { taskObj = new Deadline(taskDesc, dateDesc); } else { taskObj = new Event(taskDesc, dateDesc); } - for (int i = 0; i < items.size(); i++) { + for (int i = ZERO; i < items.size(); i++) { if (taskObj.getDateTime().equals(items.get(i).getDateTime()) && !items.get(i).isDone()) { throw new DukeException(" (>_<) OOPS!!! The date/time for " - + arr[0] + " clashes with " + items.get(i).toString() + + arr[ZERO] + " clashes with " + items.get(i).toString() + "\n Please choose another date/time! Or mark the above task as Done first!"); } } return new AddCommand(taskObj); } - } else if (arr.length > 0 && (arr[0].equals("doafter") || arr[0].equals("da"))) { + } else if (arr.length > ZERO && (arr[ZERO].equals("doafter") || arr[ZERO].equals("da"))) { //doafter /after String afterTaskDesc = ""; boolean detectBackSlash = false; - for (int i = 1; i < arr.length; i++) { - if ((arr[i].trim().isEmpty() || !arr[i].substring(0, 1).equals("/")) && !detectBackSlash) { + for (int i = ONE; i < arr.length; i++) { + if ((arr[i].trim().isEmpty() || !arr[i].substring(ZERO, ONE).equals("/")) && !detectBackSlash) { taskDesc += arr[i] + " "; } else { if (!detectBackSlash) { @@ -163,10 +173,10 @@ public static Command parse(String sentence, TaskList items, BudgetList budgetLi taskDesc = taskDesc.trim(); afterTaskDesc = afterTaskDesc.trim(); if (taskDesc.isEmpty()) { - throw new DukeException(" (>_<) OOPS!!! The description of a " + arr[0] + " cannot be empty."); + throw new DukeException(" (>_<) OOPS!!! The description of a " + arr[ZERO] + " cannot be empty."); } else if (afterTaskDesc.isEmpty()) { throw new DukeException(" (>_<) OOPS!!! The description of Task for " - + arr[0] + " cannot be empty."); + + arr[ZERO] + " cannot be empty."); } else { String currentTasks = items.getList(); if (currentTasks.contains(afterTaskDesc)) { @@ -175,13 +185,13 @@ public static Command parse(String sentence, TaskList items, BudgetList budgetLi return new AddCommand(taskObj); } else { throw new DukeException("(>_<) OOPS!!! You cant set a " - + arr[0] + " task for a task that is not in the list!"); + + arr[ZERO] + " task for a task that is not in the list!"); } } - } else if (arr.length > 0 && (arr[0].equals("repeat") || arr[0].equals("rep"))) { + } else if (arr.length > ZERO && (arr[ZERO].equals("repeat") || arr[ZERO].equals("rep"))) { //repeat /from /for 3 - for (int i = 1; i < arr.length; i++) { - if ((arr[i].trim().isEmpty() || !arr[i].substring(0, 1).equals("/")) && !getDate) { + for (int i = ONE; i < arr.length; i++) { + if ((arr[i].trim().isEmpty() || !arr[i].substring(ZERO, ONE).equals("/")) && !getDate) { taskDesc += arr[i] + " "; } else { if (!getDate) { //detect "/" @@ -195,18 +205,18 @@ public static Command parse(String sentence, TaskList items, BudgetList budgetLi dateDesc = dateDesc.trim(); if (taskDesc.isEmpty()) { - throw new DukeException(" (>_<) OOPS!!! The description of a " + arr[0] + " cannot be empty."); + throw new DukeException(" (>_<) OOPS!!! The description of a " + arr[ZERO] + " cannot be empty."); } else if (dateDesc.isEmpty()) { throw new DukeException(" (>_<) OOPS!!! The description of date/time for " - + arr[0] + " cannot be empty."); + + arr[ZERO] + " cannot be empty."); } else { String repeatSettings; int repeatTimes; String repeatPeriod; try { - repeatSettings = dateDesc.split("/for ")[1]; + repeatSettings = dateDesc.split("/for ")[ONE]; repeatTimes = Integer.parseInt(repeatSettings.replaceAll("[\\D]", "")); - repeatPeriod = repeatSettings.split(repeatTimes + " ")[1]; + repeatPeriod = repeatSettings.split(repeatTimes + " ")[ONE]; } catch (Exception e) { throw new DukeException("Format is in: repeat /from " @@ -214,50 +224,50 @@ public static Command parse(String sentence, TaskList items, BudgetList budgetLi } ArrayList repeatList = new ArrayList<>(); - String timeDesc = dateDesc.split(" ", 3)[1]; - for (int i = 0; i < repeatTimes; i++) { + String timeDesc = dateDesc.split(" ", THREE)[ONE]; + for (int i = ZERO; i < repeatTimes; i++) { Task taskObj; taskObj = new Repeat(taskDesc, dateDesc); dateDesc = DateParser.add(dateDesc, repeatPeriod) + " " + timeDesc; repeatList.add(taskObj); - for (int j = 0; j < items.size(); j++) { + for (int j = ZERO; j < items.size(); j++) { if (taskObj.getDateTime().equals(items.get(j).getDateTime()) && !items.get(j).isDone()) { throw new DukeException(" (>_<) OOPS!!! The date/time for " - + arr[0] + " clashes with " + items.get(j).toString() + + arr[ZERO] + " clashes with " + items.get(j).toString() + "\n Please choose another date/time! Or mark the above task as Done first!"); } } } return new AddMultipleCommand(repeatList); } - } else if (arr.length > 0 && (arr[0].equals("fixedduration") || arr[0].equals("fd"))) { + } else if (arr.length > ZERO && (arr[ZERO].equals("fixedduration") || arr[ZERO].equals("fd"))) { //fixedduration /for String description = ""; String durDesc; int duration; String unit; - for (int i = 1; i < arr.length; i++) { + for (int i = ONE; i < arr.length; i++) { description += arr[i] + " "; } - taskDesc = description.split(" /for ")[0].trim(); - durDesc = description.split(" /for ")[1].trim(); + taskDesc = description.split(" /for ")[ZERO].trim(); + durDesc = description.split(" /for ")[ONE].trim(); DetectDuplicate detectDuplicate = new DetectDuplicate(items); if (taskDesc.isEmpty()) { - throw new DukeException(" (>_<) OOPS!!! The description of a " + arr[0] + " cannot be empty."); + throw new DukeException(" (>_<) OOPS!!! The description of a " + arr[ZERO] + " cannot be empty."); } else if (durDesc.isEmpty()) { throw new DukeException(" (>_<) OOPS!!! The description of duration for " - + arr[0] + " cannot be empty."); - } else if (detectDuplicate.isDuplicate(arr[0], taskDesc)) { + + arr[ZERO] + " cannot be empty."); + } else if (detectDuplicate.isDuplicate(arr[ZERO], taskDesc)) { return new DuplicateFoundCommand(); } else { try { - duration = Integer.parseInt(durDesc.split(" ")[0].trim()); + duration = Integer.parseInt(durDesc.split(" ")[ZERO].trim()); } catch (Exception e) { throw new DukeException("Format is in: fixedduration /for "); } - unit = durDesc.split(" ")[1].trim(); + unit = durDesc.split(" ")[ONE].trim(); if (unit.isEmpty() || (!unit.toLowerCase().contains("min") && ! unit.toLowerCase().contains("hour"))) { throw new DukeException("Format is in: fixedduration /for "); } else { @@ -265,75 +275,75 @@ public static Command parse(String sentence, TaskList items, BudgetList budgetLi return new AddCommand(fixedDuration); } } - } else if (arr.length > 0 && (arr[0].equals("setpriority"))) { + } else if (arr.length > ZERO && (arr[ZERO].equals("setpriority"))) { //fixedduration String description = ""; int taskNum; int priority; - for (int i = 1; i < arr.length; i++) { + for (int i = ONE; i < arr.length; i++) { description += arr[i] + " "; } String[] holder = description.split(" "); - if (holder.length < 2) { + if (holder.length < TWO) { throw new DukeException(" (>_<) OOPS!!! Format is in: setpriority "); } else { try { - taskNum = Integer.parseInt(holder[0].trim()); + taskNum = Integer.parseInt(holder[ZERO].trim()); } catch (Exception e) { throw new DukeException("The task number must be an integer"); } - if (taskNum <= 0 || taskNum > items.size()) { + if (taskNum <= ZERO || taskNum > items.size()) { throw new DukeException(" (>_<) OOPS!!! Invalid task number."); } try { - priority = Integer.parseInt(holder[1].trim()); + priority = Integer.parseInt(holder[ONE].trim()); } catch (Exception e) { throw new DukeException("The priority must be an integer"); } - if (!((priority > 0) && (priority < 6))) { + if (!((priority > ZERO) && (priority < 6))) { throw new DukeException(" (>_<) OOPS!!! Invalid priority! (1 - High ~ 5 - Low)."); } return new SetPriorityCommand(taskNum, priority); } - } else if (arr.length > 0 && arr[0].equals("remind")) { + } else if (arr.length > ZERO && arr[ZERO].equals("remind")) { //remind /in String description = ""; String durDesc; int duration; String unit; - for (int i = 1; i < arr.length; i++) { + for (int i = ONE; i < arr.length; i++) { description += arr[i] + " "; } if (description.isEmpty()) { - throw new DukeException(" (>_<) OOPS!!! The description of a " + arr[0] + " cannot be empty."); + throw new DukeException(" (>_<) OOPS!!! The description of a " + arr[ZERO] + " cannot be empty."); } - duration = Integer.parseInt(description.split("/in", 2)[0].trim()) - 1; - String in = description.split(" /in ", 2)[1].trim(); - int howManyDays = Integer.parseInt(in.split(" ", 2)[0].trim()); + duration = Integer.parseInt(description.split("/in", TWO)[ZERO].trim()) - ONE; + String in = description.split(" /in ", TWO)[ONE].trim(); + int howManyDays = Integer.parseInt(in.split(" ", TWO)[ZERO].trim()); return new RemindCommand(duration, howManyDays); - } else if (arr.length > 0 && (arr[0].equals("update"))) { - if (arr.length == 1) { + } else if (arr.length > ZERO && (arr[ZERO].equals("update"))) { + if (arr.length == ONE) { throw new DukeException(" (>_<) OOPS!!! The task number cannot be empty."); } else { - int tasknum = Integer.parseInt(arr[1]) - 1; - if (tasknum < 0 || tasknum >= items.size()) { + int tasknum = Integer.parseInt(arr[ONE]) - ONE; + if (tasknum < ZERO || tasknum >= items.size()) { throw new DukeException(" (>_<) OOPS!!! Invalid task number."); - } else if (arr.length < 4) { + } else if (arr.length < FOUR) { throw new DukeException(" (>_<) OOPS!!! Insufficient parameters. " + "Format: update "); } else { - int typeOfUpdate = -1; + int typeOfUpdate = MINUS_ONE; String typeDesc = ""; - for (int i = 2; i < arr.length; i++) { - if (i == 2) { + for (int i = TWO; i < arr.length; i++) { + if (i == TWO) { if (arr[i].trim().isEmpty() || (!arr[i].equals("/desc") && !arr[i].equals("/date") @@ -342,17 +352,17 @@ public static Command parse(String sentence, TaskList items, BudgetList budgetLi + "/date, /desc, or /type."); } else { if (arr[i].equals("/desc")) { - typeOfUpdate = 1; + typeOfUpdate = ONE; } else if (arr[i].equals("/date")) { - typeOfUpdate = 2; + typeOfUpdate = TWO; } else { //equals /type - typeOfUpdate = 3; + typeOfUpdate = THREE; } } } else { - if (typeOfUpdate == 1) { + if (typeOfUpdate == ONE) { taskDesc += arr[i] + " "; - } else if (typeOfUpdate == 2) { + } else if (typeOfUpdate == TWO) { dateDesc += arr[i] + " "; } else { //type of update is number 3 typeDesc += arr[i] + " "; @@ -362,46 +372,46 @@ public static Command parse(String sentence, TaskList items, BudgetList budgetLi taskDesc = taskDesc.trim(); dateDesc = dateDesc.trim(); typeDesc = typeDesc.trim(); - if (typeOfUpdate == 1 && taskDesc.isEmpty()) { + if (typeOfUpdate == ONE && taskDesc.isEmpty()) { throw new DukeException(" (>_<) OOPS!!! The description of a " - + arr[0] + " cannot be empty."); - } else if (typeOfUpdate == 2 && dateDesc.isEmpty()) { + + arr[ZERO] + " cannot be empty."); + } else if (typeOfUpdate == TWO && dateDesc.isEmpty()) { throw new DukeException(" (>_<) OOPS!!! The description of date/time for " - + arr[0] + " cannot be empty."); - } else if (typeOfUpdate == 3 && typeDesc.isEmpty()) { + + arr[ZERO] + " cannot be empty."); + } else if (typeOfUpdate == THREE && typeDesc.isEmpty()) { throw new DukeException(" (>_<) OOPS!!! The description of type for " - + arr[0] + " cannot be empty."); + + arr[ZERO] + " cannot be empty."); } else { return new UpdateCommand(taskDesc, dateDesc, typeDesc, typeOfUpdate, tasknum); } } } - } else if (arr.length > 0 && arr[0].equals("addcontact")) { - String[] userInput = sentence.split(" ",2); - String[] contactDetails = userInput[1].split(","); + } else if (arr.length > ZERO && arr[ZERO].equals("addcontact")) { + String[] userInput = sentence.split(" ",TWO); + String[] contactDetails = userInput[ONE].split(","); try { - Contacts contactObj = new Contacts(contactDetails[0], contactDetails[1], - contactDetails[2], contactDetails[3]); + Contacts contactObj = new Contacts(contactDetails[ZERO], contactDetails[ONE], + contactDetails[TWO], contactDetails[THREE]); return new AddContactsCommand(contactObj); } catch (Exception e) { throw new DukeException("Format is in: addcontact , , , "); } } else if (sentence.equals("listcontacts")) { return new ListContactsCommand(); - } else if (arr.length > 0 && arr[0].equals("budget")) { + } else if (arr.length > ZERO && arr[ZERO].equals("budget")) { try { - String budgetCommandString = sentence.split(" ", 2)[1]; + String budgetCommandString = sentence.split(" ", TWO)[ONE]; } catch (Exception e) { throw new DukeException(" (>_<) OoPS!!! Invalid Budget Command. " + "It should be: budget "); } - String budgetCommandString = sentence.split(" ", 2)[1]; - String budgetCommand = budgetCommandString.split(" ", 2)[0]; + String budgetCommandString = sentence.split(" ", TWO)[ONE]; + String budgetCommand = budgetCommandString.split(" ", TWO)[ZERO]; if (budgetCommand.trim().equals("view")) { return new ViewBudgetCommand(budgetList); } else { try { - String budgetAmount = budgetCommandString.split(" ", 2)[1]; + String budgetAmount = budgetCommandString.split(" ", TWO)[ONE]; if (budgetCommand.trim().equals("new") || budgetCommand.trim().equals("reset")) { return new UpdateBudgetCommand(budgetList, Float.parseFloat(budgetAmount)); } else if (budgetCommand.trim().equals("add") || budgetCommand.trim().equals("+")) { diff --git a/src/main/java/duke/storage/BudgetStorage.java b/src/main/java/duke/storage/BudgetStorage.java index e3ba35753c..108be83e2b 100644 --- a/src/main/java/duke/storage/BudgetStorage.java +++ b/src/main/java/duke/storage/BudgetStorage.java @@ -11,7 +11,10 @@ import java.util.ArrayList; public class BudgetStorage { - protected String filePath = "./"; + //protected String filePath = "./"; + protected String filePath = ""; + String storageClassPath = Storage.class.getProtectionDomain().getCodeSource().getLocation().getPath(); + /** * Creates a storage with path pointing to the file in the system. @@ -19,6 +22,14 @@ public class BudgetStorage { * @param filePathForBudget The location of the file in computer. */ public BudgetStorage(String filePathForBudget) { + String[] pathSplitter = storageClassPath.split("/"); + for (String directory: pathSplitter) { + if (!directory.isEmpty() && !directory.equals("build")) { + this.filePath += directory + "/"; + } else if (directory.equals("build")) { + break; + } + } this.filePath += filePathForBudget; } diff --git a/src/main/java/duke/storage/ContactStorage.java b/src/main/java/duke/storage/ContactStorage.java index fdbe4b385e..d5dd01a28a 100644 --- a/src/main/java/duke/storage/ContactStorage.java +++ b/src/main/java/duke/storage/ContactStorage.java @@ -15,6 +15,11 @@ public class ContactStorage { //protected String filePathForContacts = "./"; protected String filePathForContacts = ""; String storageClassPath = Storage.class.getProtectionDomain().getCodeSource().getLocation().getPath(); + private static final int ZERO = 0; + private static final int ONE = 1; + private static final int TWO = 2; + private static final int THREE = 3; + /** * Creates a storage with a specified filePathForContacts. @@ -53,10 +58,10 @@ public ArrayList read() throws IOException { String office; while ((st = br.readLine()) != null) { //name + "," + contact + "," + email + "," + office String[] contactDetails = st.split(","); - name = contactDetails[0]; - contact = contactDetails[1]; - email = contactDetails[2]; - office = contactDetails[3]; + name = contactDetails[ZERO]; + contact = contactDetails[ONE]; + email = contactDetails[TWO]; + office = contactDetails[THREE]; Contacts contactObj = new Contacts(name, contact, email, office); contacts.add(contactObj); } @@ -72,7 +77,7 @@ public ArrayList read() throws IOException { */ public void write(ContactList contacts) throws IOException { String fileContent = ""; - for (int i = 0; i < contacts.size(); i++) { + for (int i = ZERO; i < contacts.size(); i++) { fileContent += contacts.get(i).toFile() + "\n"; } BufferedWriter writer = new BufferedWriter(new FileWriter(filePathForContacts)); diff --git a/src/main/java/duke/storage/PriorityStorage.java b/src/main/java/duke/storage/PriorityStorage.java index c9029bb037..0612fbd38b 100644 --- a/src/main/java/duke/storage/PriorityStorage.java +++ b/src/main/java/duke/storage/PriorityStorage.java @@ -15,6 +15,7 @@ public class PriorityStorage { //protected String filePath = "./"; protected String filePath = ""; String storageClassPath = Storage.class.getProtectionDomain().getCodeSource().getLocation().getPath(); + private static final int ZERO = 0; /** * Creates a storage with a specified filePath. @@ -62,7 +63,7 @@ public ArrayList read() throws IOException { */ public void write(PriorityList priorityList) throws IOException { String fileContent = ""; - for (int i = 0; i < priorityList.getSize(); i++) { + for (int i = ZERO; i < priorityList.getSize(); i++) { fileContent += priorityList.getList().get(i) + "\n"; } BufferedWriter writer = new BufferedWriter(new FileWriter(filePath)); diff --git a/src/main/java/duke/storage/Storage.java b/src/main/java/duke/storage/Storage.java index 24a6c9b008..2ba974260e 100644 --- a/src/main/java/duke/storage/Storage.java +++ b/src/main/java/duke/storage/Storage.java @@ -26,6 +26,11 @@ public class Storage { //protected String filePath = "./"; protected String filePath = ""; String storageClassPath = Storage.class.getProtectionDomain().getCodeSource().getLocation().getPath(); + private static final int ZERO = 0; + private static final int ONE = 1; + private static final int TWO = 2; + private static final int THREE = 3; + @@ -73,13 +78,13 @@ public ArrayList read() throws IOException { dateDesc = ""; afterDesc = ""; durDesc = ""; - for (int i = 0; i < commandList.length; i++) { - if (i == 2) { + for (int i = ZERO; i < commandList.length; i++) { + if (i == TWO) { taskDesc = commandList[i]; - } else if (i == 3) { - if (commandList[0].equals("A")) { + } else if (i == THREE) { + if (commandList[ZERO].equals("A")) { afterDesc = commandList[i]; - } else if (commandList[0].equals("F")) { + } else if (commandList[ZERO].equals("F")) { durDesc = commandList[i]; } else { dateDesc = commandList[i]; @@ -87,14 +92,14 @@ public ArrayList read() throws IOException { } } boolean checked = false; - if (commandList.length > 1) { - if (!(commandList[1].equals("1") || commandList[1].equals("0"))) { + if (commandList.length > ONE) { + if (!(commandList[ONE].equals("1") || commandList[ONE].equals("0"))) { throw new DukeException("Error reading 1 or 0, skipping to next line"); } - checked = commandList[1].equals("1"); + checked = commandList[ONE].equals("1"); } Task t; - if (commandList[0].equals("T")) { + if (commandList[ZERO].equals("T")) { if (taskDesc.trim().isEmpty()) { throw new DukeException("Error reading description, skipping to next line"); } else { @@ -102,7 +107,7 @@ public ArrayList read() throws IOException { t.setStatusIcon(checked); items.add(t); } - } else if (commandList[0].equals("D")) { + } else if (commandList[ZERO].equals("D")) { if (taskDesc.trim().isEmpty() || dateDesc.trim().isEmpty()) { throw new DukeException("Error reading description or date/time, skipping to next line"); } else { @@ -110,7 +115,7 @@ public ArrayList read() throws IOException { t.setStatusIcon(checked); items.add(t); } - } else if (commandList[0].equals("E")) { + } else if (commandList[ZERO].equals("E")) { if (taskDesc.isEmpty() || dateDesc.isEmpty()) { throw new DukeException("Error reading description or date/time, skipping to next line"); } else { @@ -118,7 +123,7 @@ public ArrayList read() throws IOException { t.setStatusIcon(checked); items.add(t); } - } else if (commandList[0].equals("R")) { + } else if (commandList[ZERO].equals("R")) { if (taskDesc.isEmpty() || dateDesc.isEmpty()) { throw new DukeException("Error reading description or date/time, skipping to next line"); } else { @@ -126,7 +131,7 @@ public ArrayList read() throws IOException { t.setStatusIcon(checked); items.add(t); } - } else if (commandList[0].equals("A")) { + } else if (commandList[ZERO].equals("A")) { if (taskDesc.isEmpty() || afterDesc.isEmpty()) { throw new DukeException("Error reading description or do after description," + " skipping to next line"); @@ -135,18 +140,18 @@ public ArrayList read() throws IOException { t.setStatusIcon(checked); items.add(t); } - } else if (commandList[0].equals("F")) { + } else if (commandList[ZERO].equals("F")) { System.out.println(taskDesc + dateDesc); if (taskDesc.isEmpty() || durDesc.isEmpty()) { throw new DukeException("Error reading description or do after description," + " skipping to next line"); } else { - int duration = Integer.parseInt(durDesc.split(" ")[0]); - t = new FixedDuration(taskDesc, duration, durDesc.split(" ")[1]); + int duration = Integer.parseInt(durDesc.split(" ")[ZERO]); + t = new FixedDuration(taskDesc, duration, durDesc.split(" ")[ONE]); t.setStatusIcon(checked); items.add(t); } - } else if (!commandList[0].isEmpty()) { + } else if (!commandList[ZERO].isEmpty()) { throw new DukeException("Error reading whether if its T, D, E, R, A, or F skipping to next line"); } } catch (Exception e) { @@ -168,7 +173,7 @@ public ArrayList read() throws IOException { */ public void write(TaskList items) throws IOException { String fileContent = ""; - for (int i = 0; i < items.size(); i++) { + for (int i = ZERO; i < items.size(); i++) { fileContent += items.get(i).toFile() + "\n"; } BufferedWriter writer = new BufferedWriter(new FileWriter(filePath)); diff --git a/src/main/java/duke/task/BudgetList.java b/src/main/java/duke/task/BudgetList.java index f49a3abd28..20213bf7d2 100644 --- a/src/main/java/duke/task/BudgetList.java +++ b/src/main/java/duke/task/BudgetList.java @@ -3,7 +3,8 @@ import java.util.ArrayList; public class BudgetList { - public static final float INITIAL_BUDGET = 0; + private static final int ZERO = 0; + public static final float INITIAL_BUDGET = ZERO; private ArrayList budgetList; /** @@ -30,9 +31,9 @@ public BudgetList(ArrayList list) { * @param amount the amount to be added into the budget. */ public void addToBudget(float amount) { - float currentBudget = budgetList.get(0); + float currentBudget = budgetList.get(ZERO); budgetList.add(amount); - budgetList.set(0, currentBudget + amount); + budgetList.set(ZERO, currentBudget + amount); } /** @@ -41,7 +42,7 @@ public void addToBudget(float amount) { * @return returns the budget that is stored in budgetList. */ public float getBudget() { - return budgetList.get(0); + return budgetList.get(ZERO); } /** @@ -50,7 +51,7 @@ public float getBudget() { * @param amount The budget amount that is to be reset to. */ public void resetBudget(float amount) { - budgetList.set(0, amount); + budgetList.set(ZERO, amount); } /** diff --git a/src/main/java/duke/task/ContactList.java b/src/main/java/duke/task/ContactList.java index 2658b6c677..13d239f08e 100644 --- a/src/main/java/duke/task/ContactList.java +++ b/src/main/java/duke/task/ContactList.java @@ -7,6 +7,8 @@ */ public class ContactList { protected ArrayList contactList; + private static final int ZERO = 0; + private static final int ONE = 1; /** * Creates an empty contact list using an array list. @@ -50,8 +52,8 @@ public Contacts get(int index) { */ public String getContactList() { String fullContactList = ""; - for (int i = 0; i < contactList.size(); i++) { - fullContactList += (i + 1) + ". " + contactList.get(i).toStringGui() + "\n"; + for (int i = ZERO; i < contactList.size(); i++) { + fullContactList += (i + ONE) + ". " + contactList.get(i).toStringGui() + "\n"; } return fullContactList; } diff --git a/src/main/java/duke/task/Deadline.java b/src/main/java/duke/task/Deadline.java index 39b671aec6..71887bf79a 100644 --- a/src/main/java/duke/task/Deadline.java +++ b/src/main/java/duke/task/Deadline.java @@ -11,6 +11,15 @@ public class Deadline extends Task { protected Date by; protected String[] suf = { "st", "nd", "rd", "th" }; protected SimpleDateFormat datetimeFormat = new SimpleDateFormat("dd/MM/yyyy HHmm"); + private static final int ZERO = 0; + private static final int ONE = 1; + private static final int MINUS_ONE = -1; + private static final int TWO = 2; + private static final int THREE = 3; + private static final int TWENTY_ONE = 21; + private static final int TWENTY_TWO = 22; + private static final int TWENTY_THREE = 23; + private static final int THIRTY_ONE = 31; /** * Creates a deadline with the specified description and date/time. @@ -44,21 +53,21 @@ public String toString() { int day = Integer.parseInt(new SimpleDateFormat("d").format(by)); int min = Integer.parseInt(new SimpleDateFormat("m").format(by)); - if (min > 0) { + if (min > ZERO) { displayDT = datetimeFormat2.format(by); } else { displayDT = datetimeFormat3.format(by); } - int sufIndex = -1; + int sufIndex = MINUS_ONE; - if (day == 1 || day == 21 || day == 31) { - sufIndex = 0; - } else if (day == 2 || day == 22) { - sufIndex = 1; - } else if (day == 3 || day == 23) { - sufIndex = 2; - } else if (day > 3 && day < 31) { - sufIndex = 3; + if (day == ONE || day == TWENTY_ONE || day == THIRTY_ONE) { + sufIndex = ZERO; + } else if (day == TWO || day == TWENTY_TWO) { + sufIndex = ONE; + } else if (day == THREE || day == TWENTY_THREE) { + sufIndex = TWO; + } else if (day > THREE && day < THIRTY_ONE) { + sufIndex = THREE; } String suffixStr = day + suf[sufIndex]; displayDT = suffixStr + " of " + displayDT; @@ -78,21 +87,21 @@ public String toStringGui() { int day = Integer.parseInt(new SimpleDateFormat("d").format(by)); int min = Integer.parseInt(new SimpleDateFormat("m").format(by)); - if (min > 0) { + if (min > ZERO) { displayDT = datetimeFormat2.format(by); } else { displayDT = datetimeFormat3.format(by); } - int sufIndex = -1; + int sufIndex = MINUS_ONE; - if (day == 1 || day == 21 || day == 31) { - sufIndex = 0; - } else if (day == 2 || day == 22) { - sufIndex = 1; - } else if (day == 3 || day == 23) { - sufIndex = 2; - } else if (day > 3 && day < 31) { - sufIndex = 3; + if (day == ONE || day == TWENTY_ONE || day == THIRTY_ONE) { + sufIndex = ZERO; + } else if (day == TWO || day == TWENTY_TWO) { + sufIndex = ONE; + } else if (day == THREE || day == TWENTY_THREE) { + sufIndex = TWO; + } else if (day > THREE && day < THIRTY_ONE) { + sufIndex = THREE; } String suffixStr = day + suf[sufIndex]; displayDT = suffixStr + " of " + displayDT; diff --git a/src/main/java/duke/task/DetectDuplicate.java b/src/main/java/duke/task/DetectDuplicate.java index e9ec5793b0..7b60811dfe 100644 --- a/src/main/java/duke/task/DetectDuplicate.java +++ b/src/main/java/duke/task/DetectDuplicate.java @@ -5,6 +5,7 @@ */ public class DetectDuplicate { protected TaskList items; + private static final int ZERO = 0; /** * Creates a DetectDuplicate task with a list of TaskList available. @@ -24,7 +25,7 @@ public DetectDuplicate(TaskList items) { */ public boolean isDuplicate(String command, String description) { if ("todo".equals(command) || "fixedduration".equals(command)) { - for (int i = 0; i < items.size(); i++) { + for (int i = ZERO; i < items.size(); i++) { if (items.get(i).isContain(description)) { //contains, is implemented in Task.java return true; diff --git a/src/main/java/duke/task/Event.java b/src/main/java/duke/task/Event.java index 8bec239351..fdb21e13c9 100644 --- a/src/main/java/duke/task/Event.java +++ b/src/main/java/duke/task/Event.java @@ -11,6 +11,15 @@ public class Event extends Task { protected Date at; protected String[] suf = { "st", "nd", "rd", "th" }; protected SimpleDateFormat datetimeFormat = new SimpleDateFormat("dd/MM/yyyy HHmm"); + private static final int ZERO = 0; + private static final int ONE = 1; + private static final int MINUS_ONE = -1; + private static final int TWO = 2; + private static final int THREE = 3; + private static final int TWENTY_ONE = 21; + private static final int TWENTY_TWO = 22; + private static final int TWENTY_THREE = 23; + private static final int THIRTY_ONE = 31; /** * Creates an event with the specified description and date/time. @@ -46,21 +55,21 @@ public String toString() { int day = Integer.parseInt(new SimpleDateFormat("d").format(at)); int min = Integer.parseInt(new SimpleDateFormat("m").format(at)); - if (min > 0) { + if (min > ZERO) { displayDT = datetimeFormat2.format(at); } else { displayDT = datetimeFormat3.format(at); } - int sufIndex = -1; + int sufIndex = MINUS_ONE; - if (day == 1 || day == 21 || day == 31) { - sufIndex = 0; - } else if (day == 2 || day == 22) { - sufIndex = 1; - } else if (day == 3 || day == 23) { - sufIndex = 2; - } else if (day > 3 && day < 31) { - sufIndex = 3; + if (day == ONE || day == TWENTY_ONE || day == THIRTY_ONE) { + sufIndex = ZERO; + } else if (day == TWO || day == TWENTY_TWO) { + sufIndex = ONE; + } else if (day == THREE || day == TWENTY_THREE) { + sufIndex = TWO; + } else if (day > THREE && day < THIRTY_ONE) { + sufIndex = THREE; } String suffixStr = day + suf[sufIndex]; displayDT = suffixStr + " of " + displayDT; @@ -80,21 +89,21 @@ public String toStringGui() { int day = Integer.parseInt(new SimpleDateFormat("d").format(at)); int min = Integer.parseInt(new SimpleDateFormat("m").format(at)); - if (min > 0) { + if (min > ZERO) { displayDT = datetimeFormat2.format(at); } else { displayDT = datetimeFormat3.format(at); } - int sufIndex = -1; + int sufIndex = MINUS_ONE; - if (day == 1 || day == 21 || day == 31) { - sufIndex = 0; - } else if (day == 2 || day == 22) { - sufIndex = 1; - } else if (day == 3 || day == 23) { - sufIndex = 2; - } else if (day > 3 && day < 31) { - sufIndex = 3; + if (day == ONE || day == TWENTY_ONE || day == THIRTY_ONE) { + sufIndex = ZERO; + } else if (day == TWO || day == TWENTY_TWO) { + sufIndex = ONE; + } else if (day == THREE || day == TWENTY_THREE) { + sufIndex = TWO; + } else if (day > THREE && day < THIRTY_ONE) { + sufIndex = THREE; } String suffixStr = day + suf[sufIndex]; displayDT = suffixStr + " of " + displayDT; diff --git a/src/main/java/duke/task/PriorityList.java b/src/main/java/duke/task/PriorityList.java index d2f9b33c1e..aa8f0c2e18 100644 --- a/src/main/java/duke/task/PriorityList.java +++ b/src/main/java/duke/task/PriorityList.java @@ -10,8 +10,12 @@ * Represents a priority list that stores a list of priorities associated with each task. */ public class PriorityList { + private static final int ZERO = 0; + private static final int ONE = 1; + private static final int FIVE = 5; + private ArrayList priorityList; - private int defultPriority = 5; + private int defultPriority = FIVE; /** * Creates an empty priority list using an array list. @@ -39,7 +43,7 @@ public PriorityList(ArrayList list) { */ public PriorityList setPriority(int taskNum, int priority) { - priorityList.set(taskNum - 1, priority); + priorityList.set(taskNum - ONE, priority); return new PriorityList(priorityList); } @@ -65,7 +69,7 @@ public PriorityList addDefaultPriority(duke.command.Command cmd) { */ public PriorityList addMultiDefaultPriority(int numOfTimes) { - for (int i = 0; i < numOfTimes; i++) { + for (int i = ZERO; i < numOfTimes; i++) { priorityList.add(defultPriority); } @@ -130,17 +134,17 @@ private PriorityList clearList() { */ public static ArrayList sortPriority(TaskList taskList, PriorityList priorities) { ArrayList pairList = new ArrayList<>(); - for (int i = 0; i < taskList.size(); i++) { + for (int i = ZERO; i < taskList.size(); i++) { Pair pair = new Pair<>(priorities.getPriority(i), taskList.get(i)); pairList.add(pair); } - for (int i = 1; i < taskList.size(); i++) { - for (int j = i; j > 0; j--) { - if (((int) pairList.get(j).getKey()) < (int) pairList.get(j - 1).getKey()) { + for (int i = ONE; i < taskList.size(); i++) { + for (int j = i; j > ZERO; j--) { + if (((int) pairList.get(j).getKey()) < (int) pairList.get(j - ONE).getKey()) { Pair temp = pairList.get(j); - pairList.set(j, pairList.get(j - 1)); - pairList.set(j - 1, temp); + pairList.set(j, pairList.get(j - ONE)); + pairList.set(j - ONE, temp); } else { break; } @@ -153,7 +157,7 @@ public static ArrayList sortPriority(TaskList taskList, PriorityList prior @Override public String toString() { String output = ""; - for (int i = 0; i < priorityList.size(); i++) { + for (int i = ZERO; i < priorityList.size(); i++) { output += priorityList.get(i) + " "; } diff --git a/src/main/java/duke/task/Repeat.java b/src/main/java/duke/task/Repeat.java index aed811664d..dc7a24a79b 100644 --- a/src/main/java/duke/task/Repeat.java +++ b/src/main/java/duke/task/Repeat.java @@ -8,6 +8,16 @@ * Represents a recursive task that stores the same description and across the different dates. */ public class Repeat extends Task { + private static final int ZERO = 0; + private static final int ONE = 1; + private static final int MINUS_ONE = -1; + private static final int TWO = 2; + private static final int THREE = 3; + private static final int TWENTY_ONE = 21; + private static final int TWENTY_TWO = 22; + private static final int TWENTY_THREE = 23; + private static final int THIRTY_ONE = 31; + protected Date from; protected String[] suf = { "st", "nd", "rd", "th" }; protected SimpleDateFormat datetimeFormat = new SimpleDateFormat("dd/MM/yyyy HHmm"); @@ -44,21 +54,21 @@ public String toString() { int day = Integer.parseInt(new SimpleDateFormat("d").format(from)); int min = Integer.parseInt(new SimpleDateFormat("m").format(from)); - if (min > 0) { + if (min > ZERO) { displayDT = datetimeFormat2.format(from); } else { displayDT = datetimeFormat3.format(from); } - int sufIndex = -1; + int sufIndex = MINUS_ONE; - if (day == 1 || day == 21 || day == 31) { - sufIndex = 0; - } else if (day == 2 || day == 22) { - sufIndex = 1; - } else if (day == 3 || day == 23) { - sufIndex = 2; - } else if (day > 3 && day < 31) { - sufIndex = 3; + if (day == ONE || day == TWENTY_ONE || day == THIRTY_ONE) { + sufIndex = ZERO; + } else if (day == TWO || day == TWENTY_TWO) { + sufIndex = ONE; + } else if (day == THREE || day == TWENTY_THREE) { + sufIndex = TWO; + } else if (day > THREE && day < THIRTY_ONE) { + sufIndex = THREE; } String suffixStr = day + suf[sufIndex]; displayDT = suffixStr + " of " + displayDT; @@ -78,21 +88,21 @@ public String toStringGui() { int day = Integer.parseInt(new SimpleDateFormat("d").format(from)); int min = Integer.parseInt(new SimpleDateFormat("m").format(from)); - if (min > 0) { + if (min > ZERO) { displayDT = datetimeFormat2.format(from); } else { displayDT = datetimeFormat3.format(from); } - int sufIndex = -1; + int sufIndex = MINUS_ONE; - if (day == 1 || day == 21 || day == 31) { - sufIndex = 0; - } else if (day == 2 || day == 22) { - sufIndex = 1; - } else if (day == 3 || day == 23) { - sufIndex = 2; - } else if (day > 3 && day < 31) { - sufIndex = 3; + if (day == ONE || day == TWENTY_ONE || day == THIRTY_ONE) { + sufIndex = ZERO; + } else if (day == TWO || day == TWENTY_TWO) { + sufIndex = ONE; + } else if (day == THREE || day == TWENTY_THREE) { + sufIndex = TWO; + } else if (day > THREE && day < THIRTY_ONE) { + sufIndex = THREE; } String suffixStr = day + suf[sufIndex]; displayDT = suffixStr + " of " + displayDT; diff --git a/src/main/java/duke/task/Task.java b/src/main/java/duke/task/Task.java index 87b9b75847..30dfe561d4 100644 --- a/src/main/java/duke/task/Task.java +++ b/src/main/java/duke/task/Task.java @@ -8,9 +8,11 @@ * Represents a task that stores description and boolean that indicates the task as completed. */ public class Task { + private static final int ZERO = 0; + protected String description; protected boolean isDone; - public int numberOfDays = 0; + public int numberOfDays = ZERO; public LocalDateTime currentDate; public LocalDateTime dueDate; diff --git a/src/main/java/duke/task/TaskList.java b/src/main/java/duke/task/TaskList.java index 4d05b54eab..13f69f5156 100644 --- a/src/main/java/duke/task/TaskList.java +++ b/src/main/java/duke/task/TaskList.java @@ -7,6 +7,9 @@ */ public class TaskList { protected ArrayList items; + private static final int ZERO = 0; + private static final int MINUS_ONE = -1; + private static final int ONE = 1; /** * Creates an empty task list using an array list. @@ -88,8 +91,8 @@ public Task get(int index) { * @return int that contains the index of task. */ public int getIndex(Task taskObj) { - int index = -1; - for (int i = 0; i < items.size(); i++) { + int index = MINUS_ONE; + for (int i = ZERO; i < items.size(); i++) { if (taskObj.equals(items.get(i))) { index = i; } @@ -104,8 +107,8 @@ public int getIndex(Task taskObj) { */ public String getList() { String listStr = ""; - for (int i = 0; i < items.size(); i++) { - listStr += " " + (i + 1) + "." + items.get(i).toString() + "\n"; + for (int i = ZERO; i < items.size(); i++) { + listStr += " " + (i + ONE) + "." + items.get(i).toString() + "\n"; } return listStr; } @@ -117,8 +120,8 @@ public String getList() { */ public String getListGui() { String listStr = ""; - for (int i = 0; i < items.size(); i++) { - listStr += " " + (i + 1) + "." + items.get(i).toStringGui() + "\n"; + for (int i = ZERO; i < items.size(); i++) { + listStr += " " + (i + ONE) + "." + items.get(i).toStringGui() + "\n"; } return listStr; } diff --git a/src/main/java/duke/ui/Ui.java b/src/main/java/duke/ui/Ui.java index 3280dc559a..1d5913ee5f 100644 --- a/src/main/java/duke/ui/Ui.java +++ b/src/main/java/duke/ui/Ui.java @@ -20,6 +20,8 @@ public class Ui { protected static final String LINE = " ____________________________________________________________"; protected final Scanner in; protected final PrintStream out; + private static final int ZERO = 0; + private static final int ONE = 1; /** * Creates an empty ui using default scanner and print stream. @@ -74,11 +76,11 @@ public String readCommand() { public static void showReminder(TaskList tasks) { ArrayList taskList = tasks.getTasks(); System.out.println(" You currently have these upcoming tasks:\n"); - int currentIndex = 1; + int currentIndex = ONE; for (Task remaining: taskList) { remaining.isTriggerReminder(); System.out.println(" " + currentIndex + "." + remaining.toString()); - currentIndex += 1; + currentIndex += ONE; } System.out.println(LINE); } @@ -104,7 +106,7 @@ public void showTaskListWithPriority(TaskList items, PriorityList priorities) { ArrayList pair = PriorityList.sortPriority(items, priorities); out.println(" Here are the tasks in your list with priority shown:\n"); out.printf(" Priority |\tTask\n"); - for (int i = 0; i < items.size() && i < priorities.getSize(); i++) { + for (int i = ZERO; i < items.size() && i < priorities.getSize(); i++) { out.printf(" [%d]\t |\t%s\n", pair.get(i).getKey(), pair.get(i).getValue()); } } @@ -138,7 +140,7 @@ public static String showTaskListGui(TaskList items) { */ public void showUpdate(TaskList items, int index) { out.println(" Nice! I've updated this task ^^:"); - out.println(" " + (index + 1) + "." + items.get(index).toString()); + out.println(" " + (index + ONE) + "." + items.get(index).toString()); } /** @@ -150,7 +152,7 @@ public void showUpdate(TaskList items, int index) { */ public static String showUpdateGui(TaskList items, int index) { String str = " Nice! I've updated this task ^^:\n" - + " " + (index + 1) + "." + items.get(index).toString(); + + " " + (index + ONE) + "." + items.get(index).toString(); return str; } @@ -209,7 +211,7 @@ public static String showDeleteGui(TaskList items, String deletedTask) { */ public void showAdd(TaskList items) { out.println(" Got it. I've added this task:"); - out.println(" " + items.get(items.size() - 1).toString()); + out.println(" " + items.get(items.size() - ONE).toString()); out.println(" Now you have " + items.size() + " tasks in the list."); } @@ -221,7 +223,7 @@ public void showAdd(TaskList items) { */ public static String showAddGui(TaskList items) { String str = " Got it. I've added this task:\n " - + items.get(items.size() - 1).toStringGui() + "\n Now you have " + + items.get(items.size() - ONE).toStringGui() + "\n Now you have " + items.size() + " tasks in the list.\n"; return str; } @@ -271,14 +273,14 @@ public static String showByeGui() { */ public void showFind(TaskList items, String keyword) { out.println(" Here are the matching tasks in your list:"); - int numFound = 0; - for (int i = 0; i < items.size(); i++) { + int numFound = ZERO; + for (int i = ZERO; i < items.size(); i++) { if (items.get(i).getDescription().contains(keyword)) { - out.println(" " + (i + 1) + "." + items.get(i).toString()); + out.println(" " + (i + ONE) + "." + items.get(i).toString()); numFound++; } } - if (numFound == 0) { + if (numFound == ZERO) { out.println(" No matching tasks found."); } } @@ -292,14 +294,14 @@ public void showFind(TaskList items, String keyword) { */ public static String showFindGui(TaskList items, String keyword) { String str = " Here are the matching tasks in your list:\n"; - int numFound = 0; - for (int i = 0; i < items.size(); i++) { + int numFound = ZERO; + for (int i = ZERO; i < items.size(); i++) { if (items.get(i).getDescription().contains(keyword)) { - str += " " + (i + 1) + "." + items.get(i).toStringGui() + "\n"; + str += " " + (i + ONE) + "." + items.get(i).toStringGui() + "\n"; numFound++; } } - if (numFound == 0) { + if (numFound == ZERO) { str += " No matching tasks found.\n"; } return str; @@ -362,7 +364,7 @@ public void showAfterBackupMsg() { * @param priority The index of the priority. */ public void showSetPriority(TaskList taskList, int taskNum, int priority) { - out.println(" Updated the priority of \n\t\t" + taskList.get(taskNum - 1)); + out.println(" Updated the priority of \n\t\t" + taskList.get(taskNum - ONE)); out.println(" Current priority: " + priority); } @@ -373,10 +375,10 @@ public void showSetPriority(TaskList taskList, int taskNum, int priority) { */ public void showAddedContact(ContactList contactList) { out.println(" Got it. Contact added:"); - if (contactList.size() == 0) { + if (contactList.size() == ZERO) { out.println(" You have no contacts!"); } else { - out.println(contactList.get(contactList.size() - 1)); + out.println(contactList.get(contactList.size() - ONE)); out.println(" Now you have " + contactList.size() + " contacts."); } }