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

Branch tag list #193

Merged
Merged
Show file tree
Hide file tree
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
4 changes: 4 additions & 0 deletions omni.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
general / 0 / go to paris / 2024-12-12 / 2 hours / hello / $50
food / 0 / food / 2024-12-12 / now / /
food / 0 / food / 2024-12-12 / 2 hours / / $30
general / 0 / hello world / 2040-12-12 / 2 hours / your mom /
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
import java.time.DateTimeException;
import java.util.NoSuchElementException;

import static seedu.omnitravel.parser.Parser.isNumeric;


public class CheckParameters {

Expand All @@ -16,6 +16,7 @@ public class CheckParameters {
* @param commandType commandType of the four input commands that add new activities into the list
* @throws OmniException when any of the corresponding input format is wrong
*/

public static void addExceptions(String[] input, String commandType, String line) throws OmniException{
String[] command = line.split("/");
if (input.length >= 3 && input[0].substring(commandType.length()).isBlank()) {
Expand All @@ -32,6 +33,8 @@ public static void addExceptions(String[] input, String commandType, String line
"/date YYYY-MM-DD /duration DURATION"
+ " or add DESCRIPTION /date YYYY-MM-DD /duration DURATION /tag TAG");
}


}

/**
Expand All @@ -56,6 +59,57 @@ public static void updateExceptions(String[] command) throws OmniException {
}
}

/**
* Checks if a string contains all the words
* @param input The input String
*/
public static void containsWords(String input) throws OmniException{

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good job in identifying the keywords for duration

String[] inputSplit = input.split(" ");
if (inputSplit.length == 2){
String[] durationKeyWords = {"day", "month", "year", "hour", "minute", "second"};
for(String word:durationKeyWords){
if(input.contains(word)){
return;
}
}
}
throw new OmniException("Your duration is invalid. Please input in terms of \"1 " +
"day, month, year, hour, minutue or second\"");
}

/**
* Checks if the string is a number
*
* @param str The string that is to be defined as a number or sentence
* @return true or false
*/
public static boolean isNumeric(String str) {
try {
Double.parseDouble(str);
return true;
} catch(NumberFormatException e){
return false;
}
}

/**
* Checks if the expense given is valid
* @param str
* @return True or false
* @throws OmniException Throws an exception when the expense given is less than $0
*/
public static boolean isValidExpense(String str) throws OmniException{
if(isNumeric(str)){
int expense = Integer.parseInt(str);
if(expense <= 0){
throw new OmniException("Your expense cannot be less than $0");
}
} else{
return false;
}
return true;
}

/**
* Checks for all format errors in the user input and throes the correct exceptions
*
Expand Down
5 changes: 4 additions & 1 deletion src/main/java/seedu/omnitravel/omnitravel/OmniTravel.java
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,9 @@ public static void main(String[] args) throws IOException {
case "list":
Parser.getList(command, list);
break;
case "listtags":
Parser.listTagsCommand(command, list);
break;
case "add":
Parser.addCommand(line, list);
break;
Expand Down Expand Up @@ -100,7 +103,7 @@ private static void invokeCommand(String[] command,
Parser.uncheckCommand(command, list);
break;
case "find":
Parser.findCommand(command, list);
Parser.findCommand(line, list);
break;
case "tag":
Parser.tagCommand(line, list);
Expand Down
84 changes: 49 additions & 35 deletions src/main/java/seedu/omnitravel/parser/Parser.java
Original file line number Diff line number Diff line change
Expand Up @@ -9,26 +9,12 @@
import seedu.omnitravel.ui.Ui;

import java.time.LocalDate;
import java.util.logging.Level;
import java.util.logging.Logger;

public class Parser {

private static Logger logger = Logger.getLogger("ParserLogger");
/**
* Checks if the string is a number
*
* @param str The string that is to be defined as a number or sentence
* @return true or false
*/
public static boolean isNumeric(String str) {
try {
Double.parseDouble(str);
return true;
} catch(NumberFormatException e){
return false;
}
}


/**
* Obtains the list of travel activities
Expand Down Expand Up @@ -63,7 +49,11 @@ public static void activityCommand(String line, TravelActivityList list) throws
CheckParameters.addExceptions(input, commandType, line);
String description = line.substring(commandIndex + 1, line.indexOf("/date")).trim();
LocalDate date = LocalDate.parse(input[1]);
if(date.isBefore(LocalDate.now())){
throw new OmniException("Please input a future date.");
}
String duration = input[2].trim();
CheckParameters.containsWords(duration);
String tag = (line.contains("/tag") && input.length == 4) ? input[3].trim() : "";
TravelActivity activity;
switch (commandType) {
Expand Down Expand Up @@ -97,11 +87,15 @@ public static void activityCommand(String line, TravelActivityList list) throws
public static void addCommand(String line, TravelActivityList list) throws OmniException{
Ui.printLine();
String[] command = line.split("/date | /duration | /tag ");
logger.log(Level.INFO, command[0] + " // " + command[1]);
//logger.log(Level.INFO, command[0] + " // " + command[1]);
CheckParameters.addExceptions(command, "add", line);
String description = line.substring(4, line.indexOf("/date"));
LocalDate date = LocalDate.parse(command[1]);
if(date.isBefore(LocalDate.now())){
throw new OmniException("Please input a future date.");
}
String duration = command[2].trim();
CheckParameters.containsWords(duration);
String tag = (line.contains("/tag") && command.length == 4) ? command[3].trim() : "";
TravelActivity newActivity = new TravelActivity(description, date, duration, tag, "");
list.addTravelActivity(newActivity);
Expand All @@ -118,11 +112,11 @@ public static void addCommand(String line, TravelActivityList list) throws OmniE
* @throws OmniException if command.length != 2 && command[1] is not numeric
*/
public static void deleteCommand(String[] command, TravelActivityList list) throws OmniException {
if (command.length == 2 && isNumeric(command[1])){
if (command.length == 2 && CheckParameters.isNumeric(command[1])){
int listNumber = Integer.parseInt(command[1]);
list.removeTravelActivity(listNumber);
} else {
throw new OmniException("Please specify which activity to delete");
throw new OmniException("Please specify which activity index to delete");
}
}

Expand All @@ -134,7 +128,7 @@ public static void deleteCommand(String[] command, TravelActivityList list) thro
* @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 && isNumeric(command[1])){
if (command.length == 2 && CheckParameters.isNumeric(command[1])){
int listNumber = Integer.parseInt(command[1]);
list.checkTravelActivity(listNumber);
} else {
Expand All @@ -150,7 +144,7 @@ public static void checkCommand(String[] command, TravelActivityList list) throw
* @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 && isNumeric(command[1])){
if (command.length == 2 && CheckParameters.isNumeric(command[1])){
int listNumber = Integer.parseInt(command[1]);

list.uncheckTravelActivity(listNumber);
Expand All @@ -169,7 +163,7 @@ public static void uncheckCommand(String[] command, TravelActivityList list) thr
*/
public static void tagCommand(String line, TravelActivityList list) throws OmniException {
String[] command = line.split(" ");
if (command.length >= 3 && isNumeric(command[1])){
if (command.length >= 3 && CheckParameters.isNumeric(command[1])){
String index = command[1];
String[] tagSplit = line.split(index);
String tag = tagSplit[1].trim();
Expand All @@ -190,7 +184,7 @@ public static void tagCommand(String line, TravelActivityList list) throws OmniE
* @throws OmniException if command.length != 2 && command[1] is not numeric
*/
public static void removeTagCommand(String[] command, TravelActivityList list) throws OmniException {
if (command.length == 2 && isNumeric(command[1])) {
if (command.length == 2 && CheckParameters.isNumeric(command[1])) {
int listNumber = Integer.parseInt(command[1]);
list.removeTag(listNumber);
} else {
Expand Down Expand Up @@ -222,10 +216,10 @@ public static void updateCommand(String line, TravelActivityList list) throws Om

public static void findTagCommand(String line, TravelActivityList list) throws OmniException {
String[] command = line.split("findtag");
if (command.length < 1) {
throw new OmniException("Please check that your update command is in this format: findtag <tag>");
} else {
if (command.length > 1) {
list.findTag(command[1].trim());
} else {
throw new OmniException("Please check that your update command is in this format: findtag <tag>");
}
}

Expand All @@ -241,6 +235,8 @@ public static void findTypeCommand(String line, TravelActivityList list) throws
String[] command = line.split("findtype");
if (command.length < 1) {
throw new OmniException("Please check that your find type command is in this format: findtype <type>");
} else if (command[1].trim().equalsIgnoreCase("general")){
list.findType("TravelActivity");
} else {
list.findType(command[1].trim());
}
Expand All @@ -249,13 +245,14 @@ public static void findTypeCommand(String line, TravelActivityList list) throws
/**
* Handles the case where the find command is given as input
*
* @param command Command array of input string without spaces
* @param line input string
* @param list List of travel activities
* @throws OmniException if command.length != 2
*/
public static void findCommand(String[] command, TravelActivityList list) throws OmniException {
if (command.length == 2) {
String keyword = command[1];
public static void findCommand(String line, TravelActivityList list) throws OmniException {
String[] command = line.split("find");
if (command.length > 1 && !command[1].isBlank()) {
String keyword = command[1].trim();
list.searchKeyword(keyword);
} else {
throw new OmniException("Please check that your find type command is in this format: find <description>");
Expand All @@ -272,11 +269,9 @@ public static void findCommand(String[] command, TravelActivityList list) throws
*/
public static void expenseCommand(String line, TravelActivityList list) throws OmniException {
String[] command = line.split(" ");
if (command.length == 3 && isNumeric(command[1])){
String index = command[1];
String[] expenseSplit = line.split(index);
if (command.length == 3 && CheckParameters.isNumeric(command[1])){
int listNumber = Integer.parseInt(command[1]);
String expense = expenseSplit[1].trim();
String expense = command[2];
list.expenseActivity(listNumber, expense);
} else if (command.length == 2) {
throw new OmniException("Please specify expense amount");
Expand All @@ -293,11 +288,11 @@ public static void expenseCommand(String line, TravelActivityList list) throws O
* @throws OmniException if command.length != 2 && command[1] is not numeric
*/
public static void removeExpenseCommand(String[] command, TravelActivityList list) throws OmniException {
if (command.length == 2 && isNumeric(command[1])) {
if (command.length == 2 && CheckParameters.isNumeric(command[1])) {
int listNumber = Integer.parseInt(command[1]);
list.removeExpense(listNumber);
} else {
throw new OmniException("Please specify which task to remove expense");
throw new OmniException("Please follow the format: removeexpense INDEX");
}
}

Expand All @@ -318,9 +313,28 @@ public static void totalExpenseCommand(String line, TravelActivityList list) thr
throw new OmniException("Please check your command is in the format totalexpense [/type TYPE]");
}
list.totalExpense("all");
} else if(command[1].trim().equals("general")){
list.totalExpense("TravelActivity");
} else {
list.totalExpense(command[1].trim());
}
}

/**
* Handles the case whereby the command is listtags
* @param command The command given by the user
* @param list The travel activity list
* @throws OmniException Throws an exception when the command is in an invalid format
*/
public static void listTagsCommand(String[] command, TravelActivityList list) throws OmniException{

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM!

Ui.printLine();
if (command.length == 1) {
System.out.println("These are the tags in your list: ");
list.listTags();
} else {
throw new OmniException("Do you mean the command listtags?");
}
Ui.printLine();
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,12 @@ public TravelActivity(String description, LocalDate date, String duration, Strin

@Override
public String toString(){
return travelActivity + " :" + date.format(DateTimeFormatter.ofPattern("d MMM yyyy")) + " :" + duration;
if(tag.isBlank()){
return travelActivity + " :" + date.format(DateTimeFormatter.ofPattern("d MMM yyyy")) + " :" + duration;
}
return travelActivity + " :" + date.format(DateTimeFormatter.ofPattern("d MMM yyyy")) + " :" + duration
+ " (" + tag +")";

}

/**
Expand Down
Loading
Loading