Skip to content

Commit

Permalink
Updated TestPlayer
Browse files Browse the repository at this point in the history
  • Loading branch information
Gavin Li committed Feb 20, 2015
1 parent f9cf1b2 commit 3418288
Show file tree
Hide file tree
Showing 4 changed files with 43 additions and 4 deletions.
9 changes: 7 additions & 2 deletions MerchantRPGCSE2102/src/model/Item.java
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ public Item(String itemName, int basePrice, int quantity)
// max and min price not specified
_maxPrice = 100000; // default max price
_minPrice = 0; // default min price
_adjustedPrice = _basePrice;
_quantity = quantity;
}

Expand Down Expand Up @@ -65,14 +66,18 @@ public void increaseQuantity(int amount)
*
* @param amount is the amount that the item will be decreased by
*/
public void decreaseQuantity(int amount)
public boolean decreaseQuantity(int amount)
{
if(amount > getQuantity())
{
System.out.println("Player does not have sufficient amount of" + getItemName());
System.out.println("Player does not have sufficient amount of " + getItemName());
return false;
}
else
{
_quantity -= amount;
return true;
}
}

/**
Expand Down
2 changes: 1 addition & 1 deletion MerchantRPGCSE2102/src/model/Merchant.java
Original file line number Diff line number Diff line change
Expand Up @@ -192,7 +192,7 @@ public int getRandomPercent()
}

/**
* Returns the merchant's item inventorys
* Returns the merchant's item inventory
* @return item inventory array
*/
public Item[] getInventory()
Expand Down
13 changes: 12 additions & 1 deletion MerchantRPGCSE2102/src/model/Player.java
Original file line number Diff line number Diff line change
Expand Up @@ -66,10 +66,21 @@ public void buy(String itemName, Merchant targetMerchant, int amount)
*
* @param itemName string containing the name of the item to be sold
* @param targetMerchant the merchant who the player is selling to
* @param amount the amount of the item that the player is selling
*/
public void sell(String itemName, Merchant targetMerchant)
public void sell(String itemName, Merchant targetMerchant, int amount)
{
int totalPrice = targetMerchant.getItemPrice(itemName)* amount; //calculates the total price of the items
Item targetItem = getItem(itemName);

if(targetItem.decreaseQuantity(amount))
{
increaseCash(totalPrice);
}
else
{
//loop back to Transaction Class
}
}

/**
Expand Down
23 changes: 23 additions & 0 deletions MerchantRPGCSE2102/src/tests/TestPlayer.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,18 +2,21 @@

import controller.RPGame;
import model.Item;
import model.Merchant;
import model.Player;
import junit.framework.TestCase;

public class TestPlayer extends TestCase
{
private Player p;
private Merchant m;

public void setup()
{
RPGame game = new RPGame(); //starting a new game
game.inventoryFromFile(); //read from file
p = new Player("TestPlayer", 1000, game.getPlayerInventoryList());
m = new Merchant("TestMerchant1", 100000, game.getMerchantInventoryList(1));
}

//test cash system for Player class
Expand All @@ -33,4 +36,24 @@ public void testPlayerInventory() {
System.out.println(item.getItemName() + " " + item.getQuantity());
}
}

public void testBuy()
{
setup();
p.buy("water", m, 10);
assertEquals(970, p.getPlayerCash());
assertEquals(10, p.getItem("water").getQuantity());
}

public void testSell()
{
setup();
testBuy();
p.sell("water", m, 11);
assertEquals(970, p.getPlayerCash());
assertEquals(10, p.getItem("water").getQuantity());
p.sell("water", m, 10);
assertEquals(1000, p.getPlayerCash());
assertEquals(0, p.getItem("water").getQuantity());
}
}

0 comments on commit 3418288

Please sign in to comment.