A program instance (Pet Store) designed by the interface)

Source: Internet
Author: User
Tags getcolor
In the past, writing a blog did not use the directory structure. It will be used now. For details about how to use the directory structure, refer to: the use of the directory structure requires a pet store. The number of pets in the pet store can be multiple (determined by the number of users, you need to search for the pet information based on the keywords of the pet. The required pet information is designed by yourself. The knowledge interface is used. You can design an object array instance to analyze pet information. We can design three attributes: there are many types of names, colors, and age pets, so pets should be a standard (Interface) pet store with a variety of pets, should be a pet object array code implementation
Interface pet {// define pet interface Public String getname (); Public String getcolor (); Public int getage ();} class cat implements pet {// The cat is a pet, implementation interface private string name; // pet name private string color; // pet color private int age; // pet age public CAT (string name, string color, int age) {This. setname (name); this. setcolor (color); this. setage (AGE);} public void setname (string name) {This. name = Name;} public void setcolor (string C Olor) {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 dog implements pet {// The dog is a pet, and the Implementation interface is private string name; // the pet name is Private string color; // The Pet color is private int age; // pet age public dog (string name, string color, int age) {This. setname (name); this. setcolor (colo R); 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 petshop {// Pet Shop private pet [] pets; // save a group of pets private int foot; Public petshop (INT Len) {If (Le N> 0) {This. pets = new pet [Len]; // open the array size} else {This. pets = new pet [1]; // open at least one space} public Boolean add (Pet) {// Add a pet if (this. foot <this. pets. length) {This. pets [this. foot] = PET; // Add pet this. foot ++; // return true for the foot array subscript;} else {return false ;}} public pet [] search (string keyword) {// determine how many pets meet the requirements pet P [] = NULL; int COUNT = 0; // record how many pets will match the query result for (INT I = 0; I <this. pets. length; I ++) {If (this. pets [I]! = NULL) {// indicates that this location has a pet if (this. Pets [I]. getname (). indexof (keyword )! =-1 | this. Pets [I]. getcolor (). indexof (keyword )! =-1) {count ++; // modify the number of records found} p = new pet [count]; // open the specified size space int f = 0; // Add the for (INT I = 0; I <this. pets. length; I ++) {If (this. pets [I]! = NULL) {// indicates that this location has a pet 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 []) {petshop PS = new petshop (5); // five pet ps. add (new CAT ("white cat", "White", 2); // Add a pet, success ps. add (new CAT ("black cat", "black", 3); // Add a pet, success ps. add (new CAT ("cat", "color", 3); // Add a pet, PS. add (new dog ("ladelado", "yellow", 3); // Add a pet. PS. add (new dog ("golden hair", "golden", 2); // Add a pet, success ps. add (new dog ("Yellow Dog", "black", 2); // Add a pet. The length of the failed array is 5 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]. getcolor () + "," + P [I]. getage ());}}}};

In summary, the most important part of this program is the interface design. Pay attention to the use of indexof during query, and the program design should be rigorous (some judgments should be used ).

A program instance (Pet Store) designed by the interface)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.