Skip to content

Commit

Permalink
Merge branch 'JUnit'
Browse files Browse the repository at this point in the history
  • Loading branch information
woodenclock committed Oct 16, 2023
2 parents 6fb4e2f + 62cdd84 commit 16b0525
Show file tree
Hide file tree
Showing 14 changed files with 181 additions and 34 deletions.
1 change: 1 addition & 0 deletions build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -43,4 +43,5 @@ checkstyle {

run{
standardInput = System.in
enableAssertions = true
}
2 changes: 1 addition & 1 deletion src/main/java/seedu/wildwatch/WildWatch.java
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,6 @@ public static void main(String[] args) {
Ui.printHorizontalLines();
System.out.print(BootUp.WELCOME_MESSAGE_2);
Ui.printHorizontalLines();
Parser.manualInputHandler();
Parser.manualInputHandler(); //Parser takes on
}
}
38 changes: 34 additions & 4 deletions src/main/java/seedu/wildwatch/command/AddCommand.java
Original file line number Diff line number Diff line change
@@ -1,12 +1,42 @@
package seedu.wildwatch.command;

import java.util.regex.Matcher;
import java.util.regex.Pattern;

import seedu.wildwatch.entry.EntryList;
import seedu.wildwatch.exception.IncorrectInputException;
import seedu.wildwatch.operation.Ui;

/**
* Command class for listing all task in EntryList
* Command class for adding task to EntryList
*/
public class AddCommand extends Command {
public static void addEntry(String inputBuffer, boolean isFromFile) {
//Todo Split input into Date, Species, Remark
//Todo Insert into EntryList

public static final Pattern ADD_ENTRY_COMMAND_FORMAT =
Pattern.compile("add"
+ " D/(?<date>[^/]+)"
+ " S/(?<species>[^/]+)"
+ " N/(?<name>[^/]+)"
+ "(?: R/(?<remark>[^/]+))?");

public static void addEntry(String inputBuffer, boolean isFromFile) throws IncorrectInputException {
//if (!isFromFile) {
// Ui.listMessagePrinter();
//}

final Matcher matcher = ADD_ENTRY_COMMAND_FORMAT.matcher(inputBuffer);
if (!matcher.matches()) {
throw new IncorrectInputException();
}

final String date = matcher.group("date");
final String species = matcher.group("species");
final String name = matcher.group("name");
final String remark = matcher.group("remark");

EntryList.addEntry(date, species, name, remark);
Ui.entryAddedMessagePrinter();
Ui.printEntry(EntryList.getArraySize() - 1);
}
}

2 changes: 1 addition & 1 deletion src/main/java/seedu/wildwatch/command/DeleteCommand.java
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ public class DeleteCommand extends Command {
*/
public static void deleteEntry(int numberInput) {
Ui.entryRemovedMessagePrinter();
EntryList.entryRemover(numberInput);
EntryList.deleteEntry(numberInput);
Ui.entryCountPrinter();
}
}
2 changes: 1 addition & 1 deletion src/main/java/seedu/wildwatch/command/HelpCommand.java
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
package seedu.wildwatch.command;

public class HelpCommand {
public class HelpCommand extends Command {
/**
* Requires UPDATE
*/
Expand Down
2 changes: 1 addition & 1 deletion src/main/java/seedu/wildwatch/command/ListCommand.java
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ public static void listEntry(boolean isFromFile) {
int arraySize = EntryList.getArraySize();
for (int i = 0; i < arraySize; i++) {
System.out.print(i + 1 + ".");
Ui.print(i);
Ui.printEntry(i);
}
Ui.entryCountPrinter();
}
Expand Down
14 changes: 9 additions & 5 deletions src/main/java/seedu/wildwatch/entry/EntryList.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
import java.util.ArrayList;

public class EntryList {
private static ArrayList<Entry> entries = new ArrayList<>(); //Keeps track of all Entry instances made
private static ArrayList<Entry> entries = new ArrayList<>(); //Keeps track of all Entry instances made

public static ArrayList<Entry> getArray() {
return entries;
Expand All @@ -14,14 +14,14 @@ public static int getArraySize() {
return entries.size();
}

public static void entryRemover(int numberInput){
entries.remove(numberInput);
}

public static void addEntry(String date, String species, String name, String remark) {
entries.add(new Entry(date, species, name, remark));
}

public static void deleteEntry(int numberInput){
entries.remove(numberInput - 1);
}

public static Entry getEntry(int nthTask) {
return entries.get(nthTask);
}
Expand All @@ -34,6 +34,10 @@ public static String getEntrySpecies(int nthTask) {
return getEntry(nthTask).getSpecies();
}

public static String getEntryName(int nthTask) {
return getEntry(nthTask).getName();
}

public static String getEntryRemark(int nthTask) {
return getEntry(nthTask).getRemark();
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
package seedu.wildwatch.exception;

public class IncorrectInputException extends Exception {
}
5 changes: 3 additions & 2 deletions src/main/java/seedu/wildwatch/operation/DateHandler.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,14 @@

import java.util.regex.Pattern;
public class DateHandler {
private static final Pattern DATE_TIME_PATTERN = Pattern.compile(
private static final Pattern DATE_PATTERN = Pattern.compile(
"(0[1-9]|[1-2][0-9]|3[0-1])-(0[1-9]|1[0-2])-\\d{2}" // dd-mm-yy
);

public static boolean isDateValid(String inputBuffer) {
//Trims off everything except date
CharSequence date = inputBuffer.substring(inputBuffer.indexOf("D/") + 2, inputBuffer.indexOf("S/")).trim();
if (DATE_TIME_PATTERN.matcher(date).matches()) {
if (DATE_PATTERN.matcher(date).matches()) {
return true;
}
return false;
Expand Down
17 changes: 15 additions & 2 deletions src/main/java/seedu/wildwatch/operation/ErrorHandler.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import java.time.format.DateTimeParseException;
import java.util.Scanner;
import java.util.logging.Logger;

import seedu.wildwatch.command.HelpCommand;
import seedu.wildwatch.entry.EntryList;
Expand All @@ -12,31 +13,43 @@
import seedu.wildwatch.exception.EntryNotFoundException;
import seedu.wildwatch.exception.UnknownInputException;
import seedu.wildwatch.exception.UnknownDateFormatException;
import seedu.wildwatch.exception.IncorrectInputException;

public class ErrorHandler {
private static final int DEFAULT_NUMBER_INPUT = -100;
private static final int DEFAULT_NUMBER_INPUT = -3710; //Number never input during normal use of WildWatch
private static final Logger LOGGER = Logger.getLogger(ErrorHandler.class.getName());
public static void handleError(String inputBuffer) {
boolean validInput = false;
try {
checkError(inputBuffer);
Parser.entryManager(inputBuffer, false);
validInput = true;
} catch (EmptyInputException exception) {
LOGGER.warning("Received an empty input.");
Ui.emptyDescriptionMessagePrinter(null);
} catch (EmptyAddException exception){
LOGGER.warning("Received an empty add input.");
Ui.emptyDescriptionMessagePrinter("add");
} catch (EmptyDeleteException exception) {
LOGGER.warning("Received an empty delete input.");
Ui.emptyDescriptionMessagePrinter("delete");
} catch (EmptyListException exception) {
LOGGER.info("List is empty.");
Ui.emptyListMessagePrinter();
validInput = true;
} catch (EntryNotFoundException exception) {
LOGGER.warning("Queried entry not found.");
Ui.entryNotFoundMessagePrinter();
validInput = true;
} catch (UnknownInputException exception) {
LOGGER.warning("Unknown input received.");
Ui.unknownInputMessagePrinter();
} catch (UnknownDateFormatException | DateTimeParseException exception) {
LOGGER.warning("Date is invalid.");
Ui.invalidDateMessagePrinter();
} catch (IncorrectInputException exception) {
LOGGER.warning("Command is invalid.");
Ui.incorrectInputMessagePrinter();
} finally {
if (!validInput) {
Ui.printHorizontalLines();
Expand All @@ -47,7 +60,7 @@ public static void handleError(String inputBuffer) {

public static void checkError(String inputBuffer) throws EmptyInputException, EmptyAddException,
EmptyListException, EmptyDeleteException, EntryNotFoundException, UnknownDateFormatException {

assert inputBuffer != null : "Input buffer should not be null.";
Scanner bufferScanner = new Scanner(inputBuffer); //Scanner for the buffer
String firstWord; //First word of input
int numberInput = DEFAULT_NUMBER_INPUT;
Expand Down
33 changes: 24 additions & 9 deletions src/main/java/seedu/wildwatch/operation/Parser.java
Original file line number Diff line number Diff line change
@@ -1,22 +1,29 @@
package seedu.wildwatch.operation;

import java.util.Scanner;

import java.util.logging.Level;
import java.util.logging.Logger;
import seedu.wildwatch.command.HelpCommand;
import seedu.wildwatch.command.ListCommand;
import seedu.wildwatch.command.DeleteCommand;
import seedu.wildwatch.command.AddCommand;
import seedu.wildwatch.exception.IncorrectInputException;
import seedu.wildwatch.exception.UnknownInputException;


public class Parser {
private static final int DEFAULT_NUMBER_INPUT = -3710; //Number that can never be input in normal use of WildWatch
private static final Logger LOGGER = Logger.getLogger(Parser.class.getName());

public static void manualInputHandler() {
while (true) {
String inputBuffer = Ui.inputRetriever(); //Retrieves input of user
LOGGER.log(Level.INFO, "Input received: {0}", inputBuffer);

if (inputBuffer.contains("bye")) { //Program exit
if (inputBuffer.equals("bye")) { //Program exit
LOGGER.log(Level.INFO, "Exiting program on user command.");
break;
} else if (inputBuffer.equals("help")) {
} else if (inputBuffer.equals("help")) { //User request "help"
Ui.printHorizontalLines();
Ui.helpRequestMessagePrinter();
Ui.printHorizontalLines();
Expand All @@ -27,26 +34,34 @@ public static void manualInputHandler() {
Ui.printHorizontalLines();
}
}
LOGGER.log(Level.INFO, "Initiating shutdown procedures.");
ShutDown.shutDown();
}

public static void entryManager(String inputBuffer, boolean isFromFile) throws UnknownInputException {
public static void entryManager(String inputBuffer, boolean isFromFile)
throws UnknownInputException, IncorrectInputException {
LOGGER.log(Level.INFO, "Managing entry for input: {0}", inputBuffer);
Scanner bufferScanner = new Scanner(inputBuffer); //Scanner for the buffer
String firstWord = bufferScanner.next(); //Stores first word in the input
boolean hasInteger = bufferScanner.hasNextInt(); //Indicates that some integer was input
int numberInput = -1; //Stores the number input
if (hasInteger) {
assert firstWord != null && !firstWord.isEmpty() : "First word shouldn't be null or empty";
boolean hasInputInteger = bufferScanner.hasNextInt(); //Indicates that some integer was input
int numberInput = DEFAULT_NUMBER_INPUT; //Stores the number input
if (hasInputInteger) {
numberInput = bufferScanner.nextInt();
assert numberInput != DEFAULT_NUMBER_INPUT : "Number input wasn't parsed correctly";
}

//Functionalities
if (firstWord.equals("add")) {
AddCommand.addEntry(inputBuffer, isFromFile);
} else if (firstWord.equals("delete") && hasInteger && !bufferScanner.hasNext()) {
} else if (firstWord.equals("delete") && hasInputInteger && !bufferScanner.hasNext()) {
assert numberInput > 0 : "Entry number to delete should be positive";
DeleteCommand.deleteEntry(numberInput);
} else if (inputBuffer.equals("list")) {
ListCommand.listEntry(isFromFile);
} else {
LOGGER.log(Level.WARNING, "Unknown input received: {0}. Throwing exception.", inputBuffer);
throw new UnknownInputException(); //Unrecognizable by Parser
}
throw new UnknownInputException();
}
}
21 changes: 16 additions & 5 deletions src/main/java/seedu/wildwatch/operation/Ui.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import seedu.wildwatch.entry.EntryList;

import java.time.format.DateTimeFormatter;
import java.util.Scanner;

/**
Expand Down Expand Up @@ -32,7 +33,7 @@ public static void listMessagePrinter() {
}

public static void entryCountPrinter() {
System.out.println("Now you have " + (EntryList.getArraySize()) + " task(s) in the list.");
System.out.println("Now you have " + (EntryList.getArraySize()) + " entries in the list.");
}

public static void unknownInputMessagePrinter() {
Expand All @@ -51,8 +52,16 @@ public static void entryNotFoundMessagePrinter() {
System.out.println("OOPS!!! The entry number could not be found :-(");
}

public static void incorrectInputMessagePrinter() {
System.out.println("OOPS!!! Format of command is incorrect.");
}

public static void entryRemovedMessagePrinter() {
System.out.println("The entry have been removed.");
System.out.println("The entry has been removed.");
}

public static void entryAddedMessagePrinter() {
System.out.println("The following entry has been added:");
}

public static void emptyDescriptionMessagePrinter(String description) {
Expand All @@ -67,9 +76,11 @@ public static void emptyDescriptionMessagePrinter(String description) {
}
}

public static void print(int nthTask) {
System.out.print("Date: [" + EntryList.getEntryDate(nthTask) + "] ");
System.out.print("Species: [" + EntryList.getEntrySpecies(nthTask) + "] ");
public static void printEntry(int nthTask) {
System.out.print("Date: [" +
EntryList.getEntryDate(nthTask).format(DateTimeFormatter.ofPattern("dd-MM-yyyy")) + "] | ");
System.out.print("Species: [" + EntryList.getEntrySpecies(nthTask) + "] | ");
System.out.print("Name: [" + EntryList.getEntryName(nthTask) + "] | ");
System.out.print("Remark: [" + EntryList.getEntryRemark(nthTask) + "]");
System.out.print(System.lineSeparator());
}
Expand Down
Loading

0 comments on commit 16b0525

Please sign in to comment.