מימוש interface
נתקלתי בשאלה ברשת של מימוש hotel manager interface - מישהו יכול לעלות את הפתרון שלו עם hashMap?
מקבלים interface + Reservation class ואמורים לממש ב-class אחר
זה הלינק לשאלה -
[URL]https://codereview.stackexchange.com/questions/176487/implementing-an-hotelmanager-interface-in-java[/URL]
וזה ה- Reservation
נתקלתי בשאלה ברשת של מימוש hotel manager interface - מישהו יכול לעלות את הפתרון שלו עם hashMap?
מקבלים interface + Reservation class ואמורים לממש ב-class אחר
זה הלינק לשאלה -
[URL]https://codereview.stackexchange.com/questions/176487/implementing-an-hotelmanager-interface-in-java[/URL]
קוד:
package test;
import java.time.LocalDate;
import java.util.List;
publicinterfaceHotelManager{
/**
* Sets the number of rooms in the hotel.
*/
void setNumberOfRooms(int numRooms);
/**
* Tries to add a reservation to the system.
* Reservation will be added successfully only if during the whole time frame from its fromDate to its toDate there is
* a free room in the hotel.
* @param reservation reservation to add
* @return true if added reservation successfully. False otherwise.
*/
boolean makeReservation(Reservation reservation);
/**
* Cancels the reservation with the given id.
* @param reservationId id of reservation to cancel
*/
void cancelReservation(int reservationId);
/**
* Get the reservation with the given id.
* @param reservationId id of reservation to fetch.
* @return Reservation with the given id or null if no reservation with that id exists.
*/
Reservation getReservation(int reservationId);
/**
* Return the number of available rooms on the given date.
* @param dateToCheck date to check number of available rooms
* @return number of available rooms on the given date.
*/
int getNumberAvailableRooms(LocalDate dateToCheck);
/**
* Get the price of all reservations that start on or after the given from date AND end on or before the given to date
* (if a reservation starts before the given from date or ends after the given to date don't count it)
* @return the sum of prices of all reservations that start and end during the given timeframe
*/
int getPriceOfReservations(LocalDate from,LocalDate to);
/**
* Gets all the reservations that start on or after the given from date AND end on or before the given to date
* sorted by price in an ASCENDING order.
*/
List<Reservation> getAllReservationsSortedByPrice(LocalDate from,LocalDate to);
/**
* Gets all the reservations that start on or after the given from date AND end on or before the given to date
* sorted by date in an ASCENDING order.
*/
List<Reservation> getAllReservationsSortedByDate(LocalDate from,LocalDate to);
}
וזה ה- Reservation
קוד:
privatefinalint id;
privatefinalLocalDate fromDate;
privatefinalLocalDate toDate;
privatefinalint price;
publicReservation(LocalDate fromDate,LocalDate toDate,int price){
this.id = generateRandomId();
this.fromDate = fromDate;
this.toDate = toDate;
this.price = price;
}
publicint getId(){
return id;
}
publicLocalDate getFromDate(){
return fromDate;
}
publicLocalDate getToDate(){
return toDate;
}
publicint getPrice(){
return price;
}
privateint generateRandomId(){
return(int)(Math.random()*10000000);
}
publicString toString(){
return"id: "+ id +", from: "+ fromDate +", to: "+ toDate +", price: "+ price;
}
}