logo头像

博学广问,自律静思

PHP实现酒店查询:WEB+SQL数据库+JSON-API

项目复述:

PROBLEM : HOTEL RESERVATION A hotel chain operating in Miami wishes to offer room reservation services over the internet. They have three hotels in Miami: Lakewood, Bridgewood and Ridgewood. Each hotel has separate weekday and weekend (Saturday and Sunday) rates. There are special rates for rewards customer as a part of loyalty program. Each hotel has a rating assigned to it.

  1. Lakewood with a rating of 3 has weekday rates as 110$ for regular customer and 80$ for rewards customer. The weekend rates are 90$ for regular customer and 80$ for a rewards customer.
  2. Bridgewood with a rating of 4 has weekday rates as 160$ for regular customer and 110$ for rewards customer. The weekend rates are 60$ for regular customer and 50$ for a rewards customer.
  3. Ridgewood with a rating of 5 has weekday rates as 220$ for regular customer and 100$ for rewards customer. The weekend rates are 150$ for regular customer and 40$ for a rewards customer.

Write a program to help an online customer find the cheapest hotel. The input to the program will be a range of dates for a regular or rewards customer. The output should be the cheapest available hotel. In case of a tie, the hotel with highest rating should be returned.

项目思路和特点

数据利于维护

对于一个实际生活中的项目,一个酒店的星级、价格不可能是一成不变的,为了更好地维护酒店和价格信息,利于系统后期的修改,极客人决定采用数据库管理数据。鉴于本题的要求,本项目在实现时只写了客户查询酒店信息的WEB界面,对于管理人员可能后续修改酒店的星级、名称、价格可以在数据库进行操作,本项目未予实现利于管理员操作的管理界面。

平台易拓展

本项目虽然采用PHP实现功能,客户的查询操作在WEB平台上实现,但是极客人特地在服务器上部署了生成JSON数据包的代码,利于将相关功能拓展到JAVA/C/C++等平台上。注:由于不涉及隐私信息,HTTP传值方式暂定为GET,当然post方式原理大致相同。

使用Bootstrap前端库构建界面

为了尽快实现界面和界面的美观,本项目采用开源WEB前端库:Bootstrap

项目文件结构

hotel    

公用代码

类hotelOrder:酒店订购安排类,构造函数传入开始时间,结束时间,顾客类型代号

startDate = $istartDate;//开始时间 $this -> endDate = $iendDate;//结束时间 $this -> customertype = $icustomertype;//顾客类型代号,0:普通顾客;1:rewards顾客 } function getChoices() { // $CheapestHotelName = "null"; require ('../db_connect.php'); $string = "select * from HOTEL_RESERVATION "; $str = @mysqli\_query($software\_db, $string); while ($row = mysqli\_fetch\_array($str, MYSQLI_ASSOC)) { $countsOfDay=$this->CountOfWeekend_WeekDay(); if($this->customertype==0){//普通顾客 $totalPrice\['name'\]=$row\['name'\]; $totalPrice\['price'\]=$countsOfDay\['Weekend'\]*$row\['rates\_for\_regular\_In\_weekend'\] +$countsOfDay\['WeekDay'\]*$row\['rates\_for\_regular\_In\_weekday'\]; $totalPrice\['rating'\]=$row\['rating'\]; $choice\[\]=$totalPrice; }elseif($this->customertype==1){//rewards顾客 $totalPrice\['name'\]=$row\['name'\]; $totalPrice\['price'\]=$countsOfDay\['Weekend'\]*$row\['rates\_for\_rewards\_In\_weekend'\] +$countsOfDay\['WeekDay'\]*$row\['rates\_for\_rewards\_In\_weekday'\]; $totalPrice\['rating'\]=$row\['rating'\]; $choice\[\]=$totalPrice; } } //echo $countsOfDay\['Weekend'\]." ".$countsOfDay\['WeekDay'\]; // $json = json_encode($choice); // return $json; return $choice; } function getCheapest() { $choices=$this->getChoices(); $prices = array(); $ratings = array(); foreach ($choices as $choice){ $prices\[\]=$choice\['price'\]; $ratings\[\]=$choice\['rating'\]; } array\_multisort($prices, SORT\_ASC, $ratings, SORT_ASC, $choices); return $choices; } /\*\* \* 判断日期是不是周末 */ private function isWeekend($date) { date\_default\_timezone_set('PRC'); $w = intval(date('w', strtotime($date))); if ($w === 0 || $w === 6) { return true; } else return false; } /\*\* \* 获取指定时间段内周末和工作日天数 */ private function CountOfWeekend_WeekDay() { $count\['Weekend'\]=0; $count\['WeekDay'\]=0; date\_default\_timezone_set('PRC'); for ($start = strtotime($this ->startDate); $start endDate)+86400; $start += 86400) //这里遍历日期,所以每次增加86400秒 { //echo $start.' '; $w = intval(date('w', $start)); if ($w === 0 || $w === 6) { $count\['Weekend'\]++; } else $count\['WeekDay'\]++; } return $count; } } ?>

web界面代码:

查询首页index.php:

酒店预订-powered by 王柏元

酒店预订查询系统

 

查询结果页:

result.php:

getChoices()); //echo json_encode($hotelorder->getCheapest()); $choices = $hotelorder -> getCheapest(); ?> 酒店预订查询结果-powered by 王柏元

酒店预订查询结果

返回

优先级从高到低
"; } ?>
酒店名称 总价格 酒店等级
".$choice\['name'\]."".$choice\['price'\]."".$choice\['rating'\]."

360截图-27702189 360截图-27714919

可扩展到其他平台的JSON输出代码

JsonCheapestHotel.php:

getCheapest()); ?>
  • 输出实例:[{“name”:”Lakewood”,”price”:720,”rating”:”3”},{“name”:”Ridgewood”,”price”:780,”rating”:”5”},{“name”:”Bridgewood”,”price”:870,”rating”:”4”}]
@极客人
极客人 commented at 2015-07-29

杨帆老师给的试水题,根据你的答案为你安排适合的工作

@小白
小白 commented at 2015-07-29

哪儿找的题目呢