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();
}
}