Write a complete program to create HotelRoom class and test class based on the class
diagram above, table below and the following requirements: HotelRoom Class is derived
from Rental and implements Comparable Interface.
The getRentalRate() method from the Rental class will be implemented in the HotelRoom
class to determine and return a room rental rate based on the room types. The room types
and its associated rental rate can be referred in Table 1.
The method toString() will return the information of HotelRoom object.
The HotelRoom class implements compareTo() method from the interface
java.langComparable to compare the room rental rate for two objects of HotelRoom.
Write a test program that creates two HotelRoom objects with any values of the rentalNum,
roomNo and roomTypes.
Display all the information of these two objects including the rental rate comparison result.
public class HotelRoom extends Rental implements Comparable<HotelRoom> {
private int rentalNum;
private int roomNo;
private String roomTypes;
public HotelRoom(int rentalNum, int roomNo, String roomTypes) {
this.rentalNum = rentalNum;
this.roomNo = roomNo;
this.roomTypes = roomTypes;
}
@Override
public double getRentalRate() {
return -1;
}
@Override
public int compareTo(HotelRoom o) {
return Double.compare(getRentalRate(), o.getRentalRate());
}
@Override
public String toString() {
return rentalNum + " " + roomNo + " " + roomTypes + " " + getRentalRate();
}
}
public class Main {
public static void main(String[] args) {
HotelRoom one = new HotelRoom(1,1,"1");
HotelRoom two = new HotelRoom(2,2,"2");
System.out.println(one);
System.out.println(two);
System.out.println(one.compareTo(two));
}
}
Comments
Leave a comment