Java接口与类集

来源:互联网 发布:sql start with的意思 编辑:程序博客网 时间:2024/05/18 03:52
假设有以下一种情况:
    书店里可以放很多种的书
    儿童书
    电脑书 
要求实现以下一种功能:
    模拟书店,书店里可以存放多种书目,而且可以 进行添加操作、而且可以进行删除操作,还可以进行查询操作。
一个类集中可以加入多个对象,对于儿童书、电脑书
书店 -> List -> 书的接口 <- 各种书   
一、先写一个书的接口
interface Book{//书的价格、书的名字、书的信息public float getPrice();public String getName();public String getInfo();};
二、写类(儿童书,电脑书)实现书的接口,书店类
//儿童书类class ChildBook implements Book{private String name;private float price;private String info;public ChildBook(String name,float price,String info){this.setName(name);this.setPrice(price);this.setInfo(info);}public String getName(){return this.name;}public float getPrice(){return this.price;}public String getInfo(){return this.info;}public void setName(String name){this.name = name;}public void setPrice(float price){this.price = price;}public void setInfo(String info){this.info = info;}public String toString(){return "儿童书:书名:" + this.getName() + ",价格:" + this.getPrice() + ",简介:" + this.getInfo();}};//电脑书类class ComputerBook implements Book{private String name;private float price;private String info;public ComputerBook(String name,float price,String info){this.setName(name);this.setPrice(price);this.setInfo(info);}public String getName(){return this.name;}public float getPrice(){return this.price;}public String getInfo(){return this.info;}public void setName(String name){this.name = name;}public void setPrice(float price){this.price = price;}public void setInfo(String info){this.info = info;}public String toString(){return "电脑书:书名:" + this.getName() + ",价格:" + this.getPrice() + ",简介:" + this.getInfo();}};
//书店类class BookShop{private String name;//一个书店包含多种书private List allBooks;public BookShop(){this.allBooks = new ArrayList();}public BookShop(String name){this();this.setName(name);}//得到全部书public List getAllBooks(){return this.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 iter = this.allBooks.iterator();while(iter.hasNext()){//进行依次的比较Book temp = (Book)iter.next();if(name.equals(temp.getName())){//如果名字相等,则表示找到了book = temp;break;}}return book;}//书的检索,书的模糊查询public List index(String keyWord){List l = new ArrayList();Iterator iter = this.allBooks.iterator();while(iter.hasNext()){Book b = (Book)iter.next();if(b.getName().indexOf(keyWord) != -1){l.add(b);}}return l;}public void setName(String name){this.name = name;}public String getName(){return this.name;}};
三、
public class ColDemo02{public static void main(String args[]){Book b1 = new ChildBook("一千零一夜",10.0f,"一些传说故事");Book b2 = new ChildBook("喜羊羊",20.0f,"羊与狼的故事");Book b3 = new ChildBook("柯南",100.0f,"推理故事");Book b4 = new ComputerBook("JAVA",80.0f,"JAVA核心技术");Book b5 = new ComputerBook("C++",86.0f,"C++核心技术");Book b6 = new ComputerBook("Linux",88.0f,"服务器搭建");BookShop bs = new BookShop("Tsang网上书店");bs.append(b1);bs.append(b2);bs.append(b3);bs.append(b4);bs.append(b5);bs.append(b6);}public static void print(List all){Iterator iter = all.iterator();while(iter.hasNext()){Book b = (Book)iter.next();System.out.println(b);}}};
查找keyword有“A”的词条
print(bs.index("A"));

从书店中查找所有的书
print(bs.getAllBooks());

假设将C++这本书从书店中删除掉
Book b = bs.findByName("C++");bs.delete(b);


0 0
原创粉丝点击