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. python3抓取异步百度瀑布流动态图片(二)get、json下载代码讲解

    制作解析网址的get def gethtml(url,postdata): header = {'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; WOW64; ...

  2. 服饰行业淘宝商城店铺首页设计报告-转载自http://bbs.paidai.com/topic/88363

    店铺的设计 和 美工是2个完全不同的工作. 很多中小卖家,往往会模糊他们之间的差别. 好比要建造一座金茂大厦,先要有建筑设计师设计图纸,明确好建造的楼层数,具体框架结构,所用材料等等. 然后建筑施工队 ...

  3. DBA日常SQL之查询数据库运行状况

    ,) Day, ,),,)) H00, ,),,)) H01, ,),,)) H02, ,),,)) H03, ,),,)) H04, ,),,)) H05, ,),,)) H06, ,),,)) H ...

  4. Oracle监控代理安装ITM(IBM Tivoli Monitoring)

    1 监控代理安装 2 1.1 安装 2 1.1.1 解压安装包 2 1.1.2 安装 2 1.2 配置 5 1.2.1 给Agent授权 5 1.2.2 配置Oracle Agent 10 目录 1  ...

  5. C#本地时间和GMT(UTC)时间的转换

    /// <summary> /// 本地时间转成GMT时间 /// </summary> 4 public static string ToGMTString(DateTime ...

  6. android studio提示unable to run mksdcard sdk

    如题,android studio提示unable to run mksdcard sdk sudo apt-

  7. HDU 3480 division

    题目大意:一个有n个数的集合,现在要求将他分成m+1个子集,对子集i设si表示该集合中最大数与最小数的差的平方.求所有si的和的最小值.n<=10000,m<=5000. 分析:最优解的m ...

  8. [vim]VIM快捷键整理

    转自:http://blog.csdn.net/ceven2010/article/details/7406341#comments 一.移动光标 1.左移h.右移l.下移j.上移k 2.向下翻页ct ...

  9. jquery .filter()过滤器

    述: 筛选元素集合中匹配表达式 或 通过传递函数测试的 那些元素集合. .filter( selector ) selector 类型: Selector                 一个用于匹配 ...

  10. 11个实用的Apache .htaccess配置

    <IfModule mod_rewrite.c>RewriteEngine onRewriteBase /RewriteRule cat_([0-9]{1,})_([0-9]{1,})_( ...