Java-商品录入系统

商品录入系统:通过简单的java知识对商品增删改查,并且通过终端与用户交互。

一、题目

通过面向对象的思想,设计并完成一个简单的商品录入系统,可以实现:
菜单显示、商品信息展示、商品信息录入、商品信息查询、修改商品信息的功能、删除商品的功能、退出系统的功能
商品的信息包含:id、姓名、价格、库存、厂商

二、分析

1、抽象出一个商品的类 goods

 类中方法:菜单显示、商品信息展示、商品信息录入、商品信息查询、修改商品信息的功能、删除商品的功能
类中属性:id、姓名、价格、库存、厂商

2、调用有参的构造函数对类中属性赋值

public goods(String name, double price, int id, int all, String addr) {
        this.name = name;
        this.price = price;
        this.id = id;
        this.all = all;
        this.addr = addr;
    }

3、定义商品信息展示的方法

需要进行非空判断,如果遇到空的值则不进行打印,没有遇到空值则正常输出,这样可以防止程序报空指针异常的错。

public void show(goods arr[]) {
        for (int i = 0; i < arr.length; i++) {
            if (arr[i] != null) {
                System.out.println("id:" + arr[i].id + ",名称:" + arr[i].name + ",价格:" + arr[i].price + ",库存:" + arr[i].all + ",厂商:" + arr[i].addr);
            }
        }
    }

4、定义商品信息录入的方法

该方法就是添加功能,需要往原有的数组中添加新的对象,同样:在进行添加之前需要进行非空判断,以防空指针异常,这里定义了一个新的数组,将旧数组中的内容一 一给到新数组后再将新加的内容加进去,最后将新数组返回。

public goods[] add(goods[] arr, goods obj) {
        goods newarr[] = new goods[arr.length * 2];
        for (int i = 0; i < arr.length; i++) {
            //将旧的数组赋值给新的数组
            newarr[i] = arr[i];
        }
        newarr[arr.length + 1] = obj;
        return newarr;
    }

5、定义商品信息查询的方法

返回值为Boolean类型,即存在返回值为true,不存在返回false,拿到传过来的数组后遍历数组,进行判断是否非空以及判断传过来的值跟数组中的值是否有相等,如果相等则代表仓库中存在该元素再将flag设置为true即可。

public boolean select(goods[] arr, String name) {
        boolean flag = false;
        for (int i = 0; i < arr.length; i++) {
            if ((arr[i] != null) && (arr[i].name.equals(name))) {
                flag = true;
                break;
            }
        }
        return flag;
    }

6、定义修改商品信息功能的方法

对已有的数据进行修改,但是需要注意的一点是:这里是根据id更改的数据,所以id应该是固定的,其余的数据是用户输入的。

public goods[] update(goods[] arr, int reId, goods obj) {
        for (int i = 0; i < arr.length; i++) {
            if ((arr[i] != null) && (arr[i].id) == reId) {
                arr[i] = obj;
                break;
            }
        }
        return arr;
    }

7、定义删除商品功能的方法

删除商品方法需要返回一个修改完后的数组,获取用户想要删除的id后进行判断,如果仓库中有该id,则将该id所在的对象设置为null

public goods[] delete(goods[] arr, int reId) {
        goods newarr[] = new goods[arr.length * 2];
        for (int i = 0; i < arr.length; i++) {
            if ((arr[i] != null) && (arr[i].id == reId)) {
                arr[i] = null;
                System.out.println("删除成功");
                break;
            }else {
                System.out.println("删除失败");
                break;
            }
        }
        return arr;
    }

三、商品类 goods

public class goods {
    String name; // 商品名称
    double price; // 商品价格
    int id; // 商品id
    int all; // 库存
    String addr; // 厂商

    //    TODO 商品信息展示
    public void show(goods arr[]) {
        for (int i = 0; i < arr.length; i++) {
            if (arr[i] != null) {
                System.out.println("id:" + arr[i].id + ",名称:" + arr[i].name + ",价格:" + arr[i].price + ",库存:" + arr[i].all + ",厂商:" + arr[i].addr);
            }
        }
    }

    //    TODO 商品信息录入
    public goods[] add(goods[] arr, goods obj) {
        goods newarr[] = new goods[arr.length * 2];
        for (int i = 0; i < arr.length; i++) {
            //将旧的数组赋值给新的数组
            newarr[i] = arr[i];
        }
        newarr[arr.length + 1] = obj;
        return newarr;
    }

    //    TODO 商品信息查询
    public boolean select(goods[] arr, String name) {
        boolean flag = false;
        for (int i = 0; i < arr.length; i++) {
            if ((arr[i] != null) && (arr[i].name.equals(name))) {
                flag = true;
                break;
            }
        }
        return flag;
    }

    //    TODO 修改商品信息
    public goods[] update(goods[] arr, int reId, goods obj) {
        for (int i = 0; i < arr.length; i++) {
            if ((arr[i] != null) && (arr[i].id) == reId) {
                arr[i] = obj;
                break;
            }
        }
        return arr;
    }

    //    TODO 删除商品信息
    public goods[] delete(goods[] arr, int reId) {
        goods newarr[] = new goods[arr.length * 2];
        for (int i = 0; i < arr.length; i++) {
            if ((arr[i] != null) && (arr[i].id == reId)) {
                arr[i] = null;
                System.out.println("删除成功");
                break;
            }else {
                System.out.println("删除失败");
                break;
            }
        }
        return arr;
    }


    public goods(String name, double price, int id, int all, String addr) {
        this.name = name;
        this.price = price;
        this.id = id;
        this.all = all;
        this.addr = addr;
    }
}

四、商品测试类 goodsTest

import java.util.Scanner;

public class goodsTest {

    public static void main(String[] args) {
        Scanner input = new Scanner(System.in);

        goods goods1 = new goods("苹果", 12.2, 1, 1000, "湖南");
        goods goods2 = new goods("香蕉", 13.2, 2, 1100, "湖南");
        goods array[] = {goods1, goods2};

        System.out.println("-----------------------");
        System.out.println("请选择下面的操作编号:");
        System.out.println("1-查询所有信息   2-添加信息  3-查询信息  4-修改信息  5-删除信息  6-退出系统");
        System.out.println("-----------------------");
        int i=1;
        while(i>0){
        int number = input.nextInt();
            switch (number) {
                // todo 信息展示
                case 1:
                    goods1.show(array);
                    System.out.println("请选择操作编号:");
                    break;

                // todo 信息录入
                case 2:
                    goods goods3 = new goods("李子", 13.2, 3, 1300, "湖南");
                    array = goods3.add(array, goods3);
                    goods1.show(array);
                    System.out.println("请选择操作编号:");
                    break;
                // todo 信息查询
                case 3:
                    System.out.print("请输入查询的名称:");
                    String res = input.next();
                    boolean flag = goods2.select(array, res);
                    if (flag) {
                        System.out.println("仓库存在该商品");
                    } else {
                        System.out.println("仓库没有该商品");
                    }
                    System.out.println("请选择操作编号:");
                    break;
                // todo 修改信息
                case 4:
                    System.out.print("请输入需要修改的商品的Id:");
                    int reId = input.nextInt();
                    System.out.println("请依次输入修改后的商品信息:");
                    System.out.print("商品名称:");
                    String name = input.next();
                    System.out.print("商品价格:");
                    double price = input.nextDouble();
                    System.out.print("商品库存:");
                    int all = input.nextInt();
                    System.out.print("厂商地址:");
                    String addr = input.next();
                    goods goods4 = new goods(name, price, reId, all, addr);
                    goods array4[] = goods4.update(array, reId, goods4);
                    goods4.show(array4);
                    System.out.println("请选择操作编号:");
                    break;
                case 5:
                    System.out.print("请输入删除的商品的id:");
                    int reId2 = input.nextInt();
                    goods array5[] = goods2.delete(array, reId2);
                    goods2.show(array5);
                    System.out.println("请选择操作编号:");
                    break;
                default:
                    i=-1;
            }

        }



    }
}

评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值