dimanche 24 décembre 2017

calculating cheapest price using oops concepts

Problem is to calculate cheapest hotel based on given data and input.

Hotel         Weekend_price                         Weekday_price
         regular_cust    priviledge_cust     regular_cust       priviledge_cust

hotel_1      100               90               80                 70


hotel_2      120               100              70                 50


hotel_3      150               120              120                80

Input is regular customer & list of days.

So output is to find out cheapest hotel based on above data. I calculated it with this way.

public class Hotel {

    String name;
    Integer rating;
    List<Price> prices = new ArrayList<Price>();
// getter, setter

public class Price {

    DayType dayType;
    CustomerType customerType;
    Integer price;
// getter, setter

all the above data is preloaded during start of application which is in 
    static List<Hotel> hotels = new ArrayList<Hotel>();


Step is to iterate through each hotel 


        String cheapestHotel = "";
        Integer totalCost = 0;

        for (final Hotel hotel : hotels) {
            Integer hotelCost = 0;
            final List<Date> dates = input.getDates();

            for (final Date date : dates) {
                if (DayType.isWeekend(date) && input.customerType.equals(CustomerType.Regular)) {

                    hotelCost += hotel.getWeekendPriceForRegular();
                } else if (DayType.isWeekend(date) && input.customerType.equals(CustomerType.Priviledge)) {
                    hotelCost += hotel.getWeekendPriceForPriviledge();
                }(!DayType.isWeekend(date) && input.customerType.equals(CustomerType.Regular)) {
                    hotelCost += hotel.getWeekdayPriceForRegular();
                }(DayType.isWeekend(date) && input.customerType.equals(CustomerType.Priviledge)) {
                    hotelCost += hotel.getWeekdayPriceForPriviledge();
                }
            }
            if (hotelCost < totalCost) {
                cheapestHotel = hotel.getName();
                totalCost = hotelCost;
            }
        }

This is working but I feel there could be better way of adding design pattern instead of if else loop but the problem is that price is based on both DayType & customer type so can't think of any better solution of applying oops concepts here.

Can you help with the design for this application?

Aucun commentaire:

Enregistrer un commentaire