欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

java实现KFC点餐小程序

程序员文章站 2024-02-22 17:57:52
本文实例为大家分享了java实现kfc点餐系统的具体代码,供大家参考,具体内容如下 package kfc点餐系统; //food 类 public cla...

本文实例为大家分享了java实现kfc点餐系统的具体代码,供大家参考,具体内容如下

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 + "]";
 }
 
 
}
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();
 }
 
 
}

运行结果:

java实现KFC点餐小程序

java实现KFC点餐小程序

java实现KFC点餐小程序

java实现KFC点餐小程序

更多学习资料请关注专题《管理系统开发》。

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持。