使用面向对象的概念表示出下面的生活场景: 小明去超市买东西,所有买到的东西都放在了购物车之中,最后到收银台一起结账。
一、设计思路:
- 购物车可以放置许多商品,有生活用品、食物、文具等;
- 商品属性应该具有商品名、价格、数量、生产日期等;
- 设置一个接口存储每一个商品类都可以用到的函数;
- 将收银台的结账功能放置到购物车类中。
二、代码块:
- 接口:
interface Goods
{
public String getName();//取得产品名
public String getProduction();//取得生产日期
public double getPrice();//取得产品价格
public int getNumber();//取得数量
public void print();//打印信息
}
- 商品类:
class Daily implements Goods
{
private String production;
private String name;
private double price;
private int number;
public Daily(String production,String name,double price,int number)
{
this.production=production;
this.name=name;
this.price=price;
this.number=number;
}
public String getName()//取得产品名
{
return this.name;
}
public String getProduction()//取得生产日期
{
return this.production;
}
public double getPrice()//取得产品价格
{
return this.price;
}
public int getNumber()
{
return this.number;
}
public void print()//打印信息
{
System.out.println("产品名:"+this.getName()+'\n'+"生产日期:"+this.getProduction()+'\n'+"价格:"+this.getPrice()+'\n'+"数量:"+this.getNumber()+'\n');
}
}
class Food implements Goods
{
private String production;
private String name;
private double price;
private int number;
public Food(String production,String name,double price,int number)
{
this.production=production;
this.name=name;
this.price=price;
this.number=number;
}
public String getName()//取得产品名
{
return this.name;
}
public String getProduction()//取得生产日期
{
return this.production;
}
public double getPrice()//取得产品价格
{
return this.price;
}
public int getNumber()
{
return this.number;
}
public void print()//打印信息
{
System.out.println("产品名:"+this.getName()+'\n'+"生产日期:"+this.getProduction()+'\n'+"价格:"+this.getPrice()+'\n'+"数量:"+this.getNumber()+'\n');
}
}
class Stationary implements Goods//定义文具类
{
private String production;
private String name;
private double price;
private int number;
public Stationary(String production,String name,double price,int number)
{
this.production=production;
this.name=name;
this.price=price;
this.number=number;
}
public String getName()//取得产品名
{
return this.name;
}
public String getProduction()//取得生产日期
{
return this.production;
}
public double getPrice()//取得产品价格
{
return this.price;
}
public int getNumber()
{
return this.number;
}
public void print()//打印信息
{
System.out.println("产品名:"+this.getName()+'\n'+"生产日期:"+this.getProduction()+'\n'+"价格:"+this.getPrice()+'\n'+"数量:"+this.getNumber()+'\n');
}
}
- 购物车类;
class ShopCar
{
private Goods[] goods;//使用接口接收对象
private int foot;//数据的保存位置
public ShopCar(int len)
{
if(len>0)
this.goods=new Goods[len];
else
this.goods=new Goods[1];
}
public boolean add(Goods g)
{
if(this.foot<this.goods.length)//自己的对象可以直接调用私有数据成员
{
this.goods[foot]=g;
this.foot++;
return true;
}
else
return false;
}
public void search(String keyword)
{
for(int i=0;i<this.goods.length;i++)
{
if(this.goods[i]==null)
System.out.println("你没有买任何东西");
if(this.goods[i].getName().equals(keyword))//不可以直接引用name
{
this.goods[i].print();
}
}
}
public double bill()
{
double sum = 0;
for(int i=0;i<this.goods.length;i++)
{
if(this.goods[i]!=null)
{
sum+=this.goods[i].getPrice()*this.goods[i].getNumber();
}
}
return sum;
}
}
- main:
public class Homework6006
{
public static void main(String[] args)
{
ShopCar sc=new ShopCar(5);
sc.add(new Food("火腿肠","2020.10.23",2.00,12));
System.out.println(sc.bill());
}
}