ultravibez

HashMap Menu

Nov 4th, 2017
81
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.46 KB | None | 0 0
  1. package com.company;
  2.  
  3. import java.util.HashMap;
  4. import java.util.Scanner;
  5.  
  6. public class Menu {
  7.  
  8.     Scanner in = new Scanner(System.in);
  9.     public HashMap<String, User> users = new HashMap<>();
  10.  
  11.     void signUp() {
  12.         String username = "";
  13.         String password = "";
  14.         while (username.length() <= 0) {
  15.             System.out.println("Please enter your Username:");
  16.             username = in.nextLine();
  17.             if (users.containsKey(username)) {
  18.                 System.out.println("This username already exist");
  19.                 username = "";
  20.             }
  21.         }
  22.         while (password.length() <= 0) {
  23.             System.out.println("Please enter your Password");
  24.             password = in.nextLine();
  25.         }
  26.         User u = new User(username, password);
  27.         users.put(u.getUsername(), u);
  28.     }
  29.  
  30.     User login() {
  31.         String username = "";
  32.         String password = "";
  33.         int counter = 0;
  34.         int counter2 = 0;
  35.         while (!users.containsKey(username)) {
  36.             System.out.print("Username: ");
  37.             username = in.nextLine();
  38.             if (!users.containsKey(username))
  39.                 System.out.println("Username does not exist");
  40.             counter++;
  41.             if (counter == 4) {
  42.                 return null;
  43.             }
  44.         }
  45.         while (password.length() <= 0) {
  46.             System.out.print("Password: ");
  47.             password = in.nextLine();
  48.             if (!users.get(username).getPassword().equals(password)) {
  49.                 System.out.println("Username and password does not match");
  50.                 password = "";
  51.                 counter++;
  52.                 if (counter2 == 4) {
  53.                     return null;
  54.                 }
  55.             }
  56.         }
  57.         System.out.println("Successfully logged in!");
  58.         return users.get(username);
  59.     }
  60.  
  61.     void changePassword(User user){
  62.         String password = "";
  63.         while(true) {
  64.             System.out.println("Please enter your current Password:");
  65.             password = in.nextLine();
  66.             if (!password.equals(user.getPassword())) {
  67.                 System.out.println("Incorrect password");
  68.             } else {
  69.                 break;
  70.             }
  71.         }
  72.         password = "";
  73.         while (password.length() <= 0) {
  74.             System.out.println("Please enter your new Password");
  75.             password = in.nextLine();
  76.         }
  77.         user.setPassword(password);
  78.     }
  79. }
Add Comment
Please, Sign In to add comment