diff --git a/MerchantRPGCSE2102/src/model/Merchant.java b/MerchantRPGCSE2102/src/model/Merchant.java index c692f17..0bb6c97 100644 --- a/MerchantRPGCSE2102/src/model/Merchant.java +++ b/MerchantRPGCSE2102/src/model/Merchant.java @@ -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) diff --git a/MerchantRPGCSE2102/src/model/Player.java b/MerchantRPGCSE2102/src/model/Player.java index c3d8d0c..d660dc5 100644 --- a/MerchantRPGCSE2102/src/model/Player.java +++ b/MerchantRPGCSE2102/src/model/Player.java @@ -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 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 * @@ -36,7 +36,7 @@ private void generatePlayerInventory(ArrayList 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 * @@ -47,7 +47,7 @@ private void generatePlayerInventory(ArrayList 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 @@ -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 * @@ -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 * @@ -115,7 +123,7 @@ public void deductCash(int deductAmount) else _playerCash -= deductAmount; } - + /** * Increase's the player's cash by the specified amount * @@ -125,7 +133,7 @@ public void increaseCash(int increaseAmount) { _playerCash += increaseAmount; } - + /** * Returns a string containing the player's name * @@ -135,7 +143,7 @@ public String getName() { return _name; } - + /** * Returns the current amount of cash the player has * @@ -145,7 +153,7 @@ public int getPlayerCash() { return _playerCash; } - + /** * Returns the player's current inventory *