Design a pet store for pet management
1. Create a Pet Class (nickname, Price, category) and derive a variety of pets, each pet has its own method of feeding
2, create a store class, you can display the kind of all pet information according to the kind
Importjava.util.Arrays; Public classHomeWork1 { Public Static voidMain (string[] args) {Dog D1=NewDog ("Little white", 2000.0f, "dogs"); Dog D2=NewDog ("Little Black", 1000.0f, "dogs"); Dog D3=NewDog ("Xiao Huang", 800.0f, "dogs"); Cat C1=NewCat ("Tom", 200.0f, "cat"); Cat C2=NewCat ("Mimi", 400.0f, "cat"); Cat C3=NewCat ("Dumb", 600.0f, "cat"); PetShop PS=NewPetShop (); Ps.add (D1); Ps.add (D2); Ps.add (D3); Ps.add (C1); Ps.add (C2); Ps.add (C3); for(Pet p:ps.getpets ()) {System.out.println (P.getinfo ()); } System.out.println ("-----------------------"); Pet[] Pets= Ps.findbytype ("Dog"); for(Pet p:pets) {System.out.println (P.getinfo ()); P.eat (); } }}//Pet Classclasspet{PrivateString name; Private floatPrice ; PrivateString type; PublicString GetName () {returnname; } Public voidsetName (String name) { This. Name =name; } Public floatGetPrice () {returnPrice ; } Public voidSetprice (floatPrice ) { This. Price =Price ; } PublicString GetType () {returntype; } Public voidsetType (String type) { This. Type =type; } PublicPet (String name,floatPrice , String type) { Super(); This. Name =name; This. Price =Price ; This. Type =type; } //the way a pet eats something Public voideat () {System.out.println (type+ "Eating Out"); } //Pet Information PublicString GetInfo () {return"I am a" +type+ ", the small friends all call Me" +name+ ", like I will take me away, just rmb+price"; }}//Dog ClassclassDogextendspet{ PublicDog (String name,floatPrice , String type) { Super(name, price, type); } //the method of carbon feeding Public voideat () {System.out.println ("I like to chew on a big stick."); }}//Cat ClassclassCatextendspet{ PublicCat (String name,floatPrice , String type) { Super(name, price, type); } //the method of carbon feeding Public voideat () {System.out.println ("I like to eat fish."); }}//Pet Shopclasspetshop{PrivatePet[] Pets =NewPet[3]; Private intCount//counter//Add Public voidAdd (Pet pet) {if(count>=pets.length) { intNewlen = (pets.length*3)/2+1; Pets=arrays.copyof (pets, Newlen); } Pets[count]=Pet; Count++; } //Get all Pet objects Publicpet[] Getpets () {pet[] PS=NewPet[count]; for(inti = 0;i<count;i++) {Ps[i]=Pets[i]; } returnPS; } //Find your pet's information according to the type of pet Publicpet[] Findbytype (String type) {petshop PS=NewPetShop (); for(inti = 0; i<count;i++){ if(Pets[i].gettype (). Equals (Type)) {//Which of the comparison here is OK in front of calling the Equals method,Ps.add (Pets[i]); } } returnps.getpets (); }}
Inherited app Case Pet Store