From a428dba0dafaaf93a04acd48619682971eb9a0b6 Mon Sep 17 00:00:00 2001 From: Gavin Li Date: Sat, 11 Apr 2015 14:35:35 -0400 Subject: [PATCH] Fixed the warning window bug in TransactionUI --- .../src/view/TransactionUI.java | 134 ++++++++++-------- 1 file changed, 75 insertions(+), 59 deletions(-) diff --git a/MerchantRPGCSE2102/src/view/TransactionUI.java b/MerchantRPGCSE2102/src/view/TransactionUI.java index ba98daa..0230e63 100644 --- a/MerchantRPGCSE2102/src/view/TransactionUI.java +++ b/MerchantRPGCSE2102/src/view/TransactionUI.java @@ -19,12 +19,14 @@ public class TransactionUI extends JFrame { private static JPanel transactionPanel; private static Transaction MASTER; //TransactionUI class will hold the instance of the Transaction that created it - + private static boolean _isWarning; + /** * Create the frame. */ public TransactionUI(Transaction master) { MASTER = master; + _isWarning = false; setTitle("Transaction Window"); setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); setBounds(100, 100, 600, 430); @@ -42,8 +44,8 @@ public void actionPerformed(ActionEvent arg0) { public void mouseReleased(MouseEvent arg0) { System.out.println("BUY"); //temporary test code createBuyWindow(); - } - }); + } + }); btnBuy.setBounds(58, 155, 169, 105); transactionPanel.add(btnBuy); @@ -64,8 +66,8 @@ public void mouseReleased(MouseEvent e) { public void mouseReleased(MouseEvent e) { System.out.println("Cancel"); //temporary test code - MASTER.actionCancel(); - exitWindow(); //Will end the transaction main screen but only if player does not have another transaction screen open + MASTER.actionCancel(); + exitWindow(); //Will end the transaction main screen but only if player does not have another transaction screen open } }); btnCancel.setBounds(210, 310, 160, 50); @@ -141,10 +143,13 @@ public void createBuyWindow() btnCancel.addMouseListener(new MouseAdapter() { @Override public void mouseReleased(MouseEvent arg0) { - getContentPane().removeAll(); - add(transactionPanel); - setVisible(false); - setVisible(true); + if(!_isWarning) + { + getContentPane().removeAll(); + add(transactionPanel); + setVisible(false); + setVisible(true); + } } }); btnCancel.setBounds(61, 306, 89, 23); @@ -153,26 +158,29 @@ public void mouseReleased(MouseEvent arg0) { 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 mouseReleased(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.actionBuy(chosenItem, chosenAmount)) //will attempt to buy the specified amount of the chosen item - { - System.out.println(chosenAmount); - getContentPane().removeAll(); - add(transactionPanel); - setVisible(false); - setVisible(true); + public void mouseReleased(MouseEvent e) { + if(!_isWarning) + { + 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.actionBuy(chosenItem, chosenAmount)) //will attempt to buy the specified amount of the chosen item + { + System.out.println(chosenAmount); + getContentPane().removeAll(); + add(transactionPanel); + setVisible(false); + setVisible(true); + } + else + throw new NumberFormatException(); //will throw a NumberFormatException if actionBuy returns a false + } + catch(NumberFormatException exception){ + System.out.println("Must input an amount that you can afford"); + createWarning("Must input an amount that you can afford"); } - else - throw new NumberFormatException(); //will throw a NumberFormatException if actionBuy returns a false - } - catch(NumberFormatException exception){ - System.out.println("Must input an amount that you can afford"); - createWarning("Must input an amount that you can afford"); } } }); @@ -228,10 +236,13 @@ public void createSellWindow() btnCancel.addMouseListener(new MouseAdapter() { @Override public void mouseReleased(MouseEvent arg0) { - getContentPane().removeAll(); - add(transactionPanel); - setVisible(false); - setVisible(true); + if(!_isWarning) + { + getContentPane().removeAll(); + add(transactionPanel); + setVisible(false); + setVisible(true); + } } }); btnCancel.setBounds(61, 306, 89, 23); @@ -241,40 +252,43 @@ public void mouseReleased(MouseEvent arg0) { btnAccept.addMouseListener(new MouseAdapter() { @Override public void mouseReleased(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); - boolean isGood = false; + if(!_isWarning) + { + String chosenItem = comboBox.getSelectedItem().toString().split(" ")[0]; //consumes the String name of the item from drop-down menu + System.out.println(chosenItem); + boolean isGood = false; - try{ 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); - getContentPane().removeAll(); - add(transactionPanel); - setVisible(false); - setVisible(true); + 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); + getContentPane().removeAll(); + add(transactionPanel); + setVisible(false); + setVisible(true); + } + else + throw new NumberFormatException(); //will throw a NumberFormatException if actionSell returns a false + } + catch(MerchantNotEnoughCashException mnecexception){ + System.out.println("Merchant does not have enough cash"); + createWarning("Merchant does not have enough cash"); } - else - throw new NumberFormatException(); //will throw a NumberFormatException if actionSell returns a false } - catch(MerchantNotEnoughCashException mnecexception){ - System.out.println("Merchant does not have enough cash"); - createWarning("Merchant does not have enough cash"); + catch(NumberFormatException exception){ + System.out.println("You do not have enough of that item"); + createWarning("You do not have enough of that item"); } } - catch(NumberFormatException exception){ - System.out.println("You do not have enough of that item"); - createWarning("You do not have enough of that item"); + catch(NotInInventoryException niiexception){ + System.out.println("Merchant does not accept that item"); + createWarning("Merchant does not accept that item"); } } - catch(NotInInventoryException niiexception){ - System.out.println("Merchant does not accept that item"); - createWarning("Merchant does not accept that item"); - } } }); btnAccept.setBounds(247, 306, 89, 23); @@ -282,8 +296,9 @@ public void mouseReleased(MouseEvent e) { //information is consumed // frame.setVisible(true); // frame.setLocationRelativeTo(null); } - + public static void createWarning(String message) { + _isWarning = true; final JFrame contentFrame = new JFrame(); JPanel warningPane = new JPanel(); contentFrame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE); @@ -297,6 +312,7 @@ public static void createWarning(String message) { btnOk.addMouseListener(new MouseAdapter() { @Override public void mouseReleased(MouseEvent arg0) { + _isWarning = false; contentFrame.dispose(); } });