Skip to content
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

Fixed Bugs where thresholds can be less than or equal to 0, causing w… #165

Merged
merged 2 commits into from
Oct 31, 2021
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 15 additions & 6 deletions src/main/java/seedu/situs/parser/Parser.java
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@
import seedu.situs.ingredients.Ingredient;



import java.io.IOException;
import java.time.LocalDate;
import java.time.format.DateTimeParseException;
Expand Down Expand Up @@ -44,7 +43,9 @@ public class Parser {
private static final String EXPIRY_FORMAT_ERROR_MESSAGE = "Invalid expiry date format!"
+ '\n' + "Please key in the expiry date in the format dd/mm/yyyy!";
private static final String INVALID_ALERT_TYPE_MESSAGE = "Not an alert type!";
private static final String INVALID_THRESHOLD_TYPE_MESSAGE = "Not a threshold type!";
private static final String SET_THRESHOLD_ERROR_MESSAGE = "Error in setting threshold";
private static final String INVALID_THRESHOLD_MESSAGE = "Thresholds cannot be less than or equal to 0";

private static final String SPACE_SEPARATOR = " ";
private static final String EMPTY_STRING = "";
Expand Down Expand Up @@ -347,13 +348,21 @@ private static String parseSetCommand(String command) throws SitusException {
try {
switch (details[1].trim()) {
case "expiry":
AlertExpiringSoonCommand.setExpiryThreshold(Long.parseLong(details[2].trim()));
return "Successfully set expiry threshold to " + details[2].trim() + " days";
long newExpiryThreshold = Long.parseLong(details[2].trim());
if (newExpiryThreshold <= 0) {
throw new SitusException(INVALID_THRESHOLD_MESSAGE);
}
AlertExpiringSoonCommand.setExpiryThreshold(newExpiryThreshold);
return "Successfully set expiry threshold to " + newExpiryThreshold + " days";
case "stock":
AlertLowStockCommand.setLowStockThreshold(Double.parseDouble(details[2].trim()));
return "Successfully set low stock threshold to " + details[2].trim() + " kg";
double newStockThreshold = Double.parseDouble(details[2].trim());
if (newStockThreshold <= 0) {
throw new SitusException(INVALID_THRESHOLD_MESSAGE);
}
AlertLowStockCommand.setLowStockThreshold(newStockThreshold);
return "Successfully set low stock threshold to " + newStockThreshold + " kg";
default:
throw new SitusException(INVALID_ALERT_TYPE_MESSAGE);
throw new SitusException(INVALID_THRESHOLD_TYPE_MESSAGE);
}
} catch (NumberFormatException e) {
throw new SitusException(NUMBER_FORMAT_ERROR_MESSAGE);
Expand Down