预订系统项目开发疑问:如何通过addHotel方法建立Hotel类与BookingSystem类的关联
Hey Israa, let's break down how to fix your addHotel method and get the composition relationship between BookingSystem and Hotel (via Bookable) working correctly. I'll walk through the issues in your current code and show you the corrected implementation step by step.
首先,分析你现有addHotel方法的问题
- 你创建了
Hotel H1但没有给它设置任何用户输入的属性(比如name),直接就加到bookable列表里,这样这个Hotel实例是没有数据的 - 输出语句
System.out.println("The Hotel " + name + "added");里的name是你声明的Scanner对象,不是用户输入的酒店名称n1,这会打印出类似java.util.Scanner[delimiters=\p{javaWhitespace}+]...的无效信息 - 没有处理酒店的其他必要属性(location、房间数量、房价等),用户无法完整添加酒店信息
修正后的addHotel实现
我们可以实现两种版本的addHotel方法:一种是直接接收已创建好的Hotel对象(更灵活,适合程序内部调用),另一种是让用户输入信息创建并添加(适合交互式操作)。
版本1:直接传入Hotel对象的addHotel方法
这个方法符合组合关系的设计——BookingSystem作为组合的容器,负责管理Bookable实例(这里是Hotel):
public void addHotel(Hotel hotel) { if (hotel != null && !bookable.contains(hotel)) { bookable.add(hotel); System.out.println("Hotel '" + hotel.getName() + "' added successfully to the booking system"); } else { System.out.println("Invalid hotel or hotel already exists in the system"); } }
版本2:交互式输入酒店信息的addHotel方法
如果你需要让用户通过控制台输入来添加酒店,修正后的代码如下(修复了输入和属性设置的问题):
import java.util.Scanner; public void addHotel() { Scanner scanner = new Scanner(System.in); Hotel newHotel = new Hotel(); System.out.println("Please Enter the name of Hotel: "); String hotelName = scanner.nextLine(); newHotel.setName(hotelName); System.out.println("Please Enter the location of Hotel: "); String hotelLocation = scanner.nextLine(); newHotel.setLocation(hotelLocation); System.out.println("Please Enter the number of rooms: "); while (!scanner.hasNextInt()) { System.out.println("Invalid input! Please enter a number: "); scanner.next(); } int roomCount = scanner.nextInt(); newHotel.setNoOfRooms(roomCount); scanner.nextLine(); // 清除换行符 System.out.println("Please Enter the room price per night: "); while (!scanner.hasNextDouble()) { System.out.println("Invalid input! Please enter a valid price: "); scanner.next(); } double price = scanner.nextDouble(); newHotel.setRoomPrice(price); bookable.add(newHotel); System.out.println("Hotel '" + hotelName + "' added successfully to the booking system"); }
关于组合关系的说明
你的BookingSystem类已经通过private ArrayList<Bookable> bookable建立了和Bookable(包括Hotel)的组合关系:
BookingSystem是容器类,它拥有多个Bookable实例- 当
BookingSystem对象被销毁时,它维护的bookable列表中的实例也会失去引用(符合组合关系“部分随整体销毁”的特性) - 你可以通过
bookable列表对所有酒店(或其他实现Bookable的类,比如Flight)进行统一管理(比如查找、删除、批量操作)
额外的小问题修正
Hotel类的Book方法逻辑问题:
你当前的Book方法逻辑混乱,比如numberOfrooms列表似乎没被初始化和使用,而且修改bookedRooms的方式会导致数据错误。这里给你一个简单的修正版本(假设预订一个房间):@Override public String Book() { if (bookedRooms.size() < noOfRooms) { // 找到第一个可用房间号(假设房间号从1开始) int roomNumber = bookedRooms.size() + 1; bookedRooms.add(roomNumber); return "Successfully booked room " + roomNumber + " at " + name; } else { return "No available rooms at " + name; } }注意要给
Bookable接口的方法加上@Override注解,保持代码规范。BookingSystem的makeABooking方法死循环:
你当前的while(true)里的条件ID == -1 && ID == 0永远不可能成立,会导致无限循环。这里修正为合理的输入验证:public void makeABooking(Customer c, Bookable b) { Scanner input = new Scanner(System.in); System.out.println("Please Enter your name: "); String name = input.nextLine(); System.out.println("Please Enter your ID: "); int ID = -1; while (true) { if (input.hasNextInt()) { ID = input.nextInt(); if (ID > 0) { // 假设ID必须是正整数 break; } else { System.out.println("Invalid ID. Enter a positive number: "); } } else { System.out.println("Invalid input. Enter a numeric ID: "); input.next(); // 清除无效输入 } } // 执行预订逻辑 String bookingResult = b.Book(); System.out.println(bookingResult); // 可以把操作记录到operation列表 operation.add(new Operation(c, b, new Date())); }
内容的提问来源于stack exchange,提问作者is89r

