第7次Java作业+LSYang

 

  1 abstract class Goods {
  2     private String name;
  3     private float price;
  4     private int count;
  5     public Goods(String name,float price ,int count){
  6       this.setName(name);
  7       this.setPrice(price);
  8       this.setCount(count);
  9     }
 10     public void setName(String name){
 11       this.name = name;
 12     }
 13     public void setPrice(float price){
 14       this.price = price;
 15     }
 16     public void setCount(int count){
 17       this.count = count;
 18     }
 19     public String getName(){
 20       return this.name;
 21     }
 22     public float getPrice(){
 23       return this.price;
 24     }
 25     public int getCount(){
 26       return this.count;
 27     }
 28     public abstract String getInfo();
 29     public String getInfo1(){
 30       return "品名:"+this.getName()+
 31           ",单价"+this.getPrice()+",数量:"+this.getCount();
 32     }
 33 }
 34 class Books extends Goods{
 35     private String author;
 36     private String publish;
 37     public Books(String name,float price,int count,String author,String publish){
 38      super(name,price,count);
 39      this.setAuthor(author);
 40      this.setPublish(publish);
 41     }
 42     public void setAuthor(String author){
 43        this.author = author;
 44     }
 45     public String getAuthor(){
 46        return  this.author;
 47     }
 48     public void setPublish(String publish){
 49        this.publish=publish;
 50     }
 51     public String getPublish(){
 52        return this.publish;
 53     }
 54     public String getInfo(){
 55       return "书名:"+this.getName()+",单价:"+this.getPrice()+",作者:"+this.getAuthor()+
 56           ",出版社:"+this.getPublish()+",数量:"+this.getCount()+
 57           ",总价:"+this.getPrice()*this.getCount();
 58     }
 59     public String getInfo1(){
 60       return "品名:"+this.getName()+
 61           ",单价"+this.getPrice()+",数量:"+this.getCount();
 62     }
 63 }
 64 class Cloths extends Goods{
 65     private String title;
 66     private String style;
 67     public Cloths(String name,float price ,int count,String title,String style){
 68        super(name,price,count);
 69        this.setTitle(title);
 70        this.setStyle(style);
 71     }
 72     public void setTitle(String title){
 73         this.title=title;
 74     }
 75     public String getTitle(){
 76         return this.title;
 77     }
 78     public void setStyle(String style){
 79         this.style =style;
 80     }
 81     public String getStyle(){
 82         return this.style;
 83     }
 84     public String getInfo(){
 85       return "品名:"+this.getName()+",单价:"+this.getPrice()+
 86           ",品牌:"+this.getTitle()+",款式:"+this.getStyle()+
 87           ",数量:"+this.getCount()+",总价:"+this.getPrice()*this.getCount();
 88     }
 89     public String getInfo1(){
 90        return "品名:"+this.getName()+
 91           ",单价"+this.getPrice()+",数量:"+this.getCount();
 92     }
 93 }
 94 class ShopCar{
 95     private Goods[] goods;
 96     private int foot;
 97     public ShopCar(int len){
 98       if(len>0){
 99         this.goods = new Goods[len];
100       }else{
101         this.goods = new Goods[1];
102       }
103     }
104     public boolean add(Goods goods){
105        if(this.foot<this.goods.length){
106          this.goods[this.foot]=goods;
107          this.foot++;
108          return true;
109        }else{
110          return false;
111        }
112     }
113     public Goods[] getContent(){
114         return this.goods;
115     }
116     public Goods[] search(String keyWord){
117        Goods g[] = null;
118        int count =0;
119        for (int i=0;i<this.goods.length ;i++ ){
120            if(this.goods[i]!=null){
121                if(this.goods[i].getName().indexOf(keyWord)!=-1){
122                    count++;
123            }
124        }
125     }
126     g = new Goods[count];
127     int f =0;
128       for (int i=0;i<this.goods.length ;i++ ){
129            if(this.goods[i]!=null){
130                if(this.goods[i].getName().indexOf(keyWord)!=-1){
131                    g[f]=this.goods[i];
132                    f++;
133            }
134        }
135     }  
136      return g;
137  }
138 }
139 public class Java6156{
140     public static void main(String args[]){
141       ShopCar sc = new ShopCar(6);
142       sc.add(new Books("解忧杂货店    ",39.8f,1,"东野圭吾 ","三联出版社"));
143       sc.add(new Books("四签名            ",79.9f,5,"柯南道尔  ","人民出版社"));
144       sc.add(new Books("yolo    ",26.9f,3,"Jane","三联出版社"));
145       sc.add(new Cloths("T恤   ",109.9f,2,"李宁","男式,M"));
146       sc.add(new Cloths("牛仔裤",169.9f,2,"森马","女士,L"));
147       sc.add(new Cloths("衬衫  ",129.9f,1,"杰克琼斯","男式,L"));
148       System.out.println("==========已买到的商品清单============");
149       print1(sc.getContent());
150       System.out.println("==========查询商品详细信息=============");
151       try
152       {
153         print(sc.search(args[0]));
154       }
155       catch (Exception e){
156           System.out.println("未输入要查询商品或输入错误,"+
157            "格式为:\njava Test 商品名(或商品名中的任意字符)");
158       }
159      }
160       public static void print1(Goods goods[]){
161         for (int i=0;i<goods.length ;i++ ){
162             System.out.println(goods[i].getInfo1());
163         }
164       }
165       public static void print(Goods goods[]){
166         for (int i=0;i<goods.length ;i++ ){
167             System.out.println(goods[i].getInfo());
168         }
169       }
170 }

程序运行结果

转载于:https://www.cnblogs.com/liusiyang1126/p/5421211.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值