简体   繁体   中英

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. Hotel class is implements Bookable. Furthermore, BookingSystem Class is composition from Bookable, So, I need to create method at BookingSystem class which called addHotel. what I must do about it to make a relationship between Hotel Class and 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...)

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:

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.

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.

Why is there a noOfRooms and a numberOfRooms list. In fact, the numberOfRooms list doesn't make sense as a list. I'd just keep the noOfRooms and get rid of numberOfRooms .

An implied property, nbrOfAvailableRooms can be derived from 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. You should focus on what you want Book to do.

The Book interface method is not documented but it looks like it should simply take an available asset (room) and consider it booked. It should return a boolean success not a String - especially not null .

I recommend writing (in pseudo code) what you want a Book implementation to do - include that in question. That is the core issue you are having.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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