有一个中餐厅,有一些厨师,负责包包子,有一些顾客来买包子,统计餐厅的收入。

作业:
有一个中餐厅,有一些厨师,负责包包子,有一些顾客来买包子,统计餐厅的收入。
 
包子类:
 1 package lession220713;
 2 
 3 /**
 4  * @author 123 包子类
 5  */
 6 public class Bun {
 7 
 8     private static float PRICE = 2.5f;// 包子价格
 9 
10     public float getPRICE() {
11         return PRICE;
12     }
13 
14     public static void setPRICE(float pRICE) {
15         PRICE = pRICE;
16     }
17 
18 }
View Code

 

厨师类:

 1 package lession220713;
 2 
 3 /**
 4  * 厨师类
 5  * 
 6  * @author 123
 7  *
 8  */
 9 public class Chef{
10     
11     // 厨师所在餐厅
12     private final Restaurant restaurant;
13 
14     public Chef(Restaurant restaurant) {
15         this.restaurant = restaurant;
16     }
17     /**
18      * 做包子
19      * @param n
20      */
21     public void makebun(int n){
22         for(int i=0;i<n;i++) {
23             Bun bun = new Bun();
24             this.restaurant.addBun(bun);
25         }
26     
27     }
28 }
View Code

 

顾客类:

 1 package lession220713;
 2 
 3 /**
 4  * 顾客类
 5  * @author 123
 6  *
 7  */
 8 public class Customer {
 9 
10     private String name;//顾客姓名
11 
12     public String getName() {
13         return name;
14     }
15 
16     public void setName(String name) {
17         this.name = name;
18     }
19 }
View Code

 

餐厅类:

 1 package lession220713;
 2 
 3 /**
 4  * 餐厅类
 5  * 
 6  * @author 123
 7  *
 8  */
 9 public class Restaurant {
10     private Bun[] bun = new Bun[100];
11     private int bunIdx = 0;// 用于指向当前有多少个包子,可能会达到数组的长度
12     private Chef[] chefs;// 餐厅的所有厨师
13     private float total;// 餐厅的总收入
14 
15     /**
16      * 向餐厅添加包子
17      * 
18      * @param bun
19      */
20     public void addBun(Bun bun) {
21         if (bunIdx < this.bun.length) {//最多做100个包子
22             this.bun[bunIdx] = bun;
23             bunIdx++;
24         }
25 
26     }
27 
28     /**
29      * 开门营业
30      * 
31      */
32     public void start() {
33 
34         for (int i = 0; i < chefs.length; i++) {
35             chefs[i].makebun((int) (Math.random() * 20));//每个厨师随机做0-20个包子
36         }
37     }
38 
39     /**
40      * 卖包子
41      * 
42      * @param c
43      * @param n
44      */
45     public void sell(Customer c, int n) {
46 
47         float total = 0;// 本次的收入
48         for (int i = 0; i < n; i++) {
49             if (bunIdx < 1) {//如果包子数小于0,退出循环
50                 break;
51             }
52             total += bun[bunIdx - 1].getPRICE();
53             bunIdx--;//包子数减1
54         }
55         this.total += total;// 计算总收益
56     }
57 
58     public Bun[] getBun() {
59         return bun;
60     }
61 
62     public void setBun(Bun[] bun) {
63         this.bun = bun;
64     }
65 
66     public int getBaoIdx() {
67         return bunIdx;
68     }
69 
70     public void setBaoIdx(int baoIdx) {
71         this.bunIdx = baoIdx;
72     }
73 
74     public Chef[] getChef() {
75         return chefs;
76     }
77 
78     public void setChef(Chef[] chef) {
79         this.chefs = chef;
80     }
81 
82     public float getTotal() {
83         return total;
84     }
85 
86     public void setTotal(float total) {
87         this.total = total;
88     }
89 
90 }
View Code

 

主函数:

 1 package lession220713;
 2 
 3 public class Test {
 4     public static void main(String[] args) {
 5         Restaurant r = new Restaurant();
 6         Chef[] chefs = new Chef[10];//设置10个厨师
 7 
 8         for (int i = 0; i < chefs.length; i++) {
 9             Chef c = new Chef(r);//创建10个厨师对象
10             chefs[i] = c;
11         }
12         
13         r.setChef(chefs);//把厨师对象穿到餐厅类的厨师数组中
14         
15         r.start();//调用方法开始营业
16         
17         System.out.println("餐厅现有" + r.getBaoIdx() + "个包子");
18 
19         int n = (int) (Math.random() * 20);//设置一个0-20的随机数当做顾客数
20         for (int i = 0; i < n; i++) {
21             Customer cust = new Customer();
22             cust.setName(f1(3));//随机给顾客姓名赋值为3个字母
23 
24             int m = (int) (Math.random() * 5 + 1);// 一个顾客买的包子数为1-6
25             System.out.println("顾客" + cust.getName() + "买" + m + "个包子");
26             r.sell(cust, m);//调用卖包子的方法
27         }
28 
29         System.out.println("餐厅总收入:" + r.getTotal() + "元");
30         System.out.println("餐厅现有" + r.getBaoIdx() + "个包子");
31     }
32 
33     private static String f1(int n) {//随机生成3个小写字母的字符串
34         String s = new String();
35         for (int i = 0; i < n; i++) {
36             char c = (char) (Math.random() * 25 + 97);
37             s += c;
38         }
39         return s;
40     }
41 
42 }
View Code

 

输出结果:

 

 

 

 

 

posted @ 2022-07-15 16:35  扫码登录  阅读(38)  评论(0)    收藏  举报