Java的枚举简单应用

/**
* 请用枚举方式实现如下应用:
* 客户去旅店住房,
* 客户分普通客户,和vip客户,vip分白金和钻石客户
* 不同的客户有不同的折扣
* 入住的房间分单人房,双人房和套房
* 不同的房间房费不同
* 入住的时间分一周七天,每天费用也不同
* 请设计代码实现某客户住房一天,计算出应付房费.
* 可以尝试计算连续N天的房费如何计算
**/

根据题意客户有普通客户,白金客户,钻石客户,则可以建一个枚举类,内容如下:
 1 public enum Customer {
 2     common("普通", 0),
 3     WhiteGold("白金", 0.8),
 4     diamond("钻石", 0.9);
 5     private String name;
 6     private double discount;
 7 
 8     Customer(String name, double discount) {
 9         this.name = name;
10         this.discount = discount;
11     }
12 
13     public String getName() {
14         return name;
15     }
16 
17     public void setName(String name) {
18         this.name = name;
19     }
20 
21     public double getDiscount() {
22         return discount;
23     }
24 
25     public void setDiscount(double discount) {
26         this.discount = discount;
27     }
28 }

之后是一周每一天也有不同价格折扣

 1 public enum Week {
 2     DAY1("周一", 0.9),
 3     DAY2("周二", 0.9),
 4     DAY3("周三", 0.8),
 5     DAY4("周四", 0.8),
 6     DAY5("周五", 0.5),
 7     DAY6("周六", 0.6),
 8     DAY7("周日", 0.6);
 9 
10     private String name;//获取每周的名字
11     private double per;
12 
13     Week(String name, double per) {
14         this.name = name;
15         this.per = per;
16     }
17 
18     public String getName() {
19         return name;
20     }
21 
22     public void setName(String name) {
23         this.name = name;
24     }
25 
26 
27     public double getPer() {
28         return per;
29     }
30 
31     public void setPer(double per) {
32         this.per = per;
33     }
34 }

房间也有单人房间、双人房间和套房

 1 public enum Room {
 2     SINGLE("单人房间", 100),
 3     DUETBED("双人房间", 150),
 4     SUITE("套房", 180);
 5     private String roomName;
 6     private int f;
 7 
 8     Room(String roomName, int f) {
 9         this.roomName = roomName;
10         this.f = f;
11     }
12 
13     public String getRoomName() {
14         return roomName;
15     }
16 
17     public void setRoomName(String roomName) {
18         this.roomName = roomName;
19     }
20 
21     public int getF() {
22         return f;
23     }
24 
25     public void setF(int f) {
26         this.f = f;
27     }
28 }

如果是住一天的话,调用getPrice方法,传来星期、顾客以及房间等级并将对应的价格累加。

 1 public class Util {
 2     private static Week wk;
 3     private static Customer customer;
 4     private static Room room;
 5 
 6     static double total = 0;
 7 
 8     /**
 9      * 客户住一天
10      *
11      * @param week
12      * @param customer 顾客
13      * @param room     房间类别
14      */
15     public static void getPrice(Week week, Customer customer, Room room) {
16         if (customer != Customer.common) {
17             total = (room.getF() * week.getPer() * customer.getDiscount());
18         } else {
19             total = (room.getF() * week.getPer());
20         }
21         System.out.println("客户类型:" + customer.getName() +
22                 "\n======================\n" +
23                 "日期:" + week.getName() + "\n" +
24                 "今日折扣:" + week.getPer() + "\n" +
25                 "房间类型:" + room.getRoomName() + "\n"
26                 + "房间价格:" + room.getF() + "元");
27         System.out.println("房价;" + total + "元");
28     }

如果是连续住N天,就要考虑一个问题,就是星期的天数的控制,如果天数大于星期天则星期要从星期一开始算起,下面代码我使用index变量来记录传来的星期参数并通过If判断后赋值,并将Week的每一天添加到数组里里,通过循环访问数组t获取每一天的金额并计算,使用一个money数组来统计每一天的金额最后通过for循环进行累加输出。

 /**
     * 住N天
*
* @param week 星期
* @param customer 顾客
* @param room 房间类型
* @param day 住的天数
*/
public static void getPrice(Week week, Customer customer, Room room, int day) {
double money[] = new double[day]; //用来存放每天用户的费用
int index = 0;//记录数组下标
total = 0;//总金额置为0

//将枚举类转为数组并存入Emu数组内,是用循环找下标
Week[] Emu = Week.values();
//获取是第几天(下标)
index = week.ordinal();

for (int i = 0; i < day; i++) {
if (index > 6) { //如果星期(ArrayList下标)>6
index = 0;//重新回到星期一
}
//将每天的费用加到money数组里
money[i] = Emu[index].getPer() * customer.getDiscount() * room.getF();
index++;
}

//累加money数组里的金额
for (int i = 0; i < day; i++) {
total += money[i];
}

System.out.println("客户类型:" + customer.getName() +
"\n===================\n"
+ "住" + day + "天");
System.out.println("房价共;" + total + "元");
}
}

主程序测试如下,可以默认住一天,也可以通过Util的getPrice住N天。

 1 public class Water {
 2     public static void main(String[] args) {
 3         Week wk = Week.DAY5; //星期3
 4         Customer customer = Customer.diamond; //钻石客户
 5         Room room = Room.DUETBED; //双人房间
 6 
 7         //默认住1天
 8         Util.getPrice(wk, customer, room);
 9         System.out.println("\n===========================================\n");
10         //住N天
11         Util.getPrice(wk, customer, room, 3);
12     }
13 }

运行效果如下:

 

posted @ 2020-07-20 19:38  勤快的懒羊羊  阅读(181)  评论(0编辑  收藏  举报