码迷,mamicode.com
首页 > 编程语言 > 详细

JAVA第七次作业

时间:2016-04-28 22:32:28      阅读:201      评论:0      收藏:0      [点我收藏+]

标签:

程序代码:

public interface Goods {
public String getName();
public double getPrice();
}
public class EatFood implements Goods{
private String name;
private double price;
public EatFood() {

}
public EatFood(String name, double price) {
super();
this.name = name;
this.price = price;
}
public double getPrice() {
return this.price;
}
public void setPrice(double price) {
this.price = price;
}
public void setName(String name) {
this.name = name;
}
public String getName() {
return this.name;
}
}
public class WashGoods implements Goods{
private String name;
private double price;
public WashGoods() {

}
public WashGoods(String name, double price) {
super();
this.name = name;
this.price = price;
}
public double getPrice() {
return this.price;
}
public void setPrice(double price) {
this.price = price;
}
public void setName(String name) {
this.name = name;
}
public String getName() {
return this.name;
}
}
public class ShopCar {
private Goods goods[]=null;
private int foot;

//数组的大小由程序外部决定
public ShopCar(int len) {
if(len>0){
goods=new Goods[len];
}else{
goods=new Goods[1];
}
}

//判断数组的内容是否已满,未满,则添加
public boolean add(Goods g){
if(this.foot<this.goods.length){
this.goods[foot]=g;
foot++;
return true;
}else{
return false;
}
}
         //关键字查找
public Goods[] search(String keyword){
  Goods go[]=null;
  int count=0;
for(int i=0;i<this.goods.length;i++){
         if(goods[i]!=null){
         if(this.goods[i].getName().indexOf(keyword)!=-1){
         count++;
             }
         }
}
go=new Goods[count];
        int f=0;
for(int i=0;i<this.goods.length;i++){
        if(goods[i]!=null){
if(this.goods[i].getName().indexOf(keyword)!=-1){
        go[f]=this.goods[i];
f++;
}
}
}
return go;
}
//得到全部信息
public Goods[] getGoods(){
return this.goods;    
}
}
public class JavaSix {

    /**
     * @param args
     */
    public static void main(String[] args) {
        // TODO Auto-generated method stub
        ShopCar s1=new ShopCar(5);
        s1.add(new EatFood("面包",12.1));
        s1.add(new EatFood("辣条",2.4));
        s1.add(new EatFood("饼干",22.3));
        s1.add(new WashGoods("洗发水",32.5));
        s1.add(new WashGoods("卫生纸",22.8));
        print(s1.search("饼干"));
        System.out.println("=============");
        print(s1.getGoods());
        }
        public static void print(Goods gs[]){
        double sum=0;
        for(int i=0;i<gs.length;i++){
        if(gs[i]!=null){
        //System.out.println(p[i]+",");
        System.out.println(gs[i].getName()+","+gs[i].getPrice());
        sum=sum+gs[i].getPrice();    
        }
        }
        System.out.println("总价格为:"+sum);
        }
        
    }

 

JAVA第七次作业

标签:

原文地址:http://www.cnblogs.com/simayige/p/5444487.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!