Wu Yuxiong - natural born JAVA object-oriented high-level programming study notes: pet store case study

interface the Pet {     // definition of the interface pet 
    public String getName ();
     public String the getColor ();
     public  int getAge (); 
} 
class Cat the implements the Pet {     // cat pet, implement the interface 
    Private String name;     // pet name 
    Private String color;     // pet color 
    Private  int Age;         // Pet Age 
    public Cat (String name, String color, int Age) {
         the this .setName (name);
         the this.setColor(color) ;
        this.setAge(age) ;
    }
    public void setName(String name){
        this.name = name ;
    }
    public void setColor(String color){
        this.color = color;
    }
    public void setAge(int age){
        this.age = age ;
    }
    public String getName(){
        return this.name ;
    }
    public String getColor(){
        return this.color ;
    }
    public  int getAge () {
         return  the this .age; 
    } 
}; 
class Dog the implements the Pet {     // dog pets, implement the interface 
    Private String name;     // pet's name 
    Private String Color;     // pet color 
    Private  int Age;         // Pet Age 
    public Dog (String name, String Color, int Age) {
         the this .setName (name);
         the this .setColor (Color);
         the this .setAge (Age); 
    } 
    public  void setName(String name){
        this.name = name ;
    }
    public void setColor(String color){
        this.color = color;
    }
    public void setAge(int age){
        this.age = age ;
    }
    public String getName(){
        return this.name ;
    }
    public String getColor(){
        return this.color ;
    }
    public int getAge(){
        return this.age; 
    } 
}; 
class a PetShop {     // pet stores 
    Private the Pet [] Qucik Facts Pets;     // save a set of pet 
    Private  int Foot;
     public a PetShop ( int len) {
         IF (len> 0 {)
             the this .pets = new new the Pet [ len];     // open array size 
        } the else {
             the this .pets = new new the Pet [. 1];     // open the at least one spatial 
        } 
    } 
    public  Boolean the Add (the Pet PET) {     // increase is a pet
         IF ( the this .foot < the this .pets.length) {
             the this .pets [ the this .foot] = PET;     // increase Pets 
            the this .foot ++ ;
             return  to true ; 
        } the else {
             return  to false ; 
        } 
    } 
    public the Pet [] Search (String keyWord) {
         // should determine how many pets comply with 
        the Pet the p-[] = null ;
         int COUNT = 0;     // record the number of pets will be in line with results 
        for (int I = 0; I <the this .pets.length; I ++ ) {
             IF ( the this .pets [I] =! null ) {         // This location is indicated pet 
                IF ( the this .pets [I] .getName () the indexOf (keyWord) = -. 1.! 
                    || the this . .pets [I] .getColor () the indexOf (keyWord) = -. 1! ) { 
                    COUNT ++;     // modify the number of records found 
                } 
            } 
        } 
        P = new new the Pet [COUNT];     // open the specified the size of the space 
        int F = 0;     // add elements of the position marker 
        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 ;

    }
};
public class{PetShopDemo
    public  static  void main (String args []) { 
        a PetShop PS = new new a PetShop (. 5);     // five pets 
        ps.add ( new new Cat ( "white", "white", 2));     // increase pet successful 
        ps.add ( new new Cat ( "black Cat", "black", 3));     // increase pet, success 
        ps.add ( new new Cat ( "cat", "suit", 3));     / / increase pet, success 
        ps.add ( new new Dog ( "pull step, Colorado", "yellow", 3));     // increase pet, success 
        ps.add ( new new Dog ( "golden", "golden" 2)) ;    // increase pet, success 
        ps.add ( new newDog ( "yellow dog", "black", 2));     // increase pets, failed 
        print (ps.search ( "black" )); 
    } 
    public  static  void Print (the Pet P []) {
         for ( int I 0 =; I <p.length; I ++ ) {
             IF (! P [I] = null ) { 
                System.out.println (P [I] .getName () + "," + P [I] .getColor ()
                     + "," + P [I] .getAge ()); 
            } 
        } 
    } 
};

 

Guess you like

Origin www.cnblogs.com/tszr/p/12153257.html