Skip to content

Commit

Permalink
Added methods to Item and Player, adjusted comments on Merchant:
Browse files Browse the repository at this point in the history
*Added the increase and decreaseQuantity methods to the Item class and
added the buy method to the player class
  • Loading branch information
Gavin Li committed Feb 20, 2015
1 parent dc1c3ee commit f9cf1b2
Show file tree
Hide file tree
Showing 3 changed files with 58 additions and 2 deletions.
25 changes: 25 additions & 0 deletions MerchantRPGCSE2102/src/model/Item.java
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,31 @@ else if(calculatedPrice < _minPrice)
return _adjustedPrice;
}

/**
* This method increases the quantity of the items
*
* @param amount is the amount that the item will be increased by
*/
public void increaseQuantity(int amount)
{
_quantity += amount;
}

/**
* This method decreases the quantity of the items
*
* @param amount is the amount that the item will be decreased by
*/
public void decreaseQuantity(int amount)
{
if(amount > getQuantity())
{
System.out.println("Player does not have sufficient amount of" + getItemName());
}
else
_quantity -= amount;
}

/**
* Returns the item's name
*
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 @@ -104,7 +104,7 @@ public void setDailyRandomPercentage(int minPercent, int maxPercent)
}

/**
* This method searches through the merchant's inventory for the specified item name and returns the corresponding item's price
* This method searches through the merchant's inventory for the specified item name and returns the corresponding item's adjusted price
*
* @param itemName string containing the name of the item to search for
* @return the price of the item
Expand Down
33 changes: 32 additions & 1 deletion MerchantRPGCSE2102/src/model/Player.java
Original file line number Diff line number Diff line change
Expand Up @@ -42,10 +42,23 @@ private void generatePlayerInventory(ArrayList<String> itemList)
*
* @param itemName string containing the name of the item to be purchased
* @param targetMerchant the merchant who the player is purchasing from
* @param amount the amount of the item that the player is buying
*/
public void buy(String itemName, Merchant targetMerchant)
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
//will have to loop back out to Transaction class
}
else
{
deductCash(totalPrice);
Item targetItem = getItem(itemName);
targetItem.increaseQuantity(amount);
}
}

/**
Expand All @@ -58,6 +71,24 @@ public void sell(String itemName, Merchant targetMerchant)
{

}

/**
* Searches through the player's inventory for the item corresponding to the specified item name
*
* @param itemName string containing the name of the item
* @return the item matching the specified item name
*/
public Item getItem(String itemName)
{
for(int i = 0; i < _playerInventory.length; i++)
{
if(_playerInventory[i].getItemName().equals(itemName))
return _playerInventory[i];
}

System.out.println("No such item exists"); // item was not found by searching the inventory
return null;
}

/**
* Decreases the player's cash by the specified amount
Expand Down

0 comments on commit f9cf1b2

Please sign in to comment.