java实现简单的小超市程序
作者:力伟成
这篇文章主要为大家详细介绍了java实现简单的小超市程序,文中示例代码介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
本文实例为大家分享了java实现小超市程序的具体代码,供大家参考,具体内容如下
一.人物包
1.顾客类
package person; public class Customer { public String name; public double money; public boolean isDrivingCar; }
二.超市包
1.小超市类
package supermarket; public class LittleSuperMarket { public String superMarketName; public String address; public int parkingCount; public double incomingSum; public Merchandise[] merchandises; public int[] merchandisesold; }
2.商品类
package supermarket; public class Merchandise { public String name; public String id; public int count; public double soldPrice; public double purchasePrice; }
三.主程序
import person.Customer; import supermarket.LittleSuperMarket; import supermarket.Merchandise; import java.util.Scanner; public class Run { public static void main(String[] args) { //通过小超市类创建超市实体 LittleSuperMarket littleSuperMarket = new LittleSuperMarket(); //为超市属性赋值 littleSuperMarket.address = "世纪大道999号"; littleSuperMarket.superMarketName = "我家超市"; littleSuperMarket.parkingCount = 200; //为超市商品属性new一个大小为200的数组,类型为Merchandise littleSuperMarket.merchandises = new Merchandise[200]; //创造下标相同的数组记录相同下标商品的卖出个数 littleSuperMarket.merchandisesold = new int[littleSuperMarket.merchandises.length]; //赋值 for (int i = 0; i < littleSuperMarket.merchandises.length; i++) { //创建类型为merchandise的局部变量m,并为其赋值 Merchandise m = new Merchandise(); m.count = 200; m.id = "ID" + i; m.name = "商品" + i; m.purchasePrice = 200 * Math.random(); m.soldPrice = (1 + Math.random()) * m.purchasePrice; //将每次局部变量的值赋给对应下标的merchandise实体,此时实体merchandise有了值 littleSuperMarket.merchandises[i] = m; } System.out.println("超市开张了!! !"); boolean open = true; Scanner scanner = new Scanner(System.in); while (open) { System.out.println("本店叫做:" + littleSuperMarket.superMarketName); System.out.println("本店地址:" + littleSuperMarket.address); System.out.println("共拥有停车位:" + littleSuperMarket.parkingCount); System.out.println("今天的营业额为:" + littleSuperMarket.incomingSum); System.out.println("共有商品:" + littleSuperMarket.merchandises.length + "种"); //为顾客的属性赋值 Customer customer = new Customer(); customer.name = "编号:" + ((int) (Math.random() * 500)); customer.isDrivingCar = Math.random() > 0.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); } //在购买流程外部创建变量统计总共花费和购买数量 double totalCast = 0; int numToBuy; //购买循环 while (true) { System.out.println("本店提供" + littleSuperMarket.merchandises.length + "种商品,欢迎选购。请输入商品编号:"); int InDex = 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 < totalCast) { System.out.println("对不起,您身上的钱不足以支付您的商品,欢迎下次选购"); //钱不足以支付,对应商品的数量再把刚才购买但未成功支付的商品数量归还 //就像你在超市里买东西,放进购物车了,货架上超市库存少了,其他顾客拿不到了,等你支付时钱不够,商品被放回,其他顾客可以拿到的数量增加,即商品库存增加 littleSuperMarket.merchandises[InDex].count += numToBuy; //这一段当时写的时候我也比较乱 //因为总价计算在外面了,此时总价已将超出了你的钱数循环从这里出去如果总价不置零,你的钱数一直小于总价,该顾客一直无法购买商品 totalCast = 0; } else { //正确购买,售出数量对应增加,顾客口袋里的钱对应减少 littleSuperMarket.merchandisesold[InDex] += numToBuy; customer.money -= totalCast; //开车来的话,顾客离开,车位释放 if (customer.isDrivingCar) { littleSuperMarket.parkingCount++; } } } littleSuperMarket.incomingSum += totalCast; //每次循环结束,该人总钱数得出将该人的totalCast累加到littleSuperMarket.incomingSum中,得出总共收入 //littleSuperMarket.incomingSum和totalIncoming数值相等,基本为同一意思 //littleSuperMarket.incomingSum += totalCast; System.out.println("顾客" + customer.name + "号的顾客共消费" + totalCast + ",还剩余" + customer.money); System.out.println("今天还要继续营业吗?"); open = scanner.nextBoolean(); } System.out.println("超市关门了!"); System.out.println("今天总的营业额为" + littleSuperMarket.incomingSum + "营业情况如下:"); //定义总收入和总净利润 double totalNetIncoming = 0; double totalIncoming = 0; for (int i = 0; i < littleSuperMarket.merchandisesold.length; i++) { //只统计售出数量大于0的商品 if (littleSuperMarket.merchandisesold[i] > 0) { //计算总利润和总净利润,通过累加求得 double incoming = littleSuperMarket.merchandises[i].soldPrice * littleSuperMarket.merchandisesold[i]; double netIncoming = (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); } }
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持脚本之家。