Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.util.*;
- public class LoginPrototype {
- static Scanner kbd = new Scanner(System.in);
- static Credentials user;
- static ArrayList<Credentials> allUsers = new ArrayList<Credentials>();
- public static void main(String[] args) {
- displayMenu();
- }
- public static void displayMenu(){
- int choice;
- do{
- System.out.println("Main menu");
- System.out.println("[1]Login");
- System.out.println("[2]Register");
- System.out.println("[0]Exit");
- System.out.print("Please select: ");
- choice = kbd.nextInt();
- switch(choice){
- case 1: System.out.println("Loging in"); handleLogin();allList();break;
- case 2: System.out.println("Registering");register();allList(); break;
- case 0: System.out.println("Closing app"); break;
- default : System.out.println("Wrong number");
- }
- }while(choice !=0);
- }
- public static Credentials handleCredentialInput(){
- String username;
- String password;
- System.out.print("Please enter username");
- username= kbd.next();
- System.out.print("Please enter password");
- password=kbd.next();
- user= new Credentials(username,password);
- return user;
- }
- private static void register(){
- Credentials c=handleCredentialInput();
- boolean doNotExist=false;
- for (int i=0; i< allUsers.size() ;i++)
- {
- if(c.equals(allUsers.get(i)))
- {
- System.out.println();
- System.out.println("User already exist");
- doNotExist =true;
- }
- }
- if(!doNotExist)
- {
- System.out.println("Added");
- allUsers.add(c);
- }
- }
- private static void allList(){
- for(Credentials c : allUsers)
- {
- System.out.println(c.getUsername());
- }
- }
- private static void handleLogin(){
- Credentials c = handleCredentialInput();
- if(allUsers.contains(c))
- {
- Credentials c1= allUsers.get(allUsers.indexOf(c));
- if(c1.getPassword().equals(c.getPassword()))
- {
- System.out.println("Correct password");
- }
- else
- System.out.println("wrong password");
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement