Skip to content

Commit

Permalink
Added actionBuy and actionSell to the Transaction Class
Browse files Browse the repository at this point in the history
  • Loading branch information
Gavin Li committed Feb 20, 2015
1 parent 672224d commit de25901
Show file tree
Hide file tree
Showing 3 changed files with 42 additions and 6 deletions.
31 changes: 31 additions & 0 deletions MerchantRPGCSE2102/src/controller/Transaction.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
}
3 changes: 2 additions & 1 deletion MerchantRPGCSE2102/src/model/Merchant.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
14 changes: 9 additions & 5 deletions MerchantRPGCSE2102/src/model/Player.java
Original file line number Diff line number Diff line change
Expand Up @@ -43,21 +43,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
* @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;
}
}

Expand All @@ -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);
Expand All @@ -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;
}
}

Expand Down

0 comments on commit de25901

Please sign in to comment.