简体   繁体   English

它们与第三个类相关联的两个类之间有什么关系?

[英]What is the relationship between two classes they are tied to a Third class?

Getting stuck with my Travail System Project, Confusing a little bit about understanding that if there Classes called Bookable, Hotel and BookingSystem.被我的 Travail System 项目困住了,对于是否有名为 Bookable、Hotel 和 BookingSystem 的类的理解有点困惑。 Hotel class is implements Bookable.酒店类是实现 Bookable 的。 Furthermore, BookingSystem Class is composition from Bookable, So, I need to create method at BookingSystem class which called addHotel.此外,BookingSystem 类是由 Bookable 组成的,因此,我需要在 BookingSystem 类中创建调用 addHotel 的方法。 what I must do about it to make a relationship between Hotel Class and BookingSystem Class.我必须做些什么才能在 Hotel Class 和 BookingSystem Class 之间建立关系。 Thanks In Advance.提前致谢。 Israa以色列

Hotal Class:热点类:

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;
}
}

Bookable class:可预订班级:

 public interface Bookable {
 public String Book();
 }

BookingSytsem Class:预订系统类:

 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();
         }   
    }
}
}

(Your question is more suitable to https://softwareengineering.stackexchange.com/ - recommend asking it there...) (您的问题更适合https://softwareengineering.stackexchange.com/ - 建议在那里询问...)

General speaking it wouldn't make sense to have a Hotel without a name, location or number of rooms so I'd recommend adding a constructor with minimal required information:一般来说,没有名称、位置或房间数量的Hotel没有意义的,所以我建议添加一个具有最少所需信息的构造函数:

public Hotel (String name, String location, int rooms) {
    this.name = name;
    this.location = location;
    this.noOfRooms = rooms;
}

bookingDate makes no sense as a single property of a hotel but rather a property of each booked room so you have a design issue - this is not addressed here. bookingDate作为酒店的单一属性没有意义,而是每个预订房间的属性,所以你有一个设计问题——这里没有解决。

Again, roomPrice usually varies by room so in a robust solution this would be a property of a room not a hotel - not addressed here.同样, roomPrice通常因房间而异,因此在稳健的解决方案中,这将是房间而非酒店的属性 - 此处未提及。

Why is there a noOfRooms and a numberOfRooms list.为什么有noOfRoomsnumberOfRooms列表。 In fact, the numberOfRooms list doesn't make sense as a list.事实上, numberOfRooms列表作为列表没有意义。 I'd just keep the noOfRooms and get rid of numberOfRooms .我只是保留noOfRooms并摆脱numberOfRooms

An implied property, nbrOfAvailableRooms can be derived from noOfRooms - bookedRooms.size();隐含属性nbrOfAvailableRooms可以从noOfRooms - bookedRooms.size();派生noOfRooms - bookedRooms.size();

I would assume your bookedRooms is a list of room numbers which are booked but that's not possible to tell from your implementation.我假设您的bookedRooms是已预订的房间号列表,但无法从您的实现中看出。 You should focus on what you want Book to do.你应该专注于你想让Book做的事情。

The Book interface method is not documented but it looks like it should simply take an available asset (room) and consider it booked. Book接口方法没有记录,但看起来它应该简单地获取可用资产(房间)并将其视为已预订。 It should return a boolean success not a String - especially not null .它应该返回一个布尔值 success 而不是 String - 特别是不是null

I recommend writing (in pseudo code) what you want a Book implementation to do - include that in question.我建议编写(用伪代码)你希望Book实现做什么 - 包括有问题的内容。 That is the core issue you are having.这是您遇到的核心问题。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM