diff --git a/MerchantRPGCSE2102/src/controller/Transaction.java b/MerchantRPGCSE2102/src/controller/Transaction.java index 4769e31..def3e51 100644 --- a/MerchantRPGCSE2102/src/controller/Transaction.java +++ b/MerchantRPGCSE2102/src/controller/Transaction.java @@ -1,5 +1,6 @@ package controller; +import exceptions.NotInInventoryException; import view.TransactionUI; import model.Item; import model.Merchant; @@ -48,8 +49,9 @@ public boolean actionBuy(String itemName, int amount) * @param itemName name of the item * @param amount amount that the player wants to buy * @return returns true if transaction successful, false otherwise + * @throws NotInInventoryException */ - public boolean actionSell(String itemName, int amount) + public boolean actionSell(String itemName, int amount) throws NotInInventoryException { if(_player.sell(itemName, _targetMerchant, amount)) return true; diff --git a/MerchantRPGCSE2102/src/exceptions/NotInInventoryException.java b/MerchantRPGCSE2102/src/exceptions/NotInInventoryException.java new file mode 100644 index 0000000..83e5086 --- /dev/null +++ b/MerchantRPGCSE2102/src/exceptions/NotInInventoryException.java @@ -0,0 +1,10 @@ +package exceptions; + +@SuppressWarnings("serial") +public class NotInInventoryException extends Exception +{ + public NotInInventoryException() + { + super(); + } +} diff --git a/MerchantRPGCSE2102/src/model/Player.java b/MerchantRPGCSE2102/src/model/Player.java index 7856a9f..1cf4ea0 100644 --- a/MerchantRPGCSE2102/src/model/Player.java +++ b/MerchantRPGCSE2102/src/model/Player.java @@ -2,6 +2,8 @@ import java.util.ArrayList; +import exceptions.NotInInventoryException; + public class Player { private String _name; // player's name @@ -70,10 +72,17 @@ public boolean buy(String itemName, Merchant targetMerchant, int amount) * @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 + * @throws NotInInventoryException */ - public boolean sell(String itemName, Merchant targetMerchant, int amount) + public boolean sell(String itemName, Merchant targetMerchant, int amount) throws NotInInventoryException { int totalPrice = targetMerchant.getItemPrice(itemName)* amount; //calculates the total price of the items + + if(totalPrice == -1 * amount) + { + throw new NotInInventoryException(); //returns false if the merchant does not buy that item + } + Item targetItem = getItem(itemName); if(targetMerchant.getCurrentCash() >= totalPrice) diff --git a/MerchantRPGCSE2102/src/tests/TestPlayer.java b/MerchantRPGCSE2102/src/tests/TestPlayer.java index da54a73..f1e424a 100644 --- a/MerchantRPGCSE2102/src/tests/TestPlayer.java +++ b/MerchantRPGCSE2102/src/tests/TestPlayer.java @@ -1,6 +1,7 @@ package tests; import controller.RPGame; +import exceptions.NotInInventoryException; import model.Item; import model.Merchant; import model.Player; @@ -46,7 +47,7 @@ public void testBuy() assertEquals(10, p.getItem("water").getQuantity()); } - public void testSell() + public void testSell() throws NotInInventoryException { setup(); testBuy(); diff --git a/MerchantRPGCSE2102/src/view/TransactionUI.java b/MerchantRPGCSE2102/src/view/TransactionUI.java index f953fc8..b7990d0 100644 --- a/MerchantRPGCSE2102/src/view/TransactionUI.java +++ b/MerchantRPGCSE2102/src/view/TransactionUI.java @@ -18,6 +18,7 @@ import javax.swing.JTextField; import controller.Transaction; +import exceptions.NotInInventoryException; import java.awt.event.MouseAdapter; import java.awt.event.MouseEvent; @@ -146,7 +147,7 @@ public static void createBuyWindow() itemList[i] = MASTER.getTargetMerchant().getInventory()[i].getItemName(); itemList[i] = itemList[i] + " ($" + MASTER.getTargetMerchant().getInventory()[i].getAdjustedPrice() + ")"; } - + JLabel lblYouHave = new JLabel("You have: $" + MASTER.getPlayer().getPlayerCash()); lblYouHave.setBounds(61, 27, 86, 14); contentPane.add(lblYouHave); @@ -168,7 +169,7 @@ public static void createBuyWindow() JLabel lblSelectTheAmount = new JLabel("Enter the amount you wish to buy:"); //label containing instructions lblSelectTheAmount.setBounds(61, 197, 275, 30); contentPane.add(lblSelectTheAmount); - + JButton btnCancel = new JButton("Cancel"); //cancel button, re-enables the player's ability to call new transaction windows btnCancel.addMouseListener(new MouseAdapter() { @Override @@ -215,7 +216,7 @@ public void mouseClicked(MouseEvent e) { //information is consumed whe @SuppressWarnings({ "unchecked", "rawtypes" }) public static void createSellWindow() { - _inTransaction = true; //does not allow new transaction windows to be opened if sell window is + _inTransaction = true; //does not allow new transaction windows to be opened if sell window is final JFrame frame = new JFrame("Sell"); frame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE); frame.setBounds(100, 100, 400, 400); @@ -225,31 +226,31 @@ public static void createSellWindow() frame.setContentPane(contentPane); contentPane.setLayout(null); - String[] itemList = new String[MASTER.getPlayer().getInventory().length]; //create a new array of strings with the length of the player's inventory //item that the player chooses from the combobox - for(int i = 0; i < itemList.length; i++) //adds all the item names to the item list + String[] itemList = new String[MASTER.getPlayer().getInventory().length]; //create a new array of strings with the length of the player's inventory //item that the player chooses from the combobox + for(int i = 0; i < itemList.length; i++) //adds all the item names to the item list { itemList[i] = MASTER.getPlayer().getInventory()[i].getItemName(); itemList[i] = itemList[i] + " (x" + MASTER.getPlayer().getInventory()[i].getQuantity() + ")"; } - final JComboBox comboBox = new JComboBox(itemList); //create a drop-down menu for the player to choose from + final JComboBox comboBox = new JComboBox(itemList); //create a drop-down menu for the player to choose from comboBox.setBounds(61, 141, 131, 20); contentPane.add(comboBox); - final JTextField textField = new JTextField(); //create a text field for the player to enter the amount desired into + final JTextField textField = new JTextField(); //create a text field for the player to enter the amount desired into textField.setBounds(61, 238, 86, 20); contentPane.add(textField); textField.setColumns(10); - JLabel lblSelectTheItem = new JLabel("Select the Item you want:"); //label containing instructions + JLabel lblSelectTheItem = new JLabel("Select the Item you want:"); //label containing instructions lblSelectTheItem.setBounds(61, 105, 168, 14); contentPane.add(lblSelectTheItem); - JLabel lblSelectTheAmount = new JLabel("Enter the amount you wish to sell:"); //label containing instructions + JLabel lblSelectTheAmount = new JLabel("Enter the amount you wish to sell:"); //label containing instructions lblSelectTheAmount.setBounds(61, 197, 275, 30); contentPane.add(lblSelectTheAmount); - - JButton btnCancel = new JButton("Cancel"); //cancel button, re-enables the player's ability to call new transaction windows + + JButton btnCancel = new JButton("Cancel"); //cancel button, re-enables the player's ability to call new transaction windows btnCancel.addMouseListener(new MouseAdapter() { @Override public void mouseClicked(MouseEvent arg0) { @@ -260,26 +261,33 @@ public void mouseClicked(MouseEvent arg0) { btnCancel.setBounds(61, 306, 89, 23); contentPane.add(btnCancel); - JButton btnAccept = new JButton("Accept"); //create an accept button to consume the information the player entered into the menu and text field + JButton btnAccept = new JButton("Accept"); //create an accept button to consume the information the player entered into the menu and text field btnAccept.addMouseListener(new MouseAdapter() { @Override - public void mouseClicked(MouseEvent e) { //information is consumed when player clicks the accept button + public void mouseClicked(MouseEvent e) { //information is consumed when player clicks the accept button String chosenItem = comboBox.getSelectedItem().toString().split(" ")[0]; //consumes the String name of the item from drop-down menu System.out.println(chosenItem); - - try{ //will attempt to parse the number player entered, will stay on the same screen if it cannot - int chosenAmount = Integer.parseInt(textField.getText()); - if(MASTER.actionSell(chosenItem, chosenAmount)) //will attempt to sell the specified amount of the chosen item - { - System.out.println(chosenAmount); - _inTransaction = false; - frame.dispose(); + 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(); + } + 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(NotInInventoryException niiexception){ + System.out.println("Merchant does not accept that item");; } } });