代码改变世界

140201126-杨鹏飞-作业七

2016-04-28 21:19  140201126杨鹏飞  阅读(191)  评论(0编辑  收藏  举报

interface Yongpin{ 
 public String getName();
 public int getCount1();
 public float getPrice();
}
class Wenju implements Yongpin{ 
 private String name ; 
 private int count1 ;
 private float price;
 public Wenju(String name,int count1,float price){
  this.setName(name);
  this.setCount1(count1);
  this.setPrice(price);
 }
 public void setName(String name){
  this.name = name ;
 }
 public void setCount1(int count1){
  this.count1 = count1 ;
 }
 public void setPrice(float price){
  this.price=price;
 }
 public String getName(){
  return this.name ;
 }
 public int getCount1(){
  return this.count1 ;
 }
 public float getPrice(){
  return price;
 }
};
class Shenghuoyongpin implements Yongpin{ 
 private String name; 
 private int count1;
 private float price;
 public Shenghuoyongpin(String name,int count1,float price){
  this.setName(name);
  this.setCount1(count1);
  this.setPrice(price);
 }
 public void setName(String name){
  this.name = name ;
 }
 public void setCount1(int count1){
  this.count1=count1;
 }
 public void setPrice(float price){
  this.price=price;
 }
 public String getName(){
  return this.name;
 }
 public int getCount1(){
  return this.count1 ;
 }
 public float getPrice(){
  return price;
 }
};
class Gouwuche{ 
 private Yongpin[] yongpins ; 
 private int foot ;
 public Gouwuche(int len){
  if(len>0){
   this.yongpins= new Yongpin[len] ; 
  }else{
   this.yongpins = new Yongpin[1] ; 
  }
 }
 public boolean add(Yongpin yongpin){ 
  if(this.foot<this.yongpins.length){
   this.yongpins[this.foot] = yongpin ; 
   this.foot++ ;
   return true ;
  }else{
   return false ;
  }
 }
 public Yongpin[] search(String keyWord){
  Yongpin y[] = null ;
  int count = 0 ; 
  for(int i=0;i<this.yongpins.length;i++){
   if(this.yongpins[i]!=null){  
    if(this.yongpins[i].getName().indexOf(keyWord)!=-1){
     count++ ; 
    }
   }
  }
  y = new Yongpin[count] ; 
  int f = 0 ; 
  for(int i=0;i<this.yongpins.length;i++){
   if(this.yongpins[i]!=null){  
    if(this.yongpins[i].getName().indexOf(keyWord)!=-1){
     y[f] = this.yongpins[i];
     f++ ;
    }
   }
  }
  return y ;
 }
};
public class ADemo6 {
 public static void main(String[] args) {
  Gouwuche g=new Gouwuche(6);
  g.add(new Wenju("单肩包",1,130.5f));
  g.add(new Wenju("碳素笔",2,3.0f));
  g.add(new Wenju("参考资料",2,43.4f));
  g.add(new Shenghuoyongpin("脸盆",2,50.8f));
  g.add(new Shenghuoyongpin("洗手液",1,15.1f));
  g.add(new Shenghuoyongpin("脸霜",1,67.2f));
  print(g.search(""));
  
 }
 public static void print(Yongpin y[]){
  float sum=0;
  for(int i=0;i<y.length;i++){
   if(y[i]!=null){
    System.out.println(y[i].getName() + "," + y[i].getCount1()+","+y[i].getPrice());
   }
   sum=sum+y[i].getPrice();
  }
  System.out.println("sum:"+sum);
 }
}