首先在程序中,定义的酒店共有四层,除去前台一层,剩下的三层是客房,一层楼一共有八间客房,在酒店中四层包括四号房间由于谐音的缘故一般不用来做楼层和房间号标记,故都用五来代替。另外,房间信息(这里主要指的是房间是否为空房)利用散列表的数据结构进行存储和修改。
以下是程序代码和运行结果:
package cn.sxt.thread;
import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.GregorianCalendar;
import java.util.Map;
import java.util.Scanner;
import java.util.concurrent.TimeUnit;
public class Hotel extends HashMap02 {
public String name;
public int roomNum;
public static boolean flag2=false;
public int print() {
System.out.println("请输入入住人的房间号:");
Scanner scanner1=new Scanner(System.in);
roomNum=scanner1.nextInt();
return roomNum;
}
public boolean date() throws ParseException {
int day,month,year,num;
boolean flag=true,sign=true,flag1=false;
System.out.println("请输入入住日期(格式为:yyyy-MM-dd):");
Scanner scanner2=new Scanner(System.in);
String str=scanner2.nextLine();
DateFormat df=new SimpleDateFormat("yyyy-MM-dd");
Date date=df.parse(str);
Calendar c=new GregorianCalendar();
c.setTime(date);
year=c.get(Calendar.YEAR);
month=c.get(Calendar.MONTH);
day=c.get(Calendar.DAY_OF_MONTH);
System.out.println("请输入入住天数:");
Scanner scanner3=new Scanner(System.in);
int days=scanner3.nextInt();
days=days+day+1;
if((year%4==0&&year%100!=0)||(year%400==0)) flag=false;
if((c.get(Calendar.MONTH)==Calendar.FEBRUARY)&&flag==false)
{
sign=false;
num=29;
if(days>num) {
days=days-num;
month=month+2;
}
}
if(flag==true&&sign==true)
{
switch (c.get(Calendar.MONTH)) {
case 0:
num=31;
if(days>num) {
days=days-num;
month=month+2;
}
else
month++;
break;
case 1:
num=28;
if(days>num) {
days=days-num;
month=month+2;
}
else
month++;
break;
case 2:
num=31;
if(days>num) {
days=days-num;
month=month+2;
}
else
month++;
break;
case 3:
num=30;
if(days>num) {
days=days-num;
month=month+2;
}
else
month++;
break;
case 4:
num=31;
if(days>num) {
days=days-num;
month=month+2;
}
else
month++;
break;
case 5:
num=30;
if(days>num) {
days=days-num;
month=month+2;
}
else
month++;
break;
case 6:
num=31;
if(days>num) {
days=days-num;
month=month+2;
}
else
month++;
break;
case 7:
num=31;
if(days>num) {
days=days-num;
month=month+2;
}
else
month++;
break;
case 8:
num=30;
if(days>num) {
days=days-num;
month=month+2;
}
else
month++;
break;
case 9:
num=31;
if(days>num) {
days=days-num;
month=month+2;
}
else
month++;
break;
case 10:
num=30;
if(days>num) {
days=days-num;
month=month+2;
}
else
month++;
break;
default:
num=31;
if(days>num) {
days=days-num;
month=month+2;
}
else
month++;
break;
}
}
if(flag2==true)
{
System.out.println("离店日期:"+year+"年"+month+"月"+days+"日");
Calendar c1=Calendar.getInstance();
int year1=c1.get(Calendar.YEAR);
int month1=c1.get(Calendar.MONTH);
int days1=c1.get(Calendar.DAY_OF_MONTH);
if(year1==year&&(month1+1)==month&&days1==days) {
System.out.println(roomNum+"房间到了退房日期了!");
System.out.println("顾客是否办理了退房手续?(回答1代表是0代表否)");
Scanner scanner4=new Scanner(System.in);
int n=scanner4.nextInt();
if(n==1) {
System.out.println(roomNum+"房间可以办理新顾客入住了!");
flag1=true;
}
else{
System.out.println("请尽快通知顾客中午12点以前办理退房手续!");
}
}
}
return flag1;
}
public static void main(String[] args) throws InterruptedException{
HashMap02<Integer,Boolean> map=new HashMap02<>();
map.put(201,false);
map.put(202,true);
map.put(203,true);
map.put(205,true);
map.put(206,false);
map.put(207,true);
map.put(208,false);
map.put(209,false);
map.put(301,true);
map.put(302,true);
map.put(303,false);
map.put(305,true);
map.put(306,true);
map.put(307,false);
map.put(308,false);
map.put(309,true);
map.put(501,false);
map.put(502,false);
map.put(503,true);
map.put(505,true);
map.put(506,true);
map.put(507,true);
map.put(508,true);
map.put(509,true);
while(true) {
Hotel hotel=new Hotel();
System.out.println("请输入入住人的姓名:");
Scanner scanner=new Scanner(System.in);
hotel.name=scanner.nextLine();
try {
int roomNum=hotel.print();
if(map.get(roomNum)==true) {
System.out.println(roomNum+"房间现为空房可以办理入住!");
map.set(roomNum, false);
flag2=true;
TimeUnit.SECONDS.sleep(2);
}
else {
System.out.println(roomNum+"房间已满房不能办理入住!");
System.out.println("该顾客是否已经登记入住?(请回复true或者false)");
Scanner in=new Scanner(System.in);
boolean answer=in.nextBoolean();
if(answer==true)
{
flag2=true;
hotel.date();
continue;
}
else
{
TimeUnit.SECONDS.sleep(2);
continue;
}
}
boolean flag1=hotel.date();
if(flag1==true) {
map.set(roomNum,true);
}
}
catch (ParseException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
finally {
Thread.sleep(3000);
}
}
}
}