Advertisement
newbninja

IMEE Simple Java Program Example

Sep 8th, 2023
1,147
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.90 KB | None | 0 0
  1. import java.util.ArrayList;
  2. import java.util.List;
  3. import java.util.Scanner;
  4.  
  5. class User {
  6.     private String email;
  7.     private String password;
  8.     private String username;
  9.  
  10.     public User(String email, String password, String username) {
  11.         this.email = email;
  12.         this.password = password;
  13.         this.username = username;
  14.     }
  15.  
  16.     public String getEmail() {
  17.         return email;
  18.     }
  19.  
  20.     public String getPassword() {
  21.         return password;
  22.     }
  23.  
  24.     public String getUsername() {
  25.         return username;
  26.     }
  27. }
  28.  
  29. class UserManager {
  30.     private List<User> users = new ArrayList<>();
  31.  
  32.     public void registerUser(String email, String password, String username) {
  33.         users.add(new User(email, password, username));
  34.         System.out.println("Registration successful!");
  35.     }
  36.  
  37.     public User loginUser(String email, String password) {
  38.         for (User user : users) {
  39.             if (user.getEmail().equals(email) && user.getPassword().equals(password)) {
  40.                 return user;
  41.             }
  42.         }
  43.         return null; // User not found
  44.     }
  45. }
  46.  
  47. public class UserRegistrationApp {
  48.     public static void main(String[] args) {
  49.         UserManager userManager = new UserManager();
  50.         Scanner scanner = new Scanner(System.in);
  51.  
  52.         while (true) {
  53.             System.out.println("1. Register");
  54.             System.out.println("2. Login");
  55.             System.out.println("3. Exit");
  56.             System.out.print("Choose an option: ");
  57.             int choice = scanner.nextInt();
  58.             scanner.nextLine(); // Consume the newline character
  59.  
  60.             if (choice == 1) {
  61.                 System.out.print("Enter your email: ");
  62.                 String email = scanner.nextLine();
  63.                 System.out.print("Enter your password: ");
  64.                 String password = scanner.nextLine();
  65.                 System.out.print("Enter your username: ");
  66.                 String username = scanner.nextLine();
  67.                 userManager.registerUser(email, password, username);
  68.             } else if (choice == 2) {
  69.                 System.out.print("Enter your email: ");
  70.                 String email = scanner.nextLine();
  71.                 System.out.print("Enter your password: ");
  72.                 String password = scanner.nextLine();
  73.                 User user = userManager.loginUser(email, password);
  74.                 if (user != null) {
  75.                     System.out.println("Welcome, " + user.getUsername() + "!");
  76.                     // You can display the logged-in page here.
  77.                 } else {
  78.                     System.out.println("Invalid email or password. Please try again.");
  79.                 }
  80.             } else if (choice == 3) {
  81.                 break;
  82.             } else {
  83.                 System.out.println("Invalid choice. Please try again.");
  84.             }
  85.         }
  86.        
  87.         scanner.close();
  88.     }
  89. }
  90.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement