Skip to content

Commit

Permalink
Merge pull request #171 from JeffinsonDarmawan/Test-Code
Browse files Browse the repository at this point in the history
JUnit Test Code
  • Loading branch information
Ijaaz01 committed Apr 14, 2024
2 parents 9120a94 + 3db412a commit a350bac
Show file tree
Hide file tree
Showing 16 changed files with 985 additions and 30 deletions.
6 changes: 5 additions & 1 deletion docs/team/jeffinsondarmawan.md
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,8 @@ Code Contribution: [Jeffinson Darmawan RepoSense Report](https://nus-cs2113-ay23
strong case that `invo Ross` is definitely closer to `info Rose` compared to `zjgh bfre`. Thus, Fuzzy Logic helps
users by correcting their typos due to accidental human errors and increase the overall convenience of using the
programme. Imagine having to retype `adds Rose /q 1 /to Bouquet for My 3th Anniversary with My Girlfriend` because
of a small mistake of having an "s" after "add"!
of a small mistake of having an "s" after "add" and repeating this over and over again!


- How it works:

Expand Down Expand Up @@ -59,6 +60,9 @@ Code Contribution: [Jeffinson Darmawan RepoSense Report](https://nus-cs2113-ay23
2. Added price information to bouquets (Pull Request [#65](https://github.com/AY2324S2-CS2113-T11-3/tp/pull/65))
3. Improving Parser class by applying FuzzyLogic.detectItem()
(Pull Request [#71](https://github.com/AY2324S2-CS2113-T11-3/tp/pull/71))
4. Wrote JUnit tests for `AddFlowerCommandTest`, `HelpTest`, `InfoCommandTest`, `ListOccasionCommandTest`,
`RemoveFlowerCommandTest`, `FuzzyLogicTest`, `ParserTest`, and wrote additional JUnit tests for `AddBouquetTest`,
`DeleteBouquetTest` which increases classes coverage from 54% to 61% and lines coverage from 29% to 50%.

**Documentation**
1. Developer Guide
Expand Down
30 changes: 28 additions & 2 deletions src/main/java/florizz/command/AddFlowerCommand.java
Original file line number Diff line number Diff line change
Expand Up @@ -18,12 +18,30 @@ public class AddFlowerCommand extends Command{
private String bouquetName;
private boolean enableUi;

/**
* Constructs a new AddFlowerCommand with the specified parameters.
*
* @param flowerName The name of the flower to add
* @param quantity The quantity of the flower to add
* @param bouquetName The name of the bouquet to add the flower to
* @param enableUi A boolean indicating whether UI should be enabled
*/
public AddFlowerCommand(String flowerName, int quantity, String bouquetName, boolean enableUi) {
this.flowerName = flowerName;
this.quantity = quantity;
this.bouquetName = bouquetName;
this.enableUi = enableUi;
}

/**
* Constructs a new AddFlowerCommand with the specified parameters.
*
* @param flowerName The name of the flower to add
* @param colour The color of the flower
* @param quantity The quantity of the flower to add
* @param bouquetName The name of the bouquet to add the flower to
* @param enableUi A boolean indicating whether UI should be enabled
*/
public AddFlowerCommand(String flowerName, Flower.Colour colour,
int quantity, String bouquetName, boolean enableUi) {
this.flowerName = flowerName;
Expand All @@ -33,6 +51,15 @@ public AddFlowerCommand(String flowerName, Flower.Colour colour,
this.enableUi = enableUi;
this.hasColour = true;
}

/**
* Executes the AddFlowerCommand by adding flowers to the specified bouquet.
*
* @param bouquetList The list of bouquets to search for the specified bouquet
* @param ui The user interface to interact with the user
* @return True if the command is executed successfully, false otherwise
* @throws FlorizzException If the specified bouquet is not found or if there is an issue adding the flower
*/
@Override
public boolean execute(ArrayList<Bouquet> bouquetList, Ui ui) throws FlorizzException {
logger.entering(AddFlowerCommand.class.getName(), "execute");
Expand Down Expand Up @@ -63,7 +90,7 @@ public boolean execute(ArrayList<Bouquet> bouquetList, Ui ui) throws FlorizzExce
throw new FlorizzException("This flower does not exist in that colour. " +
"Type info <flower> to view all available colours for this flower");
}
} else if (matchingFlowers.size()==1){
} else if (matchingFlowers.size() == 1){
flowerToAdd = matchingFlowers.get(0);
bouquetToAddFlower.addFlower(matchingFlowers.get(0), this.quantity);
if (enableUi) {
Expand All @@ -85,5 +112,4 @@ public boolean execute(ArrayList<Bouquet> bouquetList, Ui ui) throws FlorizzExce
logger.exiting(AddFlowerCommand.class.getName(), "execute");
return true;
}

}
6 changes: 2 additions & 4 deletions src/main/java/florizz/core/FlowerDictionary.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@


import java.util.ArrayList;
import java.util.Arrays;

import florizz.objects.Flower;

Expand All @@ -25,10 +26,7 @@ private static void add(String name, String colour, String[] occasions,
for (String occasion : occasions) {
occasionsArrayList.add(Flower.Occasion.valueOf(occasion.replaceAll(" ", "_").toUpperCase()));
}
ArrayList<String> meaningsArrayList = new ArrayList<>();
for (String meaning : meanings) {
meaningsArrayList.add(meaning);
}
ArrayList<String> meaningsArrayList = new ArrayList<>(Arrays.asList(meanings));
flowerDict.add(new Flower(name, colourEnum, occasionsArrayList, price, meaningsArrayList, type));
}

Expand Down
2 changes: 1 addition & 1 deletion src/main/java/florizz/core/FuzzyLogic.java
Original file line number Diff line number Diff line change
Expand Up @@ -113,7 +113,7 @@ protected static String detectItem(String userInput) throws FlorizzException {
* @return The Damerau-Levenshtein distance between the two strings.
* @throws FlorizzException if the loop tries to access elements beyond the array bounds.
*/
private static int computeDLDistance(String item, String userInput) throws FlorizzException {
protected static int computeDLDistance(String item, String userInput) throws FlorizzException {
assert item != null && userInput != null : "Strings cannot be null";

int itemLength = item.length();
Expand Down
40 changes: 37 additions & 3 deletions src/main/java/florizz/core/Ui.java
Original file line number Diff line number Diff line change
Expand Up @@ -157,7 +157,11 @@ public void printExitMessage() {
private static void printNextOrBack(int pageNo, int maxPages){

}

/**
* Prints a list of flowers with optional additional information.
*
* @param needsInfo True if additional information about the flowers is required, false otherwise
*/
private void printFlowerList(boolean needsInfo){
int maxPages = (int) Math.ceil((double)lastShownList.size() / PAGE_SIZE);
for (int i = (lastPageNo-1)*PAGE_SIZE; i < Math.min(lastPageNo*PAGE_SIZE, lastShownList.size()); i++) {
Expand Down Expand Up @@ -219,6 +223,11 @@ public void printFlowerInfo(ArrayList<Flower> flowers, String targetFlower, int
printFlowerList(true);
}

/**
* Prints the next page of flower information based on the last command executed.
*
* @throws FlorizzException If there are no more pages to display or if there is no list of flowers to view.
*/
public void printNextPage() throws FlorizzException{
switch (lastCommand.split(" ")[0]) {
case ("ALL_FLOWERS"):
Expand All @@ -245,6 +254,11 @@ public void printNextPage() throws FlorizzException{
}
}

/**
* Prints the previous page of flower information based on the last command executed.
*
* @throws FlorizzException If there are no previous pages to display or if there is no list of flowers to view.
*/
public void printBackPage() throws FlorizzException{
switch (lastCommand.split(" ")[0]) {
case ("ALL_FLOWERS"):
Expand All @@ -270,6 +284,7 @@ public void printBackPage() throws FlorizzException{
"Type 'flowers' to view a list of all flowers.");
}
}

/**
* Prints all possible occasions the user can query
*/
Expand Down Expand Up @@ -365,8 +380,8 @@ public void printAskBouquetName() {
}

/**
* ask user for colour input
* @param eligibleFlowers list of flowers that are eligible for the occasion
* Asks user for colour input
* @param eligibleFlowers List of flowers that are eligible for the occasion
*/
public void printAskColour(ArrayList<Flower> eligibleFlowers) {
System.out.println("What colour would you like your bouquets to be?");
Expand All @@ -387,6 +402,13 @@ public void printAskColour(ArrayList<Flower> eligibleFlowers) {
printBreakLine();
}

/**
* Prints a prompt asking the user if they would like to save a recommended bouquet to their list.
* Prompts the user to input 'yes' to save the bouquet or 'no' to discard it.
*
* @param recommendedBouquet The recommended bouquet to be saved
* @return The user's input ('yes' or 'no')
*/
public String printAskSaveBouquet(Bouquet recommendedBouquet) {
System.out.println("Would you like to save this bouquet to your list?");
printFullBouquet(recommendedBouquet);
Expand Down Expand Up @@ -444,13 +466,25 @@ public Flower chooseColour(ArrayList<Flower> flowers, String flowerName){
}
}
}

/**
* Prints information about the available colors of a flower and prompts the user to choose the color.
* Displays information about the flower, including its name and available colors.
* Prompts the user to input the desired color or 'cancel' to return to the main menu.
*
* @param flowers The list of flowers containing the flower with multiple colors
* @param flowerName The name of the flower with multiple colors
*/
public void printGetFlowerColour(ArrayList<Flower> flowers, String flowerName){
System.out.println("The flower you're looking for has more than one colour available, " +
"each with their own vastly different meanings. Here's some info:");
printFlowerInfo(flowers, flowerName, 1);
System.out.println("Type the colour you want to add into the bouquet, or 'cancel' to return to the main menu.");
}

/**
* Prints a message indicating that the command has been canceled and the user is returning to the main menu.
*/
public void printCancelCommand(){
System.out.println("Canceled command, returning to main menu.");
}
Expand Down
21 changes: 15 additions & 6 deletions src/main/java/florizz/objects/Bouquet.java
Original file line number Diff line number Diff line change
Expand Up @@ -49,10 +49,10 @@ public boolean equals(Object obj) {
}

/**
* checks if flower exist in bouquet.
* Checks if a flower exists in the bouquet.
*
* @param flowerName
* @return boolean true if flower exist
* @param flowerName the flower to check for its existence
* @return true if the flower exists in the bouquet, false otherwise
*/
public boolean doesFlowerExist(Flower flowerName) {
if (flowerHashMap.get(flowerName) == null) {
Expand All @@ -62,10 +62,11 @@ public boolean doesFlowerExist(Flower flowerName) {
}

/**
* add flowers into bouquet
* Adds flowers to the bouquet.
*
* @param flowerName
* @param quantity
* @param flowerName Ihe name of the flower to add
* @param quantity Ihe quantity of the flower to add
* @throws FlorizzException If the quantity of flowers to add exceeds the maximum allowed value
*/
public void addFlower(Flower flowerName, Integer quantity) throws FlorizzException {
if (doesFlowerExist(flowerName)) {
Expand All @@ -80,6 +81,14 @@ public void addFlower(Flower flowerName, Integer quantity) throws FlorizzExcepti
}
}

/**
* Removes flowers from the bouquet.
*
* @param flowerName The name of the flower to remove
* @param quantity The quantity of the flower to remove
* @return True if the flower was successfully removed, false otherwise
* @throws FlorizzException If the specified quantity is <= 0 or if it exceeds the current quantity of flowers
*/
public boolean removeFlower(Flower flowerName, Integer quantity) throws FlorizzException {
// if flower already in bouquet
if (doesFlowerExist(flowerName)) {
Expand Down
53 changes: 49 additions & 4 deletions src/test/java/florizz/command/AddBouquetTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -8,21 +8,66 @@
import java.util.ArrayList;

import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertThrows;
import static org.junit.jupiter.api.Assertions.assertTrue;

public class AddBouquetTest {
@Test
void testAddCommandExecute(){
Bouquet testBouquet = new Bouquet("for testing");
void testAddCommandExecute1() {
Bouquet testBouquet = new Bouquet("for testing"); // Two-word bouquetName
Ui ui = new Ui();
ArrayList<Bouquet> controlList = new ArrayList<>();
ArrayList<Bouquet> testList = new ArrayList<>();
controlList.add(testBouquet);
Command testAddBouquetCommand = new AddBouquetCommand(testBouquet, true);
try {
testAddBouquetCommand.execute(testList, ui);
} catch(FlorizzException error){
assertTrue(testAddBouquetCommand.execute (testList, ui));
} catch (FlorizzException error){
ui.printError(error);
}
assertEquals(controlList, testList);
}

@Test
void testAddCommandExecute2() {
Bouquet testBouquet = new Bouquet("add"); // bouquetName that is also a command name
Ui ui = new Ui();
ArrayList<Bouquet> controlList = new ArrayList<>();
ArrayList<Bouquet> testList = new ArrayList<>();
controlList.add(testBouquet);
Command testAddBouquetCommand = new AddBouquetCommand(testBouquet, true);
try {
assertTrue(testAddBouquetCommand.execute (testList, ui));
} catch (FlorizzException error){
ui.printError(error);
}
assertEquals(controlList, testList);
}

@Test
void testAddCommandExecute3() {
Bouquet testBouquet = new Bouquet("abc"); // One word bouquetName
Ui ui = new Ui();
ArrayList<Bouquet> controlList = new ArrayList<>();
ArrayList<Bouquet> testList = new ArrayList<>();
controlList.add(testBouquet);
Command testAddBouquetCommand = new AddBouquetCommand(testBouquet, true);
try {
assertTrue(testAddBouquetCommand.execute (testList, ui));
} catch (FlorizzException error){
ui.printError(error);
}
assertEquals(controlList, testList);
}

@Test
void testAddCommandExecuteException() {
Bouquet testBouquet = new Bouquet("");
Ui ui = new Ui();
ArrayList<Bouquet> controlList = new ArrayList<>();
controlList.add(testBouquet);
Command testAddBouquetCommand = new AddBouquetCommand(testBouquet, true);

assertThrows(FlorizzException.class, () -> testAddBouquetCommand.execute(controlList, ui));
}
}
Loading

0 comments on commit a350bac

Please sign in to comment.