java如何实现超市管理系统?本篇文章小编给大家分享一下java实现超市管理系统代码示例,文章代码介绍的很详细,小编觉得挺不错的,现在分享给大家供大家参考,有需要的小伙伴们可以来看看。
实现功能
使用选择结构,循环结构,数组的知识实现一个超市管理系统
运行结果:货物清单:
添加商品功能:
删除商品功能:
修改商品:
商品货物实体类
import java.util.Arrays; public class Goods { private int id; private double price; private String name; public Goods(int id, double price, String name) { this.id = id; this.price = price; this.name = name; } public int getId() { return id; } public void setId(int id) { this.id = id; } public double getPrice() { return price; } public void setPrice(double price) { this.price = price; } public String getName() { return name; } public void setName(String name) { this.name = name; } public Goods() { } //增加商品 public Goods[] add(Goods[]goods,Goods newGood){ goods= Arrays.copyOf(goods,goods.length+1); goods[goods.length-1]=newGood; return goods; } //删除商品 public static Goods[] del(Goods[]goods,int id){ int i=0; while(true){ if(goods[i].getId()==id){ goods[i]=null; return goods; } i++; if(i>=goods.length){ return goods; } } } //添加商品 public static Goods[] change(Goods[]goods,int id,int newId,double newPrice,String newName){ int i=0; while (true){ if(goods[i].getId()==id){ goods[i].setId(newId); goods[i].setPrice(newPrice); goods[i].setName(newName); return goods; } i++; if(i>=goods.length){ return goods; } } } }
超市管理系统类
import java.util.Scanner; public class marketManager { public static void main(String[] args) { Scanner sc = new Scanner(System.in); Goods g1=new Goods(1000,10,"笔记本"); Goods g2=new Goods(1001,2,"西红柿"); Goods g3=new Goods(1002,5,"辣条"); Goods []goods={g1,g2,g3}; while (true) { System.out.println("========超市管理系统======="); System.out.println("1.货物清单 2.增加商品 3.删除商品 4.修改商品 5.退出"); System.out.println("请输入你要操作的编号:"); int i = sc.nextInt(); switch (i){ case 1: System.out.println("=======商品清单======="); System.out.println("商品编号"+"tt"+"商品单价"+"tt"+"商品名称"); for (Goods a:goods) { if(a==null){ continue; } System.out.println(a.getId()+"tt"+a.getPrice()+"tt"+a.getName()); } continue; case 2: System.out.println("你选择的是增加商品的功能"); System.out.println("请输入你要添加的编号:"); int Id = sc.nextInt(); System.out.println("请输入你要添加的商品价格:"); double price = sc.nextDouble(); System.out.println("请输入你要添加的商品名称"); String name = sc.next(); Goods good=new Goods(Id,price,name); goods = good.add(goods, good); System.out.println("添加成功!"); continue; case 3: System.err.println("你选择的是删除商品功能"); System.out.println("请输入你要操作的编号:"); Id = sc.nextInt(); goods=Goods.del(goods,Id); System.out.println("删除成功!"); continue; case 4: System.out.println("你选择的是修改商品功能"); System.out.println("请输入你要操作的编号:"); Id=sc.nextInt(); System.out.println("请输入修改后的编号:"); int newId = sc.nextInt(); System.out.println("请输入修改后的价格:"); double newPrice = sc.nextDouble(); System.out.println("请输入修改后的商品名称:"); String newName=sc.next(); goods=Goods.change(goods,Id,newId,newPrice,newName); continue; case 5: return; } } } }
茶杯头甜蜜终章dlc 官方手机版v1.0.0.3
下载火柴人传说暗影格斗内置菜单 最新版v3.0.1
下载荒野乱斗测试服 安卓版v61.10.3
下载荒野乱斗彩虹服 安卓版v61.10.3
下载寒霜启示录 安卓版v1.25.10
寒霜启示录是一款生存模拟游戏,不少玩家可能对于末日都有着自己
末日城堡免广告版 安卓最新版v0.7.1
末日城堡免广告版是一款非常好玩的模拟经营类游戏,内部可以不看
甜蜜人生模拟器 最新版v1.4.5
甜蜜人生模拟器是一款非常好玩的模拟恋爱手游,玩家在这里能够对
武器锻造师内置功能菜单 v10.4
武器锻造师内置菜单版是游戏的破解版本,在该版本中为玩家提供了
开放空间overfield 安卓版v1.0.5
开放空间Overfield是一款箱庭养成经营手游,让你在广阔