Java实现商品的查找、添加、出库、入库操作完整案例

所属分类: 软件编程 / java 阅读数: 56
收藏 0 赞 0 分享

本文实例讲述了Java实现商品的查找、添加、出库、入库操作。分享给大家供大家参考,具体如下:

package com.jredu.oopch08;
public class Goods1 {
    private int id;
    private String name;
    private double price;
    private String uom;
    private int balance;
    public Goods1(int id, String name, double price, String uom, int balance) {
        super();
        this.id = id;
        this.name = name;
        this.price = price;
        this.uom = uom;
        this.balance = balance;
    }
    public int getId() {
        return id;
    }
    public void setId(int id) {
        this.id = id;
    }
    public String getName() {
        return name;
    }
    public void setName(String name) {
        this.name = name;
    }
    public double getPrice() {
        return price;
    }
    public void setPrice(double price) {
        this.price = price;
    }
    public String getUom() {
        return uom;
    }
    public void setUom(String uom) {
        this.uom = uom;
    }
    public int getBalance() {
        return balance;
    }
    public void setBalance(int balance) {
        this.balance = balance;
    }
}

package com.jredu.oopch08;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;
import java.util.Scanner;
import java.util.Set;
public class TestGoods1 {
    private static Map map = new HashMap<>();
    private static Scanner in = new Scanner(System.in);
    public static void get() {
        Goods1 goods1 = new Goods1(1001, "脉动水蜜桃 ", 7.0, "1.5l", 50);
        Goods1 goods2 = new Goods1(1002, "桃李熟切片 ", 6.5, "400g", 10);
        Goods1 goods3 = new Goods1(1003, "吉白芝麻油 ", 9.5, "125ml", 20);
        Goods1 goods4 = new Goods1(1004, "雀巢奶咖啡", 1.5, "13g", 200);
        Goods1 goods5 = new Goods1(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);
    }
    public static boolean check(int id) {
        // 检测匹配id
        if (!map.containsKey(id)) {
            // 没有匹配id
            return false;
        } else {
            // 有匹配的id
            return true;
        }
    }
    public static void add() {// 新增商品
        System.out.println(">>新增商品");
        System.out.print("请输入商品编号:");
        int id = in.nextInt();
        if (new TestGoods1().check(id)) {
            // 有匹配的id
            System.out.println("对不起,此商品已存在!");
        } else {
            System.out.print("请输入商品名称:");
            String name = in.next();
            System.out.print("请输入商品单价:");
            double price = in.nextDouble();
            System.out.print("请输入商品单位:");
            String uom = in.next();
            System.out.print("请输入商品库存:");
            int balance = in.nextInt();
            Goods1 goods6 = new Goods1(id, name, price, uom, balance);
            map.put(goods6.getId(), goods6);
            System.out.println("新增成功!");
        }
    }
    public static void show() {// 显示商品信息
        System.out.println("商品编号\t商品名称\t\t商品单价\t单位\t数量");
        Set<Map.Entry<Integer, Goods1>> entrySet = map.entrySet();
        Iterator<Map.Entry<Integer, Goods1>> iter = entrySet.iterator();
        while (iter.hasNext()) {
            Map.Entry<Integer, Goods1> 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());
        }
    }
    public static void inStore() {// 入库
        System.out.println(">>商品入库");
        System.out.print("请输入商品编号:");
      int id = in.nextInt();
      for (int i = 0; i < map.size(); i++) {
          if (new TestGoods1().check(id)) {
                //有匹配的id
                System.out.print("请输入入库数量:");
                int count = in.nextInt();
                    int c = ((Goods1) map.get(id)).getBalance()+count;
                    ((Goods1) map.get(id)).setBalance(c);
                    break;
            }else{
                //没有匹配的id
                System.out.println("对不起,此商品不存在!");
                break;
            }
        }
    }
    public void outStore() {// 出库
        System.out.println(">>商品出库");
        System.out.print("请输入商品编号:");
        int id = in.nextInt();
        for (int i = 0; i < map.size(); i++) {
          if (new TestGoods1().check(id)) {
                //有匹配的id
                System.out.print("请输入出库数量:");
                int count = in.nextInt();
                if(count>((Goods1)map.get(id)).getBalance()){
                    System.out.println("库存不足,出库失败!");
                }else{
                    int c = ((Goods1) map.get(id)).getBalance()-count;
                    ((Goods1) map.get(id)).setBalance(c);
                    break;
                }
            }else{
                //没有匹配的id
                System.out.println("对不起,此商品不存在!");
                break;
            }
        }
    }
    public static void main(String[] args) {
        // TODO Auto-generated method stub
        TestGoods1 t = new TestGoods1();
        t.get();
        //t.add();
    //    t.show();
    //    t.inStore();
        t.show();
        t.outStore();
        t.show();
    }
}

更多java相关内容感兴趣的读者可查看本站专题:《Java面向对象程序设计入门与进阶教程》、《Java数据结构与算法教程》、《Java操作DOM节点技巧总结》、《Java文件与目录操作技巧汇总》和《Java缓存操作技巧汇总

希望本文所述对大家java程序设计有所帮助。

更多精彩内容其他人还在看

JavaWeb项目部署到服务器详细步骤详解

这篇文章主要介绍了JavaWeb项目如何部署到服务器,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧
收藏 0 赞 0 分享

IDEA基于支付宝小程序搭建springboot项目的详细步骤

这篇文章主要介绍了IDEA基于支付宝小程序搭建springboot项目的详细步骤,小编觉得挺不错的,现在分享给大家,也给大家做个参考。一起跟随小编过来看看吧
收藏 0 赞 0 分享

详解SpringBoot应用服务启动与安全终止

这篇文章主要介绍了SpringBoot应用服务启动与安全终止,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧
收藏 0 赞 0 分享

Spring Boot启动及退出加载项的方法

这篇文章主要介绍了Spring Boot启动及退出加载项的方法,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧
收藏 0 赞 0 分享

Spring Data Jpa 自动生成表结构的方法示例

这篇文章主要介绍了Spring Data Jpa 自动生成表结构的方法示例,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧
收藏 0 赞 0 分享

IDEA中osgi的开发应用指南详解

这篇文章主要介绍了IDEA中osgi的开发应用指南详解,小编觉得挺不错的,现在分享给大家,也给大家做个参考。一起跟随小编过来看看吧
收藏 0 赞 0 分享

详解用maven将dubbo工程打成jar包运行

这篇文章主要介绍了详解用maven将dubbo工程打成jar包运行,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧
收藏 0 赞 0 分享

详解Java合并数组的两种实现方式

这篇文章主要介绍了Java合并数组的两种实现方式,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧
收藏 0 赞 0 分享

使用Jenkins Pipeline自动化构建发布Java项目的方法

这篇文章主要介绍了使用Jenkins Pipeline自动化构建发布Java项目的方法,小编觉得挺不错的,现在分享给大家,也给大家做个参考。一起跟随小编过来看看吧
收藏 0 赞 0 分享

使用Maven配置Spring的方法步骤

这篇文章主要介绍了使用Maven配置Spring的方法步骤,小编觉得挺不错的,现在分享给大家,也给大家做个参考。一起跟随小编过来看看吧
收藏 0 赞 0 分享
查看更多