Java實例分析:寵物商店


設計一個“寵物商店”,在寵物商店中可以有多種寵物,試表示出此種關系,並要求可以根據寵物的關鍵字查找相應的寵物信息。

//=================================================
// File Name       :	factory
//------------------------------------------------------------------------------
// Author          :	Common


// 接口名:Pet
// 屬性:
// 方法:
interface Pet{
	public String getName();
	public String getColor();
	public int getAge();
}

//類名:Cat
//屬性:
//方法:
class Cat implements Pet{
	private String Name;
	private String Color;
	private int Age;
	
	public Cat(String name, String color, int age) {	//構造方法
		this.setName(name);
		this.setColor(color);
		this.setAge(age);
	}
	
	public String getName() {
		return Name;
	}
	public void setName(String name) {
		Name = name;
	}
	public String getColor() {
		return Color;
	}
	public void setColor(String color) {
		Color = color;
	}
	public int getAge() {
		return Age;
	}
	public void setAge(int age) {
		Age = age;
	}
}

//類名:Dog
//屬性:
//方法:
class Dog implements Pet{
	private String Name;
	private String Color;
	private int Age;
	
	public Dog(String name, String color, int age) {	//構造方法
		this.setName(name);
		this.setColor(color);
		this.setAge(age);
	}
	
	public String getName() {
		return Name;
	}
	public void setName(String name) {
		Name = name;
	}
	public String getColor() {
		return Color;
	}
	public void setColor(String color) {
		Color = color;
	}
	public int getAge() {
		return Age;
	}
	public void setAge(int age) {
		Age = age;
	}
}

//類名:PetShop
//屬性:
//方法:
class PetShop{
	
	private Pet[] pets;		//定義一個Pet形數組,此數字的大小由傳入的len決定
	private int foot;		//定義數組的當前元素下標
	
	public PetShop(int len){	//數組的大小由len決定,構造方法
		if(len>0){						//判斷傳入的長度是否大於0
			this.pets = new Pet[len];		//根據傳入的大小開辟空間
		}else{									
			this.pets = new Pet[1];		//否則只開辟長度為1的空間
		}
	}
	
	public boolean add(Pet pet){			
		if(this.foot < this.pets.length){	//判斷數組是否已經滿了
			this.pets[foot] = pet;					//沒有存滿則繼續添加
			this.foot++;								//修改下標
			return true;								//添加成功
		}else{
			return false;								//添加失敗,已經存滿了
		}
	}
	
	public Pet[] search(String keyWord){	//關鍵字查找
		Pet p[] = null;										//存放查找的結果,此處的大小不是固定的
		int count = 0;										//記錄下多少個寵物符合查詢結果
		
		for (int i=0;i<this.pets.length;i++){
			if(this.pets[i] != null){
				if(this.pets[i].getName().indexOf(keyWord) != -1 || this.pets[i].getColor().indexOf(keyWord) != -1){
					count++;					//統計符合條件的寵物個數
				}
			}
		}
		p = new Pet[count];			//根據已經確定的記錄數開辟對象數組
		int f = 0;								//設置增加的位置標記
		for (int i=0;i<this.pets.length;i++){
			if(this.pets[i] != null){
				if(this.pets[i].getName().indexOf(keyWord) != -1 || this.pets[i].getColor().indexOf(keyWord) != -1){
					p[f] = this.pets[i];	//將符合查詢條件的寵物信息保存
					f++;
				}
			}
		}
		return p;
	}
	
}

//主類
//Function        : 	適配器設計模式
public class Pet_demo {

	public static void main(String[] args) {
		// TODO 自動生成的方法存根
		PetShop ps = new PetShop(5);
		ps.add(new Cat("白貓","白",2));
		ps.add(new Cat("黑貓","黑",4));
		ps.add(new Cat("黃貓","黃",3));
		ps.add(new Cat("白狗","白",2));
		ps.add(new Cat("黑狗","黑",2));
		ps.add(new Cat("黃狗","黃",3));
		print(ps.search("黑"));
	}
	
	public static void print(Pet p[]){
		for (int i = 0;i<p.length;i++){
			if(p[i] != null){
				System.out.println(p[i].getName()+p[i].getAge()+p[i].getColor());
			}
		}
	}

}

 


免責聲明!

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



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