From 6b60f82d03523a5a6f707331f13e26834d9756e2 Mon Sep 17 00:00:00 2001 From: Gavin Li Date: Mon, 9 Feb 2015 21:29:55 -0500 Subject: [PATCH] Updated Merchant and TestMerchant Classes: *added a method to adjust the prices for all items in the merchant's inventory. updated TestMerchant to test the new method. --- MerchantRPGCSE2102/src/game/Merchant.java | 19 ++++++++++++++----- .../src/tests/TestMerchant.java | 10 ++++++++++ 2 files changed, 24 insertions(+), 5 deletions(-) diff --git a/MerchantRPGCSE2102/src/game/Merchant.java b/MerchantRPGCSE2102/src/game/Merchant.java index 6256c8c..20c22a4 100644 --- a/MerchantRPGCSE2102/src/game/Merchant.java +++ b/MerchantRPGCSE2102/src/game/Merchant.java @@ -20,7 +20,7 @@ public Merchant(String name, int cashLimit, ArrayList inventory) generateMerchantInventory(inventory); _dailyRandomPercent = 0; //placeholder percentage } - + //will generate an array of items based off an ArrayList of item names //saves the array to _inventory private void generateMerchantInventory(ArrayList itemList) @@ -29,7 +29,7 @@ private void generateMerchantInventory(ArrayList itemList) String[] nameAndPrice; String name; String price; - + for(int i=0; i < itemList.size(); i++) { nameAndPrice = itemList.get(i).split("\\s+"); //splits the string into the string name and string price @@ -89,7 +89,7 @@ public void setDailyRandomPrice(int minPercent, int maxPercent) int randomizerRange = maxPercent - minPercent; _dailyRandomPercent = randomizer(randomizerRange) + minPercent; } - + //given the name of the item then it will return //the adjusted price of the item public int getItemPrice(String itemName) @@ -109,11 +109,20 @@ public Item getItem(String itemName) if(_inventory[i].getItemName().equals(itemName)) return _inventory[i]; } - + System.out.println("No such item exists"); return null; } - + + //will set adjusted prices for all of the items in the inventory + public void setAllAdjustedPrices() + { + for(Item i: _inventory) + { + i.setAdjPrice(_dailyRandomPercent); + } + } + //name getter public String getName() { diff --git a/MerchantRPGCSE2102/src/tests/TestMerchant.java b/MerchantRPGCSE2102/src/tests/TestMerchant.java index 49eb195..5d94889 100644 --- a/MerchantRPGCSE2102/src/tests/TestMerchant.java +++ b/MerchantRPGCSE2102/src/tests/TestMerchant.java @@ -58,6 +58,16 @@ public void testInventory() throws Exception } } + //testing inventory prices + public void testInventoryPrices() throws Exception + { + setup(); + m.setAllAdjustedPrices(); + assertEquals(0, m.getItemPrice("water")); + assertEquals(0, m.getItemPrice("armor")); + assertEquals(0, m.getItemPrice("food")); + } + //testing name public void testName() throws Exception {