Java实现宠物商店管理
作者:龍雅 发布时间:2023-09-14 09:14:13
标签: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();
}
}
}
}
来源:https://blog.csdn.net/Dragon_201314/article/details/78264849


猜你喜欢
- 解析:如果并不知道程序运行时会需要多少对象,或者需要更复杂方式存储对象,那么可以使用Java集合框架。如果启用集合的删除方法,那么集合中所有
- 实践过程效果代码public partial class Form1 : Form{ public Form1()
- Hi~大家好,出来创业快3个月了,一切还不错,前一段时间用了业余时间搞了个问答类网站YQMA.想做中国的stackoverflow,哈哈,只
- 概念在Java中,对象的生命周期包括以下几个阶段:创建阶段(Created)应用阶段(In Use)不可见阶段(Invisible)不可达阶
- 本篇使用java自带的MessageDigest实现对文本的md5加密算法,具体代码如下: /** *@Description
- 本文实例讲述了Java使用Math.random()结合蒙特卡洛方法计算pi值。分享给大家供大家参考,具体如下:一、概述蒙特·卡罗方法(Mo
- 本文实例讲述了C#使用foreach循环遍历数组的方法。分享给大家供大家参考,具体如下:using System;using System.
- 这篇文章主要介绍了Java线程并发访问代码分析,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友可以参
- 想在Linux进行JAVA开发吗?环境如何搭建,第一个HelloWorld如何实现,下面马上奉献:1、环境搭建1.1 Java JDK 的安
- 1.默认的静态资源目录/static/public/resources/META-INF/resources动态资源目录:/template
- 茫茫人海千千万万,感谢这一秒你看到这里。希望我的能对你的有所帮助!共勉!愿你在未来的日子,保持热爱,奔赴山海!Java基础知识(多态)多态因
- 一、简介:介绍两种使用 BitmapTransformation 来实现 Glide 加载圆形图片和圆角图片的方法。Glide 并不能直接支
- java 事务详解一、什么是事务事务是访问数据库的一个操作序列,数据库应用系统通过事务集来完成对数据库的存取。事务的正确执行使得数据库从一种
- 本文实例讲述了Java编程实现中英混合字符串数组按首字母排序的方法。分享给大家供大家参考,具体如下:在Java中对于字符串数组的排序,我们可
- 若一个实例方法声明前带有virtual关键字,那么这个方法就是虚方法。虚方法与非虚方法的最大不同是,虚方法的实现可以由派生类所取代,这种取代
- 环境IDEA :2020.1Maven:3.5.6SpringBoot: 2.0.9 (与此前整合的版本2.3.3 不同,版本适配问题,为配
- mybatis的foreach标签经常用于遍历集合,构建in条件语句或者批量操作语句。下面是foreach标签的各个属性属性描述collec
- 一、使用maven加载依赖加载了连接数据库的依赖、mybatis的依赖以及lombok的依赖<dependency>  
- 错误信息Exception in thread "main" java.lang.ClassCastException:
- 一、首先,我们来看一下效果图,这是新浪微博的Tab滑动效果。我们可以手势滑动,也可以点击上面的头标进行切换。与此同方式,白色横条会移动到相应