-3

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......

1
  • 2
    what have you tried so far? Typically a question here includes what you have tried and some sample code that you have written. If you want someone to give you code that works - you probably won't get much positive responses.
    – Mike Weber
    Apr 17, 2014 at 23:22

3 Answers 3

1
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).

5
  • I haven't looked at java for years - it sure has evolved a lot.
    – Mike Weber
    Apr 17, 2014 at 23:25
  • @MikeWeber This isn't Java, it's obviously pseudocode.
    – Jared
    Apr 17, 2014 at 23:26
  • 2
    I thought this was pretty good for giving a direction to go without just handing the OP the code.
    – jmoerdyk
    Apr 17, 2014 at 23:26
  • 1
    Yes, 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.
    – Mike Weber
    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).
    – Jared
    Apr 18, 2014 at 1:28
0
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.

-1

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");
}
1
  • 1
    Also, please, if you're going to put in code, at least format it nicely.
    – Shadow Man
    Apr 19, 2014 at 1:11

Your Answer

Reminder: Answers generated by Artificial Intelligence tools are not allowed on Stack Overflow. Learn more

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.