public class Goods {
private int id; //商品编号
private String goodsName; //商品名字
private double price; //商品价格
private String desc; //商品描述
public Goods() {
}
public Goods(int id, String goodsName, double price, String desc) {
this.id = id;
this.goodsName = goodsName;
this.price = price;
this.desc = desc;
}
@Override
public String toString() {
return "Goods{" +
"id=" + id +
", goodsName='" + goodsName + '\'' +
", price=" + price +
", desc='" + desc + '\'' +
'}';
}
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getGoodsName() {
return goodsName;
}
public void setGoodsName(String goodsName) {
this.goodsName = goodsName;
}
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;
}
}
二、创建柜台类
public class Counter {
private Goods[] goods = new Goods[10];
private int num; //柜台数量
public Counter() {
this.goods[0]=new Goods(1001,"巧克力",25,"美味可口,恋爱必备!");
this.goods[1]=new Goods(1002,"卫龙辣条",1,"隔壁小孩馋哭了!");
num=2;//相当于两个商品
}
public void show(){
for (Goods gd :
goods) {
if (gd != null)
System.out.println(gd);
}
}
}
三、在柜台类中创建show方法
public void show(){
for (Goods gd :
goods) {
if (gd != null)
System.out.println(gd);
}
四、创建CounterTest类测试show方法
public class CounterTest {
public static void main(String[] args) {
Counter counter = new Counter();
counter.show();
}
}