What Command in java that exits after the third time of entering the the wrong p
ID: 3712369 • Letter: W
Question
What Command in java that exits after the third time of entering the the wrong password in an atm gui bank account here is the code
public boolean checkCreadentials(String accNumber, int accPassowrd) {
boolean check = false;
for (account Account : accounts) {
if ((Account.getAccountNumber().equals(accNumber)) && (Account.getPassowrd() == accPassowrd)) {
check = true;
currentAccount = Account;
}
}
return check;
}
public double getBalance() {
return currentAccount.getBalance();
}
public void setBalance(Double balance) {
currentAccount.setBalance(balance);
}
public void withdraw(double Amount) {
double currentBalance = getBalance();
currentBalance -= Amount;
setBalance(currentBalance);
}
public void deposit(double Amount) {
double currentBalance = getBalance();
currentBalance += Amount;
setBalance(currentBalance);
}
public String getAccountNumber() {
return currentAccount.getAccountNumber();
}
public String getAccountHoldersName() {
return currentAccount.getAccountName();
}
}
Explanation / Answer
The command to be used to terminate the program is System.exit();
The method checkCreadentials is to be called in a loop from the main() method. Objects of account class have to be created in the main() method.
A counter integer variable has to be created and incremented every time Account.checkCreadentials returns check as false. The counter variable is initialised to 0.
The psuedocode is as follows:
class A1 {
public static void main(string args[]) {
account Account = new account();
boolean c = false;
int counter = 0;
while(!c) { //Loop runs till check returned by checkCreadentials() //becomes true
c = Account.checkCreadentials(abc, 123); //c is the value of check returned by checkCreadentials()
if(c==false)
counter++; //counter is incremented whenever c is false because of //wrong password entered
if(counter==3)
System.exit(0); //Program exits as soon as value of counter becomes 3
}
}
}
Note: It is a Java convention that class name should start with a Capital letter. (Class name given here is account)
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.