菜鸡的Java笔记 图书馆

图书大厦
        开发要求
            现在要求模拟一个图书大厦图书管理的程序结构,可以在图书大厦实现某一类图书的上架操作,下架操作,以及关键字模糊查询的操作
            注:只考虑类结构,图书属性只关注名字与价格
            
        具体内容
            分析:.........
            范例:需要定义的是图书标准
                interface Book{ // 准备出图书信息
                    public String getTitle(); // 得到书的名字
                    public double getPrice();//得到书的价钱
                }
            范例:定义图书大厦,一个图书大厦要保存有多本书的信息,所以图书大厦应该使用链表

class BookShop{
    private Link books = new LinkImpl();// 表示的是所有的书
    public void add(Book book){//上架图书
        this.books.add(book);// 向链表中保存数据
    }
    public void delete(Book book){//下架图书
        this.books.remove(book);
    }
    public Link search(String keyWord){
        Link result = new LinkImpl();
        Object[] obj = this.books.toArray();// 将所有的数据转变为 Object 数组
        for(int x = 0;x < obj.length; x ++){
            Book book = (Book)obj[x];
            if(book.getTitle().contains(keyWord)){ // 有此关键字
                result.add(book);
            }
        }
        return result;
    }
}

                现在有了接口了,下面定义子类的时候只需要实现接口覆写方法即可。程序里面包含有 Link 接口的 remove() 方法
                这个方法如果要想正常执行,则需要覆写 equals() 方法
            范例:定义计算机类图书

class ComputerBook implements Book{
    private String title;
    private double price;
    public ComputerBook(String title,double price){
        this.title = title;
        this.price = price;
    }
    public boolean equals(Object obj){
        if(this == obj){
            return true;
        }
        if(obj == null){
            return false;
        }
        if(!(obj instanceof ComputerBook)){
            return false;
        }
        ComputerBook b = (ComputerBook)obj;
        if(this.title.equals(b.title) && this.price == b.price){
            return true;
        }
        return false;
    }
    public String getTitle(){
        return this.title;
    }
    public double getPrice(){
        return this.price;
    }
    public String toString(){
        return     "【计算机类图书】名称 = "+this.title
                +",价格 = "+this.price;
    }
}


            范例:定义数字类图书

class MathBook implements Book{
    private String title;
    private double price;
    public MathBook(String title,double price){
        this.title = title;
        this.price = price;
    }
    public boolean equals(Object obj){
        if(this == obj){
            return true;
        }
        if(obj == null){
            return false;
        }
        if(!(obj instanceof MathBook)){
            return false;
        }
        MathBook b = (MathBook)obj;
        if(this.title.equals(b.title) && this.price == b.price){
            return true;
        }
        return false;
    }
    public String getTitle(){
        return this.title;
    }
    public double getPrice(){
        return this.price;
    }
    public String toString(){
        return     "【数学类图书】名称 = "+this.title
                +",价格 = "+this.price;
    }
}

               
            范例:进行代码的测试

interface Link{
    
}
class LinkImpl implements Link{ // 外部的程序只关心此类
    
}
interface Book{// 准备出图书信息
    public String getTitle(); // 得到书的名字
    public double getPrice();//得到书的价钱
}
class BookShop{
    private Link books = new LinkImpl();// 表示的是所有的书
    public void add(Book book){//上架图书
        this.books.add(book);// 向链表中保存数据
    }
    public void delete(Book book){//下架图书
        this.books.remove(book);
    }
    public Link search(String keyWord){
        Link result = new LinkImpl();
        Object[] obj = this.books.toArray();// 将所有的数据转变为 Object 数组
        for(int x = 0;x < obj.length; x ++){
            Book book = (Book)obj[x];
            if(book.getTitle().contains(keyWord)){ // 有此关键字
                result.add(book);
            }
        }
        return result;
    }
}
class ComputerBook implements Book{
    private String title;
    private double price;
    public ComputerBook(String title,double price){
        this.title = title;
        this.price = price;
    }
    public boolean equals(Object obj){
        if(this == obj){
            return true;
        }
        if(obj == null){
            return false;
        }
        if(!(obj instanceof ComputerBook)){
            return false;
        }
        ComputerBook b = (ComputerBook)obj;
        if(this.title.equals(b.title) && this.price == b.price){
            return true;
        }
        return false;
    }
    public String getTitle(){
        return this.title;
    }
    public double getPrice(){
        return this.price;
    }
    public String toString(){
        return     "【计算机类图书】名称 = "+this.title
                +",价格 = "+this.price;
    }
}
class MathBook implements Book{
    private String title;
    private double price;
    public MathBook(String title,double price){
        this.title = title;
        this.price = price;
    }
    public boolean equals(Object obj){
        if(this == obj){
            return true;
        }
        if(obj == null){
            return false;
        }
        if(!(obj instanceof MathBook)){
            return false;
        }
        MathBook b = (MathBook)obj;
        if(this.title.equals(b.title) && this.price == b.price){
            return true;
        }
        return false;
    }
    public String getTitle(){
        return this.title;
    }
    public double getPrice(){
        return this.price;
    }
    public String toString(){
        return     "【数学类图书】名称 = "+this.title
                +",价格 = "+this.price;
    }
}
public class actualCombat{
    public static void main(String args[]){
        BookShop shop = new BookShop();
        shop.add(new ComputerBook("java开发",79.0));
        shop.add(new ComputerBook("java数据库编程",69.0));
        shop.add(new ComputerBook("java网络编程",76.0));
        shop.add(new ComputerBook("数学与java",59.0));
        shop.add(new ComputerBook("java与线性代数",49.0));
        shop.add(new ComputerBook("网络数学",29.0));
        shop.delete(new ComputerBook("java数据库编程",69.0));  // 下架操作
        Link tepm = shop.search("java"); // 模糊查询
        Object obj[] = tepm.toArray(); // 变为对象数组
        for(int x = 0;x < obj.length;x ++){
            System.out.println(obj[x]);
        }
    }
}

               
                这样的程序模型可以在生活中不断演变,例如:一个公园可以有很多的树,种树和砍
                    一个停车场里可以停放轿车,卡车,电动车

    总结
        这样的操作模型之中,对于链表只是使用
        本程序是以接口为主的编程操作,这种形式在开发中随处可见


原文地址:https://www.cnblogs.com/mysterious-killer/p/10140765.html