2.柜台商品管理系统
实体类:Goods
属性
id 商品编号
goodsName 商品名称
price 商品价格
desc 商品描述
封装..提供get和set
无参构造器和有参构造器
有参构造器初始化初始化商品对象
重写toString()方法
可以直接显示数据
柜台类:Counter
属性:
柜台商品列表,固定10个商品位置
Goods[]goodses = new Goods[10];
num 柜台商品数量
构造器:
无参构造器初始化2个商品
public Counter(){
this.goodses[0]=new Goods(1001,"巧克力",25,"美味可口,恋爱必备!");
this.goodses[1]=new Goods(1002,"卫龙辣条",1,"隔壁小孩馋哭了!");
num=2;//相当于两个商品
}
业务方法:
展示柜台所有的商品(不能输出null)
public void show(){
}
测试类:CounterTest
程序入口main方法中,创建柜台对象,调用show()方法展示柜台商品
package cn.hp.demo;
public class Goods {
private int id;
private String goodsName;
private double price;
private String desc;
@Override
public String toString() {
return "Goods{" +
"id=" + id +
", goodsName='" + goodsName + '\'' +
", price=" + price +
", desc='" + 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;
}
//get set
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;
}
}
package cn.hp.demo;
public class Counter {
private Goods[] goodses = new Goods[10];
private int num;
public Counter() {
this.goodses[0]=new Goods(1001,"巧克力",25,"美味可口,恋爱必备!");
this.goodses[1]=new Goods(1002,"卫龙辣条",1,"隔壁小孩馋哭了!");
num=2;
}
public void show(){
this.goodses[2]=new Goods(1003,"巧克力",25,"美味可口,恋爱必备!");
this.goodses[3]=new Goods(1004,"卫龙辣条",1,"隔壁小孩馋哭了!");
this.goodses[4]=new Goods(1005,"巧克力",25,"美味可口,恋爱必备!");
this.goodses[5]=new Goods(1006,"卫龙辣条",1,"隔壁小孩馋哭了!");
for (int i = 0; i < goodses.length; i++) {
if (goodses[i]!=null){
System.out.println(goodses[i]);
}
}
}
}
package cn.hp.demo;
public class CounterTest {
public static void main(String[] args) {
Counter c=new Counter();
c.show();
}
}