Java实现宠物商店管理

 更新时间:2020年10月29日 14:33:22   作者:龍雅  
这篇文章主要为大家详细介绍了Java实现宠物商店管理,文中示例代码介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们可以参考一下

本文实例为大家分享了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();
   }
  }
 }
}

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

相关文章

  • idea 多模块项目依赖父工程class找不到问题的方法

    idea 多模块项目依赖父工程class找不到问题的方法

    这篇文章主要介绍了idea 多模块项目依赖父工程class找不到问题的方法,小编觉得挺不错的,现在分享给大家,也给大家做个参考。一起跟随小编过来看看吧
    2019-01-01
  • java保证对象在内存中唯一性的实现方法

    java保证对象在内存中唯一性的实现方法

    这篇文章主要介绍了java如何保证对象在内存中的唯一性,如果创建多个对象的话,可能会引发出各种各样的问题,这时,就需要我们保证这个对象在内存中的唯一性,需要的朋友可以参考下
    2019-06-06
  • 详解如何将springboot项目导出成war包

    详解如何将springboot项目导出成war包

    这篇文章主要介绍了详解如何将springboot项目导出成war包,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧
    2020-10-10
  • java金额数字转中文工具类详解

    java金额数字转中文工具类详解

    这篇文章主要为大家详细介绍了java金额数字转中文工具类的相关资料,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
    2019-04-04
  • Runtime.getRuntime().exec 路径包含空格的解决

    Runtime.getRuntime().exec 路径包含空格的解决

    这篇文章主要介绍了Runtime.getRuntime().exec 路径包含空格的解决方案,具有很好的参考价值,希望对大家有所帮助。如有错误或未考虑完全的地方,望不吝赐教
    2021-11-11
  • Redisson公平锁的源码解读分享

    Redisson公平锁的源码解读分享

    Redisson是一个在Redis的基础上实现的Java驻内存数据网格(In-Memory Data Grid)。这篇文章主要通过源码和大家聊聊Redisson公平锁,需要的可以了解一下
    2022-11-11
  • Java中valueOf和parseInt的区别详解

    Java中valueOf和parseInt的区别详解

    这篇文章主要介绍了Java中valueOf和parseInt的区别详解,在编程中,遇到类型转换,好像会经常用到 parseInt 和 valueOf,当然这里只拿 Integer 类型进行陈述,其他类型也是雷同的,需要的朋友可以参考下
    2024-01-01
  • SpringBoot整合Quartz及异步调用的案例

    SpringBoot整合Quartz及异步调用的案例

    Quartz是一个完全由java编写的开源作业调度框架、它的简单易用受到业内人士的一致好评,这篇文章主要介绍了SpringBoot整合Quartz及异步调用,需要的朋友可以参考下
    2023-03-03
  • 无感NullPointerException的值相等判断方法

    无感NullPointerException的值相等判断方法

    当我们需要去判断一个 入参/查库 返回的开关变量(通常是个Integer类型的)时,常常会写如下的if-else判断语句。但又会为在生产环境看到的「NullPointerException」感到困扰,遇到这个问题如何处理呢,下面小编通过本文给大家详细讲解,需要的朋友参考下吧
    2023-02-02
  • 详解Swagger接口文档和常用注解的使用

    详解Swagger接口文档和常用注解的使用

    Swagger是一款遵循 Restful 风格的接口文档开发神器,支持基于 API 自动生成接口文档。本文将为大家讲讲Swagger接口文档和常用注解的使用方法,需要的可以参考一下
    2022-08-08

最新评论