Java大作业(一)介绍了全部功能点
一、Main类——用于启动整个程序
......代码于Java大作业(二)
二、LoginWindowWindow类——用于登录
......代码于Java大作业(二)
三、FunctionSelectionWindow类——用于选择对应的功能
......代码于Java大作业(二)
四、AccountManagementWindow类——用于管理酒店工作人员账户
import javax.swing.*;
import javax.swing.table.DefaultTableModel;
import javax.swing.table.TableCellRenderer;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.*;
import java.util.HashMap;
import java.util.Map;
public class AccountManagementWindow extends JFrame {
private JTable accountTable;//展示数据
private DefaultTableModel tableModel;//负责管理表格的数据
private JButton addAdminButton;
private JButton changePrivilegePasswordButton;
//构造函数,构建账户管理窗口
public AccountManagementWindow() {
super("账户管理");
setSize(800, 600);
setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
setLocationRelativeTo(null);
// 左侧显示酒店账户管理
JPanel leftPanel = new JPanel();
leftPanel.setLayout(new BoxLayout(leftPanel, BoxLayout.Y_AXIS));
JLabel label = new JLabel("酒店账户管理", SwingConstants.CENTER);
label.setFont(new Font("Serif", Font.BOLD, 24));
label.setAlignmentX(Component.CENTER_ALIGNMENT);
leftPanel.add(Box.createVerticalGlue());
leftPanel.add(label);
leftPanel.add(Box.createVerticalGlue());
leftPanel.setBackground(Color.LIGHT_GRAY);
// 右侧显示账户信息
JPanel rightPanel = new JPanel(new BorderLayout());
// 账户表格
tableModel = new DefaultTableModel(new Object[]{"序号", "账号", "密码", "操作1", "操作2"}, 0);
accountTable = new JTable(tableModel);
accountTable.getColumnModel().getColumn(3).setCellRenderer(new ButtonRenderer());
accountTable.getColumnModel().getColumn(3).setCellEditor(new ButtonEditor(new JCheckBox()));
accountTable.getColumnModel().getColumn(4).setCellRenderer(new ButtonRenderer());
accountTable.getColumnModel().getColumn(4).setCellEditor(new ButtonEditor(new JCheckBox()));
JScrollPane scrollPane = new JScrollPane(accountTable);
loadAccounts();
// 按钮面板
JPanel buttonPanel = new JPanel(new GridLayout(0, 1, 5, 5));
addAdminButton = new JButton("添加管理员");
changePrivilegePasswordButton = new JButton("修改权限密码");
addAdminButton.addActionListener(new AddAdminAction(this));
changePrivilegePasswordButton.addActionListener(new ChangePrivilegePasswordAction(this));
buttonPanel.add(addAdminButton);
buttonPanel.add(changePrivilegePasswordButton);
rightPanel.add(scrollPane, BorderLayout.CENTER);
rightPanel.add(buttonPanel, BorderLayout.SOUTH);
JSplitPane splitPane = new JSplitPane(JSplitPane.HORIZONTAL_SPLIT, leftPanel, rightPanel);
splitPane.setDividerLocation(200);
setContentPane(splitPane);
setVisible(true);
}
//载入账户信息
private void loadAccounts() {
tableModel.setRowCount(0);
HashMap<String, String> accounts = loadCredentials();
int index = 1;
for (Map.Entry<String, String> entry : accounts.entrySet()) {
JButton modifyButton = new JButton("修改密码");
JButton deleteButton = new JButton("删除账户");
modifyButton.addActionListener(new ModifyPasswordAction(this, index - 1));
deleteButton.addActionListener(new DeleteAccountAction(this, index - 1));
tableModel.addRow(new Object[]{
index++,
entry.getKey(),
"******",
modifyButton,
deleteButton
});
}
}
//载入凭据
private HashMap<String, String> loadCredentials() {
HashMap<String, String> credentials = new HashMap<>();
try (BufferedReader br = new BufferedReader(new FileReader("Hotel management system/src/credentials"))) {
String line;
while ((line = br.readLine()) != null) {
String[] parts = line.split(",", 2);
if (parts.length == 2) {
credentials.put(parts[0].trim(), parts[1].trim());
}
}
} catch (IOException e) {
e.printStackTrace();
}
return credentials;
}
//保存凭证
private void saveCredentials(HashMap<String, String> credentials) {
try (BufferedWriter bw = new BufferedWriter(new FileWriter("Hotel management system/src/credentials"))) {
for (Map.Entry<String, String> entry : credentials.entrySet()) {
bw.write(entry.getKey() + "," + entry.getValue());
bw.newLine();
}
} catch (IOException e) {
e.printStackTrace();
}
}
//用于加载权限密码
private String loadPrivilegePassword() {
try (BufferedReader br = new BufferedReader(new FileReader("Hotel management system/src/privilege_password"))) {
return br.readLine();
} catch (IOException e) {
e.printStackTrace();
}
return "";
}
//用于保存权限密码
private void savePrivilegePassword(String password) {
try (BufferedWriter bw = new BufferedWriter(new FileWriter("Hotel management system/src/privilege_password"))) {
bw.write(password);
} catch (IOException e) {
e.printStackTrace();
}
}
//用于获取表格单元格渲染组件,设置按钮文本并返回自身
private class ButtonRenderer extends JButton implements TableCellRenderer {
public ButtonRenderer() {
setOpaque(true);
}
@Override
public Component getTableCellRendererComponent(JTable table, Object value, boolean isSelected, boolean hasFocus, int row, int column) {
if (value instanceof JButton) {
JButton button = (JButton) value;
setText(button.getText());
}
return this;
}
}
//处理表格中按钮编辑相关逻辑,如响应点击、获取编辑值、执行对应操作等
private class ButtonEditor extends DefaultCellEditor {
private JButton button;
private String label;
private boolean isPushed;
private int row;
public ButtonEditor(JCheckBox checkBox) {
super(checkBox);
button = new JButton();
button.setOpaque(true);
button.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
fireEditingStopped();
}
});
}
@Override
public Component getTableCellEditorComponent(JTable table, Object value, boolean isSelected, int row, int column) {
if (value instanceof JButton) {
JButton button = (JButton) value;
label = button.getText();
button.setText(label);
}
this.row = row; // 保存当前行号
isPushed = true;
return button;
}
@Override
public Object getCellEditorValue() {
if (isPushed) {
if (label.equals("修改密码")) {
modifyPassword(row);
} else if (label.equals("删除账户")) {
deleteAccount(row);
}
}
isPushed = false;
return new String(label);
}
@Override
public boolean stopCellEditing() {
isPushed = false;
return super.stopCellEditing();
}
@Override
protected void fireEditingStopped() {
super.fireEditingStopped();
}
}
//用于修改密码
private void modifyPassword(int row) {
String username = (String) accountTable.getValueAt(row, 1);
String oldPassword = JOptionPane.showInputDialog(this, "请输入原密码:", "", JOptionPane.PLAIN_MESSAGE);
if (oldPassword == null) {
return; // 用户取消输入
}
HashMap<String, String> credentials = loadCredentials();
if (oldPassword.equals(credentials.get(username))) {
String newPassword = JOptionPane.showInputDialog(this, "请输入新密码:", "", JOptionPane.PLAIN_MESSAGE);
while (newPassword != null && newPassword.equals(oldPassword)) {
JOptionPane.showMessageDialog(this, "新密码与原密码一致,请重新输入!");
newPassword = JOptionPane.showInputDialog(this, "请输入新密码:", "", JOptionPane.PLAIN_MESSAGE);
}
if (newPassword != null) {
credentials.put(username, newPassword);
saveCredentials(credentials);
loadAccounts();
JOptionPane.showMessageDialog(this, "密码修改成功!");
}
} else {
JOptionPane.showMessageDialog(this, "原密码错误!");
modifyPassword(row); // 重新提示输入
}
}
//用于删除账户
private void deleteAccount(int row) {
String username = (String) accountTable.getValueAt(row, 1);
String privilegePassword = JOptionPane.showInputDialog(this, "请输入权限密码:", "", JOptionPane.PLAIN_MESSAGE);
if (privilegePassword == null) {
return; // 用户取消输入
}
String storedPrivilegePassword = loadPrivilegePassword();
if (privilegePassword.equals(storedPrivilegePassword)) {
HashMap<String, String> credentials = loadCredentials();
credentials.remove(username);
saveCredentials(credentials);
loadAccounts();
JOptionPane.showMessageDialog(this, "账户删除成功!");
} else {
JOptionPane.showMessageDialog(this, "权限密码错误!");
}
}
//用于添加管理员
private class AddAdminAction implements ActionListener {
private final AccountManagementWindow window;
public AddAdminAction(AccountManagementWindow window) {
this.window = window;
}
@Override
public void actionPerformed(ActionEvent e) {
String privilegePassword = JOptionPane.showInputDialog(window, "请输入权限密码:", "", JOptionPane.PLAIN_MESSAGE);
if (privilegePassword == null) {
return; // 用户取消输入
}
String storedPrivilegePassword = window.loadPrivilegePassword();
if (!privilegePassword.equals(storedPrivilegePassword)) {
JOptionPane.showMessageDialog(window, "权限密码错误!");
return;
}
String username = JOptionPane.showInputDialog(window, "请输入新管理员账号:", "", JOptionPane.PLAIN_MESSAGE);
if (username == null) {
return; // 用户取消输入
}
HashMap<String, String> credentials = window.loadCredentials();
if (credentials.containsKey(username)) {
JOptionPane.showMessageDialog(window, "用户名已存在!");
return;
}
String password = JOptionPane.showInputDialog(window, "请输入新管理员密码:", "", JOptionPane.PLAIN_MESSAGE);
if (password == null) {
return; // 用户取消输入
}
credentials.put(username, password);
window.saveCredentials(credentials);
// 添加新账户到表格的最后一行
int rowCount = window.tableModel.getRowCount();
JButton modifyButton = new JButton("修改密码");
JButton deleteButton = new JButton("删除账户");
modifyButton.addActionListener(new ModifyPasswordAction(window, rowCount));
deleteButton.addActionListener(new DeleteAccountAction(window, rowCount));
window.tableModel.addRow(new Object[]{
rowCount + 1,
username,
"******",
modifyButton,
deleteButton
});
JOptionPane.showMessageDialog(window, "管理员添加成功!");
}
}
//用于修改权限密码
private class ChangePrivilegePasswordAction implements ActionListener {
private final AccountManagementWindow window;
public ChangePrivilegePasswordAction(AccountManagementWindow window) {
this.window = window;
}
@Override
public void actionPerformed(ActionEvent e) {
String oldPrivilegePassword = JOptionPane.showInputDialog(window, "请输入原权限密码:", "", JOptionPane.PLAIN_MESSAGE);
if (oldPrivilegePassword == null) {
return; // 用户取消输入
}
String storedPrivilegePassword = window.loadPrivilegePassword();
if (oldPrivilegePassword.equals(storedPrivilegePassword)) {
String newPrivilegePassword = JOptionPane.showInputDialog(window, "请输入新权限密码:", "", JOptionPane.PLAIN_MESSAGE);
while (newPrivilegePassword != null && newPrivilegePassword.equals(oldPrivilegePassword)) {
JOptionPane.showMessageDialog(window, "新权限密码与原权限密码一致,请重新输入!");
newPrivilegePassword = JOptionPane.showInputDialog(window, "请输入新权限密码:", "", JOptionPane.PLAIN_MESSAGE);
}
if (newPrivilegePassword != null) {
window.savePrivilegePassword(newPrivilegePassword);
JOptionPane.showMessageDialog(window, "权限密码修改成功!");
}
} else {
JOptionPane.showMessageDialog(window, "原权限密码错误!");
new ChangePrivilegePasswordAction(window).actionPerformed(e); // 重新提示输入
}
}
}
//按指定行号修改密码
private class ModifyPasswordAction implements ActionListener {
private final AccountManagementWindow window;
private final int row;
public ModifyPasswordAction(AccountManagementWindow window, int row) {
this.window = window;
this.row = row;
}
@Override
public void actionPerformed(ActionEvent e) {
window.modifyPassword(row);
}
}
//按指定行号删除账户
private class DeleteAccountAction implements ActionListener {
private final AccountManagementWindow window;
private final int row;
public DeleteAccountAction(AccountManagementWindow window, int row) {
this.window = window;
this.row = row;
}
@Override
public void actionPerformed(ActionEvent e) {
window.deleteAccount(row);
}
}
}