Java实现简易商品信息管理

一、实验内容(步骤与截图)

1. 概述

该实验主要有三个页面:商品信息管理页面、商品基本信息管理页面、商品销量管理页面。

商品信息管理页面:提供系统的主要功能入口,如商品基本信息管理、商品销量管理功能两个选项。

商品基本信息管理页面:用于对商品的基本信息进行导入、显示、添加、删除、修改、查找、导出以及返回主页面操作,商品的基本信息包括商品号、商品名称、商品的三个季度下销量五个字段。

商品销量管理页面:包括商品平均销量管理、商品最高和最低销量管理相关页面,以实现对商品相关信息的全面管理和操作。

1.1 设计目的

为了有效管理商品的各类信息,包括基本属性、相关关联信息等,提高商品管理的效率和准确性。

提供直观、便捷的操作界面,方便用户(如管理员)进行日常的商品信息处理工作。

实现商品信息的规范化、集中化管理,便于查询、统计和分析,为企业的决策提供数据支持。

1.2 任务及要求

用Java语言编写一个程序,模拟学生管理系统。编写一个商品信息管理系统,实现商品信息管理.

2. 需求分析

该实验的用户即为系统管理员。功能需求涵盖商品基本信息管理,如添加、修改、删除和查询等,以及商品销量管理。性能上要求响应快、处理大量数据稳定高效。安全方面需授权操作与数据保护。界面要简洁明了且有清晰导航,以构建一个实用、高效、安全的商品信息管理系统。

3. 类的定义与设计

商品信息的基本类:

public class Product {

    private String id;

    private String name;

    private float sell1;

    private float sell2;

    private float sell3;

    public Product(String id,String name,float sell1,float sell2,float sell3){

        this.id=id;

        this.name=name;

        this.sell1=sell1;

        this.sell2=sell2;

        this.sell3=sell3;

    }

    public String getId() {

        return id;

    }

    public String getName() {

        return name;

    }

    public float getSell1() {

        return sell1;

    }

    public float getSell2() {

        return sell2;

    }

    public float getSell3() {

        return sell3;

    }

    public void setId(String id) {

        this.id = id;

    }

    public void setName(String name) {

        this.name = name;

    }

    public void setSell1(float sell1) {

        this.sell1 = sell1;

    }

    public void setSell2(float sell2) {

        this.sell2 = sell2;

    }

    public void setSell3(float sell3) {

        this.sell3 = sell3;

}

@Override

    public String toString() {

        return "Product{" +

                "id='" + id + '\'' +

                ", name='" + name + '\'' +

                ", sell1=" + sell1 +

                ", sell2=" + sell2 +

                ", sell3=" + sell3 +

                '}';

    }

}

商品功能菜单的类:

public class ProductList {

    private Product[] products;

    private int total = 0;

    public ProductList(int maxLength) {

        products = new Product[maxLength];

    }

    public boolean addProduct(Product pro){

        if(total >= products.length){

            return false;

        }

        products[total++] = pro;

        return true;

    }

    public boolean delProduct(int index){

        if(index < 0 || index >= total){

            return false;

        }

        for(int i = index ;i < total -1;i++){

            products[i] = products[i + 1];

        }

        products[--total] = null;

        return true;

    }

    public boolean modifyProduct(int index,Product pro){

        if(index < 0 || index >= total){

            return false;

        }

        products[index] = pro;

        return true;

    }

    public Product[] getAllProducts(){

        Product[] pros = new Product[total];

        for(int i = 0; i < total; i++){

            pros[i] = products[i];

        }

        return pros;

    }

}

4. 编程实现

导入功能:

public void load() throws IOException {

        Scanner sc = new Scanner(System.in);

        System.out.print("请输入要导入的文件:");

        String fn = sc.next();

        File file = new File(fn);

        if (file.exists()){

            BufferedReader br = new BufferedReader(new FileReader(fn));

            String  str ;

            while((str = br.readLine()) != null){

                String[] product = str.split(",");

                String id = product[0];

                String name = product[1];

                float sell1 = Integer.parseInt(product[2]);

                float sell2 = Integer.parseInt(product[3]);

                float sell3 = Integer.parseInt(product[4]);

                Product pro = new Product(id, name, sell1, sell2, sell3);

                if (idExists(id)){

                    System.out.println("---该商品已存在---");

                }else{

                    productList.addProduct(pro);

                }

            }

            br.close();

            System.out.println("---导入成功---");

        }else{

            System.out.println("---要导入的文件不存在---");

        }

}

显示功能:

public void showall(){

        System.out.println("---------------------商品信息----------------------");

        Product[] pros = productList.getAllProducts();

        if (pros.length == 0) {

            System.out.println("尚没有商品记录...");

        } else {

            System.out.println("商品编号\t商品名称\t第一季度销量\t第二季度销量\t第三季度销量");

            for (int i = 0; i < pros.length; i++) {

                System.out.println(pros[i].getId() + "    \t"

                        + pros[i].getName() + "    \t"

                        + pros[i].getSell1() + "    \t"

                        + pros[i].getSell2() + "    \t"

                        + pros[i].getSell3());

            }

        }

        System.out.println("-------------------------------------------------");

}

增加功能:

public void addproduct(){

        Scanner sc = new Scanner(System.in);

        System.out.print("编号:");

        String id = sc.next();

        int index = find(id);

        if (index != -1) {

            System.out.println("---该商品已存在---");

        } else {

            System.out.print("商品名称:");

            String name = sc.next();

            sell1 = entersell("第一季度销量:");

            sell2 = entersell("第二季度销量:");

            sell3 = entersell("第三季度销量:");

            Product pro = new Product(id, name, sell1, sell2, sell3);

            boolean flag = productList.addProduct(pro);

            if (flag) {

                System.out.println("---添加成功---");

            } else {

                System.out.println("---添加失败---");

            }

        }

}

删除功能:

public void deleteproduct(){

        System.out.print("请输入要删除的商品编号:");

        Scanner sc = new Scanner(System.in);

        String id = sc.next();

        int index = find(id);

        if (index == -1) {

            System.out.println("---该商品不存在---");

            return;

        }

        boolean flag = productList.delProduct(index);

        if (flag) {

            System.out.println("---删除成功---");

        } else {

            System.out.println("---删除失败---");

        }

}

修改功能:

public void modifyproduct(){

        System.out.print("请输入要修改的商品编号:");

        Scanner sc = new Scanner(System.in);

        String id = sc.next();

        int index = find(id);

        if (index == -1) {

            System.out.println("---该商品不存在---");

            return;

        }

        System.out.print("商品名称:");

        String name = sc.next();

        sell1 = entersell("第一季度销量:");

        sell2 = entersell("第二季度销量:");

        sell3 = entersell("第三季度销量:");

        Product pro = new Product(id, name, sell1,sell2,sell3);

        boolean flag = productList.modifyProduct(index,pro);

        if (flag) {

            System.out.println("---修改成功---");

        } else {

            System.out.println("---修改失败---");

        }

}

查找功能:

public void findproduct() {

        while (true) {

            findmenu();

            Scanner sc = new Scanner(System.in);

            String choice = sc.next();

            switch (choice) {

                case "1":

                    findId();

                    break;

                case "2":

                    findname();

                    break;

                case "0":

                    break;

                default:

                    System.out.println("输入错误,请重新输入!");

            }

            if(choice.equals("0")){

                break;

            }

        }

    }

    public void findId(){

        Scanner sc = new Scanner(System.in);

        System.out.print("请输入要查找的商品编号:");

        String id = sc.next();

        Product[] pros = productList.getAllProducts();

        System.out.println("商品编号\t商品名称\t第一季度销量\t第二季度销量\t第三季度销量");

        for (int i = 0; i < pros.length; i++) {

            if (id.equals(pros[i].getId())) {

                System.out.println(pros[i].getId() + "    \t"

                        + pros[i].getName() + "    \t"

                        + pros[i].getSell1() + "    \t"

                        + pros[i].getSell2() + "    \t"

                        + pros[i].getSell3());

            }

        }

    }

    public void findname(){

        Scanner sc = new Scanner(System.in);

        System.out.print("请输入要查找的商品名称(全称或关键字皆可):");

        String keyWord = sc.next();

        Product[] pros = productList.getAllProducts();

        System.out.println("商品编号\t商品名称\t第一季度销量\t第二季度销量\t第三季度销量");

        for (int i = 0; i < pros.length; i++) {

            if (pros[i].getName().contains(keyWord)) {

                System.out.println(pros[i].getId() + "    \t"

                        + pros[i].getName() + "    \t"

                        + pros[i].getSell1() + "    \t"

                        + pros[i].getSell2() + "    \t"

                        + pros[i].getSell3());

            }

        }

}

导出功能:

public void save() throws IOException {

        Product[] pros = productList.getAllProducts();

        Scanner sc = new Scanner(System.in);

        System.out.print("请输入要导出的文件名:");

        String fn = sc.next();

        FileWriter fileWriter = new FileWriter(fn);

        BufferedWriter bw = new BufferedWriter(fileWriter);

        for(int i = 0;i < pros.length;i++){

            bw.write(pros[i].toString());

            bw.newLine();

        }

        bw

        bw.close();

        System.out.println("---导出成功---");

}

商品销量管理功能(平均销量、最低销量、最高销量):

//平均销量

public void avgsell(){

        float avgsell = 0;

        float sumsell=0;

        int result=0;

        Scanner sc = new Scanner(System.in);

        System.out.print("请输入要统计的商品编号:");

        String id = sc.next();

        Product[] pros = productList.getAllProducts();

        for (int i = 0; i < pros.length; i++) {

            if (id.equals(pros[i].getId())) {

                System.out.println("商品编号\t商品名称\t平均销量");

                sumsell=pros[i].getSell1()+pros[i].getSell2()+pros[i].getSell3();

                avgsell=sumsell/3;

                System.out.println(pros[i].getId() + "    \t"

                        + pros[i].getName() + "    \t"

                        + avgsell);

                result=1;

            }

        }

        if(result == 0){

            System.out.println("商品不存在!");

        }

}

//最低销量

public void minsell(){

        float minsell = 0;

        int result=0;

        Scanner sc = new Scanner(System.in);

        System.out.print("请输入要统计的商品编号:");

        String id = sc.next();

        Product[] pros = productList.getAllProducts();

        for (int i = 0; i < pros.length; i++) {

            if (id.equals(pros[i].getId())) {

                System.out.println("商品编号\t商品名称\t最低销量");

                if((pros[i].getSell1()<=pros[i].getSell2()) && (pros[i].getSell1()<=pros[i].getSell3())){

                    minsell=pros[i].getSell1();

                }

                if((pros[i].getSell2()<=pros[i].getSell1()) && (pros[i].getSell2()<=pros[i].getSell3())){

                    minsell=pros[i].getSell2();

                }

                if((pros[i].getSell3()<=pros[i].getSell2()) && (pros[i].getSell3()<=pros[i].getSell1())){

                    minsell=pros[i].getSell3();

                }

                System.out.println(pros[i].getId() + "    \t"

                        + pros[i].getName() + "    \t"

                        + minsell);

                result=1;

            }

        }

        if(result == 0){

            System.out.println("商品不存在!");

        }

}

//最高销量

public void maxsell(){

        float maxsell = 0;

        int result=0;

        Scanner sc = new Scanner(System.in);

        System.out.print("请输入要统计的商品编号:");

        String id = sc.next();

        Product[] pros = productList.getAllProducts();

        for (int i = 0; i < pros.length; i++) {

            if (id.equals(pros[i].getId())) {

                System.out.println("商品编号\t商品名称\t最高销量");

                if((pros[i].getSell1()>=pros[i].getSell2()) && (pros[i].getSell1()>=pros[i].getSell3())){

                    maxsell=pros[i].getSell1();

                }

                if((pros[i].getSell2()>=pros[i].getSell1()) && (pros[i].getSell2()>=pros[i].getSell3())){

                    maxsell=pros[i].getSell2();

                }

                if((pros[i].getSell3()>=pros[i].getSell2()) && (pros[i].getSell3()>=pros[i].getSell1())){

                    maxsell=pros[i].getSell3();

                }

                System.out.println(pros[i].getId() + "    \t"

                        + pros[i].getName() + "    \t"

                        + maxsell);

                result=1;

            }

        }

        if(result == 0){

            System.out.println("商品不存在!");

        }

}

5. 系统测试运行截图

商品信息管理页面(主菜单):

导入:

增加:

显示:

删除:

修改:

查找:

导出:

商品销量:

(商品存在的情况):

(商品不存在的情况):

  • 18
    点赞
  • 31
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值