信息系统设计

package com.example.hotel;

import javafx.application.Application;
import javafx.collections.ObservableList;
import javafx.collections.FXCollections;
import javafx.geometry.Insets;
import javafx.scene.Scene;
import javafx.scene.image.Image;
import javafx.scene.control.*;
import javafx.scene.control.cell.PropertyValueFactory;
import javafx.scene.layout.GridPane;
import javafx.stage.Stage;

import java.sql.*;

public class Hotel extends Application {
    private static final String DB_URL = "jdbc:mysql://localhost:3306/hotel?useUnicode=true&characterEncoding=UTF-8";
    private static final String DB_USERNAME = "root";
    private static final String DB_PASSWORD = "123456";

    private TableView<Customer> customerTableView;
    private TableView<Room> roomTableView;
    private TableView<Employee> employeeTableView;
    private TableView<Cleaning> cleaningTableView;

    public static void main(String[] args) {
        launch(args);
    }

    @Override
    public void start(Stage primaryStage) {
        primaryStage.setTitle("酒店管理系统");

        // TabPane
        TabPane tabPane = new TabPane();

        // ico
        Image icon = new Image("/image/icon.png");
        primaryStage.getIcons().add(icon);

        // 客户管理的Tab
        Tab customerTab = new Tab("客户管理");
        customerTab.setClosable(false);
        GridPane customerGrid = createCustomerGrid();
        customerTab.setContent(customerGrid);

        // 客房管理的Tab
        Tab roomTab = new Tab("客房管理");
        roomTab.setClosable(false);
        GridPane roomGrid = createRoomGrid();
        roomTab.setContent(roomGrid);

        // 员工管理的Tab
        Tab employeeTab = new Tab("员工管理");
        employeeTab.setClosable(false);
        GridPane employeeGrid = createEmployeeGrid();
        employeeTab.setContent(employeeGrid);

        // 清洁管理的Tab
        Tab cleaningTab = new Tab("清洁管理");
        cleaningTab.setClosable(false);
        GridPane cleaningGrid = createCleaningGrid();
        cleaningTab.setContent(cleaningGrid);

        // 把Tab加到TabPane
        tabPane.getTabs().addAll(customerTab, roomTab, employeeTab, cleaningTab);

        loadCustomerData();
        loadRoomData();
        loadEmployeeData();
        loadCleaningData();

        Scene scene = new Scene(tabPane, 600, 500);
        primaryStage.setScene(scene);
        primaryStage.show();
    }

    private GridPane createCustomerGrid() {
        // 创建GridPane
        GridPane gridPane = new GridPane();
        gridPane.setPadding(new Insets(10));
        gridPane.setHgap(10);
        gridPane.setVgap(10);

        // 创建TableView
        customerTableView = new TableView<>();
        customerTableView.setPrefWidth(600);
        TableColumn<Customer, Integer> roomNumberColumn = new TableColumn<>("房间号");
        roomNumberColumn.setCellValueFactory(new PropertyValueFactory<>("roomNumber"));
        TableColumn<Customer, String> nameColumn = new TableColumn<>("客户姓名");
        nameColumn.setCellValueFactory(new PropertyValueFactory<>("name"));
        TableColumn<Customer, String> genderColumn = new TableColumn<>("性别");
        genderColumn.setCellValueFactory(new PropertyValueFactory<>("gender"));
        TableColumn<Customer, String> phoneColumn = new TableColumn<>("电话");
        phoneColumn.setCellValueFactory(new PropertyValueFactory<>("phone"));
        customerTableView.getColumns().addAll(roomNumberColumn, nameColumn, genderColumn, phoneColumn);

        // 创建按钮
        Button registerButton = new Button("登记入住");
        Button deleteButton = new Button("删除");
        registerButton.setOnAction(event -> registerCustomer());
        deleteButton.setOnAction(event -> deleteCustomer());

        // 把TableView和按钮添加到GridPane
        gridPane.add(customerTableView, 0, 0, 4, 1);
        gridPane.add(registerButton, 0, 1);
        gridPane.add(deleteButton, 1, 1);

        return gridPane;
    }

    private GridPane createRoomGrid() {
        // 创建GridPane
        GridPane gridPane = new GridPane();
        gridPane.setPadding(new Insets(10));
        gridPane.setHgap(10);
        gridPane.setVgap(10);

        // 创建TableView
        roomTableView = new TableView<>();
        roomTableView.setPrefWidth(600);
        TableColumn<Room, Integer> roomNumberColumn = new TableColumn<>("房间号");
        roomNumberColumn.setCellValueFactory(new PropertyValueFactory<>("roomNumber"));
        TableColumn<Room, String> typeColumn = new TableColumn<>("房间类型");
        typeColumn.setCellValueFactory(new PropertyValueFactory<>("type"));
        TableColumn<Room, String> statusColumn = new TableColumn<>("房间状态");
        statusColumn.setCellValueFactory(new PropertyValueFactory<>("status"));
        roomTableView.getColumns().addAll(roomNumberColumn, typeColumn, statusColumn);

        // 创建按钮
        Button addButton = new Button("添加");
        Button deleteButton = new Button("删除");
        addButton.setOnAction(event -> addRoom());
        deleteButton.setOnAction(event -> deleteRoom());

        // 将TableView和按钮添加到GridPane
        gridPane.add(roomTableView, 0, 0, 4, 1);
        gridPane.add(addButton, 0, 1);
        gridPane.add(deleteButton, 1, 1);

        return gridPane;
    }

    private GridPane createEmployeeGrid() {
        // 创建GridPane
        GridPane gridPane = new GridPane();
        gridPane.setPadding(new Insets(10));
        gridPane.setHgap(10);
        gridPane.setVgap(10);

        // 创建TableView
        employeeTableView = new TableView<>();
        employeeTableView.setPrefWidth(600);
        TableColumn<Employee, String> typeColumn = new TableColumn<>("员工类型");
        typeColumn.setCellValueFactory(new PropertyValueFactory<>("type"));
        TableColumn<Employee, String> nameColumn = new TableColumn<>("员工姓名");
        nameColumn.setCellValueFactory(new PropertyValueFactory<>("name"));
        TableColumn<Employee, String> statusColumn = new TableColumn<>("员工状态");
        statusColumn.setCellValueFactory(new PropertyValueFactory<>("status"));
        employeeTableView.getColumns().addAll(typeColumn, nameColumn, statusColumn);

        // 创建按钮
        Button addButton = new Button("添加");
        Button deleteButton = new Button("删除");
        addButton.setOnAction(event -> addEmployee());
        deleteButton.setOnAction(event -> deleteEmployee());

        // 把TableView和按钮添加到GridPane
        gridPane.add(employeeTableView, 0, 0, 4, 1);
        gridPane.add(addButton, 0, 1);
        gridPane.add(deleteButton, 1, 1);

        return gridPane;
    }

    private GridPane createCleaningGrid() {
        // 创建GridPane
        GridPane gridPane = new GridPane();
        gridPane.setPadding(new Insets(10));
        gridPane.setHgap(10);
        gridPane.setVgap(10);

        // 创建TableView
        cleaningTableView = new TableView<>();
        cleaningTableView.setPrefWidth(600);
        TableColumn<Cleaning, Integer> roomNumberColumn = new TableColumn<>("房间号");
        roomNumberColumn.setCellValueFactory(new PropertyValueFactory<>("roomNumber"));
        TableColumn<Cleaning, String> statusColumn = new TableColumn<>("清洁状态");
        statusColumn.setCellValueFactory(new PropertyValueFactory<>("status"));
        TableColumn<Cleaning, String> employeeNameColumn = new TableColumn<>("员工姓名");
        employeeNameColumn.setCellValueFactory(new PropertyValueFactory<>("employeeName"));
        cleaningTableView.getColumns().addAll(roomNumberColumn, statusColumn, employeeNameColumn);

        // 创建按钮
        Button addButton = new Button("添加");
        Button deleteButton = new Button("删除");
        addButton.setOnAction(event -> addCleaning());
        deleteButton.setOnAction(event -> deleteCleaning());

        // 把TableView和按钮添加到GridPane
        gridPane.add(cleaningTableView, 0, 0, 4, 1);
        gridPane.add(addButton, 0, 1);
        gridPane.add(deleteButton, 1, 1);

        return gridPane;
    }

    private void loadCustomerData() {
        try {
            Connection conn = DriverManager.getConnection(DB_URL, DB_USERNAME, DB_PASSWORD);
            String query = "SELECT * FROM customer";
            Statement statement = conn.createStatement();
            ResultSet resultSet = statement.executeQuery(query);

            ObservableList<Customer> customers = FXCollections.observableArrayList();
            while (resultSet.next()) {
                int roomNumber = resultSet.getInt("roomNumber");
                String name = resultSet.getString("name");
                String gender = resultSet.getString("gender");
                String phone = resultSet.getString("phone");
                Customer customer = new Customer(roomNumber, name, gender, phone);
                customers.add(customer);
            }
            customerTableView.setItems(customers);

            resultSet.close();
            statement.close();
            conn.close();
        } catch (SQLException e) {
            e.printStackTrace();
        }
    }

    private void registerCustomer() {
        Dialog<Customer> dialog = new Dialog<>();
        dialog.setTitle("登记入住");
        dialog.setHeaderText(null);

        // 创建对话框
        GridPane gridPane = new GridPane();
        gridPane.setPadding(new Insets(10));
        gridPane.setHgap(10);
        gridPane.setVgap(10);
        TextField roomNumberField = new TextField();
        TextField nameField = new TextField();
        ComboBox<String> genderComboBox = new ComboBox<>();
        genderComboBox.getItems().addAll("男", "女");
        TextField phoneField = new TextField();
        gridPane.add(new Label("房间号:"), 0, 0);
        gridPane.add(roomNumberField, 1, 0);
        gridPane.add(new Label("客户姓名:"), 0, 1);
        gridPane.add(nameField, 1, 1);
        gridPane.add(new Label("性别:"), 0, 2);
        gridPane.add(genderComboBox, 1, 2);
        gridPane.add(new Label("电话:"), 0, 3);
        gridPane.add(phoneField, 1, 3);
        dialog.getDialogPane().setContent(gridPane);

        // 设置按钮
        ButtonType confirmButtonType = new ButtonType("确定", ButtonBar.ButtonData.OK_DONE);
        ButtonType cancelButtonType = new ButtonType("取消", ButtonBar.ButtonData.CANCEL_CLOSE);
        dialog.getDialogPane().getButtonTypes().addAll(confirmButtonType, cancelButtonType);

        // 绑定按钮事件
        dialog.setResultConverter(dialogButton -> {
            if (dialogButton == confirmButtonType) {
                int roomNumber = Integer.parseInt(roomNumberField.getText());
                String name = nameField.getText();
                String gender = genderComboBox.getValue();
                String phone = phoneField.getText();
                Customer customer = new Customer(roomNumber, name, gender, phone);
                saveCustomer(customer);
                customerTableView.getItems().add(customer);
                return customer;
            }
            return null;
        });

        dialog.showAndWait();
    }

    private void deleteCustomer() {
        Customer selectedCustomer = customerTableView.getSelectionModel().getSelectedItem();
        if (selectedCustomer != null) {
            customerTableView.getItems().remove(selectedCustomer);
            deleteCustomerFromDatabase(selectedCustomer.getRoomNumber());
        }
    }

    private void saveCustomer(Customer customer) {
        try {
            Connection conn = DriverManager.getConnection(DB_URL, DB_USERNAME, DB_PASSWORD);
            String query = "INSERT INTO customer (roomNumber, name, gender, phone) VALUES (?, ?, ?, ?)";
            PreparedStatement statement = conn.prepareStatement(query);
            statement.setInt(1, customer.getRoomNumber());
            statement.setString(2, customer.getName());
            statement.setString(3, customer.getGender());
            statement.setString(4, customer.getPhone());
            statement.executeUpdate();
            statement.close();
            conn.close();
        } catch (SQLException e) {
            e.printStackTrace();
        }
    }

    private void deleteCustomerFromDatabase(int roomNumber) {
        try {
            Connection conn = DriverManager.getConnection(DB_URL, DB_USERNAME, DB_PASSWORD);
            String query = "DELETE FROM customer WHERE roomNumber=?";
            PreparedStatement statement = conn.prepareStatement(query);
            statement.setInt(1, roomNumber);
            statement.executeUpdate();
            statement.close();
            conn.close();
        } catch (SQLException e) {
            e.printStackTrace();
        }
    }

    private void loadRoomData() {
        try {
            Connection conn = DriverManager.getConnection(DB_URL, DB_USERNAME, DB_PASSWORD);
            String query = "SELECT * FROM room";
            Statement statement = conn.createStatement();
            ResultSet resultSet = statement.executeQuery(query);

            ObservableList<Room> rooms = FXCollections.observableArrayList();
            while (resultSet.next()) {
                int roomNumber = resultSet.getInt("roomNumber");
                String type = resultSet.getString("type");
                String status = resultSet.getString("status");
                Room room = new Room(roomNumber, type, status);
                rooms.add(room);
            }
            roomTableView.setItems(rooms);

            resultSet.close();
            statement.close();
            conn.close();
        } catch (SQLException e) {
            e.printStackTrace();
        }
    }

    private void addRoom() {
        Dialog<Room> dialog = new Dialog<>();
        dialog.setTitle("添加客房");
        dialog.setHeaderText(null);

        // 创建对话框
        GridPane gridPane = new GridPane();
        gridPane.setPadding(new Insets(10));
        gridPane.setHgap(10);
        gridPane.setVgap(10);
        TextField roomNumberField = new TextField();
        TextField typeField = new TextField();
        ComboBox<String> statusComboBox = new ComboBox<>();
        statusComboBox.getItems().addAll("已入住", "未入住");
        gridPane.add(new Label("房间号:"), 0, 0);
        gridPane.add(roomNumberField, 1, 0);
        gridPane.add(new Label("房间类型:"), 0, 1);
        gridPane.add(typeField, 1, 1);
        gridPane.add(new Label("房间状态:"), 0, 2);
        gridPane.add(statusComboBox, 1, 2);
        dialog.getDialogPane().setContent(gridPane);

        // 设置按钮
        ButtonType confirmButtonType = new ButtonType("确定", ButtonBar.ButtonData.OK_DONE);
        ButtonType cancelButtonType = new ButtonType("取消", ButtonBar.ButtonData.CANCEL_CLOSE);
        dialog.getDialogPane().getButtonTypes().addAll(confirmButtonType, cancelButtonType);

        // 绑定按钮事件
        dialog.setResultConverter(dialogButton -> {
            if (dialogButton == confirmButtonType) {
                int roomNumber = Integer.parseInt(roomNumberField.getText());
                String type = typeField.getText();
                String status = statusComboBox.getValue();
                Room room = new Room(roomNumber, type, status);
                saveRoom(room);
                roomTableView.getItems().add(room);
                return room;
            }
            return null;
        });

        dialog.showAndWait();
    }

    private void deleteRoom() {
        Room selectedRoom = roomTableView.getSelectionModel().getSelectedItem();
        if (selectedRoom != null) {
            roomTableView.getItems().remove(selectedRoom);
            deleteRoomFromDatabase(selectedRoom.getRoomNumber());
        }
    }

    private void saveRoom(Room room) {
        try {
            Connection conn = DriverManager.getConnection(DB_URL, DB_USERNAME, DB_PASSWORD);
            String query = "INSERT INTO room (roomNumber, type, status) VALUES (?, ?, ?)";
            PreparedStatement statement = conn.prepareStatement(query);
            statement.setInt(1, room.getRoomNumber());
            statement.setString(2, room.getType());
            statement.setString(3, room.getStatus());
            statement.executeUpdate();
            statement.close();
            conn.close();
        } catch (SQLException e) {
            e.printStackTrace();
        }
    }

    private void deleteRoomFromDatabase(int roomNumber) {
        try {
            Connection conn = DriverManager.getConnection(DB_URL, DB_USERNAME, DB_PASSWORD);
            String query = "DELETE FROM room WHERE roomNumber=?";
            PreparedStatement statement = conn.prepareStatement(query);
            statement.setInt(1, roomNumber);
            statement.executeUpdate();
            statement.close();
            conn.close();
        } catch (SQLException e) {
            e.printStackTrace();
        }
    }

    private void loadEmployeeData() {
        try {
            Connection conn = DriverManager.getConnection(DB_URL, DB_USERNAME, DB_PASSWORD);
            String query = "SELECT * FROM employee";
            Statement statement = conn.createStatement();
            ResultSet resultSet = statement.executeQuery(query);

            ObservableList<Employee> employees = FXCollections.observableArrayList();
            while (resultSet.next()) {
                String name = resultSet.getString("name");
                String type = resultSet.getString("type");
                String status = resultSet.getString("status");
                Employee employee = new Employee(name, type, status);
                employees.add(employee);
            }
            employeeTableView.setItems(employees);

            resultSet.close();
            statement.close();
            conn.close();
        } catch (SQLException e) {
            e.printStackTrace();
        }
    }

    private void addEmployee() {
        Dialog<Employee> dialog = new Dialog<>();
        dialog.setTitle("添加员工");
        dialog.setHeaderText(null);

        // 创建对话框
        GridPane gridPane = new GridPane();
        gridPane.setPadding(new Insets(10));
        gridPane.setHgap(10);
        gridPane.setVgap(10);
        TextField nameField = new TextField();
        ComboBox<String> typeComboBox = new ComboBox<>();
        typeComboBox.getItems().addAll("前台", "保安", "清洁工");
        ComboBox<String> statusComboBox = new ComboBox<>();
        statusComboBox.getItems().addAll("在岗", "休假");
        gridPane.add(new Label("员工姓名:"), 0, 0);
        gridPane.add(nameField, 1, 0);
        gridPane.add(new Label("员工类型:"), 0, 1);
        gridPane.add(typeComboBox, 1, 1);
        gridPane.add(new Label("员工状态:"), 0, 2);
        gridPane.add(statusComboBox, 1, 2);
        dialog.getDialogPane().setContent(gridPane);

        // 设置按钮
        ButtonType confirmButtonType = new ButtonType("确定", ButtonBar.ButtonData.OK_DONE);
        ButtonType cancelButtonType = new ButtonType("取消", ButtonBar.ButtonData.CANCEL_CLOSE);
        dialog.getDialogPane().getButtonTypes().addAll(confirmButtonType, cancelButtonType);

        // 绑定按钮事件
        dialog.setResultConverter(dialogButton -> {
            if (dialogButton == confirmButtonType) {
                String name = nameField.getText();
                String type = typeComboBox.getValue();
                String status = statusComboBox.getValue();
                Employee employee = new Employee(name, type, status);
                saveEmployee(employee);
                employeeTableView.getItems().add(employee);
                return employee;
            }
            return null;
        });

        dialog.showAndWait();
    }

    private void deleteEmployee() {
        Employee selectedEmployee = employeeTableView.getSelectionModel().getSelectedItem();
        if (selectedEmployee != null) {
            employeeTableView.getItems().remove(selectedEmployee);
            deleteEmployeeFromDatabase(selectedEmployee.getName());
        }
    }

    private void saveEmployee(Employee employee) {
        try {
            Connection conn = DriverManager.getConnection(DB_URL, DB_USERNAME, DB_PASSWORD);
            String query = "INSERT INTO employee (name, type, status) VALUES (?, ?, ?)";
            PreparedStatement statement = conn.prepareStatement(query);
            statement.setString(1, employee.getName());
            statement.setString(2, employee.getType());
            statement.setString(3, employee.getStatus());
            statement.executeUpdate();
            statement.close();
            conn.close();
        } catch (SQLException e) {
            e.printStackTrace();
        }
    }

    private void deleteEmployeeFromDatabase(String employeeName) {
        try {
            Connection conn = DriverManager.getConnection(DB_URL, DB_USERNAME, DB_PASSWORD);
            String query = "DELETE FROM employee WHERE name=?";
            PreparedStatement statement = conn.prepareStatement(query);
            statement.setString(1, employeeName);
            statement.executeUpdate();
            statement.close();
            conn.close();
        } catch (SQLException e) {
            e.printStackTrace();
        }
    }

    private void loadCleaningData() {
        try {
            Connection conn = DriverManager.getConnection(DB_URL, DB_USERNAME, DB_PASSWORD);
            String query = "SELECT * FROM cleaning";
            Statement statement = conn.createStatement();
            ResultSet resultSet = statement.executeQuery(query);

            ObservableList<Cleaning> cleanings = FXCollections.observableArrayList();
            while (resultSet.next()) {
                int roomNumber = resultSet.getInt("roomNumber");
                String status = resultSet.getString("status");
                String employeeName = resultSet.getString("employeeName");
                Cleaning cleaning = new Cleaning(roomNumber, status, employeeName);
                cleanings.add(cleaning);
            }
            cleaningTableView.setItems(cleanings);

            resultSet.close();
            statement.close();
            conn.close();
        } catch (SQLException e) {
            e.printStackTrace();
        }
    }


    private void addCleaning() {
        Dialog<Cleaning> dialog = new Dialog<>();
        dialog.setTitle("添加清洁记录");
        dialog.setHeaderText(null);

        // 创建对话框控件
        GridPane gridPane = new GridPane();
        gridPane.setPadding(new Insets(10));
        gridPane.setHgap(10);
        gridPane.setVgap(10);
        TextField roomNumberField = new TextField();
        ComboBox<String> statusComboBox = new ComboBox<>();
        statusComboBox.getItems().addAll("已清洁", "未清洁");
        TextField employeeNameField = new TextField();
        gridPane.add(new Label("房间号:"), 0, 0);
        gridPane.add(roomNumberField, 1, 0);
        gridPane.add(new Label("清洁状态:"), 0, 1);
        gridPane.add(statusComboBox, 1, 1);
        gridPane.add(new Label("员工姓名:"), 0, 2);
        gridPane.add(employeeNameField, 1, 2);
        dialog.getDialogPane().setContent(gridPane);

        // 设置按钮
        ButtonType confirmButtonType = new ButtonType("确定", ButtonBar.ButtonData.OK_DONE);
        ButtonType cancelButtonType = new ButtonType("取消", ButtonBar.ButtonData.CANCEL_CLOSE);
        dialog.getDialogPane().getButtonTypes().addAll(confirmButtonType, cancelButtonType);

        // 绑定按钮事件
        dialog.setResultConverter(dialogButton -> {
            if (dialogButton == confirmButtonType) {
                int roomNumber = Integer.parseInt(roomNumberField.getText());
                String status = statusComboBox.getValue();
                String employeeName = employeeNameField.getText();
                Cleaning cleaning = new Cleaning(roomNumber, status, employeeName);
                saveCleaning(cleaning);
                cleaningTableView.getItems().add(cleaning);
                return cleaning;
            }
            return null;
        });

        dialog.showAndWait();
    }

    private void deleteCleaning() {
        Cleaning selectedCleaning = cleaningTableView.getSelectionModel().getSelectedItem();
        if (selectedCleaning != null) {
            cleaningTableView.getItems().remove(selectedCleaning);
            deleteCleaningFromDatabase(selectedCleaning.getRoomNumber());
        }
    }

    private void saveCleaning(Cleaning cleaning) {
        try {
            Connection conn = DriverManager.getConnection(DB_URL, DB_USERNAME, DB_PASSWORD);
            String query = "INSERT INTO cleaning (roomNumber, status, employeeName) VALUES (?, ?, ?)";
            PreparedStatement statement = conn.prepareStatement(query);
            statement.setInt(1, cleaning.getRoomNumber());
            statement.setString(2, cleaning.getStatus());
            statement.setString(3, cleaning.getEmployeeName());
            statement.executeUpdate();
            statement.close();
            conn.close();
        } catch (SQLException e) {
            e.printStackTrace();
        }
    }

    private void deleteCleaningFromDatabase(int roomNumber) {
        try {
            Connection conn = DriverManager.getConnection(DB_URL, DB_USERNAME, DB_PASSWORD);
            String query = "DELETE FROM cleaning WHERE roomNumber=?";
            PreparedStatement statement = conn.prepareStatement(query);
            statement.setInt(1, roomNumber);
            statement.executeUpdate();
            statement.close();
            conn.close();
        } catch (SQLException e) {
            e.printStackTrace();
        }
    }
}