Skip to content

Commit

Permalink
Item and Merchant Class changes:
Browse files Browse the repository at this point in the history
* 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
  • Loading branch information
Gavin Li committed Feb 10, 2015
1 parent 3c96e98 commit 93c8bfc
Show file tree
Hide file tree
Showing 3 changed files with 41 additions and 4 deletions.
11 changes: 11 additions & 0 deletions MerchantRPGCSE2102/src/game/Item.java
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
22 changes: 20 additions & 2 deletions MerchantRPGCSE2102/src/game/Merchant.java
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,26 @@ public Merchant(String name, int cashLimit, ArrayList<String> 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<String> 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
Expand Down
12 changes: 10 additions & 2 deletions MerchantRPGCSE2102/src/tests/TestMerchant.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import java.util.ArrayList;

import game.Item;
import game.Merchant;
import junit.framework.TestCase;

Expand All @@ -12,7 +13,10 @@ public class TestMerchant extends TestCase

public void setup()
{
ArrayList a = new ArrayList<String>();
ArrayList<String> a = new ArrayList<String>();
a.add("water 3");
a.add("armor 5");
a.add("food 10");
m = new Merchant("Test", 100, a);
}

Expand Down Expand Up @@ -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
Expand Down

0 comments on commit 93c8bfc

Please sign in to comment.