From 91b1d9c711d3a5e0678dbda285bd4ff8baa7a85f Mon Sep 17 00:00:00 2001 From: Gavin Li Date: Sat, 28 Feb 2015 15:11:06 -0500 Subject: [PATCH] Created new exception --- .../src/controller/Transaction.java | 4 ++- .../MerchantNotEnoughCashException.java | 11 +++++++ MerchantRPGCSE2102/src/model/Player.java | 6 ++-- MerchantRPGCSE2102/src/tests/TestPlayer.java | 3 +- .../src/view/TransactionUI.java | 32 +++++++++++-------- 5 files changed, 39 insertions(+), 17 deletions(-) create mode 100644 MerchantRPGCSE2102/src/exceptions/MerchantNotEnoughCashException.java diff --git a/MerchantRPGCSE2102/src/controller/Transaction.java b/MerchantRPGCSE2102/src/controller/Transaction.java index def3e51..4e1d13f 100644 --- a/MerchantRPGCSE2102/src/controller/Transaction.java +++ b/MerchantRPGCSE2102/src/controller/Transaction.java @@ -1,5 +1,6 @@ package controller; +import exceptions.MerchantNotEnoughCashException; import exceptions.NotInInventoryException; import view.TransactionUI; import model.Item; @@ -50,8 +51,9 @@ public boolean actionBuy(String itemName, int amount) * @param amount amount that the player wants to buy * @return returns true if transaction successful, false otherwise * @throws NotInInventoryException + * @throws MerchantNotEnoughCashException */ - public boolean actionSell(String itemName, int amount) throws NotInInventoryException + public boolean actionSell(String itemName, int amount) throws NotInInventoryException, MerchantNotEnoughCashException { if(_player.sell(itemName, _targetMerchant, amount)) return true; diff --git a/MerchantRPGCSE2102/src/exceptions/MerchantNotEnoughCashException.java b/MerchantRPGCSE2102/src/exceptions/MerchantNotEnoughCashException.java new file mode 100644 index 0000000..a839581 --- /dev/null +++ b/MerchantRPGCSE2102/src/exceptions/MerchantNotEnoughCashException.java @@ -0,0 +1,11 @@ +package exceptions; + +@SuppressWarnings("serial") +public class MerchantNotEnoughCashException extends Exception{ + + public MerchantNotEnoughCashException() + { + super(); + } + +} diff --git a/MerchantRPGCSE2102/src/model/Player.java b/MerchantRPGCSE2102/src/model/Player.java index 39c45d1..3ce3a68 100644 --- a/MerchantRPGCSE2102/src/model/Player.java +++ b/MerchantRPGCSE2102/src/model/Player.java @@ -2,6 +2,7 @@ import java.util.ArrayList; +import exceptions.MerchantNotEnoughCashException; import exceptions.NotInInventoryException; public class Player extends Character @@ -71,8 +72,9 @@ public boolean buy(String itemName, Merchant targetMerchant, int amount) * @param amount the amount of the item that the player is selling * @return returns true if transaction successful, false otherwise * @throws NotInInventoryException + * @throws MerchantNotEnoughCashException */ - public boolean sell(String itemName, Merchant targetMerchant, int amount) throws NotInInventoryException + public boolean sell(String itemName, Merchant targetMerchant, int amount) throws NotInInventoryException, MerchantNotEnoughCashException { int totalPrice = targetMerchant.getItemPrice(itemName)* amount; //calculates the total price of the items @@ -92,7 +94,7 @@ public boolean sell(String itemName, Merchant targetMerchant, int amount) throws return true; } else - return false; + throw new MerchantNotEnoughCashException(); } else return false; diff --git a/MerchantRPGCSE2102/src/tests/TestPlayer.java b/MerchantRPGCSE2102/src/tests/TestPlayer.java index f1e424a..1052553 100644 --- a/MerchantRPGCSE2102/src/tests/TestPlayer.java +++ b/MerchantRPGCSE2102/src/tests/TestPlayer.java @@ -1,6 +1,7 @@ package tests; import controller.RPGame; +import exceptions.MerchantNotEnoughCashException; import exceptions.NotInInventoryException; import model.Item; import model.Merchant; @@ -47,7 +48,7 @@ public void testBuy() assertEquals(10, p.getItem("water").getQuantity()); } - public void testSell() throws NotInInventoryException + public void testSell() throws NotInInventoryException, MerchantNotEnoughCashException { setup(); testBuy(); diff --git a/MerchantRPGCSE2102/src/view/TransactionUI.java b/MerchantRPGCSE2102/src/view/TransactionUI.java index 8d1be0e..180cf05 100644 --- a/MerchantRPGCSE2102/src/view/TransactionUI.java +++ b/MerchantRPGCSE2102/src/view/TransactionUI.java @@ -15,6 +15,7 @@ import javax.swing.JTextField; import controller.Transaction; +import exceptions.MerchantNotEnoughCashException; import exceptions.NotInInventoryException; import java.awt.event.MouseAdapter; @@ -233,7 +234,7 @@ public static void createSellWindow() itemList[i] = MASTER.getPlayer().getInventory()[i].getItemName(); itemList[i] = itemList[i] + " (x" + MASTER.getPlayer().getInventory()[i].getQuantity() + ")"; } - + JLabel lblYouHave = new JLabel(MASTER.getTargetMerchant().getName() + " has: $" + MASTER.getTargetMerchant().getCurrentCash()); lblYouHave.setBounds(61, 27, 299, 14); contentPane.add(lblYouHave); @@ -275,20 +276,25 @@ public void mouseClicked(MouseEvent e) { //information is consumed w boolean isGood = false; try{ - try{ //will attempt to parse the number player entered, will stay on the same screen if it cannot - int chosenAmount = Integer.parseInt(textField.getText()); - isGood = MASTER.actionSell(chosenItem, chosenAmount); //checks if the merchant will buy that item - if(isGood) //will attempt to sell the specified amount of the chosen item - { - System.out.println(chosenAmount); - _inTransaction = false; - frame.dispose(); + try{ + try{ //will attempt to parse the number player entered, will stay on the same screen if it cannot + int chosenAmount = Integer.parseInt(textField.getText()); + isGood = MASTER.actionSell(chosenItem, chosenAmount); //checks if the merchant will buy that item + if(isGood) //will attempt to sell the specified amount of the chosen item + { + System.out.println(chosenAmount); + _inTransaction = false; + frame.dispose(); + } + else + throw new NumberFormatException(); //will throw a NumberFormatException if actionSell returns a false + } + catch(NumberFormatException exception){ + System.out.println("Must input a number less than or equal to the total amount of the item that you have"); } - else - throw new NumberFormatException(); //will throw a NumberFormatException if actionSell returns a false } - catch(NumberFormatException exception){ - System.out.println("Must input a number less than or equal to the total amount of the item that you have"); + catch(MerchantNotEnoughCashException mnecexception){ + System.out.println("Merchant does not have enough cash"); } } catch(NotInInventoryException niiexception){