偉大軟體的簡易三步驟:
1.確認你的軟體客戶要它做的事。
2.運用基本的OO原則來增加軟體的彈性。
3.努力實現可維護、可重複使用的設計。
專案架構:
Guitar類別:
1 package com.headfirst.guitar; 2 3 public class Guitar { 4 private String serialNumber, builder, model, type, backWood, topWood; 5 private double price; 6 7 public Guitar(String serialNumber, double price, String builder, 8 String model, String type, String backWood, String topWood){ 9 this.serialNumber = serialNumber;10 this.price = price;11 this.builder = builder;12 this.model = model;13 this.type = type;14 this.backWood = backWood;15 this.topWood = topWood;16 }17 18 public String getSerialNumber(){19 return serialNumber;20 }21 public double getPrice(){22 return price;23 }24 public void setPrice(float newPrice){25 this.price = newPrice;26 }27 public String getBuilder(){28 return builder;29 }30 public String getModel(){31 return model;32 }33 public String getType(){34 return type;35 }36 public String getBackWood(){37 return backWood;38 }39 public String getTopWood(){40 return topWood;41 }42 }
Inventory類別:
1 package com.headfirst.guitar; 2 3 import java.util.Iterator; 4 import java.util.LinkedList; 5 import java.util.List; 6 7 public class Inventory { 8 9 private List guitars;10 11 public Inventory(){12 guitars = new LinkedList();13 }14 15 public void addGuitar(String serialNumber, double price, String builder, String model, String type,16 String backWood, String topWood){17 Guitar guitar = new Guitar(serialNumber, price, builder, 18 model, type, backWood, topWood);19 guitars.add(guitar);20 }21 22 public Guitar getGuitar(String serialNumber){23 for(Iterator i = guitars.iterator(); i.hasNext();){24 Guitar guitar = (Guitar) i.next();25 if(guitar.getSerialNumber().equals(serialNumber)){26 return guitar;27 }28 }29 30 return null;31 }32 33 public Guitar search(Guitar searchGuitar){34 for(Iterator i = guitars.iterator(); i.hasNext();){35 Guitar guitar = (Guitar) i.next();36 String builder = searchGuitar.getBuilder();37 if((builder != null) && (!builder.equals("")) && 38 (!builder.equals(guitar.getBuilder())))39 continue;40 41 String model = searchGuitar.getModel();42 if((model != null) && (!model.equals("")) && 43 (!model.equals(guitar.getModel())))44 continue;45 46 String type = searchGuitar.getType();47 if((type != null) && (!type.equals("")) && 48 (!type.equals(guitar.getType())))49 continue;50 51 String backWood = searchGuitar.getBackWood();52 if((backWood != null) && (!backWood.equals("")) && 53 (!backWood.equals(guitar.getBackWood())))54 continue;55 56 String topWood = searchGuitar.getTopWood();57 if((topWood != null) && (!topWood.equals("")) && 58 (!topWood.equals(guitar.getTopWood())))59 continue;60 return guitar;61 }62 63 return null;64 }65 }
搜尋測試程式碼:
1 package com.headfirst.guitar; 2 3 public class FindGuitarTester { 4 public static void main(String[] args){ 5 Inventory inventory = new Inventory(); 6 initializeInventory(inventory); 7 8 Guitar whatErinLikes = new Guitar("", 0, "fender", "Stratocastor", "electric", "Alder", "Alder"); 9 10 Guitar guitar = inventory.search(whatErinLikes);11 if(guitar != null){12 System.out.println("Erin, you might like this " + 13 guitar.getBuilder() + " " + guitar.getModel() + 14 " " + guitar.getType() + " guitar:\n " + 15 guitar.getBackWood() + " back and sides,\n " +16 guitar.getTopWood() + " top.\nYou can have it for only $" + 17 guitar.getPrice() + "!");18 }else{19 System.out.println("Sorry, Erin, we have nothing for you.");20 }21 }22 23 private static void initializeInventory(Inventory inventory){24 inventory.addGuitar("V95693", 1499.95, "Fender", "Stratocastor", "electric", "Alder", "Alder");25 }26 }
執行結果:
為什麼,明明有,為什麼說沒有?
檢查了程式碼,可能搜尋中「fender」的f是小寫,而我們有的「Fender」是大寫。
好,我們對吉他搜尋工具進行第一個改善,去除掉所有String比較:
專案修改架構:
Builder.java:
1 package com.headfirst.guitar; 2 3 public enum Builder { 4 5 FENDER, MARTIN, GIBSON, COLLINGS, OLSON, RYAN, PRS, ANY; 6 7 public String toString(){ 8 switch(this){ 9 case FENDER:10 return "Fender";11 case MARTIN:12 return "Martin";13 case GIBSON:14 return "Gibson";15 case COLLINGS:16 return "Collings";17 case OLSON:18 return "Olson";19 case RYAN:20 return "Ryan";21 case PRS:22 return "Prs";23 case ANY:24 return "Any";25 default: 26 return "";27 }28 }29 }
Type.java:
1 package com.headfirst.guitar; 2 3 public enum Type { 4 ACOUSTIC, ELECTRIC; 5 6 public String toString(){ 7 switch(this){ 8 case ACOUSTIC: 9 return "acoustic";10 case ELECTRIC: 11 return "electric";12 default:13 return "";14 }15 }16 }###Wood.java:######
1 package com.headfirst.guitar; 2 3 public enum Wood { 4 INDIAN_ROSEWOOD, BRAZILIAN_ROSEWOOD, ALDER; 5 6 public String toString(){ 7 switch(this){ 8 case INDIAN_ROSEWOOD: 9 return "Indian Rosewood";10 case BRAZILIAN_ROSEWOOD:11 return "Brazilian Rosewood";12 case ALDER:13 return "Alder";14 default:15 return "";16 }17 }18 }###### #修改FindGuitarTester.java第8行程式碼:###### ######1### Guitar whatErinLikes = ###new### Guitar("", 0, "Builder.FENDER", " Stratocastor", "Type.ELECTRIC", "Wood.ALDER", "Wood.ALDER");### ######修改Inventory.java中search()方法:#######
1 public Guitar search(Guitar searchGuitar){ 2 for(Iterator i = guitars.iterator(); i.hasNext();){ 3 Guitar guitar = (Guitar) i.next(); 4 5 if(searchGuitar.getBuilder() != guitar.getBuilder()) 6 continue; 7 8 String model = searchGuitar.getModel().toLowerCase(); 9 if((model != null) && (!model.equals("")) && 10 (!model.equals(guitar.getModel().toLowerCase())))11 continue;12 13 if(searchGuitar.getType() != guitar.getType())14 continue;15 16 if(searchGuitar.getBackWood() != guitar.getBackWood())17 continue;18 19 if(searchGuitar.getTopWood() != guitar.getTopWood())20 continue;21 22 return guitar;23 }24 25 return null;26 }## ####現在我們擔心的是Model屬性,我們沒有把它做成enum,而且Model沒有辦法限制,所以做不出enum。 ######除此之外:我們修改的地方還有:###############各種變數的型別,用enum的全部替換掉。 ###
以上是java中應用程式的詳細介紹的詳細內容。更多資訊請關注PHP中文網其他相關文章!