From de259019df6940248443ea69228bf544796effd3 Mon Sep 17 00:00:00 2001 From: Gavin Li Date: Fri, 20 Feb 2015 12:05:04 -0500 Subject: [PATCH] Added actionBuy and actionSell to the Transaction Class --- .../src/controller/Transaction.java | 31 +++++++++++++++++++ MerchantRPGCSE2102/src/model/Merchant.java | 3 +- MerchantRPGCSE2102/src/model/Player.java | 14 ++++++--- 3 files changed, 42 insertions(+), 6 deletions(-) diff --git a/MerchantRPGCSE2102/src/controller/Transaction.java b/MerchantRPGCSE2102/src/controller/Transaction.java index bfb2956..5ff2bcd 100644 --- a/MerchantRPGCSE2102/src/controller/Transaction.java +++ b/MerchantRPGCSE2102/src/controller/Transaction.java @@ -19,9 +19,40 @@ public Transaction(Player player, Merchant targetMerchant) /** * Will be Transaction class's main method + * incomplete method */ public void runTransaction() { } + + /** + * This method invokes the related buy methods and checks in the Player and Merchant classes + * + * @param itemName name of the item + * @param amount amount that the player wants to buy + * @return returns true if transaction successful, false otherwise + */ + public boolean actionBuy(String itemName, int amount) + { + if(_player.buy(itemName, _targetMerchant, amount)) + return true; + else + return false; + } + + /** + * This method invokes the related sell methods and checks in the Player and Merchant classes + * + * @param itemName name of the item + * @param amount amount that the player wants to buy + * @return returns true if transaction successful, false otherwise + */ + public boolean actionSell(String itemName, int amount) + { + if(_player.sell(itemName, _targetMerchant, amount)) + return true; + else + return false; + } } diff --git a/MerchantRPGCSE2102/src/model/Merchant.java b/MerchantRPGCSE2102/src/model/Merchant.java index 0bb6c97..1e2e4af 100644 --- a/MerchantRPGCSE2102/src/model/Merchant.java +++ b/MerchantRPGCSE2102/src/model/Merchant.java @@ -63,13 +63,14 @@ public void addCash(int amount) /** * Removes the specified amount of cash to the merchant's cash amount for the start of a day * @param amount the amount of cash to be subtracted + * @return the amount of cash the merchant has left or -1 if the merchant did not have sufficient cash */ public int subtractCash(int amount) { if(amount > _currentCash) // if more cash is being subtracted than the merchant has { - System.out.println("Merchant has insufficent cash"); + System.out.println("Merchant has insufficient cash"); return -1; // value of -1 indicates an unsuccessful subtraction } else { _currentCash -= amount; diff --git a/MerchantRPGCSE2102/src/model/Player.java b/MerchantRPGCSE2102/src/model/Player.java index d660dc5..ab3aa3e 100644 --- a/MerchantRPGCSE2102/src/model/Player.java +++ b/MerchantRPGCSE2102/src/model/Player.java @@ -43,21 +43,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 + * @return returns true if transaction successful, false otherwise */ - public void buy(String itemName, Merchant targetMerchant, int amount) + public boolean 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 + return false; } else { deductCash(totalPrice); Item targetItem = getItem(itemName); targetItem.increaseQuantity(amount); + return true; } } @@ -67,8 +69,9 @@ 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 + * @return returns true if transaction successful, false otherwise */ - public void sell(String itemName, Merchant targetMerchant, int amount) + public boolean sell(String itemName, Merchant targetMerchant, int amount) { int totalPrice = targetMerchant.getItemPrice(itemName)* amount; //calculates the total price of the items Item targetItem = getItem(itemName); @@ -79,15 +82,16 @@ public void sell(String itemName, Merchant targetMerchant, int amount) { increaseCash(totalPrice); targetMerchant.subtractCash(amount); + return true; } else { - //loop back to Transaction Class + return false; } } else { - //loop back to transaction Class + return false; } }