代码分析

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;
} }

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;
} }

Pet.java

 package com.ftl.petshop;

 interface Pet
{
public String getName();
public String getColor();
public int getAge();
}

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;
} };

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());
}
}
} }

源码下载

点击下载

Java实例---简单的宠物管理系统的更多相关文章

  1. Java实例---简单的上课管理系统

    源码分析 Course.java package com.ftl.many2many; import java.util.*; public class Course { private int cr ...

  2. Java实例---简单的超市管理系统

    代码分析 Customer.java package test; public class Customer { private String name; private int customerTy ...

  3. Java实例---简单的个人管理系统

    代码分析 FileOperate.java package com.ftl.testperson; import java.io.File ; import java.io.FileInputStre ...

  4. Java实例分析:宠物商店

    设计一个“宠物商店”,在宠物商店中可以有多种宠物,试表示出此种关系,并要求可以根据宠物的关键字查找相应的宠物信息. //======================================== ...

  5. Java实例---简单的数据库操作

    源码分析 DAOFactory.java package cn.ftl.mysql ; public class DAOFactory { public static IEmpDAO getIEmpD ...

  6. Java实例---简单的投票系统

    代码分析  InputData.java package vote; import java.io.BufferedReader; import java.io.IOException; import ...

  7. 主题:Java WebService 简单实例

    链接地址:主题:Java WebService 简单实例    http://www.iteye.com/topic/1135747 前言:朋友们开始以下教程前,请先看第五大点的注意事项,以避免不必要 ...

  8. Java之从头开始编写简单课程信息管理系统

    编写简单的课程管理系统对于新手并不友好,想要出色的完成并不容易以下是我的一些经验和方法 详情可参考以下链接: https://www.cnblogs.com/dream0-0/p/10090828.h ...

  9. PureMVC和Unity3D的UGUI制作一个简单的员工管理系统实例

    前言: 1.关于PureMVC: MVC框架在很多项目当中拥有广泛的应用,很多时候做项目前人开坑开了一半就消失了,后人为了填补各种的坑就遭殃的不得了.嘛,程序猿大家都不喜欢像文案策划一样组织文字写东西 ...

随机推荐

  1. eclipse添加dtd约束和xml约束的方法

    struts-2.3.dtd dtd 文件的位置 导入上面的 dtd 约束 spring-beans-4.2.xsd 为主配置文件引入新的命名空间(约束)

  2. Android开源项目xUtils HttpUtils模块分析(转)

    xUtils是github上的一个Android开源工具项目,其中HttpUtils模块是处理网络连接部分,刚好最近想整理下Android网络编程知识,今天学习下xUtils中HttpUtils. x ...

  3. Steps to install Docker on Manjaro 16.10--转

    https://manjaro-tutorial.blogspot.com/2016/12/how-to-install-docker-on-manjaro-1610.html Open Termin ...

  4. CentOS6.4将MySQL5.1升级至5.5.36

    1.为了安全期间,首先需要备份原有数据 2.卸载原有MySQL,先停止原有的MySQL服务,再查找 find / -name mysql [root@qxyw /]# find / -name mys ...

  5. WPF中C#代码触发鼠标点击事件

    1.如下代码; <Button x:Name="btnTest" Click="btnTest_Click"> <Button.Trigger ...

  6. linux 升级-杂

    apt-cache search linux apt-cache search linux | grep generic apt-cache search linux | grep 4.10. apt ...

  7. Java责任链模式

    责任链模式 顾名思义,责任链模式(Chain of Responsibility Pattern)为请求创建了一个接收者对象的链.这种模式给予请求的类型,对请求的发送者和接收者进行解耦.这种类型的设计 ...

  8. 基于开源Red5搭建的视频直播平台

    开始之前,为了便于大家了解Red5,此处引用网络文字,非原创 引言 流媒体文件是目前非常流行的网络媒体格式之一,这种文件允许用户一边下载一边播放,从而大大减少了用户等待播放的时间.另外通过网络播放流媒 ...

  9. [转]Porting to Oracle with Entity Framework NLog

    本文转自:http://izzydev.net/.net/oracle/entityframework/2017/02/01/Porting-to-Oracle-with-Entity-Framewo ...

  10. [转]Getting started with ASP.NET Web API OData in 3 simple steps

    本文转自:https://blogs.msdn.microsoft.com/webdev/2013/01/29/getting-started-with-asp-net-web-api-odata-i ...