隔江千里
package chooseHotel;import java.io.BufferedReader;import java.io.IOException;import java.io.InputStreamReader;import java.text.ParseException;import java.text.SimpleDateFormat;import java.util.ArrayList;import java.util.Calendar;import java.util.Date;import java.util.List;import java.util.Scanner;public class Hotel {String hotelName;boolean isMember;boolean isWeekend;static int[][][] prices = {{{110,90},{80,80}},{{160,60},{110,50}},{{220,150},{100,40}}};public Hotel(String hotelName,boolean isMember,boolean isWeekend) {this.hotelName = hotelName;this.isMember = isMember;this.isWeekend = isWeekend;}//输入定酒店的参数(酒店名,是否会员,是否周末),返回该天的价格。public static int getDayPrice(String hotelName,boolean isMember,boolean isWeekend){int x=0,y=0,z=0;int price;if(hotelName.equals("如家")){x=0;}if(hotelName.equals("七天")){x=1;}if(hotelName.equals("汉庭")){x=2;}if(isMember)y=1;if(isWeekend)z=1;price = prices[x][y][z];return price;}//判断某一天是否是周末public static boolean isWeekend(String StrDate) throws ParseException{SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd");Date date = simpleDateFormat.parse(StrDate);if(date.toString().contains("Sat")||date.toString().contains("Sun"))return true;return false;}//输出两个指定日期之间的所有日期public static List<String> getAllDate(String StrStartDate,String StrEndDate) throws ParseException{List<String> allDate = new ArrayList<String>();SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd");Date startDate = simpleDateFormat.parse(StrStartDate);Date endDate = simpleDateFormat.parse(StrEndDate);Calendar startCalendar = Calendar.getInstance();startCalendar.setTime(startDate);Calendar endCalendar = Calendar.getInstance();endCalendar.setTime(endDate);do{allDate.add(simpleDateFormat.format(startCalendar.getTime()));startCalendar.add(Calendar.DAY_OF_MONTH, 1);}while(startCalendar.compareTo(endCalendar)<=0);return allDate;}//输入定酒店的参数(酒店名,是否会员,起始日期,终止日期),返回总价格。public static int getSumPrice(String hotelName,boolean isMember,String StrStartDate,String StrEndDate) throws ParseException{int sumPrice = 0;List<String> allDate = Hotel.getAllDate(StrStartDate, StrEndDate);boolean isWeekend;for(String strDate:allDate){isWeekend = Hotel.isWeekend(strDate);sumPrice += Hotel.getDayPrice(hotelName,isMember,isWeekend);}return sumPrice;}//依次输入"如家"、"七天"、"汉庭"的总价,选出最优的那家public static String chooseHotel(int rujiaSumPrice,int qitianSumPrice,int hantingSumPrice){if(rujiaSumPrice<qitianSumPrice && rujiaSumPrice<hantingSumPrice){return "如家";}else if(qitianSumPrice<=rujiaSumPrice && qitianSumPrice < hantingSumPrice){return "七天";}else {return "汉庭";}}//根据样例输入,找最适合的酒店,样例为:普通顾客 2018-1-1 2018-1-7public static String chooseHotelByCustomerInput(String customerInput) throws ParseException {String[] split = customerInput.split(" ");boolean isMember = split[0].contains("会员")?true:false;String StrStartDate = split[1].trim();String StrEndDate = split[2].trim();String hotelName;hotelName = "如家";int rujiaSumPrice = Hotel.getSumPrice(hotelName, isMember, StrStartDate, StrEndDate);hotelName = "七天";int qitianSumPrice = Hotel.getSumPrice(hotelName, isMember, StrStartDate, StrEndDate);hotelName = "汉庭";int hantingSumPrice = Hotel.getSumPrice(hotelName, isMember, StrStartDate, StrEndDate);return chooseHotel(rujiaSumPrice, qitianSumPrice, hantingSumPrice);}public static void main(String[] args) throws ParseException, IOException {String customerInput = null;BufferedReader br = new BufferedReader(new InputStreamReader(System.in));customerInput = br.readLine();while(!customerInput.equals("over")){System.out.println(Hotel.chooseHotelByCustomerInput(customerInput));customerInput = br.readLine();}}}本代码已经测试,准确可用!