From a0fc2a2b599a0ab2b966bcc0a2afc9901b4f1ca5 Mon Sep 17 00:00:00 2001 From: XavierLiau34 Date: Mon, 15 Apr 2024 15:38:29 +0800 Subject: [PATCH 1/2] Increased Test Coverage --- .../java/seedu/binbash/quotes/Quotes.java | 2 +- .../binbash/command/ProfitCommandTest.java | 39 +++++++++++++++++++ .../java/seedu/binbash/parser/ParserTest.java | 17 ++++++++ 3 files changed, 57 insertions(+), 1 deletion(-) create mode 100644 src/test/java/seedu/binbash/command/ProfitCommandTest.java diff --git a/src/main/java/seedu/binbash/quotes/Quotes.java b/src/main/java/seedu/binbash/quotes/Quotes.java index aa2709338a..0367e9f699 100644 --- a/src/main/java/seedu/binbash/quotes/Quotes.java +++ b/src/main/java/seedu/binbash/quotes/Quotes.java @@ -49,7 +49,7 @@ public class Quotes { private static final Random RANDOM = new Random(); // Private constructor to prevent instantiation - private Quotes() {} + public Quotes() {} public static String getRandomQuote() { int randomIndex = RANDOM.nextInt(CUSTOM_MESSAGES.length); diff --git a/src/test/java/seedu/binbash/command/ProfitCommandTest.java b/src/test/java/seedu/binbash/command/ProfitCommandTest.java new file mode 100644 index 0000000000..c24bb0e74e --- /dev/null +++ b/src/test/java/seedu/binbash/command/ProfitCommandTest.java @@ -0,0 +1,39 @@ +package seedu.binbash.command; + +import org.junit.jupiter.api.Test; + +import seedu.binbash.item.Item; +import seedu.binbash.inventory.ItemList; + +import java.time.LocalDate; +import java.util.ArrayList; + +import static org.junit.jupiter.api.Assertions.assertTrue; +import static org.junit.jupiter.api.Assertions.assertEquals; + +public class ProfitCommandTest { + + @Test + void execute_calculateTotalProfit() { + ArrayList emptyItemList = new ArrayList<>(); + ItemList itemList = new ItemList(emptyItemList); + + AddCommand addCommand1 = new AddCommand("retail", "Item 1", "Description 1", 1, + LocalDate.now(), 5, 3, 1); + addCommand1.execute(itemList); + + AddCommand addCommand2 = new AddCommand("retail", "Item 2", "Description 2", 1, + LocalDate.now(), 10, 7, 1); + addCommand2.execute(itemList); + + SellCommand sellCommand1 = new SellCommand("Item 1", 1); + sellCommand1.execute(itemList); + + SellCommand sellCommand2 = new SellCommand("Item 2", 1); + sellCommand2.execute(itemList); + + ProfitCommand profitCommand = new ProfitCommand(); + assertTrue(profitCommand.execute(itemList)); + assertEquals("Total profit: $5.00", profitCommand.getExecutionUiOutput().trim()); + } +} diff --git a/src/test/java/seedu/binbash/parser/ParserTest.java b/src/test/java/seedu/binbash/parser/ParserTest.java index 447d66347f..e167d20e6d 100644 --- a/src/test/java/seedu/binbash/parser/ParserTest.java +++ b/src/test/java/seedu/binbash/parser/ParserTest.java @@ -20,6 +20,8 @@ import org.apache.commons.cli.ParseException; import org.junit.jupiter.api.Assertions; +import seedu.binbash.quotes.Quotes; + import static org.junit.jupiter.api.Assertions.assertTrue; import static org.junit.jupiter.api.Assertions.assertThrows; import static org.junit.jupiter.api.Assertions.assertInstanceOf; @@ -80,6 +82,21 @@ public void testParseCommand_repeatedQuoteCommand_returnsQuoteCommand() { } } + + @Test + void getRandomQuote_returnsValidQuote() { + String quote = Quotes.getRandomQuote(); + + boolean quoteFound = false; + for (String predefinedQuote : Quotes.CUSTOM_MESSAGES) { + if (predefinedQuote.equals(quote)) { + quoteFound = true; + break; + } + } + assertTrue(quoteFound, "Returned quote is not one of the predefined quotes."); + } + @Test public void testParseCommand_invalidCommand_throwsInvalidCommandException() { assertThrows(InvalidCommandException.class, () -> parser.parseCommand("invalid")); From e3ee86e56edce6d5db4bca5e6f46839e1702bf0f Mon Sep 17 00:00:00 2001 From: XavierLiau34 <98327898+XavierLiau34@users.noreply.github.com> Date: Mon, 15 Apr 2024 17:45:31 +0800 Subject: [PATCH 2/2] Update Quotes.java --- src/main/java/seedu/binbash/quotes/Quotes.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/main/java/seedu/binbash/quotes/Quotes.java b/src/main/java/seedu/binbash/quotes/Quotes.java index 0367e9f699..aa2709338a 100644 --- a/src/main/java/seedu/binbash/quotes/Quotes.java +++ b/src/main/java/seedu/binbash/quotes/Quotes.java @@ -49,7 +49,7 @@ public class Quotes { private static final Random RANDOM = new Random(); // Private constructor to prevent instantiation - public Quotes() {} + private Quotes() {} public static String getRandomQuote() { int randomIndex = RANDOM.nextInt(CUSTOM_MESSAGES.length);