ホームページ >Java >&#&チュートリアル >Java アプリケーションの詳細な紹介
優れたソフトウェアを実現するための 3 つの簡単なステップ:
1. ソフトウェアの顧客が何を望んでいるのかを特定します。
2. 基本的な OO 原則を使用してソフトウェアの柔軟性を高めます。
3. 保守可能で再利用可能なデザインの実現に努めます。
プロジェクト構造:
ギタークラス:
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 }
インベントリクラス:
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」は大文字です。
それでは、ギター検索ツールに最初の改良を加えて、すべての文字列比較を削除しましょう:
プロジェクト変更アーキテクチャ:
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", "Stratocaster", "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 に置き換えます。
以上がJava アプリケーションの詳細な紹介の詳細内容です。詳細については、PHP 中国語 Web サイトの他の関連記事を参照してください。