帮忙用eclipse编写一个简单的酒店管理程序,实现服务选择、房间查询,订房、退房和结账功能代码
时间: 2024-03-21 18:42:53 浏览: 27
好的,以下是一个简单的酒店管理程序的完整代码:
Hotel.java
```java
public class Hotel {
private String hotelName;
private String address;
private String phoneNumber;
public Hotel(String hotelName, String address, String phoneNumber) {
this.hotelName = hotelName;
this.address = address;
this.phoneNumber = phoneNumber;
}
public String getHotelName() {
return hotelName;
}
public void setHotelName(String hotelName) {
this.hotelName = hotelName;
}
public String getAddress() {
return address;
}
public void setAddress(String address) {
this.address = address;
}
public String getPhoneNumber() {
return phoneNumber;
}
public void setPhoneNumber(String phoneNumber) {
this.phoneNumber = phoneNumber;
}
}
```
Room.java
```java
public class Room {
private int roomNumber;
private String roomType;
private double price;
private boolean isAvailable;
public Room(int roomNumber, String roomType, double price) {
this.roomNumber = roomNumber;
this.roomType = roomType;
this.price = price;
this.isAvailable = true;
}
public int getRoomNumber() {
return roomNumber;
}
public void setRoomNumber(int roomNumber) {
this.roomNumber = roomNumber;
}
public String getRoomType() {
return roomType;
}
public void setRoomType(String roomType) {
this.roomType = roomType;
}
public double getPrice() {
return price;
}
public void setPrice(double price) {
this.price = price;
}
public boolean isAvailable() {
return isAvailable;
}
public void setAvailable(boolean available) {
isAvailable = available;
}
}
```
Customer.java
```java
public class Customer {
private String name;
private String phoneNumber;
private int roomNumber;
private double totalPrice;
private Date checkInDate;
private Date checkOutDate;
public Customer(String name, String phoneNumber, int roomNumber, double totalPrice, Date checkInDate, Date checkOutDate) {
this.name = name;
this.phoneNumber = phoneNumber;
this.roomNumber = roomNumber;
this.totalPrice = totalPrice;
this.checkInDate = checkInDate;
this.checkOutDate = checkOutDate;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getPhoneNumber() {
return phoneNumber;
}
public void setPhoneNumber(String phoneNumber) {
this.phoneNumber = phoneNumber;
}
public int getRoomNumber() {
return roomNumber;
}
public void setRoomNumber(int roomNumber) {
this.roomNumber = roomNumber;
}
public double getTotalPrice() {
return totalPrice;
}
public void setTotalPrice(double totalPrice) {
this.totalPrice = totalPrice;
}
public Date getCheckInDate() {
return checkInDate;
}
public void setCheckInDate(Date checkInDate) {
this.checkInDate = checkInDate;
}
public Date getCheckOutDate() {
return checkOutDate;
}
public void setCheckOutDate(Date checkOutDate) {
this.checkOutDate = checkOutDate;
}
}
```
HotelManagementSystem.java
```java
import java.util.ArrayList;
import java.util.List;
public class HotelManagementSystem {
private Hotel hotel;
private List<Room> rooms;
public HotelManagementSystem(Hotel hotel) {
this.hotel = hotel;
this.rooms = new ArrayList<>();
}
public void addRoom(Room room) {
rooms.add(room);
}
public void removeRoom(int roomNumber) {
for (Room room : rooms) {
if (room.getRoomNumber() == roomNumber) {
rooms.remove(room);
return;
}
}
}
public Room getRoom(int roomNumber) {
for (Room room : rooms) {
if (room.getRoomNumber() == roomNumber) {
return room;
}
}
return null;
}
public List<Room> getAvailableRooms() {
List<Room> availableRooms = new ArrayList<>();
for (Room room : rooms) {
if (room.isAvailable()) {
availableRooms.add(room);
}
}
return availableRooms;
}
}
```
BookingSystem.java
```java
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
public class BookingSystem {
private List<Customer> customers;
public BookingSystem() {
this.customers = new ArrayList<>();
}
public void bookRoom(Customer customer) {
customer.getRoom().setAvailable(false);
customers.add(customer);
}
public void cancelBooking(Customer customer) {
customer.getRoom().setAvailable(true);
customers.remove(customer);
}
public double checkOut(Customer customer) {
double totalPrice = customer.getTotalPrice();
customers.remove(customer);
return totalPrice;
}
}
```
Main.java
```java
import java.util.Date;
import java.util.List;
import java.util.Scanner;
public class Main {
private static HotelManagementSystem hotelManagementSystem;
private static BookingSystem bookingSystem;
public static void main(String[] args) {
Hotel hotel = new Hotel("ABC Hotel", "123 Main St", "555-1234");
hotelManagementSystem = new HotelManagementSystem(hotel);
bookingSystem = new BookingSystem();
Room room1 = new Room(101, "Single", 80);
Room room2 = new Room(102, "Single", 80);
Room room3 = new Room(103, "Double", 120);
hotelManagementSystem.addRoom(room1);
hotelManagementSystem.addRoom(room2);
hotelManagementSystem.addRoom(room3);
Scanner scanner = new Scanner(System.in);
int choice = 0;
do {
System.out.println("1. View available rooms");
System.out.println("2. Book a room");
System.out.println("3. Cancel booking");
System.out.println("4. Check out");
System.out.println("5. Exit");
System.out.print("Enter your choice: ");
choice = scanner.nextInt();
switch (choice) {
case 1:
viewAvailableRooms();
break;
case 2:
bookRoom();
break;
case 3:
cancelBooking();
break;
case 4:
checkOut();
break;
case 5:
System.out.println("Goodbye!");
break;
default:
System.out.println("Invalid choice, please try again.");
break;
}
} while (choice != 5);
}
private static void viewAvailableRooms() {
List<Room> rooms = hotelManagementSystem.getAvailableRooms();
if (rooms.size() == 0) {
System.out.println("No rooms available.");
} else {
System.out.println("Available rooms:");
for (Room room : rooms) {
System.out.println("Room " + room.getRoomNumber() + " - " + room.getRoomType() + " - $" + room.getPrice() + " per night");
}
}
}
private static void bookRoom() {
Scanner scanner = new Scanner(System.in);
System.out.print("Enter your name: ");
String name = scanner.nextLine();
System.out.print("Enter your phone number: ");
String phoneNumber = scanner.nextLine();
System.out.println("Available rooms:");
List<Room> rooms = hotelManagementSystem.getAvailableRooms();
for (Room room : rooms) {
System.out.println("Room " + room.getRoomNumber() + " - " + room.getRoomType() + " - $" + room.getPrice() + " per night");
}
System.out.print("Enter the room number you want to book: ");
int roomNumber = scanner.nextInt();
Room room = hotelManagementSystem.getRoom(roomNumber);
System.out.print("Enter the check-in date (MM/dd/yyyy): ");
String checkInDateStr = scanner.next();
Date checkInDate = parseDate(checkInDateStr);
System.out.print("Enter the check-out date (MM/dd/yyyy): ");
String checkOutDateStr = scanner.next();
Date checkOutDate = parseDate(checkOutDateStr);
double totalPrice = room.getPrice() * calculateDaysBetweenDates(checkInDate, checkOutDate);
Customer customer = new Customer(name, phoneNumber, roomNumber, totalPrice, checkInDate, checkOutDate);
customer.setRoom(room);
bookingSystem.bookRoom(customer);
System.out.println("Booking confirmed. Total price: $" + totalPrice);
}
private static void cancelBooking() {
Scanner scanner = new Scanner(System.in);
System.out.print("Enter your name: ");
String name = scanner.nextLine();
System.out.print("Enter your phone number: ");
String phoneNumber = scanner.nextLine();
for (Customer customer : bookingSystem.getCustomers()) {
if (customer.getName().equals(name) && customer.getPhoneNumber().equals(phoneNumber)) {
bookingSystem.cancelBooking(customer);
System.out.println("Booking cancelled.");
return;
}
}
System.out.println("Booking not found.");
}
private static void checkOut() {
Scanner scanner = new Scanner(System.in);
System.out.print("Enter your name: ");
String name = scanner.nextLine();
System.out.print("Enter your phone number: ");
String phoneNumber = scanner.nextLine();
for (Customer customer : bookingSystem.getCustomers()) {
if (customer.getName().equals(name) && customer.getPhoneNumber().equals(phoneNumber)) {
double totalPrice = bookingSystem.checkOut(customer);
System.out.println("Thank you for staying with us. Total price: $" + totalPrice);
return;
}
}
System.out.println("Booking not found.");
}
private static Date parseDate(String dateStr) {
try {
return new SimpleDateFormat("MM/dd/yyyy").parse(dateStr);
} catch (ParseException e) {
return null;
}
}
private static int calculateDaysBetweenDates(Date startDate, Date endDate) {
long diff = endDate.getTime() - startDate.getTime();
return (int) TimeUnit.DAYS.convert(diff, TimeUnit.MILLISECONDS);
}
}
```
希望这个程序能够满足您的需求。如果您有任何疑问或需要进一步的帮助,请随时告诉我。