Skip to content

Commit

Permalink
Merge pull request #4 from gal11002/Gavin-L
Browse files Browse the repository at this point in the history
Gavin l
  • Loading branch information
john committed Feb 13, 2015
2 parents 532211a + 68e4fa6 commit a91218b
Show file tree
Hide file tree
Showing 4 changed files with 105 additions and 36 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
85 changes: 56 additions & 29 deletions MerchantRPGCSE2102/src/game/Merchant.java
Original file line number Diff line number Diff line change
@@ -1,28 +1,42 @@
package game;

import java.util.ArrayList;
import java.util.Random;

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<String> 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<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 All @@ -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
Expand Down Expand Up @@ -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);
}
}

Expand All @@ -115,9 +142,9 @@ public int getBaseCash()
}

//dailyRandomPrice getter
public int getRandomPercentage()
public int getRandomPrice()
{
return _dailyRandomPercentage;
return _dailyRandomPercent;
}

//inventory getter
Expand Down
7 changes: 6 additions & 1 deletion MerchantRPGCSE2102/src/game/RPGame.java
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,12 @@ public ArrayList<String> 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;
}
}
}
38 changes: 32 additions & 6 deletions MerchantRPGCSE2102/src/tests/TestMerchant.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
package tests;

import java.util.ArrayList;

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

Expand All @@ -10,18 +13,22 @@ public class TestMerchant extends TestCase

public void setup()
{
m = new Merchant("Test", 100, 2);
ArrayList<String> a = new ArrayList<String>();
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());
}

Expand All @@ -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;
}
Expand All @@ -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
Expand All @@ -53,4 +74,9 @@ public void testName() throws Exception
setup();
assertEquals("Test", m.getName());
}

public void testSearch() throws Exception
{

}
}

0 comments on commit a91218b

Please sign in to comment.