java 宠物商店代码
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 宠物商店代码的更多相关文章
- Java实验项目三——宠物商店
Program:宠物商店的设计(继承,接口,线性线性表) Description:本题未实现图形用户界面,项目结构描述如下: classes.Pet:定义宠物接口,只要实现该接口的宠物类,都可存储进宠 ...
- Java实例分析:宠物商店
设计一个“宠物商店”,在宠物商店中可以有多种宠物,试表示出此种关系,并要求可以根据宠物的关键字查找相应的宠物信息. //======================================== ...
- 吴裕雄--天生自然JAVA面向对象高级编程学习笔记:宠物商店实例分析
interface Pet{ // 定义宠物接口 public String getName() ; public String getColor() ; public int getAge() ; ...
- 从.NET的宠物商店到Android MVC MVP
1 一些闲话 记得刚进公司的时候,我们除了做常规的Training Project外,每天还要上课,接受各种技术培训和公司业务介绍.当时第一次知道QA和SQA的区别.Training Project时 ...
- Microsoft-PetSop4.0(宠物商店)-数据库设计-Oracle
ylbtech-DatabaseDesgin:Microsoft-PetSop4.0(宠物商店)-数据库设计-Oracle DatabaseName:PetShop(宠物商店) Model:宠物商店网 ...
- 正则表达式学习笔记(附:Java版示例代码)
具体学习推荐:正则表达式30分钟入门教程 . 除换行符以外的任意字符\w word,正常字符,可以当做变量名的,字母.数字.下划线.汉字\s space,空白符 ...
- java俄罗斯方块游戏代码
java俄罗斯方块游戏代码: package com; import java.awt.Color; import java.awt.Graphics; import java.awt.event.K ...
- java常用用代码
/** *Java获取IP代码 */ import java.awt.GridLayout; import java.awt.event.ActionEvent; import java.awt.ev ...
- java学用代码
/** *Java获取IP代码 */ import java.awt.GridLayout; import java.awt.event.ActionEvent; import java.awt.ev ...
随机推荐
- Log4j使用详解(log4j.properties格式)
Log4j使用详解(log4j.properties格式) 1.Log4j 的引入 在应用程序中添加日志记录总的来说基于三个目的: ① 监视代码中变量的变化情况,周期性的记录到文件中供其他应用进行统计 ...
- (五)Linux引导流程解析
目录 Linux引导流程 Linux运行级别 Linux启动服务管理 GRUB配置与应用 启动故障分析与解决 Linux引导流程 Linux系统引导流程如下图: 固件(Firmware)就是写入ERO ...
- 【故障处理】ORA-12162: TNS:net service name is incorrectly specified
本文将给大家阐述一个因未设置系统环境变量ORACLE_SID导致ORA-12162错误的案例.希望大家有所思考. 1.获得有关ORA-12162报错信息的通用表述信息[oracle@asdlabdb0 ...
- == Got TLE on OJ? Here is the solution! ==
As a solo warrior in OJ, I spent about nearly 50% of my time on tackling TLE - that is innumerous ho ...
- Java 提示 JRE unbound 或者 Tomcat unbound 解决方法
解决步骤 1.右键你的项目>>build path>>config build path 2. 选中显示 unbound的 Libraries >>Edit 如下图 ...
- 【Struts2学习笔记-4】包含其他配置文件
包含其他配置文件 <?xml version="1.0" encoding="UTF-8" ?> <!DOCTYPE struts PUBLI ...
- Django数据库操作
刚学到Django的数据库操作,把它记录下来,方便以后查看: 在Django中定义数据库表,并使用Django提供的方法来对数据库进行操作(增.删.改.查) 1.定义3个数据库表: class Gro ...
- nginx的https配置
测试自签名的ssl证书 首先执行如下命令生成一个key openssl genrsa -des3 - 然后他会要求你输入这个key文件的密码.不推荐输入.因为以后要给nginx使用.每次reload ...
- Linux下高并发socket最大连接数所受的各种限制
http://blog.csdn.net/guowake/article/details/6615728 1.修改用户进程可打开文件数限制 在Linux平台上,无论编写客户端程序还是服务端程序,在进行 ...
- svn: E200030: sqlite[S10]: disk I/O error
1. 经遇到的问题,它出现(在我的情况下,至少)要与TortoiseSVN的相互作用.禁用TortoiseSVN的图标缓存(设置>图标重载>缓存“无”>申请)拥有的一切工作就好了 ( ...