diff --git a/MerchantRPGCSE2102/src/game/Item.java b/MerchantRPGCSE2102/src/game/Item.java index 5c2099c..058bc54 100644 --- a/MerchantRPGCSE2102/src/game/Item.java +++ b/MerchantRPGCSE2102/src/game/Item.java @@ -17,6 +17,17 @@ public Item(String itemName, int basePrice, int maxPrice, int minPrice) _minPrice = minPrice; _adjustedPrice = _basePrice; } + + //second Item constructor + //if max and min prices are not specified, assigns highest possible price and lowest possible price + //to the two variables respectively + public Item(String itemName, int basePrice) + { + _name = itemName; + _basePrice = basePrice; + _maxPrice = 100000; //maximum price + _minPrice = 0; + } //will set the adjusted price of the item based off of the daily percent of the merchant //will need the daily percent as an input from merchant diff --git a/MerchantRPGCSE2102/src/game/Merchant.java b/MerchantRPGCSE2102/src/game/Merchant.java index 696ad12..a4f1904 100644 --- a/MerchantRPGCSE2102/src/game/Merchant.java +++ b/MerchantRPGCSE2102/src/game/Merchant.java @@ -1,5 +1,6 @@ package game; +import java.util.ArrayList; import java.util.Random; public class Merchant @@ -7,22 +8,35 @@ public class Merchant private String _name; private int _currentCash; private int _baseCash; - private int _dailyRandomPercentage; - //************************************************************* - //should we use an array of items or maybe a linked list? - //further discussion with group needed + private int _dailyRandomPercent; private Item[] _inventory; - //************************************************************* //merchant constructor - public Merchant(String name, int cashLimit, int inventorySize) + public Merchant(String name, int cashLimit, ArrayList inventory) { _name = name; _currentCash = cashLimit; _baseCash = cashLimit; - _inventory = new Item[inventorySize]; - //placeholder number - _dailyRandomPercentage = 0; + 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) + { + _inventory = new Item[itemList.size()]; //inventory size will be the size of the list of items + 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 + name = nameAndPrice[0]; + price = nameAndPrice[1]; + _inventory[i] = new Item(name, Integer.parseInt(price)); //creates a new instance of item and adds it to the inventory + } } //will restore the current Cash amount back to the base amount @@ -40,7 +54,7 @@ public void incrementCash(int incrementAmount) } //removes the specified amount from the merchant's current cash - public int decrementCash(int decrementAmount) + public int decreaseCash(int decrementAmount) { //checks if the amount attempting to be removed is //greater than the current amount @@ -69,30 +83,43 @@ public int randomizer(int max) return randomNumber; } - //will set the dailyRandomPercentage to a number between the two given inputs - public void setDailyRandomPercentage(int minPercent, int maxPercent) + //will set the dailyRandomPrice to a number between the two given inputs + public void setDailyRandomPrice(int minPercent, int maxPercent) { int randomizerRange = maxPercent - minPercent; - _dailyRandomPercentage = randomizer(randomizerRange) + minPercent; + _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; } - //************************************************************************ - - public void adjustInventoryPrices() { - for (Item item : _inventory) { - item.setAdjPrice(_dailyRandomPercentage); + + //will set adjusted prices for all of the items in the inventory + public void setAllAdjustedPrices() + { + for(Item i: _inventory) + { + i.setAdjPrice(_dailyRandomPercent); } } @@ -115,9 +142,9 @@ public int getBaseCash() } //dailyRandomPrice getter - public int getRandomPercentage() + public int getRandomPrice() { - return _dailyRandomPercentage; + return _dailyRandomPercent; } //inventory getter diff --git a/MerchantRPGCSE2102/src/game/RPGame.java b/MerchantRPGCSE2102/src/game/RPGame.java index 9556a4c..4942c07 100644 --- a/MerchantRPGCSE2102/src/game/RPGame.java +++ b/MerchantRPGCSE2102/src/game/RPGame.java @@ -56,7 +56,12 @@ public ArrayList getMerchantInventory(int merchantNumber) { return merchantInventory1; else if (merchantNumber == 2) return merchantInventory2; - else + else if (merchantNumber == 3) return merchantInventory3; + else + { + System.out.println("Invalid merchant number"); + return null; + } } } diff --git a/MerchantRPGCSE2102/src/tests/TestMerchant.java b/MerchantRPGCSE2102/src/tests/TestMerchant.java index 87b2055..08ed6fe 100644 --- a/MerchantRPGCSE2102/src/tests/TestMerchant.java +++ b/MerchantRPGCSE2102/src/tests/TestMerchant.java @@ -1,5 +1,8 @@ package tests; +import java.util.ArrayList; + +import game.Item; import game.Merchant; import junit.framework.TestCase; @@ -10,18 +13,22 @@ public class TestMerchant extends TestCase public void setup() { - m = new Merchant("Test", 100, 2); + ArrayList a = new ArrayList(); + a.add("water 3"); + a.add("armor 5"); + a.add("food 10"); + m = new Merchant("Test", 100, a); } //testing cash system public void testCashSystem() throws Exception { setup(); - assertEquals(-1, m.decrementCash(101)); + assertEquals(-1, m.decreaseCash(101)); m.incrementCash(100); m.refreshCash(); assertEquals(200, m.getCurrentCash()); - m.decrementCash(120); + m.decreaseCash(120); assertEquals(80, m.getCurrentCash()); } @@ -30,9 +37,9 @@ public void testRandomizer() throws Exception { setup(); boolean testCheck = false; - m.setDailyRandomPercentage(10, 50); + m.setDailyRandomPrice(10, 50); - if(10 <= m.getRandomPercentage() && 50 >= m.getRandomPercentage()) + if(10 <= m.getRandomPrice() && 50 >= m.getRandomPrice()) { testCheck = true; } @@ -44,7 +51,21 @@ public void testRandomizer() throws Exception public void testInventory() throws Exception { setup(); - assertEquals(2, m.getInventory().length); + Item[] mInventory = m.getInventory(); + for(int i=0; i < mInventory.length; i++) + { + System.out.println(mInventory[i].getItemName() + " " + mInventory[i].getBasePrice()); + } + } + + //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 @@ -53,4 +74,9 @@ public void testName() throws Exception setup(); assertEquals("Test", m.getName()); } + + public void testSearch() throws Exception + { + + } }