第五章.良好之计划性。第五章.良好的计划。

聚(aggregation):聚合是关系的同样种植独特形式,表示一致码东西是出于其他一样起事物(部分地)组成。

会师(aggregation):聚合是事关的同等种植异常形式,表示同样码东西是由其余一样起事物(部分地)组成。

于Java中的称呼:抽象类(abstract
class),关系(relationship),继承(inheritance),聚合(aggregation)

每当Java中的称为:抽象类(abstract
class),关系(relationship),继承(inheritance),聚合(aggregation)

当UML中的名叫:抽象类(abstract
class),关联(association),泛化(generalization),聚合(aggregation)

于UML中之叫:抽象类(abstract
class),关联(association),泛化(generalization),聚合(aggregation)

OO原则:

OO原则:

1.用转移的物封装起来

1.将变之东西封装起来

2.针对接口编码,而无是对准贯彻

2.对接口编码,而未是本着促成

3.应用程序中的各国一个类就生一个改观之说辞

3.应用程序中的各个一个像样才出一个变更之理

灵活性(flexible),复原力(resilient),耦合度(coupling),内聚力(cohesion)

灵活性(flexible),复原力(resilient),耦合度(coupling),内聚力(cohesion)

大部底好规划都是通过分析好设计要来的。

大多数底好计划还是透过分析颇设计而来之。

不用怕犯错和转。

甭害怕犯错和反。

 

 

尚记吉他店老板为(可以关押率先回和次回)?他因此我们举行的瑞他摸索工具,吉他杀货。但是,现在外出了新的需:

还记吉他店老板娘啊(可以关押率先节跟亚节)?他于是我们做的红他找工具,吉他万分卖。但是,现在他有矣初的需要:

店长说自非思量单独请吉他,我怀念搭又多之乐器项目:比如像曼陀林(mandolin)等

店长说自己不思量就买吉他,我思念搭又多的乐器项目:比如像曼陀林(mandolin)等

扭动看咱们的代码,

扭曲看咱们的代码,

花色架构:

色架构:

lovebet体育官网 1

lovebet体育官网 2

Builder.java:

Builder.java:

lovebet体育官网 3lovebet体育官网 4

lovebet体育官网 5lovebet体育官网 6

 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 }
 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 }

View Code

View Code

Type.java:

Type.java:

lovebet体育官网 7lovebet体育官网 8

lovebet体育官网 9lovebet体育官网 10

 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 }
 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 }

View Code

View Code

Wood.java:

Wood.java:

lovebet体育官网 11lovebet体育官网 12

lovebet体育官网 13lovebet体育官网 14

 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 }
 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 }

View Code

View Code

Guitar.java:

Guitar.java:

lovebet体育官网 15lovebet体育官网 16

lovebet体育官网 17lovebet体育官网 18

 1 package com.headfirst.guitar;
 2 
 3 public class Guitar {
 4     private String serialNumber;
 5     private double price;
 6     private GuitarSpec guitarSpec;
 7     
 8     public Guitar(String serialNumber, double price, GuitarSpec guitarSpec){
 9         this.serialNumber = serialNumber;
10         this.price = price;
11         this.guitarSpec = guitarSpec;
12     }
13     
14     public String getSerialNumber(){
15         return serialNumber;
16     }
17     public double getPrice(){
18         return price;
19     }
20     public void setPrice(float newPrice){
21         this.price = newPrice;
22     }
23     public GuitarSpec getGuitarSpec(){
24         return guitarSpec;
25     }
26 }
 1 package com.headfirst.guitar;
 2 
 3 public class Guitar {
 4     private String serialNumber;
 5     private double price;
 6     private GuitarSpec guitarSpec;
 7     
 8     public Guitar(String serialNumber, double price, GuitarSpec guitarSpec){
 9         this.serialNumber = serialNumber;
10         this.price = price;
11         this.guitarSpec = guitarSpec;
12     }
13     
14     public String getSerialNumber(){
15         return serialNumber;
16     }
17     public double getPrice(){
18         return price;
19     }
20     public void setPrice(float newPrice){
21         this.price = newPrice;
22     }
23     public GuitarSpec getGuitarSpec(){
24         return guitarSpec;
25     }
26 }

View Code

View Code

GuitarSpec.java:

GuitarSpec.java:

lovebet体育官网 19lovebet体育官网 20

lovebet体育官网 21lovebet体育官网 22

 1 package com.headfirst.guitar;
 2 
 3 public class GuitarSpec {
 4     Builder builder;
 5     String model;
 6     Type type;
 7     Wood backWood;
 8     Wood topWood;
 9     int numStrings;
10     
11     
12     public GuitarSpec(Builder builder, String model, Type type, Wood backWood, Wood topWood){
13         this.builder = builder;
14         this.model = model;
15         this.type = type;
16         this.backWood = backWood;
17         this.topWood = topWood;
18         this.numStrings = numStrings;
19     }
20     
21     public Builder getBuilder(){
22         return this.builder;
23     }
24     
25     public String getModel(){
26         return this.model;
27     }
28     
29     public Type getType(){
30         return this.type;
31     }
32     
33     public Wood getBackWood(){
34         return this.backWood;
35     }
36     
37     public Wood getTopWood(){
38         return this.topWood;
39     }
40     
41     public int getNumStrings(){
42         return this.numStrings;
43     }
44     
45     public boolean matches(GuitarSpec otherSpec){
46         if(builder != otherSpec.builder)
47             return false;
48         if((model != null) && (!model.equals("")) &&
49                 (!model.equalsIgnoreCase(otherSpec.model)))
50             return false;
51         if(type != otherSpec.type)
52             return false;
53         if(numStrings != otherSpec.numStrings)
54             return false;
55         if(backWood != otherSpec.backWood)
56             return false;
57         if(topWood != otherSpec.topWood)
58             return false;
59         
60         return true;
61         }
62 }
 1 package com.headfirst.guitar;
 2 
 3 public class GuitarSpec {
 4     Builder builder;
 5     String model;
 6     Type type;
 7     Wood backWood;
 8     Wood topWood;
 9     int numStrings;
10     
11     
12     public GuitarSpec(Builder builder, String model, Type type, Wood backWood, Wood topWood){
13         this.builder = builder;
14         this.model = model;
15         this.type = type;
16         this.backWood = backWood;
17         this.topWood = topWood;
18         this.numStrings = numStrings;
19     }
20     
21     public Builder getBuilder(){
22         return this.builder;
23     }
24     
25     public String getModel(){
26         return this.model;
27     }
28     
29     public Type getType(){
30         return this.type;
31     }
32     
33     public Wood getBackWood(){
34         return this.backWood;
35     }
36     
37     public Wood getTopWood(){
38         return this.topWood;
39     }
40     
41     public int getNumStrings(){
42         return this.numStrings;
43     }
44     
45     public boolean matches(GuitarSpec otherSpec){
46         if(builder != otherSpec.builder)
47             return false;
48         if((model != null) && (!model.equals("")) &&
49                 (!model.equalsIgnoreCase(otherSpec.model)))
50             return false;
51         if(type != otherSpec.type)
52             return false;
53         if(numStrings != otherSpec.numStrings)
54             return false;
55         if(backWood != otherSpec.backWood)
56             return false;
57         if(topWood != otherSpec.topWood)
58             return false;
59         
60         return true;
61         }
62 }

View Code

View Code

Inventory.java:

Inventory.java:

lovebet体育官网 23lovebet体育官网 24

lovebet体育官网 25lovebet体育官网 26

 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, GuitarSpec guitarSpec){
16         Guitar guitar = new Guitar(serialNumber, price, guitarSpec);
17         guitars.add(guitar);
18     }
19     
20     public Guitar getGuitar(String serialNumber){
21         for(Iterator i = guitars.iterator(); i.hasNext();){
22             Guitar guitar = (Guitar) i.next();
23             if(guitar.getSerialNumber().equals(serialNumber)){
24                 return guitar;
25             }
26         }
27         
28         return null;
29     }
30     
31     public List search(GuitarSpec searchGuitar){
32         List matchingGuitars = new LinkedList();
33         for(Iterator i = guitars.iterator(); i.hasNext();){
34             Guitar guitar = (Guitar) i.next();
35             if(guitar.getGuitarSpec().matches(searchGuitar))
36                 matchingGuitars.add(guitar);
37         }
38         
39         return matchingGuitars;
40     }
41 }
 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, GuitarSpec guitarSpec){
16         Guitar guitar = new Guitar(serialNumber, price, guitarSpec);
17         guitars.add(guitar);
18     }
19     
20     public Guitar getGuitar(String serialNumber){
21         for(Iterator i = guitars.iterator(); i.hasNext();){
22             Guitar guitar = (Guitar) i.next();
23             if(guitar.getSerialNumber().equals(serialNumber)){
24                 return guitar;
25             }
26         }
27         
28         return null;
29     }
30     
31     public List search(GuitarSpec searchGuitar){
32         List matchingGuitars = new LinkedList();
33         for(Iterator i = guitars.iterator(); i.hasNext();){
34             Guitar guitar = (Guitar) i.next();
35             if(guitar.getGuitarSpec().matches(searchGuitar))
36                 matchingGuitars.add(guitar);
37         }
38         
39         return matchingGuitars;
40     }
41 }

View Code

View Code

FindGuitarTester.java:

FindGuitarTester.java:

lovebet体育官网 27lovebet体育官网 28

lovebet体育官网 29lovebet体育官网 30

 1 package com.headfirst.guitar;
 2 
 3 import java.util.Iterator;
 4 import java.util.List;
 5 
 6 public class FindGuitarTester {
 7     public static void main(String[] args){
 8         Inventory inventory = new Inventory();
 9         initializeInventory(inventory);
10         
11         GuitarSpec whatErinLikes = new GuitarSpec(Builder.FENDER, "Stratocastor", Type.ELECTRIC, Wood.ALDER, Wood.ALDER);
12         
13         List matchingGuitars = inventory.search(whatErinLikes);
14         if(!matchingGuitars.isEmpty()){
15             System.out.println("Erin, you might like these guitars: "); 
16             for(Iterator i = matchingGuitars.iterator(); i.hasNext();){
17                 Guitar guitar = (Guitar) i.next();
18                 GuitarSpec guitarSpec = guitar.getGuitarSpec();
19                 System.out.println("We have a " +
20                 guitarSpec.getBuilder() + " " + guitarSpec.getModel() + " " + 
21                 guitarSpec.getType() + " guitar:\n " + 
22                 guitarSpec.getBackWood() + " back and sides,\n " +
23                 guitarSpec.getTopWood() + " top.\nYou can have it for only $" + 
24                 guitar.getPrice() + "!");
25             }
26         }else{
27             System.out.println("Sorry, Erin, we have nothing for you.");
28         }
29     }
30     
31     private static void initializeInventory(Inventory inventory){
32         GuitarSpec guitarSpec1 = new GuitarSpec(Builder.FENDER, "Stratocastor", Type.ELECTRIC, Wood.ALDER, Wood.ALDER);
33         inventory.addGuitar("V95693", 1499.95, guitarSpec1);
34         inventory.addGuitar("B1234", 1600.35, guitarSpec1);
35     }
36 }
 1 package com.headfirst.guitar;
 2 
 3 import java.util.Iterator;
 4 import java.util.List;
 5 
 6 public class FindGuitarTester {
 7     public static void main(String[] args){
 8         Inventory inventory = new Inventory();
 9         initializeInventory(inventory);
10         
11         GuitarSpec whatErinLikes = new GuitarSpec(Builder.FENDER, "Stratocastor", Type.ELECTRIC, Wood.ALDER, Wood.ALDER);
12         
13         List matchingGuitars = inventory.search(whatErinLikes);
14         if(!matchingGuitars.isEmpty()){
15             System.out.println("Erin, you might like these guitars: "); 
16             for(Iterator i = matchingGuitars.iterator(); i.hasNext();){
17                 Guitar guitar = (Guitar) i.next();
18                 GuitarSpec guitarSpec = guitar.getGuitarSpec();
19                 System.out.println("We have a " +
20                 guitarSpec.getBuilder() + " " + guitarSpec.getModel() + " " + 
21                 guitarSpec.getType() + " guitar:\n " + 
22                 guitarSpec.getBackWood() + " back and sides,\n " +
23                 guitarSpec.getTopWood() + " top.\nYou can have it for only $" + 
24                 guitar.getPrice() + "!");
25             }
26         }else{
27             System.out.println("Sorry, Erin, we have nothing for you.");
28         }
29     }
30     
31     private static void initializeInventory(Inventory inventory){
32         GuitarSpec guitarSpec1 = new GuitarSpec(Builder.FENDER, "Stratocastor", Type.ELECTRIC, Wood.ALDER, Wood.ALDER);
33         inventory.addGuitar("V95693", 1499.95, guitarSpec1);
34         inventory.addGuitar("B1234", 1600.35, guitarSpec1);
35     }
36 }

View Code

View Code

点代码,如果起啊疑难,可以指出。

上面代码,如果来啊问题,可以指出。

据悉吉他店长,不,应该叫乐器店长,的指令。我们来扩充及改动我们地方的代码,看起什么问题?

冲吉他店长,不,应该叫乐器店长,的指示。我们来扩张和修改我们地方的代码,看有啊问题?

俺们无非出一个Guitar类,想使Mandolin类,再创一个咔嚓。想到两者似乎不怎么相同之地方,可以我们抽象出Instrument类怎么样,让新增的乐器都延续它们。

咱俩才发生一个Guitar类,想如果Mandolin类,再创一个吧。想到两者似乎不怎么相同的地方,可以我们抽象出Instrument类怎么样,让新增的乐器都蝉联其。

最好强了!GuitarSpec类为一如既往,我们抽象出InstrumentSpec类,让新增的乐器属性都连续自它。

顶巧了!GuitarSpec类为一样,我们抽象出InstrumentSpec类,让新增的乐器属性都累自它。

lovebet体育官网 31

lovebet体育官网 32

及时是咱们构建出底初的UML类图。

当时是我们构建起之初的UML类图。

依据达图写代码:

基于上图写代码:

新建项目:

新建项目:

 lovebet体育官网 33

 lovebet体育官网 34

乃可像自己同新建一个种类,也得以于原项目基础及反。

公可以像我一样新建一个门类,也堪在原来项目基础及转移。

Builder.java:

Builder.java:

lovebet体育官网 35lovebet体育官网 36

lovebet体育官网 37lovebet体育官网 38

 1 package com.headfirst.instrument;
 2 
 3 public enum Builder {
 4 FENDER, MARTIN, GIBSON, COLLINGS, OLSON, RYAN, PRS, ANY;
 5     
 6     public String toString(){
 7         switch(this){
 8         case FENDER:
 9             return "Fender";
10         case MARTIN:
11             return "Martin";
12         case GIBSON:
13             return "Gibson";
14         case COLLINGS:
15             return "Collings";
16         case OLSON:
17             return "Olson";
18         case RYAN:
19             return "Ryan";
20         case PRS:
21             return "Prs";
22         case ANY:
23             return "Any";
24         default: 
25             return "";
26         }
27     }
28 }
 1 package com.headfirst.instrument;
 2 
 3 public enum Builder {
 4 FENDER, MARTIN, GIBSON, COLLINGS, OLSON, RYAN, PRS, ANY;
 5     
 6     public String toString(){
 7         switch(this){
 8         case FENDER:
 9             return "Fender";
10         case MARTIN:
11             return "Martin";
12         case GIBSON:
13             return "Gibson";
14         case COLLINGS:
15             return "Collings";
16         case OLSON:
17             return "Olson";
18         case RYAN:
19             return "Ryan";
20         case PRS:
21             return "Prs";
22         case ANY:
23             return "Any";
24         default: 
25             return "";
26         }
27     }
28 }

View Code

View Code

Style.java:

Style.java:

lovebet体育官网 39lovebet体育官网 40

lovebet体育官网 41lovebet体育官网 42

 1 package com.headfirst.instrument;
 2 
 3 public enum Style {
 4     A, F;
 5     
 6     public String toString(){
 7         switch(this){
 8         case A:
 9             return "a";
10         case F:
11             return "F";
12         default:
13             return "kong";
14         }
15     }
16 }
 1 package com.headfirst.instrument;
 2 
 3 public enum Style {
 4     A, F;
 5     
 6     public String toString(){
 7         switch(this){
 8         case A:
 9             return "a";
10         case F:
11             return "F";
12         default:
13             return "kong";
14         }
15     }
16 }

View Code

View Code

Type.java:

Type.java:

lovebet体育官网 43lovebet体育官网 44

lovebet体育官网 45lovebet体育官网 46

 1 package com.headfirst.instrument;
 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 }
 1 package com.headfirst.instrument;
 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 }

View Code

View Code

Wood.java:

Wood.java:

lovebet体育官网 47lovebet体育官网 48

lovebet体育官网 49lovebet体育官网 50

 1 package com.headfirst.instrument;
 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 }
 1 package com.headfirst.instrument;
 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 }

View Code

View Code

Instrument.java:

Instrument.java:

lovebet体育官网 51lovebet体育官网 52

lovebet体育官网 53lovebet体育官网 54

 1 package com.headfirst.instrument;
 2 
 3 public abstract class Instrument {
 4     
 5     private String serialNumber;
 6     private double price;
 7     private InstrumentSpec spec;
 8     
 9     public Instrument(String serialNumber, double price, InstrumentSpec spec){
10         this.serialNumber = serialNumber;
11         this.price = price;
12         this.spec = spec;
13     }
14     
15     public String getSerialNumber(){
16         return serialNumber;
17     }
18     public double getPrice(){
19         return price;
20     }
21     public void setPrice(){
22         this.price = price;
23     }
24     
25     public InstrumentSpec getSpec(){
26         return spec;
27     }
28 }
 1 package com.headfirst.instrument;
 2 
 3 public abstract class Instrument {
 4     
 5     private String serialNumber;
 6     private double price;
 7     private InstrumentSpec spec;
 8     
 9     public Instrument(String serialNumber, double price, InstrumentSpec spec){
10         this.serialNumber = serialNumber;
11         this.price = price;
12         this.spec = spec;
13     }
14     
15     public String getSerialNumber(){
16         return serialNumber;
17     }
18     public double getPrice(){
19         return price;
20     }
21     public void setPrice(){
22         this.price = price;
23     }
24     
25     public InstrumentSpec getSpec(){
26         return spec;
27     }
28 }

View Code

View Code

InstrumentSpec.java:

InstrumentSpec.java:

lovebet体育官网 55lovebet体育官网 56

lovebet体育官网 57lovebet体育官网 58

 1 package com.headfirst.instrument;
 2 
 3 
 4 public abstract class InstrumentSpec {
 5     
 6     private Builder builder;
 7     private String model;
 8     private Type type;
 9     private Wood backWood;
10     private Wood topWood;
11     
12     public InstrumentSpec(Builder builder, String model, Type type, Wood backWood, Wood topWood){
13         this.builder = builder;
14         this.model = model;
15         this.type = type;
16         this.backWood = backWood;
17         this.topWood = topWood;
18     }
19     
20     public Builder getBuilder(){
21         return this.builder;
22     }
23     
24     public String getModel(){
25         return this.model;
26     }
27     
28     public Type getType(){
29         return this.type;
30     }
31     
32     public Wood getBackWood(){
33         return this.backWood;
34     }
35     
36     public Wood getTopWood(){
37         return this.topWood;
38     }
39     
40     public boolean matches(InstrumentSpec otherSpec){
41         if(builder != otherSpec.builder)
42             return false;
43         if((model != null) && (!model.equals("")) && (!model.equalsIgnoreCase(otherSpec.model)))
44             return false;
45         if(type != otherSpec.type)
46             return false;
47         if(backWood != otherSpec.backWood)
48             return false;
49         if(topWood != otherSpec.topWood)
50             return false;
51         
52         return true;
53     }
54 }
 1 package com.headfirst.instrument;
 2 
 3 
 4 public abstract class InstrumentSpec {
 5     
 6     private Builder builder;
 7     private String model;
 8     private Type type;
 9     private Wood backWood;
10     private Wood topWood;
11     
12     public InstrumentSpec(Builder builder, String model, Type type, Wood backWood, Wood topWood){
13         this.builder = builder;
14         this.model = model;
15         this.type = type;
16         this.backWood = backWood;
17         this.topWood = topWood;
18     }
19     
20     public Builder getBuilder(){
21         return this.builder;
22     }
23     
24     public String getModel(){
25         return this.model;
26     }
27     
28     public Type getType(){
29         return this.type;
30     }
31     
32     public Wood getBackWood(){
33         return this.backWood;
34     }
35     
36     public Wood getTopWood(){
37         return this.topWood;
38     }
39     
40     public boolean matches(InstrumentSpec otherSpec){
41         if(builder != otherSpec.builder)
42             return false;
43         if((model != null) && (!model.equals("")) && (!model.equalsIgnoreCase(otherSpec.model)))
44             return false;
45         if(type != otherSpec.type)
46             return false;
47         if(backWood != otherSpec.backWood)
48             return false;
49         if(topWood != otherSpec.topWood)
50             return false;
51         
52         return true;
53     }
54 }

View Code

View Code

Guitar.java:

Guitar.java:

lovebet体育官网 59lovebet体育官网 60

lovebet体育官网 61lovebet体育官网 62

1 package com.headfirst.instrument;
2 
3 public class Guitar extends Instrument {
4     
5     public Guitar(String serialNumber, double price, GuitarSpec spec){
6         super(serialNumber, price, spec);    
7     }
8 }
1 package com.headfirst.instrument;
2 
3 public class Guitar extends Instrument {
4     
5     public Guitar(String serialNumber, double price, GuitarSpec spec){
6         super(serialNumber, price, spec);    
7     }
8 }

View Code

View Code

GuitarSpec.java:

GuitarSpec.java:

lovebet体育官网 63lovebet体育官网 64

lovebet体育官网 65lovebet体育官网 66

 1 package com.headfirst.instrument;
 2 
 3 public class GuitarSpec extends InstrumentSpec {
 4     private int numStrings;//Guitar自身属性
 5     
 6     public GuitarSpec(Builder builder, String model, Type type, Wood backWood, Wood topWood, int numStrings){
 7         super(builder, model, type, backWood, topWood);
 8         
 9         this.numStrings = numStrings;
10     }
11     
12     public int getNumStrings(){
13         return numStrings;
14     }
15     
16     //Override the superclass matches()
17     public boolean matches(InstrumentSpec otherSpec){
18         if(!super.matches(otherSpec)){
19             return false;    
20         }
21         if(!(otherSpec instanceof GuitarSpec))
22             return false;
23         GuitarSpec spec = (GuitarSpec) otherSpec;
24         if( numStrings != spec.numStrings)
25             return false;
26         
27         return true;
28     }
29 }
 1 package com.headfirst.instrument;
 2 
 3 public class GuitarSpec extends InstrumentSpec {
 4     private int numStrings;//Guitar自身属性
 5     
 6     public GuitarSpec(Builder builder, String model, Type type, Wood backWood, Wood topWood, int numStrings){
 7         super(builder, model, type, backWood, topWood);
 8         
 9         this.numStrings = numStrings;
10     }
11     
12     public int getNumStrings(){
13         return numStrings;
14     }
15     
16     //Override the superclass matches()
17     public boolean matches(InstrumentSpec otherSpec){
18         if(!super.matches(otherSpec)){
19             return false;    
20         }
21         if(!(otherSpec instanceof GuitarSpec))
22             return false;
23         GuitarSpec spec = (GuitarSpec) otherSpec;
24         if( numStrings != spec.numStrings)
25             return false;
26         
27         return true;
28     }
29 }

View Code

View Code

Mandolin.java:

Mandolin.java:

lovebet体育官网 67lovebet体育官网 68

lovebet体育官网 69lovebet体育官网 70

1 package com.headfirst.instrument;
2 
3 public class Mandolin extends Instrument {
4     
5     public Mandolin(String serialNumber, double price, MandolinSpec spec){
6         super(serialNumber, price, spec);
7     }
8 }
1 package com.headfirst.instrument;
2 
3 public class Mandolin extends Instrument {
4     
5     public Mandolin(String serialNumber, double price, MandolinSpec spec){
6         super(serialNumber, price, spec);
7     }
8 }

View Code

View Code

MandolinSpec.java:

MandolinSpec.java:

lovebet体育官网 71lovebet体育官网 72

lovebet体育官网 73lovebet体育官网 74

 1 package com.headfirst.instrument;
 2 
 3 public class MandolinSpec extends InstrumentSpec {
 4     private Style style;
 5     
 6     public MandolinSpec(Builder builder, String model, Type type, Wood backWood, Wood topWood, Style style){
 7         super(builder, model, type, backWood, topWood);
 8         
 9         this.style = style;
10     }
11     
12     public Style getStyle(){
13         return style;
14     }
15     //Override the superclass matches()
16     public boolean matches(InstrumentSpec otherSpec){
17         if(!(super.matches(otherSpec)))
18             return false;
19         if(!(otherSpec instanceof MandolinSpec))
20             return false;
21         MandolinSpec spec = (MandolinSpec) otherSpec;
22         if(!style.equals(spec.style))
23             return false;
24         
25         return true;
26     }
27 }
 1 package com.headfirst.instrument;
 2 
 3 public class MandolinSpec extends InstrumentSpec {
 4     private Style style;
 5     
 6     public MandolinSpec(Builder builder, String model, Type type, Wood backWood, Wood topWood, Style style){
 7         super(builder, model, type, backWood, topWood);
 8         
 9         this.style = style;
10     }
11     
12     public Style getStyle(){
13         return style;
14     }
15     //Override the superclass matches()
16     public boolean matches(InstrumentSpec otherSpec){
17         if(!(super.matches(otherSpec)))
18             return false;
19         if(!(otherSpec instanceof MandolinSpec))
20             return false;
21         MandolinSpec spec = (MandolinSpec) otherSpec;
22         if(!style.equals(spec.style))
23             return false;
24         
25         return true;
26     }
27 }

View Code

View Code

Inventory.java:

Inventory.java:

lovebet体育官网 75lovebet体育官网 76

lovebet体育官网 77lovebet体育官网 78

 1 package com.headfirst.instrument;
 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 inventory;
10     
11     public Inventory(){
12         inventory = new LinkedList();
13     }
14     
15     public void addInstrument(String serialNumber, double price, InstrumentSpec spec){
16         Instrument instrument = null;
17         if(spec instanceof GuitarSpec){
18             instrument = new Guitar(serialNumber, price, (GuitarSpec) spec);
19         }else if(spec instanceof MandolinSpec){
20             instrument = new Mandolin(serialNumber, price, (MandolinSpec) spec);
21         }
22         
23         inventory.add(instrument);
24     }
25     
26     public Instrument get(String serialNumber){
27         for(Iterator i = inventory.iterator(); i.hasNext();){
28             Instrument instrument = (Instrument) i.next();
29             if(instrument.getSerialNumber().equals(serialNumber)){
30                 return instrument;
31             }
32         }
33         
34         return null;
35     }
36     
37     public List search(GuitarSpec searchSpec){
38         List matchingGuitars = new LinkedList();
39         for(Iterator i = inventory.iterator(); i.hasNext();){
40             Guitar guitar = (Guitar) i.next();
41             if(guitar.getSpec().matches(searchSpec))
42                 matchingGuitars.add(guitar);
43         }
44         
45         return matchingGuitars;
46     }
47     
48     public List search(MandolinSpec searchSpec){
49         List matchingMandolins = new LinkedList();
50         for(Iterator i = inventory.iterator(); i.hasNext();){
51             Mandolin mandolin = (Mandolin) i.next();
52             if(mandolin.getSpec().matches(searchSpec))
53                 matchingMandolins.add(mandolin);
54         }
55         
56         return matchingMandolins;
57     }
58 }
 1 package com.headfirst.instrument;
 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 inventory;
10     
11     public Inventory(){
12         inventory = new LinkedList();
13     }
14     
15     public void addInstrument(String serialNumber, double price, InstrumentSpec spec){
16         Instrument instrument = null;
17         if(spec instanceof GuitarSpec){
18             instrument = new Guitar(serialNumber, price, (GuitarSpec) spec);
19         }else if(spec instanceof MandolinSpec){
20             instrument = new Mandolin(serialNumber, price, (MandolinSpec) spec);
21         }
22         
23         inventory.add(instrument);
24     }
25     
26     public Instrument get(String serialNumber){
27         for(Iterator i = inventory.iterator(); i.hasNext();){
28             Instrument instrument = (Instrument) i.next();
29             if(instrument.getSerialNumber().equals(serialNumber)){
30                 return instrument;
31             }
32         }
33         
34         return null;
35     }
36     
37     public List search(GuitarSpec searchSpec){
38         List matchingGuitars = new LinkedList();
39         for(Iterator i = inventory.iterator(); i.hasNext();){
40             Guitar guitar = (Guitar) i.next();
41             if(guitar.getSpec().matches(searchSpec))
42                 matchingGuitars.add(guitar);
43         }
44         
45         return matchingGuitars;
46     }
47     
48     public List search(MandolinSpec searchSpec){
49         List matchingMandolins = new LinkedList();
50         for(Iterator i = inventory.iterator(); i.hasNext();){
51             Mandolin mandolin = (Mandolin) i.next();
52             if(mandolin.getSpec().matches(searchSpec))
53                 matchingMandolins.add(mandolin);
54         }
55         
56         return matchingMandolins;
57     }
58 }

View Code

View Code

不曾写FindInstrumentTester.java,因为此项目起个问题,很麻烦扩展。

从未写FindInstrumentTester.java,因为这类型发个问题,很麻烦扩展。

不信?我说,你来改一下,现在咱们若多乐器:古筝,短笛,箫,二胡……;一共一百种植,而且各种乐器带有自身的突出属性,不多每种不畏带来一栽吧。

不信?我说,你来窜一下,现在咱们设加乐器:古筝,短笛,箫,二胡……;一共一百种,而且各国种乐器带有自身之与众不同性质,不多每种就是带一种吧。

凡未是代码写及你手酸?不是,好吧,那您早晚也公的复制粘贴而得意。

凡不是代码写及公手酸?不是,好吧,那您必也汝的复制粘贴而得意。

脚我们本着端Instrument项目进行修改,怎样在添很多乐器的上,我们的代码改动性很粗。

下我们对上面Instrument项目进行修改,怎样当增加很多乐器的时刻,我们的代码改动性很有点。

来说几个OO原则:

来说几单OO原则:

1.接口(interface):对接口编程,而无是针对实现编程,软件再易于让扩张。通过接口编程,程序代码将下该接口的装有子类,甚至是没有为创造的那些。

1.接口(interface):对接口编程,而休是对准促成编程,软件还便于让扩大。通过接口编程,程序代码将采用该接口的享有子类,甚至是从来不吃创造的那些。

2.封装(encapsulation):将变之东西封装起来,将非转换的物为卷入起来。通过包装你转移的代码用在包装之内,对于封装之外的代码,你不要顾虑。

2.封装(encapsulation):将扭转的物封装起来,将不换的东西也卷入起来。通过包你改变的代码用以卷入之内,对于封装之外的代码,你不用担心。

3.改成(change):每个接近才发生一个改观之理。减少引起该类变动的要素,就得吃此类发生变动的机遇太小化。每个接近就做一样码事。

3.改(change):每个接近就来一个移的理。减少引起该类变动的素,就可吃此类发生反的时机太小化。每个接近才做同样起事。

说一样说,项目扩大的困难在哪里吧:

说一样说,项目扩大的难题在何吧:

首先,要经连续Instrument类,写来多兑现乐器类,通过持续InstrumentSpec类,写有许多贯彻乐器属性类

先是,要经过连续Instrument类,写起无数实现乐器类,通过持续InstrumentSpec类,写有众多贯彻乐器属性类

下,乐器有不同的异样性能,要于乐器属性类中补充加上,在对待matches方法被也使比这些特殊性能

副,乐器有两样的奇特属性,要当乐器属性类中上加上,在对待matches方法吃吗使对照这些非常属性

末,在Inventory类中,search方法而因小种乐器,写起小种检索方法

终极,在Inventory类中,search方法要基于小种乐器,写起多少种检索方法

这些问题所在,就是我们继承了Instrument抽象类和InstrumentSpec抽象类,因为虚无类不克实例化,导致来略乐器就要写多少。

这些问题所在,就是咱们继承了Instrument抽象类和InstrumentSpec抽象类,因为虚无类非能够实例化,导致来些许乐器就要写多少。

来没出点子,把具有的乐器都为此一个Instrument类表示?这样我们就毫无写尽多之子类了,我们一味当乐器属性被屡遭上加一个instrumentType变量来储存乐器的类

有无出办法,把具有的乐器都因此一个Instrument类表示?这样我们不怕绝不写最好多之子类了,我们仅仅当乐器属性被遭补充加一个instrumentType变量来存储乐器的类型

一经这样,那些乐器特有的性能怎样表示?不容许所有乐器的性都同吧。

如果这么,那些乐器特有的习性怎样表示?不容许有所乐器的属性都一律吧。

发出没发生想过包裹,我们拿乐器的习性在进一步封装起来,但我们不把性能封装到近似中,而是包装在Map中,一个性一个键值对。InstrumentSpec中蕴藏Map。想只要谁属性就失里取值就吓了。

来没发思了包裹,我们把乐器的属性在进一步封装起来,但咱无将性能封装到近似中,而是包在Map中,一个特性一个键值对。InstrumentSpec中储存Map。想如果谁属性就夺里取值就好了。

摸方法勾勒不过多,没提到,我们吧把InstrumentSpec也惟有所以一个象征,怎么样?

检索方法勾勒最好多,没涉及,我们呢把InstrumentSpec也就所以一个表示,怎么样?

lovebet体育官网 79

lovebet体育官网 80

说了这般多,具体怎么落实,是还免晓得。那么尽管叫自家执笔敲起上述代码:

说了这般多,具体怎么落实,是还未晓。那么就是给自家执笔敲起上述代码:

路架构:

路架构:

 lovebet体育官网 81

 lovebet体育官网 82

枚举多了一个InstrumentType,用来囤乐器项目,其余的枚举有的中长了有的情:

枚举多了一个InstrumentType,用来储存乐器项目,其余的枚举有的中加了一些内容:

Builder.java:

Builder.java:

lovebet体育官网 83lovebet体育官网 84

lovebet体育官网 85lovebet体育官网 86

 1 package com.headfirst.instrumentfinalversion;
 2 
 3 public enum Builder {
 4 FENDER, MARTIN, GIBSON, COLLINGS, OLSON, RYAN, PRS, ANY;
 5     
 6     public String toString(){
 7         switch(this){
 8         case FENDER:
 9             return "Fender";
10         case MARTIN:
11             return "Martin";
12         case GIBSON:
13             return "Gibson";
14         case COLLINGS:
15             return "Collings";
16         case OLSON:
17             return "Olson";
18         case RYAN:
19             return "Ryan";
20         case PRS:
21             return "Prs";
22         case ANY:
23             return "Any";
24         default: 
25             return "";
26         }
27     }
28 }
 1 package com.headfirst.instrumentfinalversion;
 2 
 3 public enum Builder {
 4 FENDER, MARTIN, GIBSON, COLLINGS, OLSON, RYAN, PRS, ANY;
 5     
 6     public String toString(){
 7         switch(this){
 8         case FENDER:
 9             return "Fender";
10         case MARTIN:
11             return "Martin";
12         case GIBSON:
13             return "Gibson";
14         case COLLINGS:
15             return "Collings";
16         case OLSON:
17             return "Olson";
18         case RYAN:
19             return "Ryan";
20         case PRS:
21             return "Prs";
22         case ANY:
23             return "Any";
24         default: 
25             return "";
26         }
27     }
28 }

View Code

View Code

InstrumentType.java:

InstrumentType.java:

lovebet体育官网 87lovebet体育官网 88

lovebet体育官网 89lovebet体育官网 90

 1 package com.headfirst.instrumentfinalversion;
 2 
 3 public enum InstrumentType {
 4     GUITAR, BANJO, DOBRO, FIDDLE, BASS, MANDOLIN;
 5     
 6     public String toString(){
 7         switch(this){
 8         case GUITAR:
 9             return "Guitar";
10         case BANJO:
11             return "Banjo";
12         case DOBRO:
13             return "Dobro";
14         case FIDDLE:
15             return "Fiddle";
16         case BASS:
17             return "Bass";
18         case MANDOLIN:
19             return "Mandolin";
20         default:
21             return "Unspecified";
22         }
23     }
24 }
 1 package com.headfirst.instrumentfinalversion;
 2 
 3 public enum InstrumentType {
 4     GUITAR, BANJO, DOBRO, FIDDLE, BASS, MANDOLIN;
 5     
 6     public String toString(){
 7         switch(this){
 8         case GUITAR:
 9             return "Guitar";
10         case BANJO:
11             return "Banjo";
12         case DOBRO:
13             return "Dobro";
14         case FIDDLE:
15             return "Fiddle";
16         case BASS:
17             return "Bass";
18         case MANDOLIN:
19             return "Mandolin";
20         default:
21             return "Unspecified";
22         }
23     }
24 }

View Code

View Code

Style.java:

Style.java:

lovebet体育官网 91lovebet体育官网 92

lovebet体育官网 93lovebet体育官网 94

 1 package com.headfirst.instrumentfinalversion;
 2 
 3 public enum Style {
 4     A, F;
 5     
 6     public String toString(){
 7         switch(this){
 8         case A:
 9             return "a";
10         case F:
11             return "F";
12         default:
13             return "kong";
14         }
15     }
16 }
 1 package com.headfirst.instrumentfinalversion;
 2 
 3 public enum Style {
 4     A, F;
 5     
 6     public String toString(){
 7         switch(this){
 8         case A:
 9             return "a";
10         case F:
11             return "F";
12         default:
13             return "kong";
14         }
15     }
16 }

View Code

View Code

Type.java:

Type.java:

lovebet体育官网 95lovebet体育官网 96

lovebet体育官网 97lovebet体育官网 98

 1 package com.headfirst.instrumentfinalversion;
 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 }
 1 package com.headfirst.instrumentfinalversion;
 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 }

View Code

View Code

Wood.java:

Wood.java:

lovebet体育官网 99lovebet体育官网 100

lovebet体育官网 101lovebet体育官网 102

 1 package com.headfirst.instrumentfinalversion;
 2 
 3 public enum Wood {
 4     INDIAN_ROSEWOOD, BRAZILIAN_ROSEWOOD, ALDER, MAPLE, SITKA, MAHOGANY, ADIRONDACK;
 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         case MAPLE:
15             return "Maple";
16         case SITKA:
17             return "Sitka";
18         case MAHOGANY:
19             return "Mahogany";
20         case ADIRONDACK:
21             return "Adirondack";
22         default:
23             return "";
24         }
25     }
26 }
 1 package com.headfirst.instrumentfinalversion;
 2 
 3 public enum Wood {
 4     INDIAN_ROSEWOOD, BRAZILIAN_ROSEWOOD, ALDER, MAPLE, SITKA, MAHOGANY, ADIRONDACK;
 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         case MAPLE:
15             return "Maple";
16         case SITKA:
17             return "Sitka";
18         case MAHOGANY:
19             return "Mahogany";
20         case ADIRONDACK:
21             return "Adirondack";
22         default:
23             return "";
24         }
25     }
26 }

View Code

View Code

 Instrument.java:

 Instrument.java:

lovebet体育官网 103lovebet体育官网 104

lovebet体育官网 105lovebet体育官网 106

 1 package com.headfirst.instrumentfinalversion;
 2 
 3 public class Instrument {
 4     private String serialNumber;
 5     private double price;
 6     private InstrumentSpec spec;
 7     
 8     public Instrument(String serialNumber, double price, InstrumentSpec spec){
 9         this.serialNumber = serialNumber;
10         this.price = price;
11         this.spec = spec;
12     }
13     
14     public String getSerialNumber(){
15         return serialNumber;
16     }
17     
18     public double getPrice(){
19         return price;
20     }
21     
22     public InstrumentSpec getSpec(){
23         return spec;
24     }
25 }
 1 package com.headfirst.instrumentfinalversion;
 2 
 3 public class Instrument {
 4     private String serialNumber;
 5     private double price;
 6     private InstrumentSpec spec;
 7     
 8     public Instrument(String serialNumber, double price, InstrumentSpec spec){
 9         this.serialNumber = serialNumber;
10         this.price = price;
11         this.spec = spec;
12     }
13     
14     public String getSerialNumber(){
15         return serialNumber;
16     }
17     
18     public double getPrice(){
19         return price;
20     }
21     
22     public InstrumentSpec getSpec(){
23         return spec;
24     }
25 }

View Code

View Code

InstrumentSpec.java:

InstrumentSpec.java:

lovebet体育官网 107lovebet体育官网 108

lovebet体育官网 109lovebet体育官网 110

 1 package com.headfirst.instrumentfinalversion;
 2 
 3 import java.util.HashMap;
 4 import java.util.Iterator;
 5 import java.util.Map;
 6 
 7 public class InstrumentSpec {
 8     private Map properties;
 9     
10     public InstrumentSpec(Map properties){
11         if(properties == null){
12             this.properties = new HashMap();
13         }else{
14             this.properties = new HashMap(properties);
15         }
16     }
17     
18     public Object getProperty(String propertyName){
19         return properties.get(propertyName);
20     }
21     
22     public Map getProperties(){
23         return properties;
24     }
25     
26     public boolean matches(InstrumentSpec otherSpec){
27         for(Iterator i = otherSpec.getProperties().keySet().iterator(); i.hasNext();){
28             String propertyName = (String) i.next();
29             if(!properties.get(propertyName).equals(otherSpec.getProperty(propertyName)))
30                 return false;
31         }
32         
33         return true;
34     }
35 }
 1 package com.headfirst.instrumentfinalversion;
 2 
 3 import java.util.HashMap;
 4 import java.util.Iterator;
 5 import java.util.Map;
 6 
 7 public class InstrumentSpec {
 8     private Map properties;
 9     
10     public InstrumentSpec(Map properties){
11         if(properties == null){
12             this.properties = new HashMap();
13         }else{
14             this.properties = new HashMap(properties);
15         }
16     }
17     
18     public Object getProperty(String propertyName){
19         return properties.get(propertyName);
20     }
21     
22     public Map getProperties(){
23         return properties;
24     }
25     
26     public boolean matches(InstrumentSpec otherSpec){
27         for(Iterator i = otherSpec.getProperties().keySet().iterator(); i.hasNext();){
28             String propertyName = (String) i.next();
29             if(!properties.get(propertyName).equals(otherSpec.getProperty(propertyName)))
30                 return false;
31         }
32         
33         return true;
34     }
35 }

View Code

View Code

Inventory.java:

Inventory.java:

lovebet体育官网 111lovebet体育官网 112

lovebet体育官网 113lovebet体育官网 114

 1 package com.headfirst.instrumentfinalversion;
 2 
 3 import java.util.Iterator;
 4 import java.util.LinkedList;
 5 import java.util.List;
 6 
 7 public class Inventory {
 8     private List inventory;
 9     
10     public Inventory(){
11         inventory = new LinkedList();
12     }
13     
14     public void addInstrument(String serialNumber, double price, InstrumentSpec spec){
15         Instrument instrument = new Instrument(serialNumber, price, spec);
16         inventory.add(instrument);
17     }
18     
19     public Instrument get(String serialNumber){
20         for(Iterator i = inventory.iterator(); i.hasNext();){
21             Instrument instrument = (Instrument) i.next();
22             if(instrument.getSerialNumber().equals(serialNumber))
23                 return instrument;
24         }
25         
26         return null;
27     }
28     
29     public List search(InstrumentSpec searchSpec){
30         List matchingInstruments = new LinkedList();
31         for(Iterator i = inventory.iterator(); i.hasNext();){
32             Instrument instrument = (Instrument) i.next();
33             if(instrument.getSpec().matches(searchSpec))
34                 matchingInstruments.add(instrument);
35         }
36         
37         return matchingInstruments;
38     }
39 }
 1 package com.headfirst.instrumentfinalversion;
 2 
 3 import java.util.Iterator;
 4 import java.util.LinkedList;
 5 import java.util.List;
 6 
 7 public class Inventory {
 8     private List inventory;
 9     
10     public Inventory(){
11         inventory = new LinkedList();
12     }
13     
14     public void addInstrument(String serialNumber, double price, InstrumentSpec spec){
15         Instrument instrument = new Instrument(serialNumber, price, spec);
16         inventory.add(instrument);
17     }
18     
19     public Instrument get(String serialNumber){
20         for(Iterator i = inventory.iterator(); i.hasNext();){
21             Instrument instrument = (Instrument) i.next();
22             if(instrument.getSerialNumber().equals(serialNumber))
23                 return instrument;
24         }
25         
26         return null;
27     }
28     
29     public List search(InstrumentSpec searchSpec){
30         List matchingInstruments = new LinkedList();
31         for(Iterator i = inventory.iterator(); i.hasNext();){
32             Instrument instrument = (Instrument) i.next();
33             if(instrument.getSpec().matches(searchSpec))
34                 matchingInstruments.add(instrument);
35         }
36         
37         return matchingInstruments;
38     }
39 }

View Code

View Code

FindInstrument.java:

FindInstrument.java:

lovebet体育官网 115lovebet体育官网 116

lovebet体育官网 117lovebet体育官网 118

 1 package com.headfirst.instrumentfinalversion;
 2 
 3 import java.util.HashMap;
 4 import java.util.Iterator;
 5 import java.util.List;
 6 import java.util.Map;
 7 
 8 public class FindInstrument {
 9     
10     private static void initializeInventory(Inventory inventory){
11         Map properties = new HashMap();
12         properties.put("InstrumentType", InstrumentType.GUITAR);
13         properties.put("builder", Builder.COLLINGS);
14         properties.put("model", "CJ");
15         properties.put("type", Type.ACOUSTIC);
16         properties.put("numStrings", 6);
17         properties.put("topWood", Wood.INDIAN_ROSEWOOD);
18         properties.put("backWood", Wood.SITKA);
19         inventory.addInstrument("11277", 3999.95, new InstrumentSpec(properties));
20         
21         properties.put("builder", Builder.MARTIN);
22         properties.put("model", "D-18");
23         properties.put("topWood", Wood.MAHOGANY);
24         properties.put("backWood", Wood.ADIRONDACK);
25         inventory.addInstrument("122784", 5495.95, new InstrumentSpec(properties));
26         
27         properties.put("builder", Builder.FENDER);
28         properties.put("model", "Stratocastor");
29         properties.put("type", Type.ELECTRIC);
30         properties.put("topWood", Wood.ALDER);
31         properties.put("backWood", Wood.ALDER);
32         inventory.addInstrument("V9512", 1549.95, new InstrumentSpec(properties));
33         
34         properties.put("builder", Builder.GIBSON);
35         properties.put("model", "Les Paul");
36         properties.put("topWood", Wood.MAPLE);
37         properties.put("backWood", Wood.MAPLE);
38         inventory.addInstrument("70108276", 2295.95, new InstrumentSpec(properties));
39         
40         properties.put("model", "SG '61 Reissue");
41         properties.put("topWood", Wood.MAHOGANY);
42         properties.put("backWood", Wood.MAHOGANY);
43         inventory.addInstrument("82765501", 1890.95, new InstrumentSpec(properties));
44         
45         properties.put("InstrumentType", InstrumentType.MANDOLIN);
46         properties.put("model", "F-5G");
47         properties.put("type", Type.ACOUSTIC);
48         properties.remove("numStrings");//假如使用相同的properties Map,别忘了为Mandolin移除numStrings
49         properties.put("topWood", Wood.MAPLE);
50         properties.put("backWood", Wood.MAPLE);
51         inventory.addInstrument("9019920", 5495.95, new InstrumentSpec(properties));
52         
53         properties.put("InstrumentType", InstrumentType.BANJO);
54         properties.put("model", "RB-3 Wreath");
55         properties.put("type", Type.ACOUSTIC);
56         properties.remove("topWood");
57         properties.put("numStrings", 5);
58         inventory.addInstrument("8900231", 2945.95, new InstrumentSpec(properties));
59     }
60     
61     public static void main(String[] args){
62         //Set up Rick's inventory
63         Inventory inventory = new Inventory();
64         initializeInventory(inventory);
65         
66         Map properties = new HashMap();
67         properties.put("builder", Builder.GIBSON);
68         properties.put("backWood", Wood.MAPLE);
69         InstrumentSpec clientSpec = new InstrumentSpec(properties);
70         
71         List matchingInstruments = inventory.search(clientSpec);
72         if(!matchingInstruments.isEmpty()){
73             System.out.println("You might like these instruments:");
74             for(Iterator i = matchingInstruments.iterator(); i.hasNext();){
75                 Instrument instrument = (Instrument)i.next();
76                 InstrumentSpec spec = instrument.getSpec();
77                 System.out.println("We have a " + spec.getProperty("instrumentType") + " with the following properties:");
78                 for(Iterator j = spec.getProperties().keySet().iterator(); j.hasNext();){
79                     String propertyName = (String)j.next();
80                     if(propertyName.equals("instrumentType"))
81                         continue;
82                     System.out.println("   " + propertyName + ": " + spec.getProperty(propertyName));
83                 }
84                 System.out.println(" You can have this " + spec.getProperty("instrumentType") + " for $" + instrument.getPrice() + "\n---");
85                 
86             }
87         }else{
88             System.out.println("Sorry, we have nothing for you .");
89         }
90     }
91     
92     
93 }
 1 package com.headfirst.instrumentfinalversion;
 2 
 3 import java.util.HashMap;
 4 import java.util.Iterator;
 5 import java.util.List;
 6 import java.util.Map;
 7 
 8 public class FindInstrument {
 9     
10     private static void initializeInventory(Inventory inventory){
11         Map properties = new HashMap();
12         properties.put("InstrumentType", InstrumentType.GUITAR);
13         properties.put("builder", Builder.COLLINGS);
14         properties.put("model", "CJ");
15         properties.put("type", Type.ACOUSTIC);
16         properties.put("numStrings", 6);
17         properties.put("topWood", Wood.INDIAN_ROSEWOOD);
18         properties.put("backWood", Wood.SITKA);
19         inventory.addInstrument("11277", 3999.95, new InstrumentSpec(properties));
20         
21         properties.put("builder", Builder.MARTIN);
22         properties.put("model", "D-18");
23         properties.put("topWood", Wood.MAHOGANY);
24         properties.put("backWood", Wood.ADIRONDACK);
25         inventory.addInstrument("122784", 5495.95, new InstrumentSpec(properties));
26         
27         properties.put("builder", Builder.FENDER);
28         properties.put("model", "Stratocastor");
29         properties.put("type", Type.ELECTRIC);
30         properties.put("topWood", Wood.ALDER);
31         properties.put("backWood", Wood.ALDER);
32         inventory.addInstrument("V9512", 1549.95, new InstrumentSpec(properties));
33         
34         properties.put("builder", Builder.GIBSON);
35         properties.put("model", "Les Paul");
36         properties.put("topWood", Wood.MAPLE);
37         properties.put("backWood", Wood.MAPLE);
38         inventory.addInstrument("70108276", 2295.95, new InstrumentSpec(properties));
39         
40         properties.put("model", "SG '61 Reissue");
41         properties.put("topWood", Wood.MAHOGANY);
42         properties.put("backWood", Wood.MAHOGANY);
43         inventory.addInstrument("82765501", 1890.95, new InstrumentSpec(properties));
44         
45         properties.put("InstrumentType", InstrumentType.MANDOLIN);
46         properties.put("model", "F-5G");
47         properties.put("type", Type.ACOUSTIC);
48         properties.remove("numStrings");//假如使用相同的properties Map,别忘了为Mandolin移除numStrings
49         properties.put("topWood", Wood.MAPLE);
50         properties.put("backWood", Wood.MAPLE);
51         inventory.addInstrument("9019920", 5495.95, new InstrumentSpec(properties));
52         
53         properties.put("InstrumentType", InstrumentType.BANJO);
54         properties.put("model", "RB-3 Wreath");
55         properties.put("type", Type.ACOUSTIC);
56         properties.remove("topWood");
57         properties.put("numStrings", 5);
58         inventory.addInstrument("8900231", 2945.95, new InstrumentSpec(properties));
59     }
60     
61     public static void main(String[] args){
62         //Set up Rick's inventory
63         Inventory inventory = new Inventory();
64         initializeInventory(inventory);
65         
66         Map properties = new HashMap();
67         properties.put("builder", Builder.GIBSON);
68         properties.put("backWood", Wood.MAPLE);
69         InstrumentSpec clientSpec = new InstrumentSpec(properties);
70         
71         List matchingInstruments = inventory.search(clientSpec);
72         if(!matchingInstruments.isEmpty()){
73             System.out.println("You might like these instruments:");
74             for(Iterator i = matchingInstruments.iterator(); i.hasNext();){
75                 Instrument instrument = (Instrument)i.next();
76                 InstrumentSpec spec = instrument.getSpec();
77                 System.out.println("We have a " + spec.getProperty("instrumentType") + " with the following properties:");
78                 for(Iterator j = spec.getProperties().keySet().iterator(); j.hasNext();){
79                     String propertyName = (String)j.next();
80                     if(propertyName.equals("instrumentType"))
81                         continue;
82                     System.out.println("   " + propertyName + ": " + spec.getProperty(propertyName));
83                 }
84                 System.out.println(" You can have this " + spec.getProperty("instrumentType") + " for $" + instrument.getPrice() + "\n---");
85                 
86             }
87         }else{
88             System.out.println("Sorry, we have nothing for you .");
89         }
90     }
91     
92     
93 }

View Code

View Code

点的代码好好看一下,很值得尝试,现在我之水准认为就是一模一样段落我打无想了之,真是简单可行。

上面的代码好好看一下,很值得尝试,现在本身之程度认为当下是同段子我由无感念过的,真是简单可行。

运作结果:

运转结果:

lovebet体育官网 119

lovebet体育官网 120

lovebet体育官网 121

lovebet体育官网 122

lovebet体育官网 123

lovebet体育官网 124

吓之知,分享给大家。

好的学问,分享给大家。

参考网址:

参照网址:

equalsIgnoreCase():http://blog.csdn.net/yujian\_bing/article/details/8171379

equalsIgnoreCase():http://blog.csdn.net/yujian\_bing/article/details/8171379

Iterator
keySet():https://zhidao.baidu.com/question/1303782909568685779.html

Iterator
keySet():https://zhidao.baidu.com/question/1303782909568685779.html

与一个Map键值对覆盖问题:http://blog.csdn.net/wide288/article/details/72828000

跟一个Map键值对覆盖问题:http://blog.csdn.net/wide288/article/details/72828000

立同样段很重点lovebet体育官网,学会思路,而休是学会敲代码!

这无异回很要紧,学会思路,而非是学会敲代码!

相关文章