简体   繁体   中英

Using if(boolean==true) in actionPerformed

I currently have a class for an ATM project. In my actionPerformed, I have 4 buttons (twenty, fifty, hundred and other). Lets stick with depositing money into account. I would like to differ chequing deposit from savings but have them in the same java file.

In my attribute I have

 private triggerChecker checker;

that decides if user is going for chequing(true boolean or saving(false boolean).

The code below will be showing my actionPerformed method

 public void actionPerformed(ActionEvent e)
 {
  //Loop through the keypad array to see if any of the buttons were pressed 
   for (int i = 0; i < 10; i++)
   {
      if (e.getSource() == keypad[i])
      {
         //Edit the input display
         cardID = cardID + i;
         display.setText("\n\n\tPlease enter amount of money:" + "\n\t" +cardID);
      }
   }

   if(e.getSource() == keypad[11]){
        cardID = "";
        display.setText("\n\n\tPlease enter amount of money:" + "\n\t" +cardID);
  }

  if(checker.getCheck() == true){
  if (e.getSource() == twenty)
  {
      display.setText(feature.DepositTOC(20.0));
      display.setFont(new Font("Arial", Font.BOLD,18));
      display.setVisible(true);

  }

  if (e.getSource() == fifty)
  {
      display.setText(feature.DepositTOC(50.0));
      display.setFont(new Font("Arial", Font.BOLD,18));
      display.setVisible(true);

  }

  if (e.getSource() == hundred)
  {
      display.setText(account.toString());
      display.setFont(new Font("Arial", Font.BOLD,20));
      display.setVisible(true);
  }

  if (e.getSource() == other)
  {
      cardID = "";
      display.setText("\n\n\tPlease enter amount of money:" + "\n\t" +cardID);
  }
  if(e.getSource() == keypad[12]){
      double num = Double.parseDouble(cardID);
      display.setText(feature.DepositTOC(num));
      display.setFont(new Font("Arial", Font.BOLD,18));
      display.setVisible(true);

  }
  }
  if(checker.getCheck() == false){
      if (e.getSource() == twenty)
      {
          display.setText(feature.DepositTOS(20.0));
          display.setFont(new Font("Arial", Font.BOLD,18));
          display.setVisible(true);

      }

      if (e.getSource() == fifty)
      {
          display.setText(feature.DepositTOS(50.0));
          display.setFont(new Font("Arial", Font.BOLD,18));
          display.setVisible(true);

      }

      if (e.getSource() == hundred)
      {
          display.setText(account.toString());
          display.setFont(new Font("Arial", Font.BOLD,20));
          display.setVisible(true);
      }

      if (e.getSource() == other)
      {
          cardID = "";
          display.setText("\n\n\tPlease enter amount of money:" + "\n\t" +cardID);
      }
      if(e.getSource() == keypad[12]){
          double num = Double.parseDouble(cardID);
          display.setText(feature.DepositTOS(num));
          display.setFont(new Font("Arial", Font.BOLD,18));
          display.setVisible(true);

      }}
     }

I have a feeling that I am not able to if(boolean == true) in actionPerformed. If you have ideas, please let me know as I am willing to work with tips and help and not planning someone to write a whole function for me. Thanks

checker class

public class triggerChecker {
private boolean checkW, checkD, checkWC, checkWS, checkDC, checkDS;

public triggerChecker(){
    checkW = false;
    checkD = false;
    checkWC = false;
    checkWS = false;
    checkDC = false;
    checkDS = false;
}

public void setCheckW(boolean s){
    checkW = s;
}
public void setCheckD(boolean s){
    checkD = s;
}
public void setCheckWC(boolean s){
    checkWC = s;
}
public void setCheckWS(boolean s){
    checkWS = s;
}
public void setCheckDC(boolean s){
    checkDC = s;
}
public void setCheckDS(boolean s){
    checkDS = s;
}


public boolean getCheckW(){
    return checkW;
}
public boolean getCheckD(){
    return checkD;
}
public boolean getCheckWC(){
    return checkWC;
}
public boolean getCheckWS(){
    return checkWS;
}
public boolean getCheckDC(){
    return checkDC;
}
public boolean getCheckDS(){
    return checkDS;
}

}

There is no reason you can't access or use a boolean inside actionPerformed. The only thing you have to watch for is actionPerformed can be called by your user at times you don't expect, so make sure your boolean is initialized at all times that the user could perform the action you're listening on.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM