Advertisement
Guest User

Untitled

a guest
Oct 13th, 2016
85
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.21 KB | None | 0 0
  1. import java.util.*;
  2. public class LoginPrototype {
  3.    
  4.     static Scanner kbd = new Scanner(System.in);
  5.     static Credentials user;
  6.     static ArrayList<Credentials> allUsers = new ArrayList<Credentials>();
  7.  
  8.     public static void main(String[] args) {
  9.        
  10.        
  11.         displayMenu();
  12.        
  13.    
  14.  
  15.     }
  16.    
  17.    
  18.     public static void displayMenu(){
  19.        
  20.         int choice;
  21.        
  22.             do{
  23.         System.out.println("Main menu");
  24.         System.out.println("[1]Login");
  25.         System.out.println("[2]Register");
  26.         System.out.println("[0]Exit");
  27.        
  28.         System.out.print("Please select: ");
  29.         choice = kbd.nextInt();
  30.        
  31.         switch(choice){
  32.        
  33.         case 1: System.out.println("Loging in"); handleLogin();allList();break;
  34.         case 2: System.out.println("Registering");register();allList(); break;
  35.         case 0: System.out.println("Closing app"); break;
  36.         default : System.out.println("Wrong number");
  37.         }
  38.             }while(choice !=0);
  39.        
  40.     }
  41.    
  42.     public static Credentials handleCredentialInput(){
  43.        
  44.         String username;
  45.         String password;
  46.        
  47.         System.out.print("Please enter username");
  48.         username= kbd.next();
  49.         System.out.print("Please enter password");
  50.         password=kbd.next();
  51.        
  52.         user= new Credentials(username,password);
  53.        
  54.         return user;
  55.                
  56.    
  57.    
  58.     }
  59.      
  60.  
  61. private static  void register(){
  62.        
  63.        
  64.     Credentials  c=handleCredentialInput();
  65.    
  66.     boolean doNotExist=false;
  67.      for (int i=0; i< allUsers.size() ;i++)
  68.      {
  69.            
  70.          if(c.equals(allUsers.get(i)))
  71.         {
  72.          
  73.         System.out.println();
  74.         System.out.println("User already exist");
  75.         doNotExist =true;
  76.        
  77.         }
  78.            
  79.    
  80.         }
  81.      if(!doNotExist)
  82.         {
  83.            
  84.        
  85.         System.out.println("Added");
  86.         allUsers.add(c);
  87.        
  88.         }
  89.    
  90.      
  91. }
  92. private static void allList(){
  93.     for(Credentials c : allUsers)
  94.     {
  95.         System.out.println(c.getUsername());
  96.     }
  97.    
  98. }
  99.  
  100. private static void handleLogin(){
  101.    
  102.     Credentials c = handleCredentialInput();
  103.     if(allUsers.contains(c))
  104.     {
  105.         Credentials c1= allUsers.get(allUsers.indexOf(c));
  106.        
  107.         if(c1.getPassword().equals(c.getPassword()))
  108.         {
  109.             System.out.println("Correct password");
  110.         }
  111.         else
  112.             System.out.println("wrong password");
  113.     }
  114.    
  115.    
  116. }
  117.  
  118.  
  119. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement