Skip to content

Commit

Permalink
Fixed the warning window bug in TransactionUI
Browse files Browse the repository at this point in the history
  • Loading branch information
Gavin Li committed Apr 11, 2015
1 parent 7963dec commit a428dba
Showing 1 changed file with 75 additions and 59 deletions.
134 changes: 75 additions & 59 deletions MerchantRPGCSE2102/src/view/TransactionUI.java
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand All @@ -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);

Expand All @@ -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);
Expand Down Expand Up @@ -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);
Expand All @@ -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");
}
}
});
Expand Down Expand Up @@ -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);
Expand All @@ -241,49 +252,53 @@ 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);
contentPane.add(btnAccept);
// 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);
Expand All @@ -297,6 +312,7 @@ public static void createWarning(String message) {
btnOk.addMouseListener(new MouseAdapter() {
@Override
public void mouseReleased(MouseEvent arg0) {
_isWarning = false;
contentFrame.dispose();
}
});
Expand Down

0 comments on commit a428dba

Please sign in to comment.