From 93c8bfc09c87069de499bc3989582e70f8b24523 Mon Sep 17 00:00:00 2001 From: Gavin Li Date: Mon, 9 Feb 2015 19:48:05 -0500 Subject: [PATCH] Item and Merchant Class changes: * added an additional constructor to the Item class that allows it to be initalized with only a name and base price, it automatically sets the max price to be 100000 and min price to 0 *Added the a method to generate the inventory of the Merchant class using the ArrayList in the the RPGame class as a parameter --- MerchantRPGCSE2102/src/game/Item.java | 11 ++++++++++ MerchantRPGCSE2102/src/game/Merchant.java | 22 +++++++++++++++++-- .../src/tests/TestMerchant.java | 12 ++++++++-- 3 files changed, 41 insertions(+), 4 deletions(-) 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 ceaf9a6..9b9ed11 100644 --- a/MerchantRPGCSE2102/src/game/Merchant.java +++ b/MerchantRPGCSE2102/src/game/Merchant.java @@ -17,8 +17,26 @@ public Merchant(String name, int cashLimit, ArrayList inventory) _name = name; _currentCash = cashLimit; _baseCash = cashLimit; - //placeholder number - _dailyRandomPercent = 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 diff --git a/MerchantRPGCSE2102/src/tests/TestMerchant.java b/MerchantRPGCSE2102/src/tests/TestMerchant.java index 7c04c06..32d8261 100644 --- a/MerchantRPGCSE2102/src/tests/TestMerchant.java +++ b/MerchantRPGCSE2102/src/tests/TestMerchant.java @@ -2,6 +2,7 @@ import java.util.ArrayList; +import game.Item; import game.Merchant; import junit.framework.TestCase; @@ -12,7 +13,10 @@ public class TestMerchant extends TestCase public void setup() { - ArrayList a = new ArrayList(); + ArrayList a = new ArrayList(); + a.add("water 3"); + a.add("armor 5"); + a.add("food 10"); m = new Merchant("Test", 100, a); } @@ -47,7 +51,11 @@ 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 name