From f9cf1b29680c11cd2f31477949b60904ad759fc8 Mon Sep 17 00:00:00 2001 From: Gavin Li Date: Thu, 19 Feb 2015 23:29:38 -0500 Subject: [PATCH] Added methods to Item and Player, adjusted comments on Merchant: *Added the increase and decreaseQuantity methods to the Item class and added the buy method to the player class --- MerchantRPGCSE2102/src/model/Item.java | 25 ++++++++++++++++ MerchantRPGCSE2102/src/model/Merchant.java | 2 +- MerchantRPGCSE2102/src/model/Player.java | 33 +++++++++++++++++++++- 3 files changed, 58 insertions(+), 2 deletions(-) diff --git a/MerchantRPGCSE2102/src/model/Item.java b/MerchantRPGCSE2102/src/model/Item.java index 7cb3947..33b1a3e 100644 --- a/MerchantRPGCSE2102/src/model/Item.java +++ b/MerchantRPGCSE2102/src/model/Item.java @@ -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 * diff --git a/MerchantRPGCSE2102/src/model/Merchant.java b/MerchantRPGCSE2102/src/model/Merchant.java index 9bab251..b00dbf4 100644 --- a/MerchantRPGCSE2102/src/model/Merchant.java +++ b/MerchantRPGCSE2102/src/model/Merchant.java @@ -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 diff --git a/MerchantRPGCSE2102/src/model/Player.java b/MerchantRPGCSE2102/src/model/Player.java index b31793a..5291f69 100644 --- a/MerchantRPGCSE2102/src/model/Player.java +++ b/MerchantRPGCSE2102/src/model/Player.java @@ -42,10 +42,23 @@ private void generatePlayerInventory(ArrayList 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); + } } /** @@ -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