Skip to content

Commit

Permalink
Merge pull request #197 from EugeneChanJiajun/master
Browse files Browse the repository at this point in the history
Add Junit test for parser class
  • Loading branch information
daryltay415 committed Apr 8, 2024
2 parents a9c3d3c + af42f4b commit 75b06b9
Show file tree
Hide file tree
Showing 6 changed files with 42 additions and 49 deletions.
12 changes: 0 additions & 12 deletions src/main/java/seedu/omnitravel/exchangerateapi/CurrencyRate.java
Original file line number Diff line number Diff line change
Expand Up @@ -34,14 +34,11 @@ public static void convertCurrency(String localCurrency, String foreignCurrency,
.build();
HttpResponse<String> response = client.send(request, HttpResponse.BodyHandlers.ofString());
String jsonResponse = response.body();


if(!jsonResponse.contains("\"" + localCurrency + "\"")){
throw new OmniException(localCurrency + " is an invalid currency!");
} else if(!jsonResponse.contains("\"" + foreignCurrency + "\"")){
throw new OmniException(foreignCurrency + " is an invalid currency!");
}

// Parses the data
int index = jsonResponse.indexOf("\"" + foreignCurrency + "\"");
String subResponse = jsonResponse.substring(index);
Expand All @@ -60,14 +57,5 @@ public static void convertCurrency(String localCurrency, String foreignCurrency,
}
}
}




}





}
3 changes: 2 additions & 1 deletion src/main/java/seedu/omnitravel/omnitravel/OmniTravel.java
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@


public class OmniTravel {

//@@author EugeneChanJiajun
public static void main(String[] args) throws IOException {

Logger logger = Logger.getLogger("Main");
Expand Down Expand Up @@ -148,6 +148,7 @@ private static void initialiseLogger(Logger logger) throws IOException{
LogManager.getLogManager().reset();
logger.setLevel(java.util.logging.Level.OFF);
}
//@@author EugeneChanJiajun
}


Expand Down
1 change: 0 additions & 1 deletion src/main/java/seedu/omnitravel/parser/Parser.java
Original file line number Diff line number Diff line change
Expand Up @@ -349,7 +349,6 @@ public static void listTagsCommand(String[] command, TravelActivityList list) th
public static void currencyExchangeCommand(String line) throws OmniException{
Ui.printLine();
try {

String delimiter = "change | /from | /to ";
String[] command = line.split(delimiter);
// Check parameters
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ public TravelActivityList() {

/**
* Adds travel activity to the travel activity list
*
* @param travelActivity The travel activity
*/
public void addTravelActivity(TravelActivity travelActivity){
Expand Down Expand Up @@ -57,6 +58,7 @@ public void listTravelActivities(){

/**
* returns the number of travel activities in the list
*
* @return the number of travel activities
*/
public int getNoOfTravelActivities(){
Expand All @@ -82,7 +84,12 @@ public void removeTravelActivity(int activityNumber) throws OmniException {
assert newSize == initialListSize - 1 :"There is an error with list size!";
}


/**
* Obtains the description of the plan that we are looking for from the travel activity list
*
* @param plan Plan that we are looking for in the travel activity list
* @return The travel activity description user is looking for
*/
public String getDescription(String plan){
for(TravelActivity travelActivity: travelActivities){
if(travelActivity.getPlan().equals(plan)){
Expand Down Expand Up @@ -121,6 +128,7 @@ public void searchKeyword (String activityName) {

/**
* Checks travel activity as completed
*
* @param activityNumber The travel activity number on the list
*/
public void checkTravelActivity(int activityNumber) throws OmniException{
Expand All @@ -141,6 +149,7 @@ public void checkTravelActivity(int activityNumber) throws OmniException{

/**
* Unchecks travel activity and sets it to uncompleted
*
* @param activityNumber The travel activity number on the list
*/
public void uncheckTravelActivity(int activityNumber) throws OmniException{
Expand All @@ -162,6 +171,7 @@ public void uncheckTravelActivity(int activityNumber) throws OmniException{

/**
* Adds a tag to travel activity
*
* @param taskNumber The travel activity number on the list
* @param tag The tag of travel activity
*/
Expand All @@ -179,6 +189,7 @@ public void tagActivity(int taskNumber, String tag) throws OmniException {

/**
* Removes the tag on a travel activity
*
* @param taskNumber The travel activity number on the list
*/
public void removeTag(int taskNumber) throws OmniException {
Expand All @@ -195,6 +206,7 @@ public void removeTag(int taskNumber) throws OmniException {

/**
* Updates the date, duration and tag of the travel activity
*
* @param travelActivityNumber The index of the travel activity
* @param date The new date of the travel activity
* @param duration The new duration of the travel activity
Expand Down Expand Up @@ -278,10 +290,9 @@ public void findType(String type){
}
}



/**
* Adds expense to travel activity
*
* @param taskNumber The travel activity number on the list
* @param expense The expense of travel activity
*/
Expand All @@ -302,6 +313,7 @@ public void expenseActivity(int taskNumber, String expense) throws OmniException

/**
* Removes the expense on a travel activity
*
* @param taskNumber The travel activity number on the list
*/
public void removeExpense(int taskNumber) throws OmniException {
Expand All @@ -318,6 +330,7 @@ public void removeExpense(int taskNumber) throws OmniException {

/**
* Calculates the total expense for the given type.
*
* @param type The type of tasks that the user wants to find
*/

Expand Down
17 changes: 2 additions & 15 deletions src/main/java/seedu/omnitravel/ui/Ui.java
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,8 @@ public static void helpCommand(){
"15. findtag <tag name>\n" +
"16. findtype <type>\n" +
"17. expense <activity number> <expense amount>\n" +
"18. removeexpense <activity number>\n");
"18. removeexpense <activity number>\n" +
"19. change <amount> /from <current currency> /to <changed currency> ");
printLine();
}

Expand Down Expand Up @@ -114,18 +115,4 @@ public static void printActivity(TravelActivity activity, int activityIndex) {
System.out.println();
}

public static String[] removeElement(String[] array, int indexToRemove) {
if (indexToRemove < 0 || indexToRemove >= array.length) {
throw new IllegalArgumentException("Index out of bounds");
}
// Create a new array with one less element
String[] newArray = new String[array.length - 1];
// Copy elements from the original array to the new array, excluding the element to remove
for (int i = 0, j = 0; i < array.length; i++) {
if (i != indexToRemove) {
newArray[j++] = array[i];
}
}
return newArray;
}
}
39 changes: 22 additions & 17 deletions src/test/java/seedu/omnitravel/OmniTravelTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@

import java.io.ByteArrayOutputStream;
import java.io.PrintStream;
import java.util.Arrays;

class OmniTravelTest {

Expand Down Expand Up @@ -361,30 +362,36 @@ public void testActivityCommand() throws OmniException {
// Test with valid input
Parser.activityCommand("accommodation description /date 2024-10-04 /duration 2 days /tag test", list);
}
/*
// Similar tests for other methods such as addCommand, deleteCommand, checkCommand, uncheckCommand, etc.


@Test
public void testTagCommand() throws OmniException {
TravelActivityList list = new TravelActivityList();
list.addTravelActivity(accommodationNew1);
// Test with valid input
Parser.tagCommand("tag 1 test", list);
}

@Test
public void testRemoveTagCommand() throws OmniException {
TravelActivityList list = new TravelActivityList();
list.addTravelActivity(accommodationNew1);
Parser.tagCommand("tag 1 test", list);
String[] input = {"untag", "1"};
// Test with valid input
Parser.removeTagCommand(new String[]{"removeTag", "1"}, list);
Parser.removeTagCommand(input, list);
}

@Test
public void testUpdateCommand() throws OmniException {
TravelActivityList list = new TravelActivityList();
list.addTravelActivity(accommodationNew1);
// Test with valid input
Parser.updateCommand("update 1 /date 2024-04-04 /duration 2 days /tag test", list);
}



@Test
public void testFindTagCommand() throws OmniException {
TravelActivityList list = new TravelActivityList();
Expand All @@ -398,35 +405,33 @@ public void testFindTypeCommand() throws OmniException {
// Test with valid input
Parser.findTypeCommand("findtype test", list);
}
@Test
public void testFindCommand() throws OmniException {
public void testExpenseCommand() throws OmniException {
TravelActivityList list = new TravelActivityList();
// Test with valid input
Parser.findCommand(new String[]{"find", "test"}, list);
list.addTravelActivity(accommodationNew1);
String input = "expense 1 $50";
Parser.expenseCommand(input, list);
}
/*

@Test
public void testExpenseCommand() throws OmniException {
public void testRemoveExpenseCommand() throws OmniException {
TravelActivityList list = new TravelActivityList();
// Test with valid input
list.addTravelActivity(accommodationNew1);
Parser.expenseCommand("expense 1 $50", list);
String[] input = {"removeExpense", "1"};
Parser.removeExpenseCommand(input, list);
}
@Test
public void testRemoveExpenseCommand() throws OmniException {
public void testFindCommand() throws OmniException {
TravelActivityList list = new TravelActivityList();
// Test with valid input
Parser.removeExpenseCommand(new String[]{"removeExpense", "1"}, list);
Parser.findCommand(Arrays.toString(new String[]{"find", "test"}), list);
}
@Test
public void testTotalExpenseCommand() throws OmniException {
TravelActivityList list = new TravelActivityList();
// Test with valid input
Parser.totalExpenseCommand("totalexpense", list);
}
*/

}

0 comments on commit 75b06b9

Please sign in to comment.