I am very new to programming and need some help. I need to write a simple program that verifies an ATM users pin number. The program will either, accept the pin and exit, tell the user it was an incorrect pin and have them try again up to three times, or tell the user their card is locked because they were wrong three times. I have searched for over an hour now and cannot find an example of this. I know i will need to use a scanner and a loop to accomplish this but not much else. Any help is appreciated as it is due by midnight......
3 Answers
for i = 1..3
prompt user for pin
read pin
check pin
if pin is correct, exit
tell user they were wrong and try again
tell user they got it incorrect three times and their card is locked.
I will give one hint which can trip some newbies up. The pin is an integer, right? So you might be tempted to use Scanner.nextInt()
to get the input--do not do that! Just get the next line and compare Strings (you may have to use String.trim()
to get rid of whitespace). It's more complicated if you try to use Scanner.nextInt()
(what if the user enters something that cannot be parsed as an integer).
-
-
-
2I thought this was pretty good for giving a direction to go without just handing the OP the code.– jmoerdykApr 17, 2014 at 23:26
-
1Yes, it does cover all the necessary functionality - I was trying to have fun with the simplicity of pseudocode, and how one day, maybe, coding will actually look like that. Apr 18, 2014 at 0:34
-
@MikeWeber Oh man, you're asking a lot, lol. You can make code look like that with object oriented programming (which I have found virtually no one knows/adheres to). Still you need to write the methods/objects to do the nitty gritty--I don't think that will ever go away (it can within a certain niche, but not in general, I don't think).– JaredApr 18, 2014 at 1:28
import java.util.Arrays;
import java.io.Console;
public class atm {
public static void main(String[] args) {
int counter = 3;
int attempt = 3;
char[] ch = null;
Console c=System.console();
System.out.println("Enter PIN: ");
ch=c.readPassword();
String pass=String.valueOf(ch);
if(pass.equals("enigma")){
System.out.println("Correct PIN entered!");
}
while(!pass.equals("enigma") && attempt != 0){
System.out.println("Invalid PIN entered!. " + --attempt + " attempts remaining.");
counter--;
if(attempt != 0){
c=System.console();
System.out.println("Enter PIN: ");
ch=c.readPassword();
pass=String.valueOf(ch);
if(pass.equals("enigma")){
System.out.println("Correct PIN entered!");
}
}
else{
System.out.println("your card has locked!");
break;
}
}
}
}
Note that you can put your PIN code instead of "enigma" string, I hope that will help.
here is the implementation of pseudo code answered by @Jared above
boolean cardLockFlag = false ;
String password;
Scanner scan = new Scanner (System.in);
password = "password" ;
if(!cardLockFlag){
for(int i = 0 ; i < 3 ; i++){
if(password.equals(scan.next().trim())){
System.out.println("Success :) ");
break ;
}else{
if(i==2){
cardLockFlag = true ;
}else{
System.out.println("Wrong Password");
}
}
}
}else{
System.out.println("Card is Locked");
}
-
1Also, please, if you're going to put in code, at least format it nicely. Apr 19, 2014 at 1:11