1.Custmer类
package hotelSystem;
public class Custmer {
private static int nextId = 10000;
private int PID;
private String name;
private String phonenumber;
private Acustmer ctype; //顾客种类
private double balance; //顾客在本店办卡后的卡内余额,也可当作是押金,押金越多优惠越大
private double expense; //顾客在本店的累计消费金额,累计消费金额越多优惠越大
private double []discounts; //存在枚举种类中的打折数值太难操作,直接取出存在数组里
public Custmer(String name,String phonenumber,double balance) {
this.name = name;
this.phonenumber = phonenumber;
this.balance = balance;
this.PID = nextId;
this.expense = 0;
this.setCtype();
nextId++;
}
public void setCtype() { //根据卡中余额和消费金额,更新顾客等级和打折数组
if(balance >= 20000 || expense >= 10000)
ctype = Acustmer.GOLDEN;
else if(balance >= 10000 || expense >= 5000)
ctype = Acustmer.DIAMOND;
else if(balance >= 5000 || expense >= 1000)
ctype = Acustmer.VIP;
else ctype = Acustmer.PEARL;
discounts = new double[] {ctype.getBreakfast(),ctype.getLunch(),ctype.getDinner(),ctype.getOffice(),ctype.getGym(),ctype.getClothes()};
}
public Acustmer getCtype() {
return ctype;
}
public int getID() {
return PID;
}
public String getName() {
return name;
}
public double getDiscount(int i) {
return discounts[i];
}
public double getBalance() {
return balance;
}
public double Payway(double money) { //若需会员卡以外的形式支付,返回大于零的数字,否则返回零
if(balance >= money)
{
return 0;
}
else {
return (money - balance);
}
}
public void plusExpense(double money) {
expense += money;
}
public void plusBalance(double money) {
balance += money;
}
public void minusBalance(double money) {
if(money <= balance)
balance -= money;
else
System.out.println("Your account balance is insufficient!");
}
public String toString() {
return "ID: " + PID + "n" + "name: " + name + "n" + "phonenumber: " + phonenumber + "n" + "rank: " + ctype.name() + "n" + "balance: " + balance + "n" + "expense: " + expense;
}
public static void main(String[] args) {
Custmer cus = new Custmer("Ann","123456",20000);
System.out.println(cus);
}
}
enum Acustmer{
GOLDEN(0.7,0,0,0,0,0,0),DIAMOND(0.8,0,0,0,0,0.8,0.8),VIP(0.9,0,0,0,0.9,0.9,0.9),PEARL(1,1,1,1,1,1,1);
private Acustmer(double room,double breakfast,double lunch,double dinner,double office,double gym,double clothes) {
this.room = room;
this.breakfast = breakfast;
this.lunch = lunch;
this.dinner = dinner;
this.office = office;
this.gym = gym;
this.clothes = clothes;
}
public double getRoom() {
return room;
}
public double getBreakfast() {
return breakfast;
}
public double getLunch() {
return lunch;
}
public double getDinner() {
return dinner;
}
public double getOffice() {
return office;
}
public double getGym() {
return gym;
}
public double getClothes() {
return clothes;
}
private double room;
private double breakfast;
private double lunch;
private double dinner;
private double office;
private double gym;
private double clothes;
}
2.Room类
package hotelSystem;
import java.time.LocalDate;
import java.time.temporal.ChronoUnit;
import java.util.ArrayList;
public class Room {
private String RoomID;
private Aroom Rtype; //用枚举表示
private ArrayList dates = new ArrayList(); //存储房间的不同预定时段
private boolean state;
public Room(String RoomID) {
this.RoomID = RoomID;
this.state = false; //初始入住状态为空
String str = RoomID.substring(0,1);
switch(str) { //根据房间ID的首字母判断房间类型
case "S":
Rtype = Aroom.STANDARD;
break;
case "E":
Rtype = Aroom.EXECUTIVE;
break;
case "B":
Rtype = Aroom.BUSINESS;
break;
case "D":
Rtype = Aroom.DOUBLE;
break;
default:
this.RoomID = null;
this.Rtype = null;
break;
}
}
public String toString() {//根据是否有顾客返回不同的字符串
return "RoomID: " + RoomID + "n" + "Room Type: " + Rtype.name() + "n";
}
public String getRoomId() {
return RoomID;
}
public Aroom getRtype() {
return Rtype;
}
public boolean getState() {
return state;
}
public void setState(boolean state) {
this.state = state;
}
public boolean checkDate(LocalDate dateIN,LocalDate dateOUT) { //判断该时间段能否入住房间
if(dates.isEmpty() && dateIN.isBefore(dateOUT))
{
return true;
}
else {
boolean flag = true;
for(int i = 0; i < dates.size()&&flag; i++) {
flag = dates.get(i).checkIN(dateIN, dateOUT);
}
return flag;
}
}
public boolean checkNow(LocalDate dateNOW) { //检查当前时段房间状态
if(dates.isEmpty())
state = false;
else {
for(int i = 0; i < dates.size(); i++) {
if(!(dateNOW.isBefore(dates.get(i).getDateIN()))&&!(dateNOW.isAfter(dates.get(i).getDateOUT())))
{
state = true;
break;
}
}
}
return state;
}
public void setDate(LocalDate dateIN,LocalDate dateOUT) {
this.dates.add(new Date(dateIN,dateOUT));
}
public double calculate(LocalDate dateIN,LocalDate dateOUT) { //计算房费(不算罚金)
long duration = ChronoUnit.DAYS.between(dateIN,dateOUT);
return Rtype.getPrice()*duration;
}
public static void main(String[] args) {
Room room = new Room("S100");
System.out.println(room);
}
}
enum Aroom{ //设立四个房间等级
STANDARD(300),EXECUTIVE(2000),BUSINESS(1000),DOUBLE(500);
private Aroom(double price) {
this.price = price;
}
public double getPrice() {
return price;
}
private double price;
}
3.Date类
package hotelSystem;
import java.time.LocalDate;
public class Date { //存储房间的预定时间段
private LocalDate dateIN;
private LocalDate dateOUT;
public Date(LocalDate dateIN,LocalDate dateOUT) {
this.dateIN = dateIN;
this.dateOUT = dateOUT;
}
public LocalDate getDateIN() {
return dateIN;
}
public LocalDate getDateOUT() {
return dateOUT;
}
public void setDateIN(LocalDate dateIN) {
this.dateIN = dateIN;
}
public void setDateOUT(LocalDate dateOUT) {
this.dateOUT = dateOUT;
}
public boolean checkIN(LocalDate dateIN,LocalDate dateOUT) { //判断下一时间段是否与当前时间段有冲突
if((dateIN.isAfter(this.dateOUT)||dateIN.isEqual(this.dateOUT)||dateOUT.isEqual(this.dateIN) || dateOUT.isBefore(this.dateIN))&& dateIN.isBefore(dateOUT))
return true;
else return false;
}
}
4.Serivice类
package hotelSystem;
import java.util.ArrayList;
public class Service {
private ArrayList services = new ArrayList(6);
private Custmer custmer;
public Service(Custmer custmer) { //初始化时默认各种服务的数量为0
services.add(new service(Aservice.BREAKFAST,0));
services.add(new service(Aservice.LUNCH,0));
services.add(new service(Aservice.DINNER,0));
services.add(new service(Aservice.OFFICE,0));
services.add(new service(Aservice.GYM,0));
services.add(new service(Aservice.CLOTHES,0));
this.custmer = custmer;
}
public void setCount(int []count) { //传入数组,改变服务的数量
for(int i = 0; i < 6; i++) {
services.get(i).setCount(count[i]);
}
}
public double calculate() { //计算各种服务在得知顾客身份的情况下,最后的价钱
double cost = 0;
for(int i = 0; i < services.size(); i++)
cost += services.get(i).count*services.get(i).Stype.getPrice()*custmer.getDiscount(i);
return cost;
}
public String toString() {
StringBuffer sb = new StringBuffer();
for(int i = 0; i < services.size(); i++) {
if(services.get(i).getCount() != 0)
sb.append(services.get(i).toString() + "n");
}
sb.append("Total Price Of Service(after discount):" + calculate() + "n");
return sb.toString();
}
class service{ //Service内部类service
private Aservice Stype;//记录服务的种类
private int count; //记录该种服务的数量
public service(Aservice Stype,int count) {
this.Stype = Stype;
this.count = count;
}
public Aservice getStype() {
return Stype;
}
public int getCount() {
return count;
}
public void setCount(int count) {
this.count = count;
}
public String toString() {
return "Stype:" + Stype.name() + "t Number:" + count + "t Price(before discount):" + Stype.getPrice()*count;
}
}
}
enum Aservice{ //用枚举类记录服务种类
BREAKFAST(50),LUNCH(50),DINNER(100),OFFICE(200),GYM(100),CLOTHES(100);
private Aservice(double price) {
this.price = price;
}
public double getPrice() {
return price;
}
private double price;
}
5.Hotel类
package hotelSystem;
import java.time.LocalDate;
import java.util.ArrayList;
public class Hotel {
private ArrayList rooms = new ArrayList(16); //存储16个房间
private ArrayList custmers = new ArrayList(50);//存入顾客
private ArrayList bills = new ArrayList(100);//存入订单
private static int BillId = 100000;
public Custmer getCustmer(int ID) { //根据ID返回顾客
for(int i = 0; i < custmers.size(); i++) {
if(custmers.get(i).getID() == ID) {
return custmers.get(i);
}
}
return null;
}
public Room bookRoom(LocalDate dateIN,LocalDate dateOUT,String mark) { //根据房间种类和入住日期、离开日期返回合适的房间
for(int i = 0; i < rooms.size(); i++) {
if((rooms.get(i).getRoomId().substring(0,1).equals(mark))&&rooms.get(i).checkDate(dateIN, dateOUT))
return rooms.get(i);
}
return null;
}
public void setCustmer(Custmer custmer) {
custmers.add(custmer);
}
public void setBill(Room room,Custmer custmer,LocalDate dateIN,LocalDate dateOUT) {
bills.add(new Bill(room,custmer,dateIN,dateOUT));
}
public Bill getBill(int custmerid) { //根据顾客ID 返回顾客未支付的订单(若存在)
for(int i = bills.size()-1; i > -1; i--) {
if(bills.get(i).custmer.getID() == custmerid && bills.get(i).flag == false)
return bills.get(i);
}
return null;
}
public String toString() { //打印顾客和房间
StringBuffer sb = new StringBuffer();
for(int i = 0; i < rooms.size();i++) {
sb = sb.append(rooms.get(i).toString() + "n");
sb = sb.append("------------------------------------------------------n");
}
for(int i = 0; i < custmers.size();i++) {
sb = sb.append(custmers.get(i).toString() + "n");
sb = sb.append("------------------------------------------------------n");
}
return sb.toString();
}
public void addCustmer(String name,String phonenumber,double balance) { //增加用户
custmers.add(new Custmer(name,phonenumber,balance));
}
public static void main(String[] args) {
Hotel hotel = new Hotel();
System.out.println(hotel);
}
class Bill{ //内部类:订单
private int ID;
private Room room;
private Custmer custmer;
private Service service;
private double money = 0;
private boolean flag = false; //是否支付
private LocalDate dateIN;
private LocalDate dateOUT;
private static double penalty = 1.2; //罚金(未及时支付)
public Bill(Room room,Custmer custmer,LocalDate dateIN,LocalDate dateOUT){ //建立订单
ID = BillId ++;
this.room = room;
this.custmer = custmer;
this.room.checkNow(LocalDate.now()); //更新房间状态(目前是否住人)
this.dateIN = dateIN;
this.dateOUT = dateOUT;
this.room.setDate(dateIN,dateOUT);
this.service = new Service(this.custmer);
}
public void setService(int []count) { //更新服务数量
service.setCount(count);
}
public double calculate(LocalDate checkTime) { //计算总的消费金额(住房和服务)(住房超时需要罚金)
money = room.calculate(dateIN,dateOUT)*custmer.getCtype().getRoom();
if(checkTime.isAfter(dateOUT)) {
money = money * penalty;
}
money += service.calculate();
return money;
}
public double getMoney() {
return money;
}
public double payway() { //支付方式
return custmer.Payway(money);
}
public boolean isCash(double cash) { //根据其他方式付款
if(cash == payway()) //若现金支付与需支付的消费金额相等
{
flag = true; //订单已支付
room.checkNow(LocalDate.now());//房间为空
custmer.plusExpense(money);//消费者累计消费金额增加
if(payway() == 0) //若顾客的会员卡可支付,就在会员卡中减去消费金额
custmer.minusBalance(money);
else
custmer.minusBalance(money - cash); //否则,会员卡余额清空
}
return flag;
}
public String toString() {
StringBuffer sb = new StringBuffer();
sb.append("ID:" + ID + "n");
sb.append("Custmer:" + custmer.getID() + " " + custmer.getName() + "n");
sb.append("Room:" + room.getRoomId() + "n");
sb.append("isPaid:" + flag + "n");
sb.append("services:n" + service.toString());
sb.append("Total Price:" + money + "n");
return sb.toString();
}
}
public Hotel() { //酒店初始化,有16套房和6个顾客,一个订单
rooms.add(new Room("E101"));
rooms.add(new Room("E201"));
rooms.add(new Room("E301"));
rooms.add(new Room("E401"));
rooms.add(new Room("B102"));
rooms.add(new Room("B202"));
rooms.add(new Room("B302"));
rooms.add(new Room("B402"));
rooms.add(new Room("D103"));
rooms.add(new Room("D203"));
rooms.add(new Room("D303"));
rooms.add(new Room("D403"));
rooms.add(new Room("S104"));
rooms.add(new Room("S204"));
rooms.add(new Room("S304"));
rooms.add(new Room("S404"));
custmers.add(new Custmer("###","0000000",0));
custmers.add(new Custmer("Ann","111111",30000));
custmers.add(new Custmer("Bob","222222",15000));
custmers.add(new Custmer("Carl","333333",10000));
custmers.add(new Custmer("Daniel","444444",9000));
custmers.add(new Custmer("Evan","555555",5000));
custmers.add(new Custmer("Ford","666666",1000));
custmers.add(new Custmer("Jack","777777",0));
setBill(rooms.get(0),custmers.get(0),LocalDate.now(),LocalDate.now().plusDays(1));
}
}
6.Main类
package hotelSystem;
import java.time.LocalDate;
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
// TODO Auto-generated method stub
Hotel hotel = new Hotel(); //建立酒店
boolean flag1 = true;
Scanner scan = new Scanner(System.in);
int s1 = 0;
int s2 = 0;
String str1;
String str2;
LocalDate dateIN;
LocalDate dateOUT;
Custmer custmer = null;
Room room = null;
while(flag1) { //第一部分:登录(刷卡)、注册或退出系统
System.out.println("--------------------------------------------");
System.out.println("Welcome to WQY Hotel!");
System.out.println("1.Swipe your card");
System.out.println("2.register");
System.out.println("3.log off");
System.out.println("--------------------------------------------");
System.out.println("please input your choice:");
boolean flag2 = true;
while(flag2) {
s1 = scan.nextInt();
switch(s1) {
case 1:
flag2 = false;
System.out.println("--------------------------------------------");
System.out.println("please input your ID:"); //如10003
s2 = scan.nextInt();
System.out.println("--------------------------------------------");
custmer = hotel.getCustmer(s2);
while(custmer == null) {
System.out.println("Doesn't exist!Please input your ID again:");
s2 = scan.nextInt();
System.out.println("--------------------------------------------");
custmer = hotel.getCustmer(s2);
}
System.out.println("How much would you want to save in your credit?"); //登录成功后存钱(相当于押金)
s2 = scan.nextInt();
while(s2 < 0) {
System.out.println("Input Error!Please try again!");
s2 = scan.nextInt();
}
custmer.plusBalance(s2);
break;
case 2:
flag2 = false;
System.out.println("--------------------------------------------");
System.out.println("Please input your name:");
str1 = scan.next();
System.out.println("Please input your phone number:");
str2 = scan.next();
System.out.println("How much money do you want placed to your hotel credit?");
s2 = scan.nextInt();
System.out.println("--------------------------------------------");
custmer = new Custmer(str1,str2,s2);
hotel.setCustmer(custmer);
break;
case 3:
flag1 = false;
flag2 = false;
System.exit(0);
break;
default:
System.out.println("Input Error!Please try again!");
break;
}
}
System.out.println("--------------------------------------------");
System.out.println("Your message:"); //第一部分结束后打印顾客身份
System.out.println(custmer.toString());
System.out.println("--------------------------------------------");
while(hotel.getBill(custmer.getID()) == null) { //第二部分:确定入住房间(顾客当前没有未支付订单的情况下才会有第二部分出现)
System.out.println("When will you get in? Please input year,month and day:");
int year = scan.nextInt();
int month = scan.nextInt();
int day = scan.nextInt();
dateIN =LocalDate.of(year, month, day);
while(dateIN.isBefore(LocalDate.now())) {
System.out.println("Input Error!Please try again!");
year = scan.nextInt();
month = scan.nextInt();
day = scan.nextInt();
dateIN =LocalDate.of(year, month, day);
}
System.out.println("How long will you stay here?");
s2 = scan.nextInt();
while(s2 <= 0) {
System.out.println("Input Error!Please try again!");
s2 = scan.nextInt();
}
dateOUT = dateIN.plusDays(s2);
System.out.println("--------------------------------------------"); //打印房间菜单
System.out.println("1.STANDARD ROOM("+ Aroom.STANDARD.getPrice() +" yuan)");
System.out.println("2.DOUBLE ROOM("+Aroom.DOUBLE.getPrice() +" yuan)");
System.out.println("3.BUSINESS ROOM("+Aroom.BUSINESS.getPrice() + " yuan)");
System.out.println("4.EXECUTIVE ROOM("+ Aroom.EXECUTIVE.getPrice() + " yuan)");
boolean flag4 = true;
while(flag4) {
System.out.println("--------------------------------------------");
System.out.println("please input your choice:");
s1 = scan.nextInt();
switch(s1) {
case 1:
room = hotel.bookRoom(dateIN, dateOUT, "S");
flag4 = false;
break;
case 2:
room = hotel.bookRoom(dateIN, dateOUT, "D");
flag4 = false;
break;
case 3:
room = hotel.bookRoom(dateIN, dateOUT, "B");
flag4 = false;
break;
case 4:
room = hotel.bookRoom(dateIN, dateOUT, "E");
flag4 = false;
break;
default:
System.out.println("Input Error!Please try again!");
break;
}
if(room == null) { //若是当前时段该种类的房间已预定完,建议顾客重新选择种类
System.out.println("Sorry,this type of room is booked out,please try another type.");
flag4 = true;
}
}
System.out.println("--------------------------------------------");
System.out.println(room);
hotel.setBill(room, custmer, dateIN, dateOUT);
}
boolean flag5 = true;
while(flag5) { //第三部分:选择服务或打印账单或结算或退卡
System.out.println("--------------------------------------------");
System.out.println("1.service reservation");
System.out.println("2.print the bill");
System.out.println("3.settle accounts");
System.out.println("4.return card");
System.out.println("--------------------------------------------");
System.out.println("please input your choice:");
s1 = scan.nextInt();
if(s1 == 1) {
System.out.println("--------------------------------------------");
System.out.println("1.breakfast("+ Aservice.BREAKFAST.getPrice() + " yuan)");
System.out.println("2.lunch("+ Aservice.LUNCH.getPrice() + " yuan)");
System.out.println("3.dinner("+ Aservice.DINNER.getPrice() + " yuan)");
System.out.println("4.office("+ Aservice.OFFICE.getPrice() + " yuan)");
System.out.println("5.gym("+ Aservice.GYM.getPrice() + " yuan)");
System.out.println("6.clothes("+ Aservice.CLOTHES.getPrice() + " yuan)");
System.out.println("--------------------------------------------");
int [] services = new int[6]; //用数组存储顾客想要的服务的数量
System.out.println("please input your choice(the number of these 6 service you want to order):");
System.out.println("breakfast:");
services[0] = scan.nextInt();
System.out.println("lunch:");
services[1] = scan.nextInt();
System.out.println("dinner:");
services[2] = scan.nextInt();
System.out.println("office:");
services[3] = scan.nextInt();
System.out.println("gym:");
services[4] = scan.nextInt();
System.out.println("clothes:");
services[5] = scan.nextInt();
hotel.getBill(custmer.getID()).setService(services);
System.out.println("--------------------------------------------");
System.out.println(hotel.getBill(custmer.getID())); //打印当前账单
}
else if(s1 == 2) {
System.out.println(hotel.getBill(custmer.getID()));
}
else if(s1 == 3) { //结算
System.out.println("When will you pay? Please input year,month and day:");
int year = scan.nextInt();
int month = scan.nextInt();
int day = scan.nextInt();
LocalDate checkout = LocalDate.of(year, month, day);
hotel.getBill(custmer.getID()).calculate(checkout);
System.out.println("--------------------------------------------");
System.out.println(hotel.getBill(custmer.getID()).toString());
System.out.println("--------------------------------------------");
double cash = hotel.getBill(custmer.getID()).payway();
if(cash == 0) { //只需卡支付
hotel.getBill(custmer.getID()).isCash(cash);
System.out.println("You have paid through your card!");
}
else { //还需其他方式支付
System.out.println("You should pay :" + cash + ",please input your cash:");
double s3 = scan.nextInt();
while(s3 != cash) {
System.out.println("You should pay :" + cash + ",please input again:");
s3 = scan.nextShort();
}
hotel.getBill(custmer.getID()).isCash(cash);
System.out.println("You have paid through your cash!");
}
flag5 = false;
}
else if(s1 == 4) {
flag5 = false;
}
else {
System.out.println("Input Error!Please try again!");
}
}
}
scan.close();
}
}



