java中类集的应用,简单的一个图书超市的例子

7 篇文章 0 订阅

(1)

package com.jinlong.cn;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

 

public class BookShop {
 private String name;
 private Book book;
 private List getAllBooks;
 public BookShop()
 {}
 public BookShop(String name)
 {
  this();
  this.name=name;
  this.getAllBooks=new ArrayList();
 }
 
 public String getName() {
  return name;
 }
 public void setName(String name) {
  this.name = name;
 }
 public Book getBook() {
  return book;
 }
 public void setBook(Book book) {
  this.book = book;
 }
 public List getGetAllBooks() {
  return getAllBooks;
 }
 public void setGetAllBooks(List getAllBooks) {
  this.getAllBooks = getAllBooks;
 }
 public void append(Book book)
 {
  this.getAllBooks.add(book);
 }
 public void delete(Book book)
 {
  this.getAllBooks.remove(book);
 }
   public Book findByName(String name)
   {
   
    Iterator iterator=this.getAllBooks.iterator();
    Book book=null;
    while (iterator.hasNext()) {
  Book b = (Book) iterator.next();
  if (name.equals(b.getName())) {
   book=b;
   break;
  }
  
  
 }
 return book;
 
   }
   public List index(String keyWord)
   {
    List a=new ArrayList();
    Iterator ierIterator=this.getAllBooks.iterator();
    while (ierIterator.hasNext()) {
  Book b = (Book) ierIterator.next();
  if (b.getName().indexOf(keyWord)!=-1) {
  a.add(b);
   
  }
  
 }
    return a;
    }
  public List getAllBooks()
  {
   return this.getAllBooks;
  }
}
(2) 

package com.jinlong.cn;

public interface Book {
 public String getId();
 public String getName();
 public String getInfo();
 public float getPrice();

}
(3)

package com.jinlong.cn;

import java.util.Iterator;
import java.util.List;

public class ALLBookTest {
 public static void main(String[] args) {
  BookShop bookShop=new BookShop("图书大厦");
  Book b1=new ComputeBook("efdf", "malong", "info", 89.0f);
  Book b2=new ComputeBook("ffdf", "majinlong", "ingfg", 89.0f);
  Book b3=new ComputeBook("xfdf", "majgfgfinlong", "ingffgfo", 89.0f);
  Book b4=new ComputeBook("afdf", "majinlong", "ingfgffo", 89.0f);
  Book b5=new ComputeBook("jfdf", "nlong", "ingffo", 89.0f);
  Book b6=new ChildBook("dfdf", "ojhjg", "fjhfo", 04.0f);
  Book b7=new ChildBook("dfdf", "aonjkjg", "fjhjfo", 984.0f);
  Book b9=new ChildBook("Hellow owrdf", "ojhjng", "ffo", 84.0f);
  Book b0=new ChildBook("gf", "onjhjhg", "ffo", 84.0f);
  bookShop.append(b0);
  bookShop.append(b1);
  bookShop.append(b2);
  bookShop.append(b3);
  bookShop.append(b4);
  bookShop.append(b5);
  bookShop.append(b6);
  bookShop.append(b7);
  bookShop.append(b9);
  
  
  Book b=bookShop.findByName("nlong");
  System.out.println(b);
  //print(bookShop.getAllBooks());
//      Book book= bookShop.findByName("malong");
//      System.out.println(book);
  //print(bookShop.index("a"));
  
  //bookShop.delete(b9);
  //print(bookShop.getAllBooks());
 }
     public static void print(List a)
     {
      Iterator iterator=a.iterator();
      while (iterator.hasNext()) {
   Book b = (Book) iterator.next();
   System.out.println(b);
   
  }
     
     }
}
(4)

package com.jinlong.cn;

public class ChildBook implements Book {
 private String id;
 private String name;
 private String info;
 private float price;

 public ChildBook(String id, String name, String info, float price) {
  super();
  this.id = id;
  this.name = name;
  this.info = info;
  this.price = price;
 }

 public String getId() {
  // TODO Auto-generated method stub
  return this.id;
 }

 public String getName() {
  // TODO Auto-generated method stub
  return this.name;
 }

 public String getInfo() {
  // TODO Auto-generated method stub
  return this.info;
 }

 public float getPrice() {
  // TODO Auto-generated method stub
  return this.price;
 }
  public String toString()
  {
   return   "book id:  " +this.getId()+     "bookname:  "+this.getName()+
   "bookInfo:  "+     this.getInfo()+    "BookPrice: "+   this.getPrice();
  }


 
}

 

(5)
package com.jinlong.cn;

public class ComputeBook implements Book{
 private String id;
 private  String name;
 private String info;
 private float price;
 

 public ComputeBook(String id, String name, String info, float price) {
  super();
  this.id = id;
  this.name = name;
  this.info = info;
  this.price = price;
 }

 
 public String getId() {
  // TODO Auto-generated method stub
  return this.id;
 }

 


 public String getInfo() {
  // TODO Auto-generated method stub
  return this.info;
 }

 @Override
 public float getPrice() {
  // TODO Auto-generated method stub
  return this.price;
 }

 @Override
 public String toString() {
  // TODO Auto-generated method stub
    return    "computer id:" +this.getId()+       "computer info:"+this.getName()+       "computer: "
    +this.getInfo()+      "computerPrice:"+this.getPrice();
 }


 @Override
 public String getName() {
  // TODO Auto-generated method stub
  return name;
 }


 

}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值