为了账号安全,请及时绑定邮箱和手机立即绑定

在某城市有三家连锁酒店,希望大神们帮忙分析分析

在某城市有三家连锁酒店,希望大神们帮忙分析分析

慕少森 2023-01-02 19:15:03

分别为:如家, 七天和汉庭.每一家分店都有自己的评级和不同的价格。
如家 3星级周一到周五普通顾客110,会员80。周末普通顾客 90 ,会员 80;
七天 4星级周一到周五普通顾客160,会员110。周末普通顾客 60 ,会员50;
汉庭 5星级周一到周五普通顾客220,会员100。周末普通顾客 150 ,会员40;
设计一个程序帮助顾客能够根据自己的需要找到最便宜的酒店。
样例输入:普通顾客 2009-3-16 2009-3-18
样例输出: 如家
样例输入:会员顾客 2016-7-31 2016-8-2
样例输出: 汉庭
如果,在结果中有两个或两个以上价格相同的酒店,则选择评级高的那一个

要求:
1.只可以使用jdk和junit,不许使用其它任何第三方包
2.程序以main函数启动,在控制台读取用户的输入,并在控制台输出结果。

查看完整描述

2 回答

?
喵喔喔

TA贡献1492条经验 获得超5个赞

public static void main(String[] args) {
Scanner input = new Scanner(System.in);
System.out.println("是否是会员?y/n");
String H = input.next();
System.out.println("请输入入住日期:");
System.out.println("例如2009-03-16");
String in = input.next();
System.out.println("请输离开日期:");
System.out.println("例如2009-03-16");
String out = input.next();
//String in = s.substring(5,15);
//String out = s.substring(15,26);
//System.out.println(in+" "+out);
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
Calendar c1 = Calendar.getInstance();
Calendar c2 = Calendar.getInstance();
try {
c1.setTime(sdf.parse(in));
c2.setTime(sdf.parse(out));
} catch (ParseException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
long time1 = c1.getTimeInMillis();
long time2 = c2.getTimeInMillis();
long tian = (time2-time1)/(1000*24*3600);
long tian1 = tian;
System.out.println("tian "+tian);
int week1 = c1.get(Calendar.DAY_OF_WEEK)-1;
//System.out.println(week1);
//int week2 = c2.get(Calendar.DAY_OF_WEEK)-1;
//System.out.println(week2);
int count = 0;
while(tian1>0){
if(week1 == 6 || week1==0){
count++;
}
if(week1==6){
week1 = 0;
week1--;
}
tian1--;
week1++;
}
System.out.println("count "+count);
long rujia;
long qitian;
long hanting;
if(H.equals("y")){
rujia = (tian-count)*80+count*80;
System.out.println("tian "+tian);
qitian = (tian-count)*110+count*50;
hanting = (tian-count)*100+count*40;
}else{
rujia = (tian-count)*110+count*90;
qitian = (tian-count)*160+count*60;
hanting = (tian-count)*220+count*150;
}
System.out.println("tian " + tian);
System.out.println("rujia "+rujia);
System.out.println("hanting "+hanting);
System.out.println("qitian "+qitian);
if(rujia<qitian && rujia<hanting){
System.out.println("如家");
}else if(qitian<rujia && qitian<hanting){
System.out.println("七天");
}else if(hanting<rujia && hanting<qitian){
System.out.println("汉庭");
}else if(hanting==rujia || hanting==qitian){
System.out.println("汉庭");
}else if(qitian==rujia && qitian<hanting){
System.out.println("七天");
}
}
我没怎么测试 自己测测吧 还有一些约束用户输入啥的 严谨性什么的


查看完整回答
反对 回复 2023-01-06
?
隔江千里

TA贡献0条经验 获得超10个赞

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-7
public 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();
}
}
}
本代码已经测试,准确可用!

 


查看完整回答
反对 回复 2023-01-06

添加回答

举报

0/150
提交
取消
意见反馈 帮助中心 APP下载
官方微信