import java.util.Iterator;
import java.util.List;
interface Book {
public float getPrice();
public String getName();
public String getInfo();
}
//书店
class BookShop {
private String name;
//一个书店中有多种书 将书放在集合中
private List<Book> allBooks;
//无参构造 用于初始化参数
public BookShop() {
this.allBooks = new ArrayList<Book>();
}
public BookShop(String name) {
this();
this.name = name;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public List<Book> getAllBooks() {
return allBooks;
}
public void setAllBooks(List<Book> allBooks) {
this.allBooks = allBooks;
}
//添加书籍
public void append(Book book) {
this.allBooks.add(book);
}
//删除书籍
public void delete(Book book) {
this.allBooks.remove(book);
}
//根据书名查找书籍
public Book findByName(String name) {
Book book = null;
Iterator<Book> iter = this.allBooks.iterator();
while(iter.hasNext()) {
Book temp = iter.next();
if(name.equals(temp.getName())) {
book = temp;
break;
}
}
return book;
}
//检索书籍
public List<Book> index(String keyWord) {
List<Book> bList = new ArrayList<Book>();
Iterator<Book> iter = this.allBooks.iterator();
while(iter.hasNext()) {
Book temp = iter.next();
//如果遍历出的书名中没有包含keyWord的 则返回-1
if(temp.getName().indexOf(keyWord)!=-1) {
//将包含搜索关键字的书添加到集合中
bList.add(temp);
}
}
return bList;
}
}
//儿童书
class ChildBook implements Book{
private float price;
private String name;
private String info;
public ChildBook(String name,float price,String info) {
this.setName(name);
this.setPrice(price);
this.setInfo(info);
}
public float getPrice() {
return price;
}
public void setPrice(float price) {
this.price = price;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getInfo() {
return info;
}
public void setInfo(String info) {
this.info = info;
}
public String toString() {
return "书名:"+this.getName()+",简介:"+this.getInfo()+",价格:"+this.getPrice();
}
}
//电脑书
class ComputerBook implements Book{
private float price;
private String name;
private String info;
public ComputerBook(String name,float price,String info) {
this.setName(name);
this.setPrice(price);
this.setInfo(info);
}
public float getPrice() {
return price;
}
public void setPrice(float price) {
this.price = price;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getInfo() {
return info;
}
public void setInfo(String info) {
this.info = info;
}
public String toString() {
return "书名:"+this.getName()+",简介:"+this.getInfo()+",价格:"+this.getPrice();
}
}
public class ColDemo {
public static void main(String[] args) {
Book b1 = new ChildBook("海底三千里",50.0f,"科幻小说");
Book b2 = new ChildBook("老鼠捉猫",50.0f,"猫和老鼠的故事");
Book b3 = new ChildBook("会飞的鲨鱼",50.0f,"这一刻我飞上了天空");
Book b4 = new ComputerBook("Java咖啡馆",30.0f,"JAVA的咖啡哲学");
Book b5 = new ComputerBook("Oracle数据库编程",80.0f,"Oracle详解");
Book b6 = new ComputerBook("UML教程",40.0f,"UML基础教程");
Book b7 = new ComputerBook("Web开发详解",50.0f,"JavaWeb开发书籍");
BookShop bs = new BookShop("网上书店");
bs.append(b1);
bs.append(b2);
bs.append(b3);
bs.append(b4);
bs.append(b5);
bs.append(b6);
bs.append(b7);
//打印出所有书籍列表
print(bs.getAllBooks());
//打印含有关键字的书名列表
print(bs.index("J"));
Book book = bs.findByName("会飞的鲨鱼");
System.out.println(book);
}
//打印方法
public static void print(List all) {
Iterator iter = all.iterator();
while(iter.hasNext()) {
Book b = (Book)iter.next();
System.out.println(b);
}
}
}