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. 前端优化:DNS预解析提升页面速度

    在网页体验中我们常会遇到这种情况,即在调用百度联盟.谷歌联盟以及当前网页所在域名外的域名文件时会遇到请求延时非常严重的情况.那么有没有方法去解决这种请求严重延时的现象呢? 一般来说这种延时的原因不会是 ...

  2. 15条变量&方法命名的最佳实践【转】

    原文地址:15 Best Practices of Variable & Method Naming 不同的代码段采用不同的命名长度.通常来说,循环计数器(loop counters)采用1位 ...

  3. dubbo 解决Multicast java.net.SocketException: No such device

    log4j:WARN No appenders could be found for logger (com.alibaba.dubbo.common.logger.LoggerFactory). l ...

  4. 如何利用RMAN Debug和10046 Trace来诊断RMAN问题?

    学习转摘:https://blogs.oracle.com/Database4CN/entry/%E5%A6%82%E4%BD%95%E5%88%A9%E7%94%A8rman_debug%E5%92 ...

  5. smarty变量

    前台: 注释的两种方式:<{**}>和<!--注释html代码-->比如: <{* <div style="width:100px; height:100 ...

  6. 会话控制:SESSION,COOKIE

    1.http协议: HTTP—超文本传输协议,在TCP协议(长连接.像一个硬件)基础上; 特点:短连接,无状态协议,没法记录本次连接的状态;适用于静态页面的访问,对于后期某些页面是需要浏览器预知客户信 ...

  7. SPOJ #440. The Turtle´s Shortest Path

    Coding a Dijkstra is not hard. %70 of my time spent on tackling TLE, as my last post. Dijkstra works ...

  8. CSS hack样式兼容模式收藏

    part1 —— 浏览器测试仪器,测试您现在使用的浏览器类型 IE6   IE7   IE8   Firefox   Opera   Safari (Chrome)   IE6   IE7   IE8 ...

  9. 287. Find the Duplicate Number

    Given an array nums containing n + 1 integers where each integer is between 1 and n (inclusive), pro ...

  10. JavaScript 设置、读取Cookie

    1.设置Cookie //设置cookie function setCookie(cookieName, cookieValue, cookieExpires, cookiePath) { cooki ...