Skip to content

Commit

Permalink
Adjusted the sell method in Player class
Browse files Browse the repository at this point in the history
  • Loading branch information
Gavin Li committed Feb 20, 2015
1 parent 3418288 commit 5049c7a
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 16 deletions.
2 changes: 1 addition & 1 deletion MerchantRPGCSE2102/src/model/Merchant.java
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ public void refreshCash()
}

/**
* Adds the specified amount of cash to the merchant's cash amount for the start of a day
* Adds the specified amount of cash to the merchant's maximum allowed cash
* @param amount the amount of cash to be added
*/
public void addCash(int amount)
Expand Down
38 changes: 23 additions & 15 deletions MerchantRPGCSE2102/src/model/Player.java
Original file line number Diff line number Diff line change
Expand Up @@ -7,15 +7,15 @@ public class Player
private String _name; // player's name
private int _playerCash; // current amount of cash the player has
private Item[] _playerInventory; // the player's current inventory


public Player(String playerName, int startingCash, ArrayList<String> startingInventory)
{
_name = playerName;
_playerCash = startingCash;
generatePlayerInventory(startingInventory);
}

/**
* Read's the player's item list provided by RPGame and store's instances of each item into the player's inventory
*
Expand All @@ -36,7 +36,7 @@ private void generatePlayerInventory(ArrayList<String> itemList)
_playerInventory[i] = new Item(name, Integer.parseInt(price), 0); // stores a new instance of the item in the player's inventory (quantity begins at 0)
}
}

/**
* Allows player to buy an item from a target merchant
*
Expand All @@ -47,7 +47,7 @@ private void generatePlayerInventory(ArrayList<String> itemList)
public void buy(String itemName, Merchant targetMerchant, int amount)
{
int totalPrice = targetMerchant.getItemPrice(itemName)* amount; //calculates the total price of the items

if(totalPrice > getPlayerCash())
{
System.out.println("You do not have sufficent cash"); //will output error message if total price is over the player's cash amount
Expand All @@ -60,7 +60,7 @@ public void buy(String itemName, Merchant targetMerchant, int amount)
targetItem.increaseQuantity(amount);
}
}

/**
* Allows player to sell an item to a target merchant
*
Expand All @@ -72,17 +72,25 @@ 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))

if(targetMerchant.getCurrentCash() >= totalPrice)
{
increaseCash(totalPrice);
if(targetItem.decreaseQuantity(amount))
{
increaseCash(totalPrice);
targetMerchant.subtractCash(amount);
}
else
{
//loop back to Transaction Class
}
}
else
{
//loop back to Transaction Class
//loop back to transaction Class
}
}

/**
* Searches through the player's inventory for the item corresponding to the specified item name
*
Expand Down Expand Up @@ -115,7 +123,7 @@ public void deductCash(int deductAmount)
else
_playerCash -= deductAmount;
}

/**
* Increase's the player's cash by the specified amount
*
Expand All @@ -125,7 +133,7 @@ public void increaseCash(int increaseAmount)
{
_playerCash += increaseAmount;
}

/**
* Returns a string containing the player's name
*
Expand All @@ -135,7 +143,7 @@ public String getName()
{
return _name;
}

/**
* Returns the current amount of cash the player has
*
Expand All @@ -145,7 +153,7 @@ public int getPlayerCash()
{
return _playerCash;
}

/**
* Returns the player's current inventory
*
Expand Down

0 comments on commit 5049c7a

Please sign in to comment.