Hotel Booking
unknown
java
a month ago
7.4 kB
2
Indexable
Never
interface Room { String getRoomNumber(); RoomType getRoomType(); double getPrice(); boolean isAvailable(Date startDate, Date endDate); void addBooking(Booking booking); } interface Booking { String getId(); Guest getGuest(); Room getRoom(); Date getStartDate(); Date getEndDate(); void cancel(); boolean conflictsWith(Date startDate, Date endDate); } interface Payment { boolean processPayment(); boolean refundPayment(); } interface Service { String getDescription(); double getCost(); void provideService(Guest guest); } interface Hotel { void addRoom(Room room); List<Room> getAvailableRooms(Date startDate, Date endDate); } class StandardRoom implements Room { private String roomNumber; private RoomType type; private double price; private List<Booking> bookings; public StandardRoom(String roomNumber, RoomType type, double price) { this.roomNumber = roomNumber; this.type = type; this.price = price; this.bookings = new ArrayList<>(); } @Override public String getRoomNumber() { return roomNumber; } @Override public RoomType getRoomType() { return type; } @Override public double getPrice() { return price; } @Override public boolean isAvailable(Date startDate, Date endDate) { for (Booking booking : bookings) { if (booking.conflictsWith(startDate, endDate)) { return false; } } return true; } @Override public void addBooking(Booking booking) { bookings.add(booking); } } class RoomBooking implements Booking { private String id; private Guest guest; private Room room; private Date startDate; private Date endDate; private boolean isCancelled; public RoomBooking(String id, Guest guest, Room room, Date startDate, Date endDate) { this.id = id; this.guest = guest; this.room = room; this.startDate = startDate; this.endDate = endDate; this.isCancelled = false; } @Override public String getId() { return id; } @Override public Guest getGuest() { return guest; } @Override public Room getRoom() { return room; } @Override public Date getStartDate() { return startDate; } @Override public Date getEndDate() { return endDate; } @Override public void cancel() { this.isCancelled = true; System.out.println("Booking cancelled successfully."); } @Override public boolean conflictsWith(Date startDate, Date endDate) { if (isCancelled) { return false; } return !(endDate.before(this.startDate) || startDate.after(this.endDate)); } } class BookingPayment implements Payment { private String id; private Booking booking; private double amount; private String paymentMethod; private boolean isProcessed; public BookingPayment(String id, Booking booking, double amount, String paymentMethod) { this.id = id; this.booking = booking; this.amount = amount; this.paymentMethod = paymentMethod; this.isProcessed = false; } @Override public boolean processPayment() { if (!isProcessed) { // Payment processing logic System.out.println("Payment processed for amount: " + amount); isProcessed = true; return true; } return false; } @Override public boolean refundPayment() { if (isProcessed) { // Refund logic System.out.println("Payment refunded for amount: " + amount); isProcessed = false; return true; } return false; } } class HotelService implements Service { private String id; private String description; private double cost; public HotelService(String id, String description, double cost) { this.id = id; this.description = description; this.cost = cost; } @Override public String getDescription() { return description; } @Override public double getCost() { return cost; } @Override public void provideService(Guest guest) { System.out.println("Service provided to guest: " + guest.getName()); } } class HotelImpl implements Hotel { private String id; private String name; private String location; private List<Room> rooms; public HotelImpl(String id, String name, String location) { this.id = id; this.name = name; this.location = location; this.rooms = new ArrayList<>(); } @Override public void addRoom(Room room) { rooms.add(room); } @Override public List<Room> getAvailableRooms(Date startDate, Date endDate) { List<Room> availableRooms = new ArrayList<>(); for (Room room : rooms) { if (room.isAvailable(startDate, endDate)) { availableRooms.add(room); } } return availableRooms; } } class Guest { private String id; private String name; public Guest(String id, String name) { this.id = id; this.name = name; } public String getName() { return name; } public Booking makeBooking(Hotel hotel, Room room, Date startDate, Date endDate) { if (room.isAvailable(startDate, endDate)) { Booking booking = new RoomBooking(UUID.randomUUID().toString(), this, room, startDate, endDate); room.addBooking(booking); System.out.println("Booking made successfully."); return booking; } else { System.out.println("Room is not available for the selected dates."); return null; } } public void cancelBooking(Booking booking) { booking.cancel(); } } public class HotelManagementSystem { public static void main(String[] args) { // Create hotel Hotel hotel = new HotelImpl("H1", "The Grand Hotel", "New York"); // Create rooms Room room1 = new StandardRoom("101", RoomType.SINGLE, 100.0); Room room2 = new StandardRoom("102", RoomType.DOUBLE, 150.0); hotel.addRoom(room1); hotel.addRoom(room2); // Create guest Guest guest = new Guest("G1", "Alice"); // Make a booking Calendar cal = Calendar.getInstance(); cal.set(2024, Calendar.AUGUST, 20); Date startDate = cal.getTime(); cal.set(2024, Calendar.AUGUST, 25); Date endDate = cal.getTime(); Booking booking = guest.makeBooking(hotel, room1, startDate, endDate); // Process payment Payment payment = new BookingPayment("P1", booking, room1.getPrice() * 5, "Credit Card"); payment.processPayment(); // Provide a service Service roomService = new HotelService("S1", "Room Service", 20.0); roomService.provideService(guest); // Cancel booking and refund payment if (booking != null) { guest.cancelBooking(booking); payment.refundPayment(); } } }
Leave a Comment