给出一个用java写的酒店管理系统,有图形化界面
时间: 2024-03-20 17:43:21 浏览: 82
很抱歉,我无法为您提供完整的Java酒店管理系统代码,但我可以给出一个简单的酒店管理系统的实现思路和示例代码以供参考。
首先,我们需要选择一个适合的图形化界面库,比如Swing或JavaFX。然后,我们需要设计数据库的结构,包括表的设计、字段的选择和数据类型的定义等。这里我们可以使用MySQL或者其他关系型数据库。
接着,我们需要编写Java代码来实现对数据库的增删查改操作。可以使用JDBC来连接数据库并执行SQL语句。具体的步骤如下:
1. 连接数据库:使用JDBC连接MySQL数据库。
```java
public class DatabaseConnection {
private static final String DB_URL = "jdbc:mysql://localhost/hotel";
private static final String USER = "root";
private static final String PASSWORD = "password";
private static Connection connection = null;
public static Connection getConnection() {
if (connection == null) {
try {
Class.forName("com.mysql.jdbc.Driver");
connection = DriverManager.getConnection(DB_URL, USER, PASSWORD);
} catch (ClassNotFoundException | SQLException e) {
e.printStackTrace();
}
}
return connection;
}
}
```
2. 定义数据模型:设计数据库的表结构,并将其映射到Java类中,例如Room、Guest、Invoice等。
```java
public class Room {
private int roomId;
private String roomNumber;
private String roomType;
private double roomRate;
// Getter and Setter
}
public class Guest {
private int guestId;
private String firstName;
private String lastName;
private String address;
private String phone;
// Getter and Setter
}
public class Invoice {
private int invoiceId;
private int roomId;
private int guestId;
private Date checkInDate;
private Date checkOutDate;
private double totalAmount;
// Getter and Setter
}
```
3. 实现基本的增删查改功能:编写Java代码来实现对数据库的增删查改操作。
```java
public class RoomDAO {
public static List<Room> findAll() {
List<Room> rooms = new ArrayList<>();
try {
Connection connection = DatabaseConnection.getConnection();
Statement statement = connection.createStatement();
ResultSet resultSet = statement.executeQuery("SELECT * FROM rooms");
while (resultSet.next()) {
Room room = new Room();
room.setRoomId(resultSet.getInt("room_id"));
room.setRoomNumber(resultSet.getString("room_number"));
room.setRoomType(resultSet.getString("room_type"));
room.setRoomRate(resultSet.getDouble("room_rate"));
rooms.add(room);
}
} catch (SQLException e) {
e.printStackTrace();
}
return rooms;
}
public static Room findById(int roomId) {
Room room = null;
try {
Connection connection = DatabaseConnection.getConnection();
PreparedStatement statement = connection.prepareStatement("SELECT * FROM rooms WHERE room_id=?");
statement.setInt(1, roomId);
ResultSet resultSet = statement.executeQuery();
if (resultSet.next()) {
room = new Room();
room.setRoomId(resultSet.getInt("room_id"));
room.setRoomNumber(resultSet.getString("room_number"));
room.setRoomType(resultSet.getString("room_type"));
room.setRoomRate(resultSet.getDouble("room_rate"));
}
} catch (SQLException e) {
e.printStackTrace();
}
return room;
}
public static void save(Room room) {
try {
Connection connection = DatabaseConnection.getConnection();
PreparedStatement statement = connection.prepareStatement(
"INSERT INTO rooms (room_number, room_type, room_rate) VALUES (?, ?, ?)");
statement.setString(1, room.getRoomNumber());
statement.setString(2, room.getRoomType());
statement.setDouble(3, room.getRoomRate());
statement.executeUpdate();
} catch (SQLException e) {
e.printStackTrace();
}
}
public static void update(Room room) {
try {
Connection connection = DatabaseConnection.getConnection();
PreparedStatement statement = connection.prepareStatement(
"UPDATE rooms SET room_number=?, room_type=?, room_rate=? WHERE room_id=?");
statement.setString(1, room.getRoomNumber());
statement.setString(2, room.getRoomType());
statement.setDouble(3, room.getRoomRate());
statement.setInt(4, room.getRoomId());
statement.executeUpdate();
} catch (SQLException e) {
e.printStackTrace();
}
}
public static void delete(Room room) {
try {
Connection connection = DatabaseConnection.getConnection();
PreparedStatement statement = connection.prepareStatement("DELETE FROM rooms WHERE room_id=?");
statement.setInt(1, room.getRoomId());
statement.executeUpdate();
} catch (SQLException e) {
e.printStackTrace();
}
}
}
```
4. 实现图形化界面:使用Swing或JavaFX来设计并实现图形化界面。
这里给出一个简单的Swing界面实现示例。
```java
public class RoomManagementSystem extends JFrame {
public RoomManagementSystem() {
super("Room Management System");
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setSize(500, 400);
setLocationRelativeTo(null);
setLayout(new BorderLayout());
JTable table = new JTable(new RoomTableModel(RoomDAO.findAll()));
JScrollPane scrollPane = new JScrollPane(table);
add(scrollPane, BorderLayout.CENTER);
JPanel buttonPanel = new JPanel(new FlowLayout(FlowLayout.RIGHT));
JButton addButton = new JButton("Add");
addButton.addActionListener(e -> {
Room room = new Room();
room.setRoomNumber(JOptionPane.showInputDialog(this, "Enter room number:"));
room.setRoomType(JOptionPane.showInputDialog(this, "Enter room type:"));
room.setRoomRate(Double.parseDouble(JOptionPane.showInputDialog(this, "Enter room rate:")));
RoomDAO.save(room);
((RoomTableModel) table.getModel()).setData(RoomDAO.findAll());
});
buttonPanel.add(addButton);
JButton editButton = new JButton("Edit");
editButton.addActionListener(e -> {
int selectedRow = table.getSelectedRow();
if (selectedRow < 0) {
JOptionPane.showMessageDialog(this, "Please select a row.");
} else {
RoomTableModel model = (RoomTableModel) table.getModel();
Room room = model.getData().get(selectedRow);
room.setRoomNumber(JOptionPane.showInputDialog(this, "Enter room number:", room.getRoomNumber()));
room.setRoomType(JOptionPane.showInputDialog(this, "Enter room type:", room.getRoomType()));
room.setRoomRate(Double.parseDouble(JOptionPane.showInputDialog(this, "Enter room rate:", room.getRoomRate())));
RoomDAO.update(room);
model.setData(RoomDAO.findAll());
}
});
buttonPanel.add(editButton);
JButton deleteButton = new JButton("Delete");
deleteButton.addActionListener(e -> {
int selectedRow = table.getSelectedRow();
if (selectedRow < 0) {
JOptionPane.showMessageDialog(this, "Please select a row.");
} else {
RoomTableModel model = (RoomTableModel) table.getModel();
Room room = model.getData().get(selectedRow);
RoomDAO.delete(room);
model.setData(RoomDAO.findAll());
}
});
buttonPanel.add(deleteButton);
add(buttonPanel, BorderLayout.SOUTH);
setVisible(true);
}
public static void main(String[] args) {
new RoomManagementSystem();
}
}
class RoomTableModel extends AbstractTableModel {
private static final String[] COLUMN_NAMES = {"Room ID", "Room Number", "Room Type", "Room Rate"};
private List<Room> data;
public RoomTableModel(List<Room> data) {
this.data = data;
}
public List<Room> getData() {
return data;
}
public void setData(List<Room> data) {
this.data = data;
fireTableDataChanged();
}
@Override
public int getRowCount() {
return data.size();
}
@Override
public int getColumnCount() {
return COLUMN_NAMES.length;
}
@Override
public String getColumnName(int column) {
return COLUMN_NAMES[column];
}
@Override
public Object getValueAt(int rowIndex, int columnIndex) {
Room room = data.get(rowIndex);
switch (columnIndex) {
case 0:
return room.getRoomId();
case 1:
return room.getRoomNumber();
case 2:
return room.getRoomType();
case 3:
return room.getRoomRate();
default:
return null;
}
}
}
```
5. 将数据库操作与图形化界面进行交互:将Java代码中实现的基本增删查改功能与图形化界面进行交互。
在上面的示例代码中,我们使用了JTable来显示数据库中的数据,并使用JOptionPane来实现添加、编辑和删除操作。
6. 进行测试和调试:确保系统能够稳定运行。
需要注意的是,为了确保系统的安全性和性能问题,我们需要考虑如何防止SQL注入攻击和提高系统的响应速度等。
这只是一个简单的Java酒店管理系统的实现思路和示例代码,如果您需要更详细的帮助,可以提供更多的具体要求和需求,我可以给您提供更具体的方案和建议。
阅读全文