【无标题】实体类:Goods

该博客展示了如何创建一个简单的商品管理类`Goods`以及一个商品柜台类`Counter`。`Goods`类包含了商品的基本属性如ID、名称、价格和描述,并实现了toString方法。`Counter`类则初始化了一些商品实例并提供了展示所有商品的方法。在测试类`CounterTest`中,创建了`Counter`对象并显示了商品柜中的商品信息。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

package demo04;

public class Goods {
        private int id;
        private String name;
        private double price;
        private String desc;

        public Goods() {
        }

        public Goods(int id, String name, double price, String desc) {
            this.id = id;
            this.name = name;
            this.price = price;
            this.desc = desc;
        }

        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 getDesc() {
            return desc;
        }

        public void setDesc(String desc) {
            this.desc = desc;
        }

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

package demo04;

public class Counter {
        //商品柜
        Goods[]goodsArrays = new Goods[9];
        int num = 0;
        public Counter(){
            this.goodsArrays[0] = new Goods(1001,"巧克力",25,"美味可口,恋爱必备!");
            this.goodsArrays[1] = new Goods(1002,"卫龙辣条",2,"隔壁小孩馋哭了!");
            this.goodsArrays[2] = new Goods(1003,"蜜雪冰城",7,"你爱我我爱你蜜雪冰城甜蜜蜜~~");
            this.goodsArrays[3] = new Goods(1004,"盼盼小面包",36,"面包还是盼盼好!");
            this.goodsArrays[4] = new Goods(1005,"百世",10,"好喝!");
            this.goodsArrays[5] = new Goods(1006,"奥利奥",16,"好吃不贵!!!");
            num=6;//已占用6个位置
        }
        //展示商品
        public void show(){
            if(num == 0){
                System.out.println("------------商品柜没有商品了----------");
                return;
            }
            for (int i = 0; i < goodsArrays.length; i++) {
                if(goodsArrays[i] != null){
                    System.out.println(goodsArrays[i]);
                }
            }
        }
    }

package demo04;
public class CounterTest {
        public static void main(String[] args) {
            Counter ct = new Counter();
            ct.show();
        }
    }

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值