Guest User

Untitled

a guest
Mar 1st, 2018
67
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 6.18 KB | None | 0 0
  1. import java.io.BufferedReader;
  2. import java.io.InputStreamReader;
  3. import java.security.MessageDigest;
  4. import java.util.HashMap;
  5. import java.util.Map;
  6.  
  7. class User {
  8.   // fucntion to check if user is valid or not
  9.     public int isUserValid(String username, String password) throws Exception
  10.     {
  11.  
  12.         int validId = 0;
  13.        
  14.       // <userName,hash password>
  15.       Map<String, String> credentials = new HashMap<>();
  16.           credentials.put("griffin.keyes", "93f725a07423fe1c889f448b33d21f46");   // password == java
  17.           credentials.put("rosario.dawson", "2bff04a9b5f68427573d086983df5eed");  // password  == pizza
  18.           credentials.put("bernie.gorilla", "1801bc89e752077204c92b3dd9f9f62d");  // password  == dew
  19.           credentials.put("donald.monkey", "23eeeb4347bdd26bfc6b7ee9a3b755dd");     // password  == python
  20.           credentials.put("jerome.grizzlybear",    "fc35fdc70d5fc69d269883a822c7a53e");     // password  == html
  21.           credentials.put("bruce.grizzlybear", "e77989ed21758e78331b20e477fc5582");         // password  == dev
  22.          
  23.          
  24.           // here we are generating the md5 hash of our password
  25.           MessageDigest md = MessageDigest.getInstance("MD5");
  26.           md.update(password.getBytes());
  27.           byte[] digest = md.digest();
  28.            
  29.           StringBuffer passward_hash = new StringBuffer();
  30.           for (byte b : digest) {
  31.               passward_hash.append(String.format("%02x", b & 0xff));
  32.           }
  33.  
  34.          
  35.           // we are iterating through the hash map and comparing the input passward and username with saved ones
  36.           // by using for each loop
  37.           for(Map.Entry it : credentials.entrySet())
  38.         {
  39.               // if password and username match with the password and username in hash map return the id of hashmap          
  40.               if(username.contentEquals(it.getKey().toString()) && passward_hash.toString().contentEquals(it.getValue().toString()))
  41.               {
  42.                   return validId;
  43.               }
  44.              
  45.               validId ++;
  46.  
  47.         }
  48.           // if not match return -1
  49.  
  50.       return -1;
  51.     }
  52.    
  53.    
  54.     // this function shows diffrent msg according to diffrent user
  55.     public void prineWelcomeMsg(int id)
  56.     {
  57.         switch(id)
  58.         {
  59.         case 0:
  60.         {
  61.              System.out.println();
  62.              
  63.              
  64.              System.out.println("Hello, Zookeeper!\n" +
  65.                      "\n" + "As zookeeper, you have access to all of the"
  66.                      + " animals'" + " information and their" + " daily"
  67.                      + " monitoring logs. This " + "allows you to track their "
  68.                      + "feeding habits, habitat conditions, and general welfare.");
  69.              System.out.println();
  70.              break;
  71.         }
  72.        
  73.        
  74.        
  75.         case 1:
  76.         {
  77.             System.out.println();
  78.            
  79.             System.out.println("Hello, Zookeeper!\n" +
  80.                     "\n" + "As zookeeper, you have access to all of the"
  81.                     + " animals'" + " information and their" + " daily"
  82.                     + " monitoring logs. This " + "allows you to track their "
  83.                     + "feeding habits, habitat conditions, and general welfare.");
  84.             System.out.println();
  85.             break;
  86.            
  87.         }
  88.        
  89.         case 2:
  90.         {
  91.            
  92.             System.out.println();
  93.            
  94.             System.out.println("Hello, Veterinarian!\n" + "\n" + "As "
  95.                     + "veterinarian, you have access to all of the animals'"
  96.                     + " health records. This allows you to view each animal's "
  97.                     + "medical history and current treatments/illnesses "
  98.                     + "(if any), and to maintain a vaccination log.");
  99.             System.out.println();
  100.            
  101.            
  102.             break ;
  103.         }
  104.        
  105.        
  106.        
  107.         case 3:
  108.         {
  109.            
  110.            
  111.               System.out.println();
  112.              
  113.               System.out.println("Hello, Veterinarian!\n" + "\n" + "As "
  114.                       + "veterinarian, you have access to all of the animals'"
  115.                       + " health records. This allows you to view each animal's "
  116.                       + "medical history and current treatments/illnesses "
  117.                       + "(if any), and to maintain a vaccination log.");
  118.               System.out.println();
  119.            
  120.            
  121.             break;
  122.         }
  123.        
  124.  
  125.        
  126.         case 4:
  127.            
  128.         {
  129.            
  130.          System.out.println();
  131.              
  132.               System.out.println("Hello, System Admin!\n" + "\n" + "As"
  133.                       + " administrator, you have access to the zoo's main "
  134.                       + "computer system.  This allows you to monitor users in "
  135.                       + "the system and their roles.");
  136.            
  137.             break;
  138.         }
  139.        
  140.         case 5:
  141.         {
  142.               System.out.println();
  143.              
  144.               System.out.println("Hello, System Admin!\n" + "\n" + "As"
  145.                       + " administrator, you have access to the zoo's main "
  146.                       + "computer system.  This allows you to monitor users in "
  147.                       + "the system and their roles.");
  148.               break;
  149.         }
  150.        
  151.        
  152.        
  153.         default :
  154.         {
  155.             // empty or fill anything
  156.         }
  157.        
  158.    
  159.        
  160.         }
  161.     }
  162. }
  163.  
  164.  
  165.  
  166.  
  167.  
  168. public class Authentication {
  169.  
  170.     public static void main(String[] args) throws Exception {
  171.      
  172.         BufferedReader scnr = new BufferedReader(new InputStreamReader(System.in));
  173.         String userName = null;
  174.         String usersType [] = {"zookeeper","zookeeper1","veterinarian","veterinarian1","Admin","Admin1"};
  175.  
  176.         User userObj = new User()
  177.                 ;
  178.         int isValidId = -1;
  179.        
  180.     while(isValidId == -1)
  181.     {
  182.         System.out.println("Enter username:");
  183.         userName = scnr.readLine();
  184.         System.out.println("Enter password:");
  185.         String password1 = scnr.readLine();
  186.        
  187.        
  188.        
  189.         isValidId     = userObj.isUserValid(userName, password1) ;
  190.        
  191.        
  192.         if (isValidId != -1 )    
  193.         {
  194.             System.out.println("Valid User");
  195.             userObj.prineWelcomeMsg(isValidId);
  196.         }
  197.        
  198.         else
  199.             System.out.println("InValid User");
  200.  
  201.  
  202.     }
  203.     }
  204. }
Add Comment
Please, Sign In to add comment