代码分析
Cat.java
- package com.ftl.petshop;
- class Cat implements Pet
- {
- private String name;
- private String color;
- private int age;
- public Cat(String name, String color, int age)
- {
- this.name = name;
- this.color = color;
- this.age = age;
- }
- public String getName()
- {
- return name;
- }
- public void setName(String name)
- {
- this.name = name;
- }
- public String getColor()
- {
- return color;
- }
- public void setColor(String color)
- {
- this.color = color;
- }
- public int getAge()
- {
- return age;
- }
- public void setAge(int age)
- {
- this.age = age;
- }
- }
- View Code
- Dog.java
- package com.ftl.petshop;
- class Dog implements Pet
- {
- private String name;
- private String color;
- private int age;
- public Dog(String name, String color, int age)
- {
- this.name = name;
- this.color = color;
- this.age = age;
- }
- public String getName()
- {
- return name;
- }
- public void setName(String name)
- {
- this.name = name;
- }
- public String getColor()
- {
- return color;
- }
- public void setColor(String color)
- {
- this.color = color;
- }
- public int getAge()
- {
- return age;
- }
- public void setAge(int age)
- {
- this.age = age;
- }
- }
- View Code
- Pet.java
- package com.ftl.petshop;
- interface Pet
- {
- public String getName();
- public String getColor();
- public int getAge();
- }
- View Code
- PetShop.java
- package com.ftl.petshop;
- class PetShop
- {
- private Pet pets[];
- private int foot;
- public PetShop(int len)
- {
- if(len> 0)
- {
- this.pets = new Pet[len];
- }
- else
- {
- this.pets = new Pet[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++;
- }
- }
- }
- System.out.println("Sum" + count + "is Right...");
- p = new Pet[count];
- int f = 0;
- for (int i = 0; i < this.pets.length;i++)
- {
- if(this.pets[i].getName().indexOf(keyWord)!=-1
- && this.pets[i].getColor().indexOf(keyWord)!=-1)
- {
- p[f] = this.pets[i];
- f++;
- }
- }
- return p;
- }
- };
- View Code
- PetShopDemo.java
- package com.ftl.petshop;
- public class PetShopDemo
- {
- public static void main(String[] args)
- {
- // TODO 自动生成的方法存根
- PetShop ps = new PetShop(6);
- ps.add(new Cat("W","W",2));
- ps.add(new Dog("W","W",2));
- ps.add(new Cat("B","B",2));
- ps.add(new Cat("B","W",2));
- ps.add(new Dog("W","BB",2));
- ps.add(new Cat("WW","W",2));
- ps.add(new Dog("AW","W",2));
- print(ps.search("W"));
- }
- public static void print(Pet p[])
- {
- for (int i = 0; i < p.length; i++)
- {
- if(p[i]!=null)
- {
- System.out.println("Age"+ p[i].getAge() +"Name:"+ p[i].getColor() +"Color:" + p[i].getName());
- }
- }
- }
- }
- View Code
源码下载
点击下载 https://files.cnblogs.com/files/ftl1012/简单的宠物管理系统.rar
来源: http://www.bubuko.com/infodetail-2696313.html