Java實驗項目三——寵物商店


Program:寵物商店的設計(繼承,接口,線性線性表)

 

Description:本題未實現圖形用戶界面,項目結構描述如下:

 

classes.Pet:定義寵物接口,只要實現該接口的寵物類,都可存儲進寵物商店

      (本例定義的接口為標識接口,未定義任何方法,只用於標識)

 

classes.PetShop:寵物商店類,采用了單例設計模式

classes.entity.Dog:寵物狗類,實現了Pet接口

classes.entity.Cat:寵物貓類,實現了Pet接口

main.TestDemo:測試類

 

classes.Pet

 

 1 /*
 2  * Description:定義寵物標識接口,只要是實現此接口的類都為寵物類,都可放進寵物商店
 3  * 
 4  * Written By:Cai
 5  * 
 6  * Date Written:2017-10-18
 7  * 
 8  * */
 9 
10 
11 package classes;
12 
13 public interface Pet {
14 
15 }

 

classes.PetShop

 

 1 /*
 2  * Description:定義寵物商店,采用單例設計模式
 3  * 
 4  * Written By:Cai
 5  * 
 6  * Date Written:2017-10-18
 7  * 
 8  * */
 9 
10 
11 package classes;
12 
13 import java.util.Iterator;
14 import java.util.List;
15 import java.util.ArrayList;
16 
17 public class PetShop {
18 
19     private List<Pet> pets = new ArrayList<Pet>();     //存取寵物
20     private static PetShop instance = null;            //聲明私有對象
21     
22     //定義私有構造方法
23     private PetShop() {    }
24     
25     //取得寵物商店的實例
26     public static PetShop getInstance() {
27         
28         if( PetShop.instance == null ) {
29             
30             PetShop.instance = new PetShop();
31         }
32         return PetShop.instance;
33     }
34     
35     //取得寵物的數量
36     public int getCount() {
37         
38         return this.pets.size();
39     }
40     
41     //添加寵物
42     public void add(Pet pet) {
43         
44         if( pet != null ) {
45             
46             this.pets.add(pet);
47         }
48     }
49     
50     //打印所有寵物
51     
52     public void displayInfo() {
53         
54         Iterator<Pet> ite = this.pets.iterator();
55         while( ite.hasNext() ) {
56             
57             System.out.println( ite.next() );
58         }
59     }
60     
61 }

 

classes.entity.Dog

 1 /*
 2  * Description:定義寵物狗類
 3  * 
 4  * Written By:Cai
 5  * 
 6  * Date Written:2017-10-18
 7  * 
 8  * */
 9 
10 package classes.entity;
11 
12 import classes.Pet;
13 
14 public class Dog implements Pet {
15     
16     private String name;
17     private String color;
18     private int age;
19     
20     //定義構造方法
21     
22     public Dog() {}
23     
24     public Dog(String name, String color, int age) {
25         
26         super();
27         this.name = name;
28         this.color = color;
29         this.age = age;
30     }
31 
32     
33     //定義setter()和getter()方法
34 
35     public String getName() {
36         return name;
37     }
38 
39     public void setName(String name) {
40         this.name = name;
41     }
42 
43     public String getColor() {
44         return color;
45     }
46 
47     public void setColor(String color) {
48         this.color = color;
49     }
50 
51     public int getAge() {
52         return age;
53     }
54 
55     public void setAge(int age) {
56         this.age = age;
57     }
58 
59     
60     //覆寫toString()方法
61     @Override
62     public String toString() {
63         return "Dog [name=" + name + ", color=" + color + ", age=" + age + "]";
64     }
65 
66 }

 

classes.entity.Cat

 1 /*
 2  * Description:定義寵物貓類
 3  * 
 4  * Written By:Cai
 5  * 
 6  * Date Written:2017-10-18
 7  * 
 8  * */
 9 
10 package classes.entity;
11 
12 import classes.Pet;
13 
14 public class Cat implements Pet {
15 
16     private String name;
17     private String color;
18     private int age;
19     
20     //定義構造方法
21     
22     public Cat() {}
23     
24     public Cat(String name, String color, int age) {
25         
26         super();
27         this.name = name;
28         this.color = color;
29         this.age = age;
30     }
31 
32     
33     //定義setter()和getter()方法
34 
35     public String getName() {
36         return name;
37     }
38 
39     public void setName(String name) {
40         this.name = name;
41     }
42 
43     public String getColor() {
44         return color;
45     }
46 
47     public void setColor(String color) {
48         this.color = color;
49     }
50 
51     public int getAge() {
52         return age;
53     }
54 
55     public void setAge(int age) {
56         this.age = age;
57     }
58 
59     
60     //覆寫toString()方法
61     @Override
62     public String toString() {
63         return "Cat [name=" + name + ", color=" + color + ", age=" + age + "]";
64     }
65     
66 }

 

main.TestDemo

 

 1 /*
 2  * Description:定義測試類,測試寵物商店
 3  * 
 4  * Written By:Cai
 5  * 
 6  * Date Written:2017-10-18
 7  * 
 8  * */
 9 
10 package main;
11 
12 import classes.*;
13 import classes.entity.Cat;
14 import classes.entity.Dog;
15 
16 public class TestDemo {
17 
18     public static void main(String args[]) {
19         
20         //取得寵物商店的實例
21         PetShop shop = PetShop.getInstance();
22         
23         //添加寵物
24         shop.add( new Dog("小黑","黑色",2) );
25         shop.add( new Dog("小白","白色",3) );
26         shop.add( new Cat("小喵","黃色",1) );
27         shop.add( new Cat("大喵","白色",3) );
28         
29         //打印所有寵物
30         shop.displayInfo();
31 
32     }        
33 
34 }

 

 1 /* * 
 2  * Description:定義寵物商店,采用單例設計模式 
 3  *
 4  * Written By:Cai 
 5  * 
 6  * Date Written:2017-10-18 
 7  * 
 8  */
 9 
10 package classes;
11 import java.util.Iterator;
12 import java.util.List;
13 import java.util.ArrayList;
14 public class PetShop {
15     private List<Pet> pets = new ArrayList<Pet>();//存取寵物
16     private static PetShop instance = null;//聲明私有對象
17     //定義私有構造方法
18     private PetShop() {}
19     //取得寵物商店的實例
20     public static PetShop getInstance() {
21         if( PetShop.instance == null ) {
22             PetShop.instance = new PetShop();
23         }
24         return PetShop.instance;
25     }
26     //取得寵物的數量
27     public int getCount() {
28         return this.pets.size();
29     }
30     //添加寵物
31     public void add(Pet pet) {
32         if( pet != null ) {
33             this.pets.add(pet);
34         }
35     }
36     //打印所有寵物
37     public void displayInfo() {
38         Iterator<Pet> ite = this.pets.iterator();
39         while( ite.hasNext() ) {
40             System.out.println( ite.next() );
41         }
42     }
43 }                                            

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM