packageshangpin;publicclassGoods{privateString id;//商品编号privateString name;//名称privatedouble price;//价格privateString miaoshu;//描述@Override//重写toStringpublicStringtoString(){return"商品信息:"+id+","+name+","+price+","+miaoshu;}publicGoods(String id,String name,double price,String miaoshu){this.id = id;this.name = name;this.price = price;this.miaoshu = miaoshu;}publicStringgetId(){return id;}publicvoidsetId(String id){this.id = id;}publicStringgetName(){return name;}publicvoidsetName(String name){this.name = name;}publicdoublegetPrice(){return price;}publicvoidsetPrice(double price){this.price = price;}publicStringgetMiaoshu(){return miaoshu;}publicvoidsetMiaoshu(String miaoshu){this.miaoshu = miaoshu;}}
packageshangpin;importjava.util.List;publicclassZsgc{//添加,删除,修改,单查询,展示全查//添加(上架商品)publicstaticbooleanadd(Goods go,List<Goods> dd){boolean f =true;for(Goods bianhao : dd){if(go.getId().equals(bianhao.getId())){
f =false;break;}}if(f){
dd.add(go);}return f;}//修改(调整价格)publicstaticbooleanmodify(Goods go,List<Goods> dd){boolean f =false;for(Goods bianhao : dd){if(go.getId().equals(bianhao.getId())){
f =true;
bianhao.setName(go.getName());
bianhao.setPrice(go.getPrice());
bianhao.setMiaoshu(go.getMiaoshu());break;}}return f;}//删除(下架商品 )publicstaticbooleandelete(String id,List<Goods> dd){boolean f =false;for(Goods bianhao : dd){if(bianhao.getId().equals(id)){
dd.remove(bianhao);
f =true;break;}}return f;}//单查publicstaticGoodsselect(String id,List<Goods> dd){Goods g =null;for(Goods bianhao : dd){if(bianhao.getId().equals(id)){
g = bianhao;break;}}return g;}//展示(商品列表)publicstaticvoidshow(List<Goods> dd){for(Goods bianhao : dd){System.out.println(bianhao);}}}
packageshangpin;importjava.util.ArrayList;importjava.util.List;importjava.util.Scanner;publicclassSpin{//添加静态字段staticList<Goods> dd =newArrayList<Goods>();staticScanner read =newScanner(System.in);publicstaticvoidmain(String[] args){
dd.add(newGoods("11","汽水",3,"气泡水"));
dd.add(newGoods("12","饼干",8,"饿了就吃"));
dd.add(newGoods("13","辣条",5,"卫龙"));//定义数据类型int n =0;String id,name,miaoshu;double price;Goods g;boolean f;do{System.out.println("欢迎使用999商品管理系统");System.out.println("---商品列表请输入:1---");System.out.println("---上架商品请输入:2---");System.out.println("---下架商品请输入:3---");System.out.println("---调整商品请输入:4---");System.out.println("---单查某个商品请输入:5---");System.out.println("---退出:0----");System.out.print("请选择:");
n = read.nextInt();switch(n){case1:System.out.println("所有商品列表为:");System.out.println("-------编号 名称 价格 描述");Zsgc.show(dd);break;case2:System.out.println("请输入新商品编号:");
id = read.next();System.out.println("请输入新商品名称:");
name = read.next();System.out.println("请输入新商品价格:");
price = read.nextDouble();System.out.println("请输入新商品描述:");
miaoshu = read.next();
g =newGoods(id,name,price,miaoshu);
f =Zsgc.add(g,dd);if(f){System.out.println("上架成功");}else{System.out.println("上架失败");}break;case3:System.out.println("请输入要删除的商品编号:");
id = read.next();
f =Zsgc.delete(id,dd);if(f){System.out.println("删除成功");}else{System.out.println("删除失败");}break;case4:System.out.println("请输入要修改的商品编号");
id = read.next();System.out.println("请输入要修改的商品名称");
name = read.next();System.out.println("请输入要修改的商品价格");
price = read.nextDouble();System.out.println("请输入要修改的商品描述");
miaoshu = read.next();
g =newGoods(id,name,price,miaoshu);
f =Zsgc.modify(g,dd);if(f){System.out.println("修改成功");}else{System.out.println("修改失败");}break;case5:System.out.println("请输入要查询的商品编号");
id = read.next();
g =Zsgc.select(id,dd);if(g !=null){System.out.println(g);}else{System.out.println("没有此商品");}break;}}while(n !=0);}}
运行结果:
本文转载自: https://blog.csdn.net/BO2345/article/details/125706010
版权归原作者 BO2345 所有, 如有侵权,请联系我们删除。
版权归原作者 BO2345 所有, 如有侵权,请联系我们删除。