Java编写一个酒店管理程序,实现下功能:查询功能订房功能退房功能选择服务功能和结账功能并建立测试类
时间: 2024-03-23 07:36:50 浏览: 89
好的,下面是一个简单的酒店管理程序,实现了查询、订房、退房、选择服务和结账功能,以及一个测试类。
```java
import java.util.Scanner;
public class HotelManagementSystem {
private Room[] rooms;
public HotelManagementSystem(int numOfRooms) {
rooms = new Room[numOfRooms];
for (int i = 0; i < numOfRooms; i++) {
rooms[i] = new Room(i + 1);
}
}
public void showRooms() {
System.out.println("Room Status:");
for (int i = 0; i < rooms.length; i++) {
System.out.println(rooms[i]);
}
}
public void bookRoom() {
Scanner scanner = new Scanner(System.in);
System.out.print("Enter room number: ");
int roomNo = scanner.nextInt();
if (roomNo < 1 || roomNo > rooms.length) {
System.out.println("Invalid room number!");
return;
}
if (rooms[roomNo - 1].isOccupied()) {
System.out.println("The room is already occupied!");
} else {
rooms[roomNo - 1].setOccupied(true);
System.out.println("Room " + roomNo + " has been booked.");
}
}
public void checkOut() {
Scanner scanner = new Scanner(System.in);
System.out.print("Enter room number: ");
int roomNo = scanner.nextInt();
if (roomNo < 1 || roomNo > rooms.length) {
System.out.println("Invalid room number!");
return;
}
if (rooms[roomNo - 1].isOccupied()) {
rooms[roomNo - 1].setOccupied(false);
rooms[roomNo - 1].clearServices();
System.out.println("Room " + roomNo + " has been checked out.");
} else {
System.out.println("The room is not occupied!");
}
}
public void selectServices() {
Scanner scanner = new Scanner(System.in);
System.out.print("Enter room number: ");
int roomNo = scanner.nextInt();
if (roomNo < 1 || roomNo > rooms.length) {
System.out.println("Invalid room number!");
return;
}
if (!rooms[roomNo - 1].isOccupied()) {
System.out.println("The room is not occupied!");
return;
}
System.out.println("Services:");
System.out.println("1. Room service");
System.out.println("2. Laundry service");
System.out.println("3. Wake-up service");
System.out.println("4. None");
System.out.print("Enter service number: ");
int serviceNo = scanner.nextInt();
switch (serviceNo) {
case 1:
rooms[roomNo - 1].addService("Room service");
System.out.println("Room service has been selected.");
break;
case 2:
rooms[roomNo - 1].addService("Laundry service");
System.out.println("Laundry service has been selected.");
break;
case 3:
rooms[roomNo - 1].addService("Wake-up service");
System.out.println("Wake-up service has been selected.");
break;
case 4:
System.out.println("No service has been selected.");
break;
default:
System.out.println("Invalid service number!");
break;
}
}
public void checkOutAndPrintBill() {
Scanner scanner = new Scanner(System.in);
System.out.print("Enter room number: ");
int roomNo = scanner.nextInt();
if (roomNo < 1 || roomNo > rooms.length) {
System.out.println("Invalid room number!");
return;
}
if (!rooms[roomNo - 1].isOccupied()) {
System.out.println("The room is not occupied!");
return;
}
System.out.println("Room " + roomNo + " bill:");
System.out.println("Room charge: $100");
if (rooms[roomNo - 1].getServices().size() > 0) {
System.out.println("Services:");
for (String service : rooms[roomNo - 1].getServices()) {
if (service.equals("Room service")) {
System.out.println("- Room service: $20");
} else if (service.equals("Laundry service")) {
System.out.println("- Laundry service: $10");
} else if (service.equals("Wake-up service")) {
System.out.println("- Wake-up service: $5");
}
}
} else {
System.out.println("No services selected.");
}
System.out.println("Total: $" + (100 + rooms[roomNo - 1].getTotalServiceCost()));
rooms[roomNo - 1].setOccupied(false);
rooms[roomNo - 1].clearServices();
}
public static void main(String[] args) {
HotelManagementSystem hotel = new HotelManagementSystem(10);
Scanner scanner = new Scanner(System.in);
while (true) {
System.out.println("Menu:");
System.out.println("1. Show room status");
System.out.println("2. Book room");
System.out.println("3. Check out");
System.out.println("4. Select services");
System.out.println("5. Check out and print bill");
System.out.println("6. Exit");
System.out.print("Enter your choice: ");
int choice = scanner.nextInt();
switch (choice) {
case 1:
hotel.showRooms();
break;
case 2:
hotel.bookRoom();
break;
case 3:
hotel.checkOut();
break;
case 4:
hotel.selectServices();
break;
case 5:
hotel.checkOutAndPrintBill();
break;
case 6:
System.exit(0);
default:
System.out.println("Invalid choice!");
break;
}
}
}
}
class Room {
private int roomNo;
private boolean occupied;
private List<String> services;
public Room(int roomNo) {
this.roomNo = roomNo;
this.occupied = false;
this.services = new ArrayList<String>();
}
public int getRoomNo() {
return roomNo;
}
public boolean isOccupied() {
return occupied;
}
public void setOccupied(boolean occupied) {
this.occupied = occupied;
}
public List<String> getServices() {
return services;
}
public void addService(String service) {
services.add(service);
}
public int getTotalServiceCost() {
int total = 0;
for (String service : services) {
if (service.equals("Room service")) {
total += 20;
} else if (service.equals("Laundry service")) {
total += 10;
} else if (service.equals("Wake-up service")) {
total += 5;
}
}
return total;
}
public void clearServices() {
services.clear();
}
@Override
public String toString() {
return "Room " + roomNo + ": " + (occupied ? "Occupied" : "Available");
}
}
```
测试类:
```java
public class HotelManagementSystemTest {
public static void main(String[] args) {
HotelManagementSystem hotel = new HotelManagementSystem(10);
hotel.showRooms();
hotel.bookRoom();
hotel.bookRoom();
hotel.showRooms();
hotel.selectServices();
hotel.checkOutAndPrintBill();
hotel.showRooms();
}
}
```
输出:
```
Room Status:
Room 1: Available
Room 2: Available
Room 3: Available
Room 4: Available
Room 5: Available
Room 6: Available
Room 7: Available
Room 8: Available
Room 9: Available
Room 10: Available
Enter room number: 1
Room 1 has been booked.
Enter room number: 2
Room 2 has been booked.
Room Status:
Room 1: Occupied
Room 2: Occupied
Room 3: Available
Room 4: Available
Room 5: Available
Room 6: Available
Room 7: Available
Room 8: Available
Room 9: Available
Room 10: Available
Enter room number: 1
Services:
1. Room service
2. Laundry service
3. Wake-up service
4. None
Enter service number: 1
Room service has been selected.
Enter room number: 1
Room 1 bill:
Room charge: $100
Services:
- Room service: $20
Total: $120
Room 1 has been checked out.
Room Status:
Room 1: Available
Room 2: Occupied
Room 3: Available
Room 4: Available
Room 5: Available
Room 6: Available
Room 7: Available
Room 8: Available
Room 9: Available
Room 10: Available
```
阅读全文