我的Travail系统项目卡住了,有点困惑,如果有类叫Bookable, Hotel和BookingSystem。Hotel类实现了Bookable。此外,BookingSystem类是由Bookable组成的,所以,我需要在BookingSystem类中创建名为addHotel的方法。我该怎么做才能把Hotel Class和BookingSystem Class联系起来。提前感谢。Israa
酒店类:
public class Hotel implements Bookable {
private String name, location;
private int noOfRooms;
private double roomPrice;
private Date bookingDate;
private ArrayList<Integer> bookedRooms = new ArrayList<Integer>();
private ArrayList<Integer> numberOfrooms = new ArrayList<Integer>();
public Hotel() {
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getLocation() {
return location;
}
public void setLocation(String location) {
this.location = location;
}
public int getNoOfRooms() {
return noOfRooms;
}
public void setNoOfRooms(int noOfRooms) {
this.noOfRooms = noOfRooms;
}
public double getRoomPrice() {
return roomPrice;
}
public void setRoomPrice(double roomPrice) {
this.roomPrice = roomPrice;
}
public Date getBookingDate() {
return bookingDate;
}
public void setBookingDate(Date bookingDate) {
this.bookingDate = bookingDate;
}
public ArrayList<Integer> getBookedRooms() {
return bookedRooms;
}
public void setBookedRooms(ArrayList<Integer> bookedRooms) {
this.bookedRooms = bookedRooms;
}
public String Book() {
if ( numberOfrooms.size() != (bookedRooms.size())) {
for (int i = 0; i < bookedRooms.size(); i++) {
int oldVal = bookedRooms.get(i);
int newVal = oldVal + 1;
bookedRooms.add(bookedRooms.set(i, newVal));
}
}
return null;
}
}
可预订的类:
public interface Bookable {
public String Book();
}
BookingSytsem类:
public class BookingSystem {
private ArrayList<Customer> customer = new ArrayList<Customer>();
private ArrayList<Bookable> bookable = new ArrayList<Bookable>();
private ArrayList<Operation> operation = new ArrayList<Operation>();
public BookingSystem() {
}
// **
public void addCustomer(String name, int id) {
Customer customers = new Customer(id, name);
customer.add(customers);
System.out.println("new customer " + customers.getName() + " added");
}
// **
public void deleteCustomer(String name, int id) {
Customer customers = new Customer(id, name);
if (customer.contains(name)) {
customer.remove(name);
}
System.out.println("Customer " + customers.getName() + " deleted");
}
public Customer findCustomer(int id) {
for (Customer c : customer) {
if (c.getId() == id) {
return c;
}
}
return null;
}
public void addHotel() {
Hotel H1 = new Hotel();
Scanner name = new Scanner(System.in);
System.out.println("Please Enter the name of Hotel: ");
String n1 = name.nextLine();
bookable.add(H1);
System.out.println("The Hotel " + name + "added");
}
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 = input.nextInt();
while(true) {
if(ID == -1 && ID == 0) {
System.out.println("Invalid ID. Enter again: ");
name = input.nextLine();
System.out.println("Please Enter your ID: ");
ID = input.nextInt();
}
}
}
}
(您的问题更适合https://softwareengineering.stackexchange.com/-建议在那里询问…)
一般来说,没有名称,位置或房间数量的Hotel
是没有意义的,所以我建议添加一个具有最少所需信息的构造函数:
public Hotel (String name, String location, int rooms) {
this.name = name;
this.location = location;
this.noOfRooms = rooms;
}
bookingDate
作为酒店的单个属性是没有意义的,而是每个预订房间的属性,所以你有一个设计问题-这里没有解决这个问题。
同样,roomPrice
通常因房间而异,因此在一个健壮的解决方案中,这将是房间的属性,而不是酒店的属性——这里没有提到。
为什么有noOfRooms
和numberOfRooms
列表。事实上,numberOfRooms
列表作为一个列表没有意义。我会保留noOfRooms
,去掉numberOfRooms
。
隐含属性,nbrOfAvailableRooms
可以从noOfRooms - bookedRooms.size();
导出我认为你的bookedRooms
是预订的房间号码列表,但这是不可能从你的实施中告诉。你应该专注于你想让Book
做什么。
Book
接口方法没有文档化,但看起来它应该简单地获取可用资产(房间)并将其视为已预订。它应该返回一个布尔值,而不是字符串——尤其是null
。
我建议写(在伪代码)你想要一个Book
实现做什么-包括问题。这是你的核心问题。