Skip to content

Commit

Permalink
Fix bugs
Browse files Browse the repository at this point in the history
  • Loading branch information
daryltay415 committed Apr 14, 2024
1 parent 35d943f commit 498d3b6
Show file tree
Hide file tree
Showing 7 changed files with 68 additions and 45 deletions.
4 changes: 3 additions & 1 deletion docs/UserGuide.md
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ ____________________________________________________________
> 📒 Notes about the command format :
> * The words that are in `UPPER_CASE` represents the parameters that the users are required to input
> e.g. `delete INDEX`, `INDEX` is a parameter which can be used as `delete 1`.
> * Commands such as `help`, `list` and `bye` do not require additional parameters. Hence, any extra parameters will be ignored.
> * Commands such as `help` and `bye` do not require additional parameters. Hence, any extra parameters will be ignored.
> e.g. `help 123` will just be intepreted as `help`.
> * Words that are in square brackets such as `[/tag TAG]` indicates that it is
> optional to include in the command.
Expand Down Expand Up @@ -341,6 +341,7 @@ their travel activities.
Format: `tag INDEX TAGNAME`

* The `INDEX` must be a valid activity index.
* There must not be any trailing spaces in `INDEX`.

Example of usage:
* `tag 1 activity 1`
Expand All @@ -359,6 +360,7 @@ Removes a tag from an existing travel activity.
Format: `untag INDEX`

* The `INDEX` must be a valid activity index.
* * There must not be any trailing spaces in `INDEX`.

Example of usage:
* `untag 1 `
Expand Down
1 change: 1 addition & 0 deletions omni.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
general / 0 / go to paris / 2024-12-12 / 2 hours / /
26 changes: 14 additions & 12 deletions src/main/java/seedu/omnitravel/errorhandlers/CheckParameters.java
Original file line number Diff line number Diff line change
Expand Up @@ -34,9 +34,10 @@ public static void addExceptions(String[] input, String commandType, String line
throw new OmniException("The tag cannot be empty!");
} else if (input.length < 3 || input[2].contains("/tag") || !command[1].contains("date")
|| !command[2].contains("duration")){
throw new OmniException("Please check that your add command is in this format: add DESCRIPTION " +
"/date YYYY-MM-DD /duration DURATION"
+ " or add DESCRIPTION /date YYYY-MM-DD /duration DURATION /tag TAG");
throw new OmniException("Please check that your " + commandType + " command is in this format: \n" +
commandType + " DESCRIPTION " +
"/date YYYY-MM-DD /duration DURATION\n"
+ "or " + commandType + " DESCRIPTION /date YYYY-MM-DD /duration DURATION /tag TAG");
}
}

Expand All @@ -49,8 +50,9 @@ public static void addExceptions(String[] input, String commandType, String line
*/
public static void updateExceptions(String[] command, String line) throws OmniException {
String[] lineSplit = line.split("/");
if (command.length >= 4 && (command[1].isBlank() || !isNumeric(command[1]))) {
throw new OmniException("The update index cannot be empty or non numerical!");
if (command.length >= 4 && (command[1].isBlank() || !isNumeric(command[1].trim()))) {
throw new OmniException("The update index cannot be empty or non numerical!\n" +
"It must be a single number");
} else if (command.length >= 4 && command[2].isBlank()) {
throw new OmniException("The date cannot be empty!");
} else if (command.length >= 4 && command[3].isBlank()) {
Expand All @@ -59,9 +61,9 @@ public static void updateExceptions(String[] command, String line) throws OmniEx
throw new OmniException("The tag cannot be empty!");
} else if (command.length < 4 || command[3].contains("/tag") || !lineSplit[1].contains("date")
|| !lineSplit[2].contains("duration")) {
throw new OmniException("Please check that your update command is in this format: update INDEX " +
"/date YYYY-MM-DD /duration DURATION"
+ " or update INDEX /date YYYY-MM-DD /duration DURATION /tag TAG");
throw new OmniException("Please check that your update command is in this format: \nupdate INDEX " +
"/date YYYY-MM-DD /duration DURATION\n"
+ "or update INDEX /date YYYY-MM-DD /duration DURATION /tag TAG");
}
}

Expand All @@ -86,7 +88,7 @@ public static void listExceptions(String[] command, String[] input, String line)
// command length greater than 4
boolean case4 = command.length > 4;
if (case1 || case2 || case3 || case4) {
throw new OmniException("Please check that your list command is in this format:" +
throw new OmniException("Please check that your list command is in this format:\n" +
"list [/date YYYY-MM-DD] [/sort]");
}
}
Expand All @@ -109,7 +111,7 @@ public static void containsWords(String input) throws OmniException{
}
}
}
throw new OmniException("Your duration is invalid. Please input in terms of \"1 " +
throw new OmniException("Your duration is invalid. Please input in terms of \n\"1 " +
"day/s, week/s, month/s, year/s, hour/s, minutue/s or second/s\"");
}

Expand All @@ -136,7 +138,7 @@ public static boolean isNumeric(String str) {
*/
public static boolean isValidExpense(String str) throws OmniException{
if(isNumeric(str)){
int expense = Integer.parseInt(str);
float expense = Float.parseFloat(str);
if(expense <= 0){
throw new OmniException("Your expense cannot be less than $0");
}
Expand All @@ -162,7 +164,7 @@ public static void checkCurrencyParameters(String[] command, String line) throws
} else if(command.length == 4 && command[2].trim().equalsIgnoreCase(command[3].trim())){
throw new OmniException("The 2 currencies cannot be the same!");
} else if(command.length < 4 || !lineSplit[1].contains("from")){
throw new OmniException("Please check that your format is correct:" +
throw new OmniException("Please check that your format is correct:\n" +
"change AMOUNT /from CURRENCY /to CURRENCY");
}

Expand Down
4 changes: 2 additions & 2 deletions src/main/java/seedu/omnitravel/omnitravel/OmniTravel.java
Original file line number Diff line number Diff line change
Expand Up @@ -105,10 +105,10 @@ private static void invokeCommand(String[] command,
Parser.deleteCommand(command, list, line);
break;
case "check":
Parser.checkCommand(command, list);
Parser.checkCommand(command, list, line);
break;
case "uncheck":
Parser.uncheckCommand(command, list);
Parser.uncheckCommand(command, list, line);
break;
case "find":
Parser.findCommand(line, list);
Expand Down
49 changes: 26 additions & 23 deletions src/main/java/seedu/omnitravel/parser/Parser.java
Original file line number Diff line number Diff line change
Expand Up @@ -118,7 +118,7 @@ public static void addCommand(String line, TravelActivityList list) throws OmniE
String[] command = line.split("/date | /duration | /tag ");
//logger.log(Level.INFO, command[0] + " // " + command[1]);
CheckParameters.addExceptions(command, "add", line);
String description = line.substring(4, line.indexOf("/date"));
String description = line.substring(4, line.indexOf("/date")).trim();
LocalDate date = LocalDate.parse(command[1]);
if(date.isBefore(LocalDate.now())){
throw new OmniException("Please input a future date.");
Expand All @@ -144,15 +144,16 @@ public static void deleteCommand(String[] command, TravelActivityList list, Stri
if(list.getNoOfTravelActivities() == 0){
throw new OmniException("The list is empty!");
}
String[] lineSplit = line.split("delete");
try {
if (command.length < 2) {
throw new OmniException("Please specify which activity index or description to delete");
}
int input = Integer.parseInt(command[1]);
int input = Integer.parseInt(lineSplit[1].trim());
list.removeTravelActivity(input);
} catch (NumberFormatException e) {
int indexOfDescription = line.indexOf(command[1]);
String description = line.substring(indexOfDescription);
int indexOfDescription = line.indexOf(lineSplit[1]);
String description = line.substring(indexOfDescription).trim();
list.removeTravelActivity(description);
}
}
Expand All @@ -165,9 +166,10 @@ public static void deleteCommand(String[] command, TravelActivityList list, Stri
* @param list List of travel activities
* @throws OmniException if command.length != 2 && command[1] is not numeric
*/
public static void checkCommand(String[] command, TravelActivityList list) throws OmniException {
if (command.length == 2 && CheckParameters.isNumeric(command[1])){
int listNumber = Integer.parseInt(command[1]);
public static void checkCommand(String[] command, TravelActivityList list, String line) throws OmniException {
String[] lineSplit = line.split("check");
if (command.length >= 2 && CheckParameters.isNumeric(lineSplit[1].trim())){
int listNumber = Integer.parseInt(lineSplit[1].trim());
list.checkTravelActivity(listNumber);
} else {
throw new OmniException("Please specify which activity to check");
Expand All @@ -181,9 +183,10 @@ public static void checkCommand(String[] command, TravelActivityList list) throw
* @param list List of travel activities
* @throws OmniException if command.length != 2 && command[1] is not numeric
*/
public static void uncheckCommand(String[] command, TravelActivityList list) throws OmniException {
if (command.length == 2 && CheckParameters.isNumeric(command[1])){
int listNumber = Integer.parseInt(command[1]);
public static void uncheckCommand(String[] command, TravelActivityList list, String line) throws OmniException {
String[] lineSplit = line.split("uncheck");
if (command.length == 2 && CheckParameters.isNumeric(command[1].trim())){
int listNumber = Integer.parseInt(command[1].trim());

list.uncheckTravelActivity(listNumber);
} else {
Expand All @@ -207,8 +210,8 @@ public static void tagCommand(String line, TravelActivityList list) throws OmniE
logger.log(Level.INFO, "Tagging command: " + line);

String[] command = line.split(" ");
if (command.length >= 3 && CheckParameters.isNumeric(command[1])) {
int listNumber = Integer.parseInt(command[1]);
if (command.length >= 3 && CheckParameters.isNumeric(command[1].trim())) {
int listNumber = Integer.parseInt(command[1].trim());
// Extract tags starting from the third element onwards
String[] tagArray = Arrays.copyOfRange(command, 2, command.length);
// Join the tags into a single string
Expand Down Expand Up @@ -238,8 +241,8 @@ public static void removeTagCommand(String[] command, TravelActivityList list) t

logger.log(Level.INFO, "Remove tag command: " + Arrays.toString(command));

if (command.length == 2 && CheckParameters.isNumeric(command[1])) {
int listNumber = Integer.parseInt(command[1]);
if (command.length == 2 && CheckParameters.isNumeric(command[1].trim())) {
int listNumber = Integer.parseInt(command[1].trim());
list.removeTag(listNumber);

} else {
Expand All @@ -265,7 +268,7 @@ public static void updateCommand(String line, TravelActivityList list) throws Om
}
String duration = command[3].trim();
CheckParameters.containsWords(duration);
list.updateTravelActivity(Integer.parseInt(command[1]), date, duration,
list.updateTravelActivity(Integer.parseInt(command[1].trim()), date, duration,
tag);
}

Expand All @@ -288,7 +291,7 @@ public static void findTagCommand(String line, TravelActivityList list) throws O
String exclusion = command[2].trim();
list.findTag(keyword, exclusion);
} else {
throw new OmniException("Please check that your find tag command is in this format: " +
throw new OmniException("Please check that your find tag command is in this format:\n" +
"findtag <description> " + "or findtag <description> /exclude <exclusion>");
}
}
Expand All @@ -313,7 +316,7 @@ public static void findTypeCommand(String line, TravelActivityList list) throws
keyword = keyword.equalsIgnoreCase("general")? "TravelActivity":keyword;
list.findType(keyword, exclusion);
} else {
throw new OmniException("Please check that your find type command is in this format: + " +
throw new OmniException("Please check that your find type command is in this format:\n" +
"findtype <description> " + "or findtype <description> /exclude <exclusion>");
}
}
Expand All @@ -335,7 +338,7 @@ public static void findCommand(String line, TravelActivityList list) throws Omni
String exclusion = command[2].trim();
list.searchKeyword(keyword, exclusion);
} else {
throw new OmniException("Please check that your find command is in this format: + " +
throw new OmniException("Please check that your find command is in this format:\n" +
"find <description> " + "or find <description> /exclude <exclusion>");
}
}
Expand All @@ -356,8 +359,8 @@ public static void expenseCommand(String line, TravelActivityList list) throws O
logger.log(Level.INFO, "Expense command: " + line);

String[] command = line.split(" ");
if (command.length == 3 && CheckParameters.isNumeric(command[1])) {
int listNumber = Integer.parseInt(command[1]);
if (command.length == 3 && CheckParameters.isNumeric(command[1].trim())) {
int listNumber = Integer.parseInt(command[1].trim());
String expense = command[2];
list.expenseActivity(listNumber, expense);
} else if (command.length == 2) {
Expand All @@ -382,8 +385,8 @@ public static void removeExpenseCommand(String[] command, TravelActivityList lis

logger.log(Level.INFO, "Remove expense command: " + Arrays.toString(command));

if (command.length == 2 && CheckParameters.isNumeric(command[1])) {
int listNumber = Integer.parseInt(command[1]);
if (command.length == 2 && CheckParameters.isNumeric(command[1].trim())) {
int listNumber = Integer.parseInt(command[1].trim());
list.removeExpense(listNumber);
} else {
logger.log(Level.WARNING, "Invalid command format: " + Arrays.toString(command));
Expand All @@ -401,7 +404,7 @@ public static void removeExpenseCommand(String[] command, TravelActivityList lis
*/
public static void totalExpenseCommand(String line, TravelActivityList list) throws OmniException {
String[] command = line.split("/type");
if (command.length < 1 || command.length > 2) {
if ((command.length == 1 && line.contains("type")) || command.length > 2) {
throw new OmniException("Please check your command is in the format totalexpense [/type TYPE]");
}
if (command.length == 1) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -188,10 +188,13 @@ public void searchKeyword (String activityName) {

public void searchKeyword (String activityName, String exclusion) {
int foundCounter = 0;
String lowerCaseActivityName = activityName.toLowerCase();
String lowerCaseExclusion = exclusion.toLowerCase();
for (TravelActivity travelActivity : travelActivities) {
assert !(foundCounter > travelActivities.size()) : "Error: There is more activities found than possible";
if (travelActivity.getPlan().contains(activityName) && !travelActivity.getPlan().contains(exclusion) &&
!travelActivity.getPlan().isEmpty()) {
if (travelActivity.getPlan().toLowerCase().contains(lowerCaseActivityName)
&& !travelActivity.getPlan().toLowerCase().contains(lowerCaseExclusion)
&& !travelActivity.getPlan().isEmpty()) {
foundCounter += 1;
if (foundCounter == 1) {
System.out.println("Here are what you are looking for:");
Expand Down Expand Up @@ -289,6 +292,9 @@ public void removeTag(int taskNumber) throws OmniException {

int indexOfTask = taskNumber - 1;
TravelActivity taggedTask = travelActivities.get(indexOfTask);
if (taggedTask.getTag().isBlank()){
throw new OmniException("Travel activity does not have a tag!");
}
taggedTask.removeTag();
System.out.println("Tag removed from the task:");
System.out.println(taggedTask);
Expand Down Expand Up @@ -360,10 +366,11 @@ public void findTag(String tag){

public void findTag(String tag, String exclude){
int foundCounter = 0;
String lowerCaseExclude = exclude.toLowerCase();
for (TravelActivity travelActivity : travelActivities) {
assert !(foundCounter > travelActivities.size()) : "Error: There is more activities found than possible";
if (travelActivity.getTag().contains(tag) && !travelActivity.getTag().isEmpty() &&
!travelActivity.getPlan().contains(exclude)) {
!travelActivity.getPlan().toLowerCase().contains(lowerCaseExclude)) {
foundCounter += 1;
if (foundCounter == 1) {
System.out.println("Here are what you are looking for:");
Expand Down Expand Up @@ -414,11 +421,12 @@ public void findType(String type, String exclude) {
assert exclude != null && !exclude.isEmpty() : "Exclude parameter should not be null or empty";

logger.log(Level.INFO, "Finding type: " + type + ", excluding: " + exclude);

String lowerCaseExclude = exclude.toLowerCase();
for (TravelActivity activity : travelActivities) {
assert !(foundCounter > travelActivities.size()) : "Error: There are more activities found than possible";

if (activity.getClass().getSimpleName().equalsIgnoreCase(type) && !activity.getPlan().contains(exclude)) {
if (activity.getClass().getSimpleName().equalsIgnoreCase(type)
&& !activity.getPlan().toLowerCase().contains(lowerCaseExclude)) {
foundCounter++;
if (foundCounter == 1) {
logger.log(Level.INFO, "Found matching activities:");
Expand Down Expand Up @@ -482,6 +490,9 @@ public void removeExpense(int taskNumber) throws OmniException {

int indexOfTask = taskNumber - 1;
TravelActivity task = travelActivities.get(indexOfTask);
if (task.getExpense().isBlank()){
throw new OmniException("Travel activity does not have a expense!");
}
task.removeExpense();
System.out.println("Expense removed from the task:");
System.out.println(task);
Expand Down Expand Up @@ -514,6 +525,9 @@ public void totalExpense(String type) throws OmniException {
}
}
}
if(type.equalsIgnoreCase("travelactivity")){
type = "General";
}
System.out.println("The total expense for " + type + " travel activities is: $" + tot);
}

Expand Down
5 changes: 3 additions & 2 deletions src/main/java/seedu/omnitravel/ui/Ui.java
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ public static void printBye(){
* Prints line
*/
public static void printLine(){
System.out.println("____________________________________________________________");
System.out.println("_______________________________________________________________________________");
}

/**
Expand All @@ -66,7 +66,8 @@ public static void printNoSuchElementException(NoSuchElementException exception)
* @param exception The exception to be printed
*/
public static void printNumberTooLargeException(NumberFormatException exception) {
System.out.println("Warning! " + exception.getMessage() + " number too large!");
System.out.println("Warning! " + exception.getMessage() + " number too large!\n" +
"or number format is wrong!");
printLine();
}

Expand Down

0 comments on commit 498d3b6

Please sign in to comment.