Untitled
unknown
plain_text
a year ago
8.0 kB
9
Indexable
import java.util.ArrayList;
import java.util.Scanner;
import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
// 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;
}
}
// GUI for Sign In Screen
class SignInScreen {
private JFrame frame;
public SignInScreen(ArrayList<User> users) {
frame = new JFrame("Sign In");
frame.setSize(300, 200);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
JPanel panel = new JPanel();
frame.add(panel);
placeComponents(panel, users);
frame.setVisible(true);
}
private void placeComponents(JPanel panel, ArrayList<User> users) {
panel.setLayout(null);
JLabel userLabel = new JLabel("Username:");
userLabel.setBounds(10, 20, 80, 25);
panel.add(userLabel);
JTextField userText = new JTextField(20);
userText.setBounds(100, 20, 165, 25);
panel.add(userText);
JLabel passwordLabel = new JLabel("Password:");
passwordLabel.setBounds(10, 50, 80, 25);
panel.add(passwordLabel);
JPasswordField passwordText = new JPasswordField(20);
passwordText.setBounds(100, 50, 165, 25);
panel.add(passwordText);
JLabel confirmLabel = new JLabel("Confirm:");
confirmLabel.setBounds(10, 80, 80, 25);
panel.add(confirmLabel);
JPasswordField confirmText = new JPasswordField(20);
confirmText.setBounds(100, 80, 165, 25);
panel.add(confirmText);
JButton registerButton = new JButton("Sign In");
registerButton.setBounds(10, 110, 150, 25);
panel.add(registerButton);
registerButton.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
String username = userText.getText();
String password = new String(passwordText.getPassword());
String confirmPassword = new String(confirmText.getPassword());
if (!password.equals(confirmPassword)) {
JOptionPane.showMessageDialog(frame, "Passwords do not match.", "Error", JOptionPane.ERROR_MESSAGE);
return;
}
if (!validatePassword(password)) {
JOptionPane.showMessageDialog(frame, "Password must be 6+ characters with uppercase, lowercase, and a special character.", "Error", JOptionPane.ERROR_MESSAGE);
return;
}
users.add(new User(username, password));
JOptionPane.showMessageDialog(frame, "User created successfully!", "Success", JOptionPane.INFORMATION_MESSAGE);
frame.dispose();
new LoginScreen(users);
}
});
}
private 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;
}
}
// GUI for Login Screen
class LoginScreen {
private JFrame frame;
public LoginScreen(ArrayList<User> users) {
frame = new JFrame("Log In");
frame.setSize(300, 200);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
JPanel panel = new JPanel();
frame.add(panel);
placeComponents(panel, users);
frame.setVisible(true);
}
private void placeComponents(JPanel panel, ArrayList<User> users) {
panel.setLayout(null);
JLabel userLabel = new JLabel("Username:");
userLabel.setBounds(10, 20, 80, 25);
panel.add(userLabel);
JTextField userText = new JTextField(20);
userText.setBounds(100, 20, 165, 25);
panel.add(userText);
JLabel passwordLabel = new JLabel("Password:");
passwordLabel.setBounds(10, 50, 80, 25);
panel.add(passwordLabel);
JPasswordField passwordText = new JPasswordField(20);
passwordText.setBounds(100, 50, 165, 25);
panel.add(passwordText);
JButton loginButton = new JButton("Log In");
loginButton.setBounds(10, 80, 150, 25);
panel.add(loginButton);
loginButton.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
String username = userText.getText();
String password = new String(passwordText.getPassword());
for (User user : users) {
if (user.getUsername().equals(username) && user.checkPassword(password)) {
JOptionPane.showMessageDialog(frame, "Login successful!", "Success", JOptionPane.INFORMATION_MESSAGE);
frame.dispose();
new UserScreen(username);
return;
}
}
JOptionPane.showMessageDialog(frame, "Invalid username or password.", "Error", JOptionPane.ERROR_MESSAGE);
}
});
}
}
// GUI for User Screen
class UserScreen {
private JFrame frame;
public UserScreen(String username) {
frame = new JFrame("Welcome, " + username);
frame.setSize(400, 300);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
JPanel panel = new JPanel();
frame.add(panel);
placeComponents(panel);
frame.setVisible(true);
}
private void placeComponents(JPanel panel) {
panel.setLayout(null);
JLabel welcomeLabel = new JLabel("Welcome to your notes, " + "User!");
welcomeLabel.setBounds(10, 20, 300, 25);
panel.add(welcomeLabel);
JButton manageNotesButton = new JButton("Manage Notes");
manageNotesButton.setBounds(10, 60, 150, 25);
panel.add(manageNotesButton);
manageNotesButton.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
// Placeholder for managing notes
JOptionPane.showMessageDialog(frame, "Manage notes functionality here.", "Info", JOptionPane.INFORMATION_MESSAGE);
}
});
JButton logoutButton = new JButton("Logout");
logoutButton.setBounds(10, 100, 150, 25);
panel.add(logoutButton);
logoutButton.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
frame.dispose();
new LoginScreen(NoteTakingApp.users);
}
});
}
}
// Main application class
public class NoteTakingApp {
public static ArrayList<User> users = new ArrayList<>();
public static void main(String[] args) {
new SignInScreen(users);
}
}
Editor is loading...
Leave a Comment