From 1b66563ef5ca402ec1aeaab99eb6dad722e8867b Mon Sep 17 00:00:00 2001 From: Gavin Li Date: Mon, 9 Feb 2015 20:46:32 -0500 Subject: [PATCH] Merchant class updates: *created search method that returns the item based off the inputted string name. Also finished the getItemPrice method to return the adjusted price of the item given a the name of the item --- MerchantRPGCSE2102/src/game/Merchant.java | 28 +++++++++++++------ .../src/tests/TestMerchant.java | 5 ++++ 2 files changed, 24 insertions(+), 9 deletions(-) diff --git a/MerchantRPGCSE2102/src/game/Merchant.java b/MerchantRPGCSE2102/src/game/Merchant.java index 9b9ed11..6256c8c 100644 --- a/MerchantRPGCSE2102/src/game/Merchant.java +++ b/MerchantRPGCSE2102/src/game/Merchant.java @@ -90,20 +90,30 @@ public void setDailyRandomPrice(int minPercent, int maxPercent) _dailyRandomPercent = randomizer(randomizerRange) + minPercent; } - //************************************************************************ //given the name of the item then it will return - //the price of the item - //incomplete method + //the adjusted price of the item public int getItemPrice(String itemName) { - //placeholder return - //should discuss best way to organize the item array to - //minimize the runtime needed to look up the items - int placeHolder = 0; - return placeHolder; + Item targetItem = getItem(itemName); + int itemPrice = targetItem.getAdjustedPrice(); + return itemPrice; } - //************************************************************************ + //given the item name, it will search through the merchant's inventory and return the item + //if it cannot be found, it will return null and output an error message + //big O(n) runtime + public Item getItem(String itemName) + { + for(int i = 0; i < _inventory.length; i++) + { + if(_inventory[i].getItemName().equals(itemName)) + return _inventory[i]; + } + + System.out.println("No such item exists"); + return null; + } + //name getter public String getName() { diff --git a/MerchantRPGCSE2102/src/tests/TestMerchant.java b/MerchantRPGCSE2102/src/tests/TestMerchant.java index 32d8261..49eb195 100644 --- a/MerchantRPGCSE2102/src/tests/TestMerchant.java +++ b/MerchantRPGCSE2102/src/tests/TestMerchant.java @@ -64,4 +64,9 @@ public void testName() throws Exception setup(); assertEquals("Test", m.getName()); } + + public void testSearch() throws Exception + { + + } }