In: Computer Science
Define the class HotelRoom. The class has the following private data members: the room number (an integer) and daily rate (a double). Include a default constructor as well as a constructor with two parameters to initialize the room number and the room’s daily rate. The class should have get/set functions for all its private data members [20pts]. The constructors and the get/set functions must throw an invalid_argument exception if either one of the parameter values are negative. The exception handler should display the message “Negative Parameter” [20pts]. Include a toString() function that nicely formats and returns a string that displays the information about the hotel room [10pts].
Thanks for the question
Here is the completed code for this problem. Comments are
included so that you understand whats going on. Let me know if you
have any doubts or if you need anything to change.
Thank You & please do give a thumbs up ;
regards, Friend !
==========================================================================================
public class HotelRoom { private int roomNumber; private double dailyRate; public HotelRoom() { roomNumber = 0; dailyRate = 0.0; } //must throw an invalid_argument exception if either one of the parameter values are negative. public HotelRoom(int roomNumber, double dailyRate) { if (roomNumber < 0 || dailyRate < 0) throw new IllegalArgumentException("Negative Parameter"); this.roomNumber = roomNumber; this.dailyRate = dailyRate; } public int getRoomNumber() { return roomNumber; } //must throw an invalid_argument exception if either one of the parameter values are negative. public void setRoomNumber(int roomNumber) { if (roomNumber < 0) throw new IllegalArgumentException("Negative Parameter"); this.roomNumber = roomNumber; } public double getDailyRate() { return dailyRate; } //must throw an invalid_argument exception if either one of the parameter values are negative. public void setDailyRate(double dailyRate) { if (dailyRate < 0) throw new IllegalArgumentException("Negative Parameter"); this.dailyRate = dailyRate; } @Override public String toString() { return "HotelRoom: " + "Room Number:" + roomNumber + ", Daily Rate: $" + dailyRate; } }
==========================================================================================
public class GuestRoom extends HotelRoom { private int capacity; private int status; private int days; public GuestRoom(int capacity, int status, int days) { if (status > capacity) throw new IllegalArgumentException("Out of Range"); this.capacity = capacity; this.status = status; this.days = days; } public GuestRoom(int roomNumber, double dailyRate, int capacity, int status, int days) { super(roomNumber, dailyRate); if (status > capacity) throw new IllegalArgumentException("Out of Range"); this.capacity = capacity; this.status = status; this.days = days; } public int getCapacity() { return capacity; } public void setCapacity(int capacity) { this.capacity = capacity; } public int getStatus() { return status; } public void setStatus(int status) { if (status > getCapacity()) throw new IllegalArgumentException("Out of Range"); this.status = status; } public int getDays() { return days; } public void setDays(int days) { this.days = days; } public double calculateBill() { return getDailyRate() * getDays() * getStatus() / getCapacity(); } @Override public String toString() { return "Guest Room: " + "Room Number:" + getRoomNumber() + ", Daily Rate: $" + getDailyRate()+ ", Room Capacity: " + capacity + ", Persons: " + status + ", Booked for " + days +" days." ; } }
==========================================================================================
public class MeetingRoom extends HotelRoom { private int seats; private int status; private boolean isBooked; public MeetingRoom(int seats, int status, boolean isBooked) { this.seats = seats; // if status is isBooked then we set the status to 0 this.status = isBooked ? status : 0; this.isBooked = isBooked; } public MeetingRoom(int roomNumber, double dailyRate, int seats, int status, boolean isBooked) { super(roomNumber, dailyRate); this.seats = seats; // if status is isBooked then we set the status to 0 this.status = isBooked ? status : 0; this.isBooked = isBooked; } public int getSeats() { return seats; } public void setSeats(int seats) { this.seats = seats; } public int getStatus() { return status; } public void setStatus(int status) { this.status = status; } public boolean isBooked() { return isBooked; } public void setBooked(boolean booked) { isBooked = booked; } @Override public String toString() { return "Meeting Room: " + "Room Number:" + getRoomNumber() + ", Daily Rate: $" + getDailyRate() + ", Seat: " + seats + ", Persons: " + status + ", Booked: " + isBooked; } public double calculateBill() { return getSeats() * 10 + 500; } }
==========================================================================================
public class MainHotel { public static void main(String[] args) { //Write a main function to test the class HotelRoom, create a // HotelRoom object. Try to set the room rate to an invalid value // to generate an exception. Invoke the toSting() function to // display the HotelRoom object. [20pts] HotelRoom hyaat = new HotelRoom(121, 4500); //Invoke the toSting() function to //display the HotelRoom object. System.out.println(hyaat); try { // Try to set the room rate to an invalid value hyaat.setDailyRate(-4000); } catch (IllegalArgumentException e) { System.out.println(e.getMessage()); } //Write a main function to test the classes GuestRoom and MeetingRoom. GuestRoom guestRoom = new GuestRoom(122, 599, 4, 3, 10); MeetingRoom meetingRoom = new MeetingRoom(145, 200, 20, 15, true); //Invoke the calculateBills and toStirng() in each of the objects. System.out.println(guestRoom); System.out.println(meetingRoom); System.out.println("Guess Room Cost: $"+guestRoom.calculateBill()); System.out.println("Meeting Room Cost: $"+meetingRoom.calculateBill()); } }
==========================================================================================
==========================================================================================