【无标题】第一次作业(柜台管理系统)

本文介绍了Java编程中创建的`GoodsEntity`类,展示了商品的基本属性和构造方法,并详细讲解了`GoodsManager`类,包括商品数组初始化、展示、添加、删除和修改功能。通过`GoodsTest`测试类,演示了如何使用这些管理工具进行商品操作。
摘要由CSDN通过智能技术生成

实体类

package com.hp.goods;

public class GoodsEntity {
    private Integer id;//商品id
    private String name;//商品名称
    private Double price;//商品价格
    //无参
    public GoodsEntity() {
    }
    //有参
    public GoodsEntity(Integer id, String name, Double price) {
        this.id = id;
        this.name = name;
        this.price = price;
    }
    //gat and set
    public Integer getId() {
        return id;
    }

    public void setId(Integer 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;
    }

    @Override
    public String toString() {
        return "GoodsEntity{" +
                "id=" + id +
                ", name='" + name + '\'' +
                ", price=" + price +
                '}';
    }
}

 管理层

package com.hp.goods;

import java.util.Scanner;

public class GoodsManager {
    private static GoodsEntity[] goodsArrays = new GoodsEntity[5];
    private static int num = 0;

    /**
     * 定义商品数组;商品柜台
     */
    static {
        goodsArrays[0] = new GoodsEntity(1,"鞋",1000.00);
        goodsArrays[1] = new GoodsEntity(2,"上衣",100.00);
    }

    /**
     * 展示全部商品
     */
    public static  void show(){
        for (int i = 0; i < goodsArrays.length; i++) {
            if (goodsArrays[i]!=null){
                System.out.println(goodsArrays[i]);
            }
        }
    }
    /**
     * 添加商品
     */

    public static void add() {
        if (num == 5) {
            System.out.println("--商品满了--");
            return;
        }

        GoodsEntity goods = new GoodsEntity(3,"手机",1600.00);
        System.out.println("--请输入商品id--");
//        Scanner sc = new Scanner(System.in);
        goods.setId(new Scanner(System.in).nextInt());

        System.out.println("--请输入商品名字--");
        goods.setName(new Scanner(System.in).nextLine());

        System.out.println("--请输入商品价格--");
        goods.setPrice(new Scanner(System.in).nextDouble());

        for (int i = 0; i < goodsArrays.length; i++) {
            if (goodsArrays[i] == null) {
                goodsArrays[i] = goods;
                System.out.println("--添加成功--");
                num++;
                return;
            }
        }
    }

    /**
     * 删除商品
     */
    public static void del(){
        System.out.println("--请输入要删除的商品id--");
        int id = new Scanner(System.in).nextInt();
        if (id>5 && id<1){
            System.out.println("--商品不存在--");
            return;
        }else {
            for (int i = 0; i < goodsArrays.length; i++) {
                if (i == id-1){
                    goodsArrays[i]=null;
                    System.out.println("删除成功");
                    return;
                }
            }
        }

    }

    //修改商品

    public static void update(){
        System.out.println("--请输入要修改商品的id--");
        int id = new Scanner(System.in).nextInt();
        if(id>5 && id<1){
            System.out.println("--商品不存在--");
            return;
        }else {
            System.out.println("修改名字");
            String name = new Scanner(System.in).nextLine();
            System.out.println("修改价格");
            Double price = new Scanner(System.in).nextDouble();

            GoodsEntity goodsEntity = new GoodsEntity(id,name,price);
            for (int i = 0; i < goodsArrays.length; i++) {
                if (i == id-1){
                    goodsArrays[i]=goodsEntity;
                    System.out.println("修改成功");
                    return;
                }
            }

        }
    }

    public static void main(String[] args) {
        add();
    }

}

测试类

package com.hp.goods;

import java.util.Scanner;

public class GoodsTest {
    public static void main(String[] args) {
        GoodsManager gm = new GoodsManager();
        System.out.println("===商品柜台菜单===");
        System.out.println("1.展示商品 2.添加商品");
        System.out.println("3.删除商品 4.修改商品");
        System.out.println("0.退出");

        while (true){
            System.out.println("--请输入操作指令--");
            int key = new Scanner(System.in).nextInt();
            switch (key){
                case 1:GoodsManager.show();break;
                case 2:GoodsManager.add();break;
                case 3:GoodsManager.del();break;
                case 4:GoodsManager.update();break;
                case 5:System.exit(0);
            }
        }
    }
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值