圖書大廈
開發要求
如今要求模擬一個圖書大廈圖書管理的程序結構,能夠在圖書大廈實現某一類圖書的上架操做,下架操做,以及關鍵字模糊查詢的操做
注:只考慮類結構,圖書屬性只關注名字與價格
具體內容
分析:.........
範例:須要定義的是圖書標準
interface Book{ // 準備出圖書信息
public String getTitle(); // 獲得書的名字
public double getPrice();//獲得書的價錢
}
範例:定義圖書大廈,一個圖書大廈要保存有多本書的信息,因此圖書大廈應該使用鏈表java
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]); } } }
這樣的程序模型能夠在生活中不斷演變,例如:一個公園能夠有不少的樹,種樹和砍
一個停車場裏能夠停放轎車,卡車,電動車
總結
這樣的操做模型之中,對於鏈表只是使用
本程序是以接口爲主的編程操做,這種形式在開發中隨處可見
網絡