酒店管理系統是一個復雜的信息管理系統,涉及到客房管理、預訂管理、客戶管理、賬單管理等多個模塊。本文將介紹如何使用Java代碼實現一個簡單的酒店管理系統,涵蓋基本的增刪改查功能。
在開始編寫代碼之前,我們需要明確系統的基本需求。一個簡單的酒店管理系統應該具備以下功能:
根據需求分析,我們可以設計以下幾個類:
為了簡化實現,我們可以使用Java集合類(如ArrayList
)來模擬數據庫。每個類對應一個集合,用于存儲相應的數據。
public class Room {
private int roomNumber;
private String type;
private double price;
public Room(int roomNumber, String type, double price) {
this.roomNumber = roomNumber;
this.type = type;
this.price = price;
}
// Getters and Setters
public int getRoomNumber() {
return roomNumber;
}
public void setRoomNumber(int roomNumber) {
this.roomNumber = roomNumber;
}
public String getType() {
return type;
}
public void setType(String type) {
this.type = type;
}
public double getPrice() {
return price;
}
public void setPrice(double price) {
this.price = price;
}
@Override
public String toString() {
return "Room{" +
"roomNumber=" + roomNumber +
", type='" + type + '\'' +
", price=" + price +
'}';
}
}
public class Customer {
private int customerId;
private String name;
private String contactInfo;
public Customer(int customerId, String name, String contactInfo) {
this.customerId = customerId;
this.name = name;
this.contactInfo = contactInfo;
}
// Getters and Setters
public int getCustomerId() {
return customerId;
}
public void setCustomerId(int customerId) {
this.customerId = customerId;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getContactInfo() {
return contactInfo;
}
public void setContactInfo(String contactInfo) {
this.contactInfo = contactInfo;
}
@Override
public String toString() {
return "Customer{" +
"customerId=" + customerId +
", name='" + name + '\'' +
", contactInfo='" + contactInfo + '\'' +
'}';
}
}
import java.util.Date;
public class Reservation {
private int reservationId;
private int customerId;
private int roomNumber;
private Date reservationDate;
public Reservation(int reservationId, int customerId, int roomNumber, Date reservationDate) {
this.reservationId = reservationId;
this.customerId = customerId;
this.roomNumber = roomNumber;
this.reservationDate = reservationDate;
}
// Getters and Setters
public int getReservationId() {
return reservationId;
}
public void setReservationId(int reservationId) {
this.reservationId = reservationId;
}
public int getCustomerId() {
return customerId;
}
public void setCustomerId(int customerId) {
this.customerId = customerId;
}
public int getRoomNumber() {
return roomNumber;
}
public void setRoomNumber(int roomNumber) {
this.roomNumber = roomNumber;
}
public Date getReservationDate() {
return reservationDate;
}
public void setReservationDate(Date reservationDate) {
this.reservationDate = reservationDate;
}
@Override
public String toString() {
return "Reservation{" +
"reservationId=" + reservationId +
", customerId=" + customerId +
", roomNumber=" + roomNumber +
", reservationDate=" + reservationDate +
'}';
}
}
public class Bill {
private int billId;
private int customerId;
private double amount;
public Bill(int billId, int customerId, double amount) {
this.billId = billId;
this.customerId = customerId;
this.amount = amount;
}
// Getters and Setters
public int getBillId() {
return billId;
}
public void setBillId(int billId) {
this.billId = billId;
}
public int getCustomerId() {
return customerId;
}
public void setCustomerId(int customerId) {
this.customerId = customerId;
}
public double getAmount() {
return amount;
}
public void setAmount(double amount) {
this.amount = amount;
}
@Override
public String toString() {
return "Bill{" +
"billId=" + billId +
", customerId=" + customerId +
", amount=" + amount +
'}';
}
}
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
public class HotelManagementSystem {
private List<Room> rooms;
private List<Customer> customers;
private List<Reservation> reservations;
private List<Bill> bills;
public HotelManagementSystem() {
rooms = new ArrayList<>();
customers = new ArrayList<>();
reservations = new ArrayList<>();
bills = new ArrayList<>();
}
// 添加客房
public void addRoom(Room room) {
rooms.add(room);
}
// 刪除客房
public void removeRoom(int roomNumber) {
rooms.removeIf(room -> room.getRoomNumber() == roomNumber);
}
// 查詢客房
public Room findRoom(int roomNumber) {
for (Room room : rooms) {
if (room.getRoomNumber() == roomNumber) {
return room;
}
}
return null;
}
// 添加客戶
public void addCustomer(Customer customer) {
customers.add(customer);
}
// 刪除客戶
public void removeCustomer(int customerId) {
customers.removeIf(customer -> customer.getCustomerId() == customerId);
}
// 查詢客戶
public Customer findCustomer(int customerId) {
for (Customer customer : customers) {
if (customer.getCustomerId() == customerId) {
return customer;
}
}
return null;
}
// 添加預訂
public void addReservation(Reservation reservation) {
reservations.add(reservation);
}
// 刪除預訂
public void removeReservation(int reservationId) {
reservations.removeIf(reservation -> reservation.getReservationId() == reservationId);
}
// 查詢預訂
public Reservation findReservation(int reservationId) {
for (Reservation reservation : reservations) {
if (reservation.getReservationId() == reservationId) {
return reservation;
}
}
return null;
}
// 添加賬單
public void addBill(Bill bill) {
bills.add(bill);
}
// 刪除賬單
public void removeBill(int billId) {
bills.removeIf(bill -> bill.getBillId() == billId);
}
// 查詢賬單
public Bill findBill(int billId) {
for (Bill bill : bills) {
if (bill.getBillId() == billId) {
return bill;
}
}
return null;
}
// 打印所有客房
public void printAllRooms() {
for (Room room : rooms) {
System.out.println(room);
}
}
// 打印所有客戶
public void printAllCustomers() {
for (Customer customer : customers) {
System.out.println(customer);
}
}
// 打印所有預訂
public void printAllReservations() {
for (Reservation reservation : reservations) {
System.out.println(reservation);
}
}
// 打印所有賬單
public void printAllBills() {
for (Bill bill : bills) {
System.out.println(bill);
}
}
public static void main(String[] args) {
HotelManagementSystem system = new HotelManagementSystem();
// 添加客房
system.addRoom(new Room(101, "Single", 100.0));
system.addRoom(new Room(102, "Double", 150.0));
// 添加客戶
system.addCustomer(new Customer(1, "Alice", "123-456-7890"));
system.addCustomer(new Customer(2, "Bob", "987-654-3210"));
// 添加預訂
system.addReservation(new Reservation(1, 1, 101, new Date()));
system.addReservation(new Reservation(2, 2, 102, new Date()));
// 添加賬單
system.addBill(new Bill(1, 1, 100.0));
system.addBill(new Bill(2, 2, 150.0));
// 打印所有信息
system.printAllRooms();
system.printAllCustomers();
system.printAllReservations();
system.printAllBills();
}
}
本文介紹了如何使用Java代碼實現一個簡單的酒店管理系統。通過設計Room
、Customer
、Reservation
和Bill
等類,并使用ArrayList
來模擬數據庫,我們實現了基本的增刪改查功能。這個系統雖然簡單,但可以作為進一步開發的基礎。在實際應用中,可以考慮使用數據庫來存儲數據,并增加更多的功能模塊,如用戶權限管理、報表生成等。
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。