本文实例为大家分享了Java实现宠物商店管理的具体代码,供大家参考,具体内容如下

第一种实现方式:抽象类和对象数组

public abstract class AbstractPet //定义宠物模板

{

private String name; //名称

private String color; //颜色

private int age; //年龄

public AbstractPet(){}

public AbstractPet(String name, String color, int age){

this.setName(name);

this.setColor(color);

this.setAge(age);

}

public String getName(){

return this.name;

}

public String getColor(){

return this.color;

}

public int getAge(){

return this.age;

}

public void setName(String name){

this.name = name;

}

public void setColor(String color){

this.color = color;

}

public void setAge(int age){

if (age > 0)

{

this.age = age;

}else{

this.age = 1;

}

}

//定义抽象方法

public abstract void printInfo(); //自我介绍

}

public class Dog extends AbstractPet

{

public Dog(String name, String color, int age){

super(name, color, age);

}

//实现抽象方法

public void printInfo(){ //自我介绍

System.out.println("狗: " + super.getName() + ",年龄 " + super.getAge() + "岁,颜色:" + super.getColor());

}

}

public class Cat extends AbstractPet

{

public Cat(String name, String color, int age){

super(name, color, age);

}

//实现抽象方法

public void printInfo(){ //自我介绍

System.out.println("狗: " + super.getName() + ",年龄 " + super.getAge() + "岁,颜色:" + super.getColor());

}

}

public class PetShop

{

private AbstractPet[] pets;

private int foot; //定义下标

public PetShop(int len){ //宠物数量由用户确定

if (len > 0)

{

this.pets = new AbstractPet[len];

}else{

this.pets = new AbstractPet[1];

}

}

//添加宠物的方法

public boolean add(AbstractPet pet){

if (this.foot < this.pets.length)

{

this.pets[foot] = pet;

this.foot ++;

return true;

}else{

return false;

}

}

//定义查询宠物的方法[提供按照种类查询和按照姓名查询两种方法]

public AbstractPet[] search(String keyword){

int n = 0; //定义查询到的宠物数量

AbstractPet[] searchPets = null;

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)

{

n++;

}

}

}

searchPets = new AbstractPet[n];

n = 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)

{

searchPets[n] = this.pets[i];

n ++;

}

}

}

return searchPets;

}

}

public class testPetShop

{

public static void main(String[] args){

PetShop p = new PetShop(5);

p.add(new Dog("狗1", "黑色的", 3));

p.add(new Dog("狗2", "红色的", 2));

p.add(new Cat("猫1", "褐色的", 3));

p.add(new Cat("猫2", "黄色的", 3));

p.add(new Cat("猫3", "黑色的", 5));

p.add(new Dog("狗3", "棕色的", 4));

print(p.search("黑"));

}

public static void print(AbstractPet pets[]){

for (int i = 0; i < pets.length; i++)

{

if (pets[i] != null)

{

pets[i].printInfo();

}

}

}

}

第二种实现方式:接口和对象数组

interface IPet

{

String getName(); //取得宠物姓名

String getColor(); //取得宠物颜色

int getAge(); //取得宠物年龄

void show(); //显示宠物信息

}

public class Dog implements IPet

{

private String name;

private String color;

private int age;

public Dog(String name, String color, int age){

this.setName(name);

this.setColor(color);

this.setAge(age);

}

public String getName(){

return this.name;

}

public String getColor(){

return this.color;

}

public int getAge(){

return this.age;

}

public void setName(String name){

this.name = name;

}

public void setColor(String color){

this.color = color;

}

public void setAge(int age){

if (age < 0 || age > 50)

{

this.age = 1; //默认值

}else{

this.age = age;

}

}

public void show(){

System.out.println(this.toString());

}

public String toString(){

return "狗:" + this.getName() + " " + this.getColor() + " " + this.getAge();

}

}

public class Cat implements IPet

{

private String name;

private String color;

private int age;

public Cat(String name, String color, int age){

this.setName(name);

this.setColor(color);

this.setAge(age);

}

public String getName(){

return this.name;

}

public String getColor(){

return this.color;

}

public int getAge(){

return this.age;

}

public void setName(String name){

this.name = name;

}

public void setColor(String color){

this.color = color;

}

public void setAge(int age){

if (age < 0 || age > 50)

{

this.age = 1; //默认值

}else{

this.age = age;

}

}

public void show(){

System.out.println(this.toString());

}

public String toString(){

return "猫:" + this.getName() + " " + this.getColor() + " " + this.getAge();

}

}

public class PetShop

{

private IPet[] pets;

private int foot;

public PetShop(int len){ //宠物店的宠物数量由用户决定

if (len > 0)

{

pets = new IPet[len];

}else{

pets = new IPet[1]; //默认最小数量为1

}

}

public boolean add(IPet pet){

if (this.foot < this.pets.length)

{

this.pets[this.foot] = pet;

this.foot ++;

return true;

}else{

return false;

}

}

public IPet[] search(String keyword){

//定义一个新的宠物对象数组,用来存储符合查询条件的宠物

IPet[] resultPet = 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 ++;

}

}

}

resultPet = new IPet[count];

int n = 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)

{

resultPet[n] = this.pets[i];

n ++;

}

}

}

return resultPet;

}

}

public class TestPetShop

{

public static void main(String[] args){

//创建一个宠物商店

PetShop ps = new PetShop(7); //假设可以放置5只宠物

ps.add(new Dog("旺旺", "黑色的",4));

ps.add(new Dog("旺财", "白色的",6));

ps.add(new Dog("小黑", "黄色的",3));

ps.add(new Cat("波波", "褐色的",7));

ps.add(new Cat("咪咪", "黑色的",8));

ps.add(new Cat("小云", "灰色的",2));

ps.add(new Dog("仔仔", "黄色的",5));

print(ps.search("色"));

}

public static void print(IPet[] pet){

for (int i = 0; i < pet.length; i++)

{

if (pet[i] != null)

{

pet[i].show();

}

}

}

}

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持免费资源网。

java关联宠物商店项目_Java实现宠物商店管理相关推荐

  1. java宠物商店管理系统_Java实现宠物商店管理系统

    Java实现宠物商店管理系统,宠物,宠物商店,数组,您的,还需要 Java实现宠物商店管理系统 易采站长站,站长之家为您整理了Java实现宠物商店管理系统的相关内容. 本文实例为大家分享了Java实现 ...

  2. 宠物商店项目_充分利用宠物项目的7个技巧

    宠物商店项目 I've started so many side projects. Little or not so little, most of them were focused on one ...

  3. java电商和企业项目_java电商和企业项目

    这里比较的都是国外的开源项目,备选项目有: Smilehouse Workspace.Pulse.Shopizer.ofbiz.bigfish.broadleaf1.Smilehouse Worksp ...

  4. java程序设计教程与项目_Java程序设计教程与项目实训

    书名:Java程序设计教程与项目实训 作者:温秀梅.司亚超 出版社:清华大学出版社 出版日期:2017/8/1 字数: 页数: 版次: ISBN:9787#302473701 定价:49.5 目录 章 ...

  5. java集成开发工具项目_Java项目开发(一)-不借助集成工具创建Java项目并编写编译执行脚本...

    java-project(项目根目录)|--src| |--main(主目录)| | |--java(存放项目的.java文件)| | | |--com(包目录)| | | | |--mycompan ...

  6. java程序设计实训项目_Java程序设计教程与项目实训

    本书以现代教育理念为指导,在讲授方式上注意结合应用开发实例,注重培养学生理解面向对象程序设计思想,以提高分析问题和解决实际问题的能力.采用由浅入深.理论与实践相结合的教学思路,通过大量的实例阐述Jav ...

  7. java面向对象的小项目_java第二季面向对象结课小项目之答答租车系统

    这个小项目是学完java面相对象的练习小项目,还有很多知识点没有用上,是因为并没有完全吸收所学的知识.粗略的完成了这个小项目希望大家指点! 创建Car父类 package com.car; publi ...

  8. java的tcp通信项目_java实现TCP通信

    1.概述 通过ServerSocket与Socket实现的TCP的通信,这个例子配合了swing使用,其实作者之前发过一篇ServerSocker与Socket的通信,在这里,这边文章就是在这个例子上 ...

  9. java爬虫小说网项目_java爬虫之下载txt小说

    最近迷上了天蚕土豆写的<大主宰>这本玄幻小说,无奈找不到下载链接.于是就萌生了自己爬取小说章节的想法,代码其实很简单,主要在于分析网页结构.正则匹配以及文件保存. 1. 分析网页结构 爬取 ...

最新文章

  1. 某云数据中心网络解决方案(分享二十一)
  2. python twisted安装
  3. BZOJ2091 [Poi2010]The Minima Game
  4. sqlplus几个存储过程执行变量值窜掉了_基于大数据的冷连轧过程控制优化技术研究...
  5. Hive 外部表关联分区数据
  6. OpenCV3学习(7.1)——图像分割之一(漫水填充FloodFill)
  7. 突然发现一个很好用Golang的json库
  8. 思科交换机2950 强制恢复出厂设置(清密码)
  9. 简谈Java的join()方法(转)
  10. 实时即未来!Flink Forward Asia 2021 议程正式上线!
  11. radare2命令介绍
  12. Turnserver服务器搭建
  13. 中兴e8820刷openwrt_中兴E8820V2(电信天翼宽带类似新路由3歌华链)-拆机及OpenWrt固件...
  14. Excel自定义格式详解
  15. 易康(eCognition)对象几何特征--2:几何(Geometry)_ 形状(Shape)
  16. mac 电脑如何从双系统恢复原mac系统,无需u盘一键重新安装macos
  17. Maximo安全控制相关表
  18. C++ increment/decrement/dereference 操作符典型写法
  19. pycharm的主菜单消失如何解决(“File-Edit-Navigate-View”等菜单丢失)
  20. ESimCSE:无监督句子表示对比学习的增强样本构建方法

热门文章

  1. PS中内容感知移动工具的使用
  2. GFocalV2解读
  3. 网页中无法添加微信好友怎么办?如何一键唤起微信添加好友?
  4. Python学习笔记——流程控制(拉勾教育数据分析实战训练营学习笔记)
  5. 电脑端如何访问手机SD卡中的文件
  6. Android tts语音播报设置最大音量
  7. php cpu占有率过高怎么办,system占用cpu过高怎么办
  8. 【智能优化算法】基于倭黑猩猩优化算法求解单目标优化问题附matlab代码
  9. 大学物理电磁学——磁场对载流导线的作用
  10. 刚刚,2022中国大学排行榜发布