Skip to content

Commit

Permalink
Merge remote-tracking branch 'upstream/master'
Browse files Browse the repository at this point in the history
# Conflicts:
#	omni.txt
#	src/main/java/seedu/duke/Parser.java
  • Loading branch information
daryltay415 committed Apr 1, 2024
2 parents 6de20bd + 9e97d0e commit 1c117e6
Show file tree
Hide file tree
Showing 5 changed files with 115 additions and 75 deletions.
Empty file added mylog.txt
Empty file.
4 changes: 0 additions & 4 deletions omni.txt
Original file line number Diff line number Diff line change
@@ -1,4 +0,0 @@
general / 1 / germany / 2019-12-12 / now / / $50
food / 1 / mala / 2023-12-12 / now / /
accommodation / 0 / hotel / 2023-12-12 / now / /
landmark / 0 / statue of liberty / 2023-12-12 / now / /
73 changes: 73 additions & 0 deletions src/main/java/seedu/duke/CheckParameters.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
package seedu.duke;

import java.io.IOException;
import java.time.DateTimeException;
import java.util.NoSuchElementException;

import static seedu.duke.Parser.isNumeric;

public class CheckParameters {

/**
* Checks for all possible input errors that users may make and throws the corresponding exceptions
*
* @param input Input array that users placed into the chatbot
* @throws OmniException when any of the corresponding input format is wrong
*/
public static void addExceptions(String[] input) throws OmniException{
if (input.length >= 4 && input[1].isBlank()) {
throw new OmniException("The description cannot be empty!");
} else if (input.length >= 4 && input[2].isBlank()) {
throw new OmniException("The date cannot be empty!");
} else if (input.length >= 4 && input[3].isBlank()) {
throw new OmniException("The duration cannot be empty!");
} else if (input.length >= 5 && input[4].isBlank()) {
throw new OmniException("The tag cannot be empty!");
} else if (input.length < 4 || input[3].contains("/tag")){
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");
}
}

/**
* Checks for all possible input errors that users may make when updating and throws the corresponding exceptions
*
* @param command Command array that users placed into the chatbot
* @throws OmniException when any of the corresponding input format is wrong
*/
public static void updateExceptions(String[] command) throws OmniException {
if (command.length >= 4 && (command[1].isBlank() || !isNumeric(command[1]))) {
throw new OmniException("The update index cannot be empty or non numerical!");
} else if (command.length >= 4 && command[2].isBlank()) {
throw new OmniException("The date cannot be empty!");
} else if (command.length >= 4 && command[3].isBlank()) {
throw new OmniException("The duration cannot be empty!");
} else if(command.length >= 5 && command[4].isBlank()){
throw new OmniException("The tag cannot be empty!");
} else if (command.length >= 4 && !command[3].contains("/tag")) {
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");
}
}

/**
* Checks for all format errors in the user input and throes the correct exceptions
*
* @param exception Exception thrown
*/
public static void handleException(Exception exception) {
if (exception instanceof OmniException) {
Ui.printException((OmniException) exception);
} else if (exception instanceof NoSuchElementException) {
Ui.printNoSuchElementException((NoSuchElementException) exception);
} else if (exception instanceof NumberFormatException) {
Ui.printNumberTooLargeException((NumberFormatException) exception);
} else if (exception instanceof DateTimeException) {
Ui.printDateTimeExceptionError();
} else if (exception instanceof IOException) {
Ui.printSavingError();
}
}
}
38 changes: 21 additions & 17 deletions src/main/java/seedu/duke/Duke.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,22 @@
import java.time.DateTimeException;
import java.util.NoSuchElementException;
import java.util.Scanner;
import java.util.logging.FileHandler;
import java.util.logging.Level;
import java.util.logging.Logger;
import java.util.logging.SimpleFormatter;
import java.util.logging.LogManager;

public class Duke {

public static void main(String[] args) {
public static void main(String[] args) throws IOException {
Logger logger = Logger.getLogger("Main");
FileHandler filehandler = new FileHandler("mylog.txt");
SimpleFormatter formatter = new SimpleFormatter();
filehandler.setFormatter(formatter);
logger.addHandler(filehandler);
LogManager.getLogManager().reset();
logger.setLevel(java.util.logging.Level.OFF);
Ui.printGreeting();
FileSave file = new FileSave("omni.txt");
TravelActivityList list = new TravelActivityList();
Expand Down Expand Up @@ -61,12 +70,21 @@ public static void main(String[] args) {
file.saveActivityList(list);
} catch (OmniException | NoSuchElementException | NumberFormatException | DateTimeException
| IOException exception) {
handleException(exception);
CheckParameters.handleException(exception);
}
}
}

/**
* Handles the respective command inputs used by the user
*
* @param command Command array of the input without spaces
* @param line Line arry of the full input
* @param list List of travel activities
* @throws OmniException when any input format is wrong
*/
private static void invokeCommand(String[] command,
String line, TravelActivityList list) throws OmniException, IOException {
String line, TravelActivityList list) throws OmniException {
Ui.printLine();
switch (command[0].toLowerCase()) {
case "delete":
Expand Down Expand Up @@ -107,20 +125,6 @@ private static void invokeCommand(String[] command,
}
Ui.printLine();
}

private static void handleException(Exception exception) {
if (exception instanceof OmniException) {
Ui.printException((OmniException) exception);
} else if (exception instanceof NoSuchElementException) {
Ui.printNoSuchElementException((NoSuchElementException) exception);
} else if (exception instanceof NumberFormatException) {
Ui.printNumberTooLargeException((NumberFormatException) exception);
} else if (exception instanceof DateTimeException) {
Ui.printDateTimeExceptionError();
} else if (exception instanceof IOException) {
Ui.printSavingError();
}
}
}


Expand Down
75 changes: 21 additions & 54 deletions src/main/java/seedu/duke/Parser.java
Original file line number Diff line number Diff line change
Expand Up @@ -49,18 +49,7 @@ public static void activityCommand(String line, TravelActivityList list) throws
String[] command = line.split(" ");
String delimiter = command[0] + "| /date | /duration | /tag ";
String[] input = line.split(delimiter);
//String tag = (input.length > 4 && !input[4].isBlank()) ? input[4].trim() : "";
if (input.length >= 4 && input[1].isBlank()) {
throw new OmniException("The description of accommodation cannot be empty!");
} else if (input.length >= 4 && input[2].isBlank()) {
throw new OmniException("The date cannot be empty!");
} else if (input.length >= 4 && input[3].isBlank()) {
throw new OmniException("The duration cannot be empty!");
} else if (input.length >= 5 && input[4].isBlank()) {
throw new OmniException("The tag cannot be empty!");
} else if (input.length < 4 || input[3].contains("/tag")){
throw new OmniException("Wrong format");
}
CheckParameters.addExceptions(input);
String description = input[1].trim();
LocalDate date = LocalDate.parse(input[2]);
String duration = input[3].trim();
Expand Down Expand Up @@ -92,31 +81,21 @@ public static void activityCommand(String line, TravelActivityList list) throws
*
* @param line Line that the user inputs into the chatbot
* @param list List of travel activities
* @throws OmniException if command.length < 2
* @throws OmniException if any of the conditions are not met in the addExceptions() method
*/
public static void addCommand(String line, TravelActivityList list) throws OmniException{
Ui.printLine();
String[] command = line.split("add | /date | /duration | /tag ");
//String tag = (command.length > 4 && !command[4].isBlank()) ? command[4].trim() : "";
logger.log(Level.INFO, command[0] + " // " + command[1]);
if (command.length >= 4 && command[1].isBlank()) {
throw new OmniException("The description of add cannot be empty!");
} else if(command.length >= 4 && command[2].isBlank()){
throw new OmniException("The date cannot be empty!");
} else if (command.length >= 4 && command[3].isBlank()){
throw new OmniException("The duration cannot be empty!");
} else if (command.length >= 4 && !command[3].contains("/tag")){
String description = command[1].trim();
LocalDate date = LocalDate.parse(command[2]);
String duration = command[3].trim();
String tag = (line.contains("/tag") && command.length == 5) ? command[4].trim() : "";
TravelActivity newActivity = new TravelActivity(description, date, duration, tag, "");
list.addTravelActivity(newActivity);
System.out.println("I added a new travel activity");
System.out.println(newActivity);
} else {
throw new OmniException("Wrong format");
}
CheckParameters.addExceptions(command);
String description = command[1].trim();
LocalDate date = LocalDate.parse(command[2]);
String duration = command[3].trim();
String tag = (line.contains("/tag") && command.length == 5) ? command[4].trim() : "";
TravelActivity newActivity = new TravelActivity(description, date, duration, tag, "");
list.addTravelActivity(newActivity);
System.out.println("I added a new travel activity");
System.out.println(newActivity);
Ui.printLine();
}

Expand All @@ -131,7 +110,6 @@ public static void deleteCommand(String[] command, TravelActivityList list) thro
if (command.length == 2 && isNumeric(command[1])){
int listNumber = Integer.parseInt(command[1]);
list.removeTravelActivity(listNumber);

} else {
throw new OmniException("Please specify which activity to delete");
}
Expand Down Expand Up @@ -179,9 +157,12 @@ 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])){
int listNumber = Integer.parseInt(command[1]);
String tag = command[2];
String index = command[1];
String[] tagSplit = line.split(index);
String tag = tagSplit[1].trim();
System.out.println(tag);
if (command.length >= 3 && isNumeric(command[1])){
int listNumber = Integer.parseInt(index);
list.tagActivity(listNumber, tag);
} else if (command.length == 2) {
throw new OmniException("Please specify a tag name");
Expand Down Expand Up @@ -214,23 +195,9 @@ public static void removeTagCommand(String[] command, TravelActivityList list) t
*/
public static void updateCommand(String line, TravelActivityList list) throws OmniException {
String[] command = line.split("update | /date | /duration | /tag ");
if (command.length >= 4 && (command[1].isBlank() || !isNumeric(command[1]))) {
throw new OmniException("The update index cannot be empty or non numerical!");
} else if (command.length >= 4 && command[2].isBlank()) {
throw new OmniException("The date cannot be empty!");
} else if (command.length >= 4 && command[3].isBlank()) {
throw new OmniException("The duration cannot be empty!");
} else if(command.length >= 5 && command[4].isBlank()){
throw new OmniException("The tag cannot be empty!");
} else if (command.length >= 4 && !command[3].contains("/tag")) {
String tag = (line.contains("/tag") && command.length == 5)? command[4].trim() : "";
list.updateTravelActivity(Integer.parseInt(command[1]), LocalDate.parse(command[2]), command[3].trim(),
tag);
} else {
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");
}
String tag = (line.contains("/tag") && command.length == 5)? command[4].trim() : "";
list.updateTravelActivity(Integer.parseInt(command[1]), LocalDate.parse(command[2]), command[3].trim(),
tag);
}

/**
Expand All @@ -254,7 +221,7 @@ public static void findTagCommand(String line, TravelActivityList list) throws O
* Handles the case where the findtype command is given as input
*
* @param line User's input into Omnitravel
* @param list list List of travel activities
* @param list List of travel activities
* @throws OmniException if command.length < 1
*/

Expand Down

0 comments on commit 1c117e6

Please sign in to comment.