如何找到“最便宜的酒店价格”?

时间:2012-06-28 19:20:21

标签: javascript algorithm

  

酒店管理

     

一家着名的酒店在迈阿密有三家分店。即x,y和   z(实际上他们给出了名字)。每个客户都有两种类型。定期   和叛徒。此外,每个分支都有自己的等级x给出3星   评级,而y有5星评级,z有4星评级。

     

每家酒店都有特定的周末和工作日价格。 x收费100美元   平日为普通客户,周末为120美元,而90美元   工作日为获奖者,周末为60美元。同样y收费130美元   平日为普通客户,周末为150美元。而它的100美元   工作日为获奖者,周末为95美元。 z收费195美元   平日的普通客户和周末的150美元。而它的120美元   工作日收费,周末收费90美元。现在当顾客   要求您提供特定详细信息,以便找到哪家酒店   产生客户利润。如果酒店之间的平局比较   评级并提供结果。

     

输入格式:

     

常规:16Mar2010(太阳),19M2010(星期三),21Mar2010(星期五)

我已经制作了这段代码......请告诉我,我是否朝着正确的方向前进。我怎样才能使酒店价格动态

getTheday = function(aText,typeofcustomer)
    {

        this.typeofcustomer = typeofcustomer;
        myDays = ["Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday", "Sunday"];
        if(new Date(aText).getDay() == 0 || new Date(aText).getDay() == 5 || new Date(aText).getDay() == 6)
        {
            console.log("its weekend!!!");
            this.weekend(this.typeofcustomer);
        }

        console.log("it is ", myDays[new Date(aText).getDay()]);

    }

    getTheday.prototype.weekend = function(typeofcustomer)
    {
        console.log(typeofcustomer);
        this.hoteloptionsforweekend();
    }

    getTheday.prototype.hoteloptionsforweekend = function()
    {

        if(this.typeofcustomer == "rewardee")
        {
            this.hotelpricex_ = 60;
            this.hotelpricey_ = 95;
            this.hotelpricez_ = 90;
            this.minhotelprice = Math.min(this.hotelpricex_, this.hotelpricey_, this.hotelpricez_)

            console.log("min price is of hotel", this.minhotelprice);

        }

        if(this.typeofcustomer == "regular")
        {
            this.hotelpricex_ = 120;
            this.hotelpricey_ = 150;
            this.hotelpricez_ = 150;

        }

    }

1 个答案:

答案 0 :(得分:1)

// Put your price listings outside of the constructor
// so that you could fetch them from somewhere else
// later
var hotelPrices = [
        {
            name:    'Hotel X',
            daily:   { regular: 120, rewardee: 60 },
            weekend: { regular: 140, rewardee: 70 }
        },
        {
            name:    'Hotel Y',
            daily:   { regular: 150, rewardee: 95 },
            weekend: { regular: 180, rewardee: 110 }
        },
        {
            name:    'Hotel Z',
            daily:   { regular: 150, rewardee: 90 },
            weekend: { regular: 180, rewardee: 110 }
        }
    ];

// Be consistent and use correct camelCase for naming.
// And, btw, `getTheDay` name is misleading and confusing.
getTheDay = function( aText, typeOfCustomer, prices ) {
    var day = ( new Date( aText ) ).getDay();

    this.typeOfCustomer = typeOfCustomer;
    this.prices = prices;

    // isWeekend is a Boolean, indicating whether the date passed
    // is a weekend. 
    this.isWeekend = day == 0 || day == 5 || day == 6;
};

getTheDay.prototype.getMinimumPrice = function() {
    var prices = [],
        i, len;

    for( i = 0, len = this.prices.length; i < len; i += 1 ) {
        // Taking the appropriate price from the price list
        // and moving it to the new list
        prices.push( this.prices[i][ this.isWeekend ? 'weekend' : 'daily' ][ this.typeOfCustomer ] )
    }

    // A small trick to feed Math.min an array
    // instead of a list of arguments
    return Math.min.apply( Math, prices );
};

// Example usage:
var hotelRates = new getTheDay( aText, typeOfCustomer, hotelPrices );
console.log( hotelRates.getMinimumPrice() );

现在您的主要任务是创建一个方法,返回最便宜的酒店的名称。现在,在DailyJS上发布了一系列适合JavaScript初学者的文章:JS 101