Java实例---简单的宠物管理系统

Posted 小a玖拾柒

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Java实例---简单的宠物管理系统相关的知识,希望对你有一定的参考价值。

代码分析

Cat.java

 1 package com.ftl.petshop;
 2 
 3 class Cat implements Pet
 4 {
 5     private String name;
 6     private String color;
 7     private int age;
 8     public Cat(String name, String color, int age)
 9     {
10         this.name = name;
11         this.color = color;
12         this.age = age;
13     }
14     public String getName()
15     {
16         return name;
17     }
18     public void setName(String name)
19     {
20         this.name = name;
21     }
22     public String getColor()
23     {
24         return color;
25     }
26     public void setColor(String color)
27     {
28         this.color = color;
29     }
30     public int getAge()
31     {
32         return age;
33     }
34     public void setAge(int age)
35     {
36         this.age = age;
37     }
38     
39 }
View Code

Dog.java

 1 package com.ftl.petshop;
 2 
 3 class Dog implements Pet
 4 {
 5     private String name;
 6     private String color;
 7     private int age;
 8     public Dog(String name, String color, int age)
 9     {
10         this.name = name;
11         this.color = color;
12         this.age = age;
13     }
14     public String getName()
15     {
16         return name;
17     }
18     public void setName(String name)
19     {
20         this.name = name;
21     }
22     public String getColor()
23     {
24         return color;
25     }
26     public void setColor(String color)
27     {
28         this.color = color;
29     }
30     public int getAge()
31     {
32         return age;
33     }
34     public void setAge(int age)
35     {
36         this.age = age;
37     }
38     
39 }
View Code

Pet.java

1 package com.ftl.petshop;
2 
3 interface Pet
4 {
5     public String getName();
6     public String getColor();
7     public int getAge();
8 }
View Code

PetShop.java

 1 package com.ftl.petshop;
 2 
 3 
 4 class PetShop
 5 {
 6     private Pet pets[];
 7     private int foot;
 8     public PetShop(int len)
 9     {
10         if(len > 0)
11         {
12             this.pets = new Pet[len];
13         }
14         else
15         {
16             this.pets = new Pet[1];
17         }
18     }
19     
20     public boolean add(Pet pet)
21     {
22         if (this.foot < this.pets.length)
23         {
24             this.pets[foot] = pet;
25             this.foot++;
26             return true;
27         }
28         else
29         {
30             return false;
31         }
32     }
33     
34     public Pet[] search(String keyWord)
35     {
36         Pet[] p = null;
37         int count = 0;
38         for ( int i = 0; i <this.pets.length; i++)
39         {
40             if(this.pets[i]!=null)
41             {
42                 if(this.pets[i].getName().indexOf(keyWord)!=-1
43                     && this.pets[i].getColor().indexOf(keyWord)!=-1)
44                 {
45                     count++;
46                 }
47             }
48         }
49         System.out.println("Sum " + count + "is Right...");
50         p = new Pet[count];
51         int f = 0;
52         for (int i = 0; i < this.pets.length;i++)
53         {
54             if(this.pets[i].getName().indexOf(keyWord)!=-1
55                 && this.pets[i].getColor().indexOf(keyWord)!=-1)
56             {
57                 p[f] = this.pets[i];
58                 f++;
59             }
60         }
61         
62         return p;
63     }
64     
65     
66 };
View Code

PetShopDemo.java

 1 package com.ftl.petshop;
 2 
 3 public class PetShopDemo
 4 {
 5 
 6     public static void main(String[] args)
 7     {
 8         // TODO 自动生成的方法存根
 9         PetShop ps = new PetShop(6);
10         ps.add(new Cat("W","W",2));
11         ps.add(new Dog("W","W",2));
12         ps.add(new Cat("B","B",2));
13         ps.add(new Cat("B","W",2));
14         ps.add(new Dog("W","BB",2));
15         ps.add(new Cat("WW","W",2));
16         ps.add(new Dog("AW","W",2));
17         print(ps.search("W"));
18     }
19     public static void print(Pet p[])
20     {
21         for (int i = 0; i < p.length; i++)
22         {
23             if(p[i]!=null)
24             {
25                 System.out.println("Age "+ p[i].getAge() +"  Name:"+ p[i].getColor() +"  Color:" + p[i].getName());
26             }
27         }
28     }
29 
30 }
View Code

源码下载

点击下载

以上是关于Java实例---简单的宠物管理系统的主要内容,如果未能解决你的问题,请参考以下文章

求宠物管理系统Java代码,具有增删改查功能,用数组实现,并且有输入功能

用java写宠物商店,1.系统启动,2.宠物主人登陆,3.宠物主人购买库存宠

创建片段而不从 java 代码实例化它

IDEA+Java控制台实现宠物管理系统建议收藏

IDEA+Java+JSP+Mysql+Tomcat实现Web宠物信息管理系统

java程序设计题,设计一个宠物商店,内有猫和狗两种宠物,研究名字、颜色和年龄等属