Untitled
import java.util.ArrayList; import java.util.Scanner; // User class class User { private String username; private String password; public User(String username, String password) { this.username = username; this.password = password; } public String getUsername() { return username; } public boolean checkPassword(String password) { return this.password.equals(password); } } // Note class class Note { private String content; private boolean isSecure; public Note(String content, boolean isSecure) { this.content = content; this.isSecure = isSecure; } public String getContent() { return content; } public void setContent(String content) { this.content = content; } public boolean isSecure() { return isSecure; } @Override public String toString() { return (isSecure ? "[Secured] " : "") + content; } } // Main application class public class NoteTakingApp { private static ArrayList<User> users = new ArrayList<>(); private static User loggedInUser = null; private static ArrayList<Note> notes = new ArrayList<>(); public static void main(String[] args) { Scanner scanner = new Scanner(System.in); while (true) { System.out.println("1. Sign In"); System.out.println("2. Log In"); System.out.println("3. Exit"); System.out.print("Choose an option: "); int choice = scanner.nextInt(); scanner.nextLine(); // Consume newline switch (choice) { case 1: signIn(scanner); break; case 2: login(scanner); break; case 3: System.out.println("Exiting..."); return; default: System.out.println("Invalid option. Try again."); } } } private static void signIn(Scanner scanner) { System.out.print("Enter username: "); String username = scanner.nextLine(); System.out.print("Enter password: "); String password = scanner.nextLine(); System.out.print("Confirm password: "); String confirmPassword = scanner.nextLine(); if (!password.equals(confirmPassword)) { System.out.println("Passwords do not match."); return; } if (!validatePassword(password)) { System.out.println("Password must be 6+ characters with uppercase, lowercase, and a special character."); return; } users.add(new User(username, password)); System.out.println("User created successfully!"); } private static void login(Scanner scanner) { System.out.print("Enter username: "); String username = scanner.nextLine(); System.out.print("Enter password: "); String password = scanner.nextLine(); for (User user : users) { if (user.getUsername().equals(username) && user.checkPassword(password)) { loggedInUser = user; System.out.println("Login successful!"); manageNotes(scanner); return; } } System.out.println("Invalid username or password."); } private static void manageNotes(Scanner scanner) { while (true) { System.out.println("1. Add Note"); System.out.println("2. View Notes"); System.out.println("3. Update Note"); System.out.println("4. Delete Note"); System.out.println("5. Logout"); System.out.print("Choose an option: "); int choice = scanner.nextInt(); scanner.nextLine(); // Consume newline switch (choice) { case 1: System.out.print("Enter your note: "); String content = scanner.nextLine(); System.out.print("Is this note secure? (yes/no): "); boolean isSecure = scanner.nextLine().equalsIgnoreCase("yes"); notes.add(new Note(content, isSecure)); System.out.println("Note added successfully!"); break; case 2: System.out.println("Your Notes:"); for (int i = 0; i < notes.size(); i++) { System.out.println((i + 1) + ". " + notes.get(i)); } break; case 3: System.out.print("Enter the note number to update: "); int updateIndex = scanner.nextInt() - 1; scanner.nextLine(); // Consume newline if (updateIndex >= 0 && updateIndex < notes.size()) { System.out.print("Enter the new content: "); String newContent = scanner.nextLine(); notes.get(updateIndex).setContent(newContent); System.out.println("Note updated successfully!"); } else { System.out.println("Invalid note number."); } break; case 4: System.out.print("Enter the note number to delete: "); int deleteIndex = scanner.nextInt() - 1; scanner.nextLine(); // Consume newline if (deleteIndex >= 0 && deleteIndex < notes.size()) { notes.remove(deleteIndex); System.out.println("Note deleted successfully!"); } else { System.out.println("Invalid note number."); } break; case 5: System.out.println("Logged out."); loggedInUser = null; return; default: System.out.println("Invalid option. Try again."); } } } private static boolean validatePassword(String password) { if (password.length() < 6) return false; boolean hasUppercase = false; boolean hasLowercase = false; boolean hasSpecial = false; for (char c : password.toCharArray()) { if (Character.isUpperCase(c)) hasUppercase = true; else if (Character.isLowerCase(c)) hasLowercase = true; else if (!Character.isLetterOrDigit(c)) hasSpecial = true; } return hasUppercase && hasLowercase && hasSpecial; } }
Leave a Comment