-
While LOOP
i wrote this piece of code
Code:
public class CheckBankAccount{
public static void main(String[] argv) {
int pinNo, userTries, counter;
float cashAmount;
// Start of an endless loop since cashpoints never sleep.
do {
// Users name should appear
BankAccount b = new BankAccount("James",1234);
System.out.println(b.getName());
// set the try counter to zero
counter = 0;
// loop to check max. 3 times for correct PIN starts here.
do {
//ask user for PIN.
System.out.println("Please enter your PIN");
// user enters PIN into the pinNo variable.
pinNo = UserInput.readInt();
if (pinNo == 9999){
System.out.println ("bye");
System.exit(0);
}
// increment counter for tries
++ counter;
// end of the 'do' loop. Use a while here that checks if the PIN is incorrect AND
// the counter for tries is smaller than 3
} while( (pinNo != 1234) && (counter < 3) );
// this part of the program only executes if the counter for tries is smaller than 3
// Use an if statement here that checks that.
if (counter < 3) {
// Ask how much the user wants to withdraw.
System.out.println("How much do you want to withdraw?");
// User enters amount.
cashAmount = UserInput.readFloat();
// Print message confirming transaction: "You have withdrawn xxx Pounds"
System.out.println(" has withdrawn " +cashAmount+ " Pounds");
// end of the if statement
}
// end of the endless 'do'-'while' loop.
} while (true);
} // end of main
} // end class
I’m not sure on how to approach this problem
if the user inputs the wrong PIN this message should appear
"Invalid PIN, please try again"
if the user inputs the PIN incorrect 3 times this message should appear.
"3 x invalid PIN: aborting"
and exit the program using the System.exit(0) function
any ideas???
-
public class CheckBankAccount
{
public static void main(String[] argv)
{
int pinNo, userTries, counter;
float cashAmount;
// Start of an endless loop since cashpoints never sleep.
do
{
// Users name should appear
BankAccount b = new BankAccount("James",1234);
System.out.println(b.getName());
int invalidPinCount = 0;
pinNo = getPin();
boolean isValidPin = isValidPin(pinNo);
while (! isValidPin)
{
invalidPinCount++;
if (invalidPnCount > 3)
{
System.out.println("3 x invalid PIN: aborting" );
System.exit(0);
}
System.out.println("Invalid PIN, please try again" );
pinNo = getPin();
isValidPin = isValidPin(pinNo);
}
if (pinNo == 9999)
{
System.out.println ("bye");
System.exit(0);
}
// Ask how much the user wants to withdraw.
System.out.println("How much do you want to withdraw?");
// User enters amount.
cashAmount = UserInput.readFloat();
// Print message confirming transaction: "You have withdrawn xxx Pounds"
System.out.println(" has withdrawn " +cashAmount+ " Pounds");
// end of the endless 'do'-'while' loop.
} while (true);
}
public int getPin()
{
//ask user for PIN.
System.out.println("Please enter your PIN");
// user enters PIN into the pinNo variable.
pinNo = UserInput.readInt();
if (pinNo == 9999)
{
System.out.println ("bye");
System.exit(0);
}
}
public bool isValidPin(int pin)
{
// validate pin here & return true or false
return true;
}
} // end class