本文实例为大家分享了java实现kfc点餐系统的具体代码,供大家参考,具体内容如下
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
|
package kfc点餐系统; //food 类 public class kfcfood { private string fname ; private int fnumb= 1 ; private float fprice; public kfcfood() { super (); } public kfcfood(string fname, int fnumb, float fprice) { super (); this .fname = fname; this .fnumb = fnumb; this .fprice = fprice; } public string getfname() { return fname; } public void setfname(string fname) { this .fname = fname; } public int getfnumb() { return fnumb; } public void setfnumb( int fnumb) { this .fnumb = fnumb; } public float getfprice() { return fprice*fnumb; } public void setfprice( float fprice) { this .fprice = fprice; } @override public string tostring() { return fname + " 价格 [" + fprice + "]" ; } } |
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
|
package kfc点餐系统; /* * 1.正常餐品结算和找零。 2.基本套餐结算和找零。 3.使用优惠劵购买餐品结算和找零。 4.可在一定时间段参与店内活动(自行设计或参考官网信息)。 5.模拟打印小票的功能(写到文件中)。 * */ /*@author:kxd * @date:2018.10.25 * * 本人写这个小程序,第一创建kfcfood类属性有foodname foodprice foodnumb 和一些必要函数 这个类负责封装食物属性 * demo1 是主程序 核心 是hashmap 集合,hashmap<kfcfood , string > 做购物车 添加食物 和遍历打印小票 * 用字符输出流 很简洁的写法写入文本 * bufferedwriter bw = new bufferedwriter(new outputstreamwriter(new fileoutputstream("out.txt",true)));//字符缓冲输出流 * * */ import java.io.bufferedwriter; import java.io.filenotfoundexception; import java.io.fileoutputstream; import java.io.ioexception; import java.io.outputstreamwriter; import java.util.date; import java.util.hashmap; import java.util.scanner; import java.util.set; public class demo1 { public static void main(string[] args) throws ioexception { kfcfood k1= new kfcfood( "全家桶" , 1 ,( float )( 79.9 )); kfcfood k2= new kfcfood( "奥尔良鸡腿堡" , 1 ,( float )( 19.9 )); kfcfood k3= new kfcfood( "热辣鸡腿堡" , 1 ,( float )( 18.9 )); kfcfood k4= new kfcfood( "帕尼尼早餐套餐" , 1 ,( float )( 29.9 )); kfcfood k5= new kfcfood( "可乐" , 1 ,( float )( 9.9 )); kfcfood k6= new kfcfood( "鸡米花" , 1 ,( float )( 9.9 )); kfcfood k7= new kfcfood( "薯条" , 1 ,( float )( 8.8 )); kfcfood k8= new kfcfood( "热辣鸡翅" , 1 ,( float )( 11.9 )); kfcfood k9= new kfcfood( "冰淇淋" , 1 ,( float )( 9.9 )); hashmap<kfcfood , string > hm= new hashmap<kfcfood , string >(); system.out.println( "**********************************" ); system.out.println( "*********欢迎光临肯德基餐厅*************" ); system.out.println( " 菜单 " ); system.out.println( "1:" +k1.tostring()); system.out.println( "2:" +k2.tostring()); system.out.println( "3:" +k3.tostring()); system.out.println( "4:" +k4.tostring()); system.out.println( "5:" +k5.tostring()); system.out.println( "6:" +k6.tostring()); system.out.println( "7:" +k7.tostring()); system.out.println( "8:" +k8.tostring()); system.out.println( "9:" +k9.tostring()); boolean z= true ; int count= 1 ; //计数器 float total= 0 ; while (z) { system.out.println( "请点餐 :输入编号" ); scanner sc = new scanner (system.in ); int i = sc.nextint(); switch (i) { case 1 : system.out.println(count+k1.getfname()); system.out.println( "请输入数量:" ); scanner sc1= new scanner (system.in ); int n = sc1.nextint(); k1.setfnumb(n); //输入数量 total=total+k1.getfprice(); //计算入总价 hm.put(k1, "数量:" +string.valueof( k1.getfnumb()) ); //添加进树 break ; case 2 : system.out.println(count+k2.getfname()); system.out.println( "请输入数量:" ); scanner sc2= new scanner (system.in ); int n2 = sc2.nextint(); k2.setfnumb(n2); total=total+k2.getfprice(); //计算入总价 hm.put(k2, "数量:" +string.valueof( k2.getfnumb()) ); //添加进树 break ; case 3 : system.out.println(count+k3.getfname()); system.out.println( "请输入数量:" ); scanner sc3= new scanner (system.in ); int n3 = sc3.nextint(); k3.setfnumb(n3); total=total+k3.getfprice(); //计算入总价 hm.put(k3, "数量:" +string.valueof( k3.getfnumb()) ); //添加进树 break ; case 4 : system.out.println(count+k4.getfname()); system.out.println( "请输入数量:" ); scanner sc4= new scanner (system.in ); int n4 = sc4.nextint(); k4.setfnumb(n4); total=total+k4.getfprice(); //计算入总价 hm.put(k4, "数量:" +string.valueof( k4.getfnumb()) ); //添加进树 break ; case 5 : system.out.println(count+k5.getfname()); system.out.println( "请输入数量:" ); scanner sc5= new scanner (system.in ); int n5 = sc5.nextint(); k5.setfnumb(n5); total=total+k5.getfprice(); //计算入总价 hm.put(k5, "数量:" +string.valueof( k5.getfnumb()) ); //添加进树 break ; case 6 : system.out.println(count+k6.getfname()); system.out.println( "请输入数量:" ); scanner sc6= new scanner (system.in ); int n6 = sc6.nextint(); k6.setfnumb(n6); total=total+k6.getfprice(); //计算入总价 hm.put(k6, "数量:" +string.valueof( k6.getfnumb()) ); //添加进树 break ; case 7 : system.out.println(count+k7.getfname()); system.out.println( "请输入数量:" ); scanner sc7= new scanner (system.in ); int n7 = sc7.nextint(); k7.setfnumb(n7); total=total+k7.getfprice(); //计算入总价 hm.put(k7, "数量:" +string.valueof( k7.getfnumb()) ); //添加进树 break ; case 8 : system.out.println(count+k8.getfname()); system.out.println( "请输入数量:" ); scanner sc8= new scanner (system.in ); int n8 = sc8.nextint(); k8.setfnumb(n8); total=total+k8.getfprice(); //计算入总价 hm.put(k8, "数量:" +string.valueof( k8.getfnumb()) ); //添加进树 break ; case 9 : system.out.println(count+k9.getfname()); system.out.println( "请输入数量:" ); scanner sc9= new scanner (system.in ); int n9 = sc9.nextint(); k9.setfnumb(n9); total=total+k9.getfprice(); //计算入总价 hm.put(k9, "数量:" +string.valueof( k9.getfnumb()) ); //添加进树 break ; default :system.out.println( "请输入正确的号码!" ); break ; } //遍历树 set<kfcfood> ks1 = hm.keyset(); //遍历map for (kfcfood s2:ks1) { system.out.println( "(" +s2+ "," +hm.get(s2)+ ")" ); } system.out.println( "1 继续点餐 2 重新点餐 3 结束点餐 " ); scanner sca= new scanner (system.in ); int n = sca.nextint(); switch (n) { case 1 : count++; break ; case 2 : system.out.println( "重新点餐!" ); hm.clear(); break ; case 3 : system.out.println( "总价" +total); system.out.println( "结束点餐" ); z= false ; break ; default :system.out.println( "请输入正确的号码!" ); break ; } } system.out.println( "****************************************" ); system.out.println( "*********欢迎光临肯德基餐厅*************" ); system.out.println( " 您的菜单为! " ); //遍历树 set<kfcfood> ks1 = hm.keyset(); //遍历map for (kfcfood s2:ks1) { system.out.println( " " +s2+ ", " +hm.get(s2)+ " " ); } system.out.println( "总价" +total); system.out.println( "订单时间: " + new date()); system.err.println( " 欢迎下次光临!" ); system.out.println( "**********************************" ); bufferedwriter bw = new bufferedwriter( new outputstreamwriter( new fileoutputstream( "out.txt" ))); //字符缓冲输出流 bw.write( "****************************************" );bw.newline(); bw.write( "*********欢迎光临肯德基餐!*************" );bw.newline(); bw.write( " 您的菜单为! " );bw.newline(); set<kfcfood> ks2 = hm.keyset(); //遍历map for (kfcfood s2:ks2) { // system.out.println(" "+s2+", "+hm.get(s2)+" "); bw.write(s2+ ", " +hm.get(s2));bw.newline(); } //system.out.println("总价"+total); bw.write( "订单时间: " + new date());bw.newline(); bw.write( " 欢迎下次光临!" );bw.newline(); bw.write( "**********************************" );bw.newline(); bw.flush(); bw.close(); } } |
运行结果:
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持服务器之家。
原文链接:https://blog.csdn.net/harvestUachieve/article/details/83422283