Pet.java

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

Cat.java

class Cat implements Pet
{
private String name;
private String color;
private int age;
private float price;
public Cat(String name,String color,int age,float price){
this.name = name;
this.color = color;
this.age = age;
this.price = price;
}

public void setName(String name){
this.name=name;
}
public void setColor(String name){
this.color=color;
}
public void setAge(int age){
this.age=age;
}
public void setPrice(float price){
this.price=price;
}

public String getName(){
return this.name;
}
public String getColor(){
return this.color;
}
public int getAge(){
return this.age;
}
public float getPrice(){
return this.price;
}
}

Dog.java

class Dog implements Pet
{
private String name;
private String color;
private int age;
private float price;
public Dog(String name,String color,int age,float price){
this.name = name;
this.color = color;
this.age = age;
this.price = price;
}

public void setName(String name){
this.name=name;
}
public void setColor(String name){
this.color=color;
}
public void setAge(int age){
this.age=age;
}
public void setPrice(float price){
this.price=price;
}
public String getName(){
return this.name;
}
public String getColor(){
return this.color;
}
public int getAge(){
return this.age;
}
public float getPrice(){
return this.price;
}
}

PetShop.java

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[l];
}
}

public boolean add(Pet pet){
if(foot < this.pets.length){
this.pets[foot] = pet;
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++;
}
}
}
p = new Pet[count];
int f = 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){
p[f] = this.pets[i];
f++;
}
}
}
return p;
}
}

TestPetShop.java

public class PetShopDemo
{
public static void main(String[] args)
{
PetShop ps = new PetShop(8);
ps.add(new Cat("白猫","黑色",1,23.5f));
ps.add(new Cat("黑猫","黑色",2,23.5f));
ps.add(new Cat("白猫","黑色",3,23.5f));
ps.add(new Dog("白狗","黑色",1,23.5f));
ps.add(new Dog("黑狗","黑色",2,23.5f));
ps.add(new Dog("白狗","黑色",3,23.5f));
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()+", "+p[i].getPrice());
}
}
}
}

java 宠物商店代码的更多相关文章

  1. Java实验项目三——宠物商店

    Program:宠物商店的设计(继承,接口,线性线性表) Description:本题未实现图形用户界面,项目结构描述如下: classes.Pet:定义宠物接口,只要实现该接口的宠物类,都可存储进宠 ...

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

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

  3. 吴裕雄--天生自然JAVA面向对象高级编程学习笔记:宠物商店实例分析

    interface Pet{ // 定义宠物接口 public String getName() ; public String getColor() ; public int getAge() ; ...

  4. 从.NET的宠物商店到Android MVC MVP

    1 一些闲话 记得刚进公司的时候,我们除了做常规的Training Project外,每天还要上课,接受各种技术培训和公司业务介绍.当时第一次知道QA和SQA的区别.Training Project时 ...

  5. Microsoft-PetSop4.0(宠物商店)-数据库设计-Oracle

    ylbtech-DatabaseDesgin:Microsoft-PetSop4.0(宠物商店)-数据库设计-Oracle DatabaseName:PetShop(宠物商店) Model:宠物商店网 ...

  6. 正则表达式学习笔记(附:Java版示例代码)

    具体学习推荐:正则表达式30分钟入门教程 .         除换行符以外的任意字符\w      word,正常字符,可以当做变量名的,字母.数字.下划线.汉字\s        space,空白符 ...

  7. java俄罗斯方块游戏代码

    java俄罗斯方块游戏代码: package com; import java.awt.Color; import java.awt.Graphics; import java.awt.event.K ...

  8. java常用用代码

    /** *Java获取IP代码 */ import java.awt.GridLayout; import java.awt.event.ActionEvent; import java.awt.ev ...

  9. java学用代码

    /** *Java获取IP代码 */ import java.awt.GridLayout; import java.awt.event.ActionEvent; import java.awt.ev ...

随机推荐

  1. PO、VO、BO、DTO

    J2EE开发中大量的专业缩略语很是让人迷惑,尤其是跟一些高手讨论问题的时候,三分钟就被人家满口的专业术语喷晕了,PO VO BO DTO POJO DAO,一大堆的就来了(听过老罗对这种现象的批判的朋 ...

  2. 【linux】pkill 踢在线用户

    [root@Centos ~]# w 01:52:52 up 18 min,  3 users,  load average: 0.00, 0.02, 0.05USER     TTY      FR ...

  3. android学习笔记13——ExpandableListView

    ExpandableListView==>可展开的列表组件 ==> ExpandableListView是ListView的子类,对其进行了扩展,其将应用中的列表项分为几组,每组中又包含多 ...

  4. Spring进阶之路(1)-Spring核心机制:依赖注入/控制反转

    原文地址:http://blog.csdn.net/wangyang1354/article/details/50757098 我们经常会遇到这样一种情景,就是在我们开发项目的时候经常会在一个类中调用 ...

  5. Navicat(连接)-1

    连接 要在 Navicat 开始使用你的服务器,你要首先用连接窗口创建一个或多个连接.如果你是一个服务器新手和不肯定如何工作,你可能想看看: MySQL 用户手册 Oracle 数据库文件 Postg ...

  6. UDP程序设计

        UDP是不可靠的连接,广泛应用于各种聊天工具     使用UDP发送的信息,对方不一定会接收到.所有的信息使用数据报的形式发送出去,这就要求客户端要始终等待服务器发送的信息才能进行接收.在Ja ...

  7. [jQuery]attr和prop的区别

    转自:http://www.cnblogs.com/Showshare/p/different-between-attr-and-prop.html 在高版本的jquery引入prop方法后,什么时候 ...

  8. poj 2104 K-th Number(主席树 视频)

    K-th Number 题意: 给你一些数,让你求一个区间内,第k大的数是多少. 题解: 主席树第一题,看的qsc视频写的,戳戳戳 学到了unique函数,他的作用是:把相邻的重复的放到后面,返回值是 ...

  9. POJ 1185 炮兵阵地(状态压缩DP)

    题解:nState为状态数,state数组为可能的状态 代码: #include <map> #include <set> #include <list> #inc ...

  10. catalan数及笔试面试里那些相关的问题(转)

    一.catalan数由来和性质 1)由来 catalan数(卡塔兰数)取自组合数学中一个常在各种计数问题中出现的数列.以比利时的数学家欧仁·查理·卡塔兰 (1814–1894)命名. 卡塔兰数的一般项 ...