Java实现商品的查找、添加、出库、入库操作完整案例
本文实例讲述了Java实现商品的查找、添加、出库、入库操作。分享给大家供大家参考,具体如下:
packagecom.jredu.oopch08; publicclassGoods1{ privateintid; privateStringname; privatedoubleprice; privateStringuom; privateintbalance; publicGoods1(intid,Stringname,doubleprice,Stringuom,intbalance){ super(); this.id=id; this.name=name; this.price=price; this.uom=uom; this.balance=balance; } publicintgetId(){ returnid; } publicvoidsetId(intid){ this.id=id; } publicStringgetName(){ returnname; } publicvoidsetName(Stringname){ this.name=name; } publicdoublegetPrice(){ returnprice; } publicvoidsetPrice(doubleprice){ this.price=price; } publicStringgetUom(){ returnuom; } publicvoidsetUom(Stringuom){ this.uom=uom; } publicintgetBalance(){ returnbalance; } publicvoidsetBalance(intbalance){ this.balance=balance; } }
packagecom.jredu.oopch08; importjava.util.HashMap; importjava.util.Iterator; importjava.util.Map; importjava.util.Scanner; importjava.util.Set; publicclassTestGoods1{ privatestaticMapmap=newHashMap<>(); privatestaticScannerin=newScanner(System.in); publicstaticvoidget(){ Goods1goods1=newGoods1(1001,"脉动水蜜桃",7.0,"1.5l",50); Goods1goods2=newGoods1(1002,"桃李熟切片",6.5,"400g",10); Goods1goods3=newGoods1(1003,"吉白芝麻油",9.5,"125ml",20); Goods1goods4=newGoods1(1004,"雀巢奶咖啡",1.5,"13g",200); Goods1goods5=newGoods1(1005,"白玉黄豆芽",2.4,"350g",50); map.put(goods1.getId(),goods1); map.put(goods2.getId(),goods2); map.put(goods3.getId(),goods3); map.put(goods4.getId(),goods4); map.put(goods5.getId(),goods5); } publicstaticbooleancheck(intid){ //检测匹配id if(!map.containsKey(id)){ //没有匹配id returnfalse; }else{ //有匹配的id returntrue; } } publicstaticvoidadd(){//新增商品 System.out.println(">>新增商品"); System.out.print("请输入商品编号:"); intid=in.nextInt(); if(newTestGoods1().check(id)){ //有匹配的id System.out.println("对不起,此商品已存在!"); }else{ System.out.print("请输入商品名称:"); Stringname=in.next(); System.out.print("请输入商品单价:"); doubleprice=in.nextDouble(); System.out.print("请输入商品单位:"); Stringuom=in.next(); System.out.print("请输入商品库存:"); intbalance=in.nextInt(); Goods1goods6=newGoods1(id,name,price,uom,balance); map.put(goods6.getId(),goods6); System.out.println("新增成功!"); } } publicstaticvoidshow(){//显示商品信息 System.out.println("商品编号\t商品名称\t\t商品单价\t单位\t数量"); Set>entrySet=map.entrySet(); Iterator >iter=entrySet.iterator(); while(iter.hasNext()){ Map.Entry entry=iter.next(); System.out.print(entry.getKey()+"\t"); System.out.println(entry.getValue().getName()+"\t\t"+entry.getValue().getPrice()+"\t" +entry.getValue().getUom()+"\t"+entry.getValue().getBalance()); } } publicstaticvoidinStore(){//入库 System.out.println(">>商品入库"); System.out.print("请输入商品编号:"); intid=in.nextInt(); for(inti=0;i >商品出库"); System.out.print("请输入商品编号:"); intid=in.nextInt(); for(inti=0;i ((Goods1)map.get(id)).getBalance()){ System.out.println("库存不足,出库失败!"); }else{ intc=((Goods1)map.get(id)).getBalance()-count; ((Goods1)map.get(id)).setBalance(c); break; } }else{ //没有匹配的id System.out.println("对不起,此商品不存在!"); break; } } } publicstaticvoidmain(String[]args){ //TODOAuto-generatedmethodstub TestGoods1t=newTestGoods1(); t.get(); //t.add(); //t.show(); //t.inStore(); t.show(); t.outStore(); t.show(); } }
更多java相关内容感兴趣的读者可查看本站专题:《Java面向对象程序设计入门与进阶教程》、《Java数据结构与算法教程》、《Java操作DOM节点技巧总结》、《Java文件与目录操作技巧汇总》和《Java缓存操作技巧汇总》
希望本文所述对大家java程序设计有所帮助。
声明:本文内容来源于网络,版权归原作者所有,内容由互联网用户自发贡献自行上传,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任。如果您发现有涉嫌版权的内容,欢迎发送邮件至:czq8825#qq.com(发邮件时,请将#更换为@)进行举报,并提供相关证据,一经查实,本站将立刻删除涉嫌侵权内容。