Java - ATM银行登录3次尝试密码锁定
Posted
技术标签:
【中文标题】Java - ATM银行登录3次尝试密码锁定【英文标题】:Java - ATM Banking login with 3 times try pin lock 【发布时间】:2021-09-09 01:45:39 【问题描述】:我m new to java programming. I
m 尝试进行银行系统登录,在输入错误密码 3 次后锁定用户。我正在尝试合并 2 段代码。知道为什么“错误的 Pin。再试一次”在控制台中出现两次吗?
ATM类
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("Enter an amount to deposit: ");
int amountToDeposit = scanner.nextInt();
bankAccount.deposit(amountToDeposit);
break;
case 'C':
System.out.println("Enter an amount to withdraw: ");
int amountToWithdraw = scanner.nextInt();
bankAccount.withdraw(amountToWithdraw);
break;
case 'X':
System.out.println("The transaction is over. ");
break;
default:
System.out.println("Not a valid option. Choose another option.");
break;
while (option != 'X');
银行账户类
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 <= this.balance)
this.balance = this.balance - amount;
System.out.println("You withdrew " + amount);
else
System.out.println("Not enough money. ");
BankingSystemWithPin 主类
import java.util.Scanner;
public class BankingSystemWithPin
public static void main(String[] args)
BankAccount bankAccount1 = new BankAccount("RO29455302311322", 200, "1234");
BankAccount bankAccount2 = new BankAccount("RO43593530521134", 600, "7530");
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("Wrong Pin. Try Again.");
return null;
菜单类
public class Menu
public static void showMenu()
System.out.println("Enter an option: ");
System.out.println("A. Check balance ");
System.out.println("B. Deposit ");
System.out.println("C. Withdraw ");
System.out.println("X. Exit");
public static void welcome()
System.out.println("Welcome! ");
System.out.println("Please insert your pin: ");
我尝试在我的登录系统中合并的下一段代码。将它合并到主代码中的正确方法是什么?我应该尝试将它粘贴到 BanckAccount 类中,还是制作一个方法在 main 中调用它?
import java.util.Scanner;
public class PinLockout
Scanner scanner = new Scanner(System.in);
int pin = 1234;
int tries = 0;
System.out.println("ENTER YOUR PIN: ");
int entry = scanner.nextInt();
tries++;
while (entry != pin && tries < 3)
System.out.println("\nINCORRECT PIN. TRY AGAIN.");
System.out.println("ENTER YOUR PIN: ");
entry = scanner.nextInt();
tries++;
if (entry == pin)
System.out.println("\nPIN ACCEPTED. ACCESS GRANTED.");
else if (tries >= 3)
System.out.println("\nYOU HAVE RUN OUT OF TRIES. ACCOUNT LOCKED.");
【问题讨论】:
【参考方案1】:Any idea why the "Wrong Pin. Try Again" shows up twice in console?
是的...在您的 BankingSystemWithPin.getBankAccountByPin() 方法中,您的 for
循环用于遍历不同的银行帐户以查找相关的密码。这对于您的特定用例来说很好,但是您确实需要摆脱 else if
代码块,原因如下:
例如,如果您有十个特定帐户,并且每个帐户都包含一个唯一的密码,并且输入的密码与第五个帐户相关,那么在每次迭代直到第五个帐户时,else if
条件都满足,因此输出 @ 987654325@。第五次迭代持有正确的 PIN,然后以return bankAccount;
退出该方法。您将在控制台窗口中看到的是:
Wrong Pin. Try Again.
Wrong Pin. Try Again.
Wrong Pin. Try Again.
Wrong Pin. Try Again.
Enter an option:
A. Check balance
B. Deposit
C. Withdraw
X. Exit
您不希望 else if
循环中的 else if
代码块,所以只需删除它。相反,将以下代码行直接放在之后 for for
循环块:
System.out.println("Wrong Pin. Try Again.");
请记住,因为您无论如何都会在for
循环中退出该方法,如果可以找到与银行帐户相关的正确密码,则上述行将永远到达。只有在找不到提供的 PIN 码时才能访问。您的 getBankAccountByPin() 方法应如下所示:
public static BankAccount getBankAccountByPin(BankAccount[] bankAccounts, String pin)
for (BankAccount bankAccount : bankAccounts)
if (bankAccount.pin.equals(pin))
System.out.println();
System.out.println("Account Number: " + bankAccount.IBAN);
return bankAccount;
System.err.println("Wrong PIN. Try Again.");
System.err.println();
return null;
这还不是全部......在调用上述方法后继续之前,您需要确保它确实提供了银行帐号。毕竟,如果提供的密码错误,则不会有银行帐号,而是会返回 null 并且您不想继续使用 null。在显示菜单之前,您需要验证确实获得了银行帐号。为此,您需要在 while
循环中调用 getBankAccountByPin() 方法,如下所示:
import java.util.Scanner;
public class BankingSystemWithPin
private static int pinAttempts = 0;
public static void main(String[] args)
BankAccount bankAccount1 = new BankAccount("RO29455302311322", 200, "1234");
BankAccount bankAccount2 = new BankAccount("RO43593530521134", 600, "7530");
BankAccount[] bankAccounts = bankAccount1, bankAccount2;
Scanner scanner = new Scanner(System.in);
BankAccount currentBankAccount = null;
while (currentBankAccount == null)
pinAttempts++;
if (pinAttempts > 3)
System.err.println();
System.err.println("You have exceeded the allowable number of login attempts!");
System.err.println("The transaction is over. ");
System.exit(0);
Menu.welcome();
String pin = scanner.nextLine();
currentBankAccount = BankingSystemWithPin.getBankAccountByPin(bankAccounts, pin);
// PIN correct and account number acquired!
ATM.useATM(currentBankAccount);
public static BankAccount getBankAccountByPin(BankAccount[] bankAccounts, String pin)
for (BankAccount bankAccount : bankAccounts)
if (bankAccount.pin.equals(pin))
System.out.println();
System.out.println("Account Number: " + bankAccount.IBAN);
return bankAccount;
System.err.println("Wrong Pin. Try Again.");
System.err.println();
return null;
现在,用于锁定用户。您怎么知道要锁定哪个用户?无法锁定用户,原因很简单,因为不涉及 ATM 卡,您永远不知道该锁定谁。但是,您可以关闭应用程序并强制用户重新启动它,以便在三次尝试失败后重试。
这显然最好在您尝试验证 PIN 并获取与其相关的银行帐号时完成...... BankingSystemWithPin 类。上面的代码还通过添加一个名为 pinAttempts 的私有整数类成员变量并将对 getBankAccountByPin() 方法的调用包装在 while
循环中来演示这一点。这使事情变得紧凑和简单。
【讨论】:
以上是关于Java - ATM银行登录3次尝试密码锁定的主要内容,如果未能解决你的问题,请参考以下文章