title: Java - ATM Banking login with 3 times try pin lock
body: <p>I'm new to java programming. I'm trying to make a banking system log-in that locks the user after entering the wrong pin 3 times. I'm trying to merge 2 pieces of code. Any ideas why &quot;Wrong Pin. Try Again&quot; shows up twice in the console?</p>
  <p>ATM class</p>
  <pre><code>import java.util.Scanner;
  
  public class ATM {
      public static void useATM(BankAccount bankAccount) {
          Scanner scanner = new Scanner(System.in);
          char option = '\0';
          do {
              Menu.showMenu();
              option = scanner.next().charAt(0);
              switch (option) {
                  case 'A':
                      System.out.println(bankAccount.getBalance());
                      break;
                  case 'B':
                      System.out.println(&quot;Enter an amount to deposit: &quot;);
                      int amountToDeposit = scanner.nextInt();
                      bankAccount.deposit(amountToDeposit);
                      break;
                  case 'C':
                      System.out.println(&quot;Enter an amount to withdraw: &quot;);
                      int amountToWithdraw = scanner.nextInt();
                      bankAccount.withdraw(amountToWithdraw);
                      break;
                  case 'X':
                      System.out.println(&quot;The transaction is over. &quot;);
                      break;
                  default:
                      System.out.println(&quot;Not a valid option. Choose another option.&quot;);
                      break;
              }
          } while (option != 'X');
      }
  }
  </code></pre>
  <p>BankAccount class</p>
  <pre><code>public class BankAccount {
      String IBAN;
      int balance;
      String pin;
  
      public BankAccount(String IBAN, int balance, String pin) {
          this.IBAN = IBAN;
          this.balance = balance;
          this.pin = pin;
      }
  
      public int getBalance() {
          return this.balance;
      }
  
      public void deposit(int amount) {
          this.balance = this.balance + amount;
      }
  
      public void withdraw(int amount) {
          if (amount &lt;= this.balance) {
              this.balance = this.balance - amount;
              System.out.println(&quot;You withdrew &quot; + amount);
          } else {
              System.out.println(&quot;Not enough money. &quot;);
          }
      }
  }
  </code></pre>
  <p>BankingSystemWithPin main class</p>
  <pre><code>import java.util.Scanner;
  
  public class BankingSystemWithPin {
      public static void main(String[] args) {
          BankAccount bankAccount1 = new BankAccount(&quot;RO29455302311322&quot;, 200, &quot;1234&quot;);
          BankAccount bankAccount2 = new BankAccount(&quot;RO43593530521134&quot;, 600, &quot;7530&quot;);
          BankAccount[] bankAccounts = {bankAccount1, bankAccount2};
          Scanner scanner = new Scanner(System.in);
          Menu.welcome();
          String pin = scanner.nextLine();
          BankAccount currentBankAccount = BankingSystemWithPin.getBankAccountByPin(bankAccounts, pin);
          ATM.useATM(currentBankAccount);
      }
          public static BankAccount getBankAccountByPin(BankAccount[] bankAccounts, String pin) {
          for (BankAccount bankAccount : bankAccounts) {
              if (bankAccount.pin.equals(pin)) {
                  return bankAccount;
              } else if (!bankAccount.pin.equals(pin) ) {
                  System.out.println(&quot;Wrong Pin. Try Again.&quot;);
              }
          }
          return null;
      }
  }
  </code></pre>
  <p>Menu class</p>
  <pre><code>public class Menu {
      public static void showMenu() {
          System.out.println(&quot;Enter an option: &quot;);
          System.out.println(&quot;A. Check balance &quot;);
          System.out.println(&quot;B. Deposit &quot;);
          System.out.println(&quot;C. Withdraw &quot;);
          System.out.println(&quot;X. Exit&quot;);
      }
          public static void welcome() {
          System.out.println(&quot;Welcome! &quot;);
          System.out.println(&quot;Please insert your pin: &quot;);
      }
  }
  </code></pre>
  <p>For the next piece of code, I'm trying to merge it with my login system. What is the proper way to merge it into the main code? Should I try to paste it in BanckAccount class, or make a method to call it in main?</p>
  <pre><code>import java.util.Scanner;
  
  public class PinLockout {
     
              Scanner scanner = new Scanner(System.in);
              int pin = 1234;
              int tries = 0;
              System.out.println(&quot;ENTER YOUR PIN: &quot;);
              int entry = scanner.nextInt();
              tries++;
              while (entry != pin &amp;&amp; tries &lt; 3) {
                  System.out.println(&quot;\nINCORRECT PIN. TRY AGAIN.&quot;);
                  System.out.println(&quot;ENTER YOUR PIN: &quot;);
                  entry = scanner.nextInt();
                  tries++;
              }
              if (entry == pin)
                  System.out.println(&quot;\nPIN ACCEPTED. ACCESS GRANTED.&quot;);
              else if (tries &gt;= 3)
                  System.out.println(&quot;\nYOU HAVE RUN OUT OF TRIES. ACCOUNT LOCKED.&quot;);
          }
      }
  
  }
  </code></pre>