使用面向对象的概念表示出下面的生活场景: 小明去超市买东西,所有买到的东西都放在了购物车之中,最后到收银台一起结账

使用面向对象的概念表示出下面的生活场景: 小明去超市买东西,所有买到的东西都放在了购物车之中,最后到收银台一起结账。

一、设计思路:

  1. 购物车可以放置许多商品,有生活用品、食物、文具等;
  2. 商品属性应该具有商品名、价格、数量、生产日期等;
  3. 设置一个接口存储每一个商品类都可以用到的函数;
  4. 将收银台的结账功能放置到购物车类中。

二、代码块:

  1. 接口:
interface Goods
{
 public String getName();//取得产品名
 public String getProduction();//取得生产日期
 public double getPrice();//取得产品价格
 public int getNumber();//取得数量
 public void print();//打印信息
}
  1. 商品类:
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');
 }
}
  1. 购物车类;
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;
 }
}
  1. 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());
 }
}
  • 6
    点赞
  • 34
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值