java实现简单的小超市程序
本文实例为大家分享了java实现小超市程序的具体代码,供大家参考,具体内容如下
一.人物包
1.顾客类
packageperson; publicclassCustomer{ publicStringname; publicdoublemoney; publicbooleanisDrivingCar; }
二.超市包
1.小超市类
packagesupermarket; publicclassLittleSuperMarket{ publicStringsuperMarketName; publicStringaddress; publicintparkingCount; publicdoubleincomingSum; publicMerchandise[]merchandises; publicint[]merchandisesold; }
2.商品类
packagesupermarket; publicclassMerchandise{ publicStringname; publicStringid; publicintcount; publicdoublesoldPrice; publicdoublepurchasePrice; }
三.主程序
importperson.Customer; importsupermarket.LittleSuperMarket; importsupermarket.Merchandise; importjava.util.Scanner; publicclassRun{ publicstaticvoidmain(String[]args){ //通过小超市类创建超市实体 LittleSuperMarketlittleSuperMarket=newLittleSuperMarket(); //为超市属性赋值 littleSuperMarket.address="世纪大道999号"; littleSuperMarket.superMarketName="我家超市"; littleSuperMarket.parkingCount=200; //为超市商品属性new一个大小为200的数组,类型为Merchandise littleSuperMarket.merchandises=newMerchandise[200]; //创造下标相同的数组记录相同下标商品的卖出个数 littleSuperMarket.merchandisesold=newint[littleSuperMarket.merchandises.length]; //赋值 for(inti=0;i0.4; customer.money=(1+Math.random())*10000; //先判断顾客是否开车 if(customer.isDrivingCar){ System.out.println("欢迎顾客"+customer.name+"号的顾客驾车而来!"); //顾客开车的条件下车位数>0,车位减一,否则重新进入循环(车位小于零,顾客无法停车) if(littleSuperMarket.parkingCount>0){ littleSuperMarket.parkingCount--; System.out.println("欢迎顾客"+customer.name+"号的顾客光临"+littleSuperMarket.superMarketName+",车位剩余"+littleSuperMarket.parkingCount+",钱包剩余"+customer.money); }else{ System.out.println("对不起,停车位已满,欢迎下次光临"); continue; } //没开车,直接进入 }else{ System.out.println("欢迎顾客"+customer.name+"号的顾客光临"+littleSuperMarket.superMarketName+"钱包剩余"+customer.money); } //在购买流程外部创建变量统计总共花费和购买数量 doubletotalCast=0; intnumToBuy; //购买循环 while(true){ System.out.println("本店提供"+littleSuperMarket.merchandises.length+"种商品,欢迎选购。请输入商品编号:"); intInDex=scanner.nextInt(); //对商品索引的合法性进行检测 if(InDex<=0){ break; } if(InDex>littleSuperMarket.merchandises.length-1){ System.out.println("对不起,本店没有那么多商品。请输入0到"+(littleSuperMarket.merchandises.length-1)+"之间的数字"); continue; } System.out.println("您选购的商品的名字是:"+littleSuperMarket.merchandises[InDex].name+"\t售价为:"+littleSuperMarket.merchandises[InDex].soldPrice); System.out.println("请问你要买几个?"); numToBuy=scanner.nextInt(); //对购买数量的合法性进行检测 if(numToBuy<=0){ System.out.println("不买看看也好,欢迎继续选购"); continue; } if(numToBuy>littleSuperMarket.merchandises[InDex].count){ System.out.println("本店没有那么多商品,欢迎继续挑选"); continue; } /*其实这里的逻辑不太好,但是懒得改了,等再学习一段时间再好好完善这个小程序。这里把花费放在购买循环外面才是比较好的逻辑,然后在这里用if循环判断购买的一系列合法性即可。 或者把售卖个数的属性也在这里改变赋值会容易懂,我这里放到后面了,懒得改了。等下次优化功能时再整体优化逻辑 */ //输入合法,求总共花费,并对商品库存进行相应的扣除 totalCast+=littleSuperMarket.merchandises[InDex].soldPrice*numToBuy; littleSuperMarket.merchandises[InDex].count-=numToBuy; //仍然属于合法性检测,检测顾客的钱是否够支付他所购买商品的总价 if(customer.money 0){ //计算总利润和总净利润,通过累加求得 doubleincoming=littleSuperMarket.merchandises[i].soldPrice*littleSuperMarket.merchandisesold[i]; doublenetIncoming=(littleSuperMarket.merchandises[i].soldPrice-littleSuperMarket.merchandises[i].purchasePrice)*littleSuperMarket.merchandisesold[i]; totalIncoming+=incoming; totalNetIncoming+=netIncoming; System.out.println(littleSuperMarket.merchandises[i].name+"售出了"+littleSuperMarket.merchandisesold[i]+"个。销售额为"+incoming+",净利润为"+netIncoming); } } System.out.println("今日总销售额为:"+totalIncoming+";今日总净利润为:"+totalNetIncoming); } }
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持毛票票。
声明:本文内容来源于网络,版权归原作者所有,内容由互联网用户自发贡献自行上传,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任。如果您发现有涉嫌版权的内容,欢迎发送邮件至:czq8825#qq.com(发邮件时,请将#更换为@)进行举报,并提供相关证据,一经查实,本站将立刻删除涉嫌侵权内容。