Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.util.ArrayList;
- import java.util.List;
- import java.util.Scanner;
- class User {
- private String email;
- private String password;
- private String username;
- public User(String email, String password, String username) {
- this.email = email;
- this.password = password;
- this.username = username;
- }
- public String getEmail() {
- return email;
- }
- public String getPassword() {
- return password;
- }
- public String getUsername() {
- return username;
- }
- }
- class UserManager {
- private List<User> users = new ArrayList<>();
- public void registerUser(String email, String password, String username) {
- users.add(new User(email, password, username));
- System.out.println("Registration successful!");
- }
- public User loginUser(String email, String password) {
- for (User user : users) {
- if (user.getEmail().equals(email) && user.getPassword().equals(password)) {
- return user;
- }
- }
- return null; // User not found
- }
- }
- public class UserRegistrationApp {
- public static void main(String[] args) {
- UserManager userManager = new UserManager();
- Scanner scanner = new Scanner(System.in);
- while (true) {
- System.out.println("1. Register");
- System.out.println("2. Login");
- System.out.println("3. Exit");
- System.out.print("Choose an option: ");
- int choice = scanner.nextInt();
- scanner.nextLine(); // Consume the newline character
- if (choice == 1) {
- System.out.print("Enter your email: ");
- String email = scanner.nextLine();
- System.out.print("Enter your password: ");
- String password = scanner.nextLine();
- System.out.print("Enter your username: ");
- String username = scanner.nextLine();
- userManager.registerUser(email, password, username);
- } else if (choice == 2) {
- System.out.print("Enter your email: ");
- String email = scanner.nextLine();
- System.out.print("Enter your password: ");
- String password = scanner.nextLine();
- User user = userManager.loginUser(email, password);
- if (user != null) {
- System.out.println("Welcome, " + user.getUsername() + "!");
- // You can display the logged-in page here.
- } else {
- System.out.println("Invalid email or password. Please try again.");
- }
- } else if (choice == 3) {
- break;
- } else {
- System.out.println("Invalid choice. Please try again.");
- }
- }
- scanner.close();
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement