欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

Java实现宠物商店管理

程序员文章站 2022-07-02 14:01:45
本文实例为大家分享了java实现宠物商店管理的具体代码,供大家参考,具体内容如下第一种实现方式:抽象类和对象数组public abstract class abstractpet //定义宠物模板{...

本文实例为大家分享了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 商店管理