Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package com.company;
- import java.util.HashMap;
- import java.util.Scanner;
- public class Menu {
- Scanner in = new Scanner(System.in);
- public HashMap<String, User> users = new HashMap<>();
- void signUp() {
- String username = "";
- String password = "";
- while (username.length() <= 0) {
- System.out.println("Please enter your Username:");
- username = in.nextLine();
- if (users.containsKey(username)) {
- System.out.println("This username already exist");
- username = "";
- }
- }
- while (password.length() <= 0) {
- System.out.println("Please enter your Password");
- password = in.nextLine();
- }
- User u = new User(username, password);
- users.put(u.getUsername(), u);
- }
- User login() {
- String username = "";
- String password = "";
- int counter = 0;
- int counter2 = 0;
- while (!users.containsKey(username)) {
- System.out.print("Username: ");
- username = in.nextLine();
- if (!users.containsKey(username))
- System.out.println("Username does not exist");
- counter++;
- if (counter == 4) {
- return null;
- }
- }
- while (password.length() <= 0) {
- System.out.print("Password: ");
- password = in.nextLine();
- if (!users.get(username).getPassword().equals(password)) {
- System.out.println("Username and password does not match");
- password = "";
- counter++;
- if (counter2 == 4) {
- return null;
- }
- }
- }
- System.out.println("Successfully logged in!");
- return users.get(username);
- }
- void changePassword(User user){
- String password = "";
- while(true) {
- System.out.println("Please enter your current Password:");
- password = in.nextLine();
- if (!password.equals(user.getPassword())) {
- System.out.println("Incorrect password");
- } else {
- break;
- }
- }
- password = "";
- while (password.length() <= 0) {
- System.out.println("Please enter your new Password");
- password = in.nextLine();
- }
- user.setPassword(password);
- }
- }
Add Comment
Please, Sign In to add comment