用java写一个酒店管理系统
时间: 2023-09-13 21:05:00 浏览: 100
好的,我可以为您提供一个简单的酒店管理系统的Java代码示例,包括以下功能:
1. 房间预订
2. 房间取消预订
3. 显示所有房间信息
4. 显示空房间信息
代码如下:
Room.java
```
public class Room {
private int roomNumber;
private String roomType;
private boolean isBooked;
public Room(int roomNumber, String roomType) {
this.roomNumber = roomNumber;
this.roomType = roomType;
this.isBooked = false;
}
public int getRoomNumber() {
return roomNumber;
}
public String getRoomType() {
return roomType;
}
public boolean isBooked() {
return isBooked;
}
public void bookRoom() {
this.isBooked = true;
}
public void cancelBooking() {
this.isBooked = false;
}
}
```
Hotel.java
```
import java.util.ArrayList;
public class Hotel {
private String hotelName;
private ArrayList<Room> rooms;
public Hotel(String hotelName) {
this.hotelName = hotelName;
this.rooms = new ArrayList<Room>();
initializeRooms();
}
private void initializeRooms() {
for (int i = 1; i <= 10; i++) {
if (i <= 5) {
rooms.add(new Room(i, "Single"));
} else {
rooms.add(new Room(i, "Double"));
}
}
}
public void displayAllRooms() {
System.out.println("Hotel Name: " + hotelName);
System.out.println("Room Number\tRoom Type\tBooked");
for (Room room : rooms) {
System.out.println(room.getRoomNumber() + "\t\t" + room.getRoomType() + "\t\t" + room.isBooked());
}
}
public void displayAvailableRooms() {
System.out.println("Hotel Name: " + hotelName);
System.out.println("Room Number\tRoom Type");
for (Room room : rooms) {
if (!room.isBooked()) {
System.out.println(room.getRoomNumber() + "\t\t" + room.getRoomType());
}
}
}
public void bookRoom(int roomNumber) {
for (Room room : rooms) {
if (room.getRoomNumber() == roomNumber) {
if (room.isBooked()) {
System.out.println("Room is already booked!");
} else {
room.bookRoom();
System.out.println("Room " + roomNumber + " booked successfully!");
}
return;
}
}
System.out.println("Invalid Room Number!");
}
public void cancelBooking(int roomNumber) {
for (Room room : rooms) {
if (room.getRoomNumber() == roomNumber) {
if (!room.isBooked()) {
System.out.println("Room is not booked!");
} else {
room.cancelBooking();
System.out.println("Booking for Room " + roomNumber + " cancelled successfully!");
}
return;
}
}
System.out.println("Invalid Room Number!");
}
}
```
Main.java
```
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Hotel hotel = new Hotel("My Hotel");
Scanner scanner = new Scanner(System.in);
while (true) {
System.out.println("\n\n1. Display All Rooms");
System.out.println("2. Display Available Rooms");
System.out.println("3. Book a Room");
System.out.println("4. Cancel Booking");
System.out.println("5. Exit");
System.out.print("Enter your choice: ");
int choice = scanner.nextInt();
switch (choice) {
case 1:
hotel.displayAllRooms();
break;
case 2:
hotel.displayAvailableRooms();
break;
case 3:
System.out.print("Enter room number to book: ");
int roomNumber = scanner.nextInt();
hotel.bookRoom(roomNumber);
break;
case 4:
System.out.print("Enter room number to cancel booking: ");
int roomNumberToCancel = scanner.nextInt();
hotel.cancelBooking(roomNumberToCancel);
break;
case 5:
scanner.close();
System.exit(0);
default:
System.out.println("Invalid Choice!");
}
}
}
}
```
这个酒店管理系统示例中,我们使用Room类来表示每个房间,并在Hotel类中管理它们。Main类包含一个简单的用户界面,允许用户执行不同的操作来管理酒店。您可以根据需要进行修改和扩展。
阅读全文