Java 动物类

运行效果

主菜单

若用户输入 1-4 之外的其他内容,提示用户输入错误,重新输入,并重新显示主菜单
主菜单

命令1

命令1

命令2

命令2

命令3

命令3

命令4

命令4

完整代码

动物类(基类)

package animal;

public abstract class Animal {
	private int num; // 编号 
	private int age;     // 月龄
	private String type; // 种类
	private String remarks;//备注
	
	public Animal(int num, int age, String type,String remarks) {
		this.num = num;
		this.age = age;
		this.type = type;
		this.remarks=remarks;
	}
	
	public int getNum() {
		return num;
	}
	public void setNum(int num) {
		this.num = num;
	}
	
	public int getAge() {
		return age;
	}
	public void setAge(int age) {
		this.age = age;
	}
	
	public String getType() {
		return type;
	}
	public void setType(String type) {
		this.type = type;
	}
	
	public String getRemarks() {
		return remarks;
	}
	public void setRemarks(String remarks) {
		this.remarks = remarks;
	}
	
	public abstract String toString(); // 抽象方法toString,猫类和狗类用于重载方法

	protected abstract Object getDognum();
}

猫类(继承动物类)

package animal;

public class Cat extends Animal {//猫类继承动物类

	public Cat(int num, int age, String type, String remarks) {
		super(num, age, type, remarks);
	}

	@Override
	public String toString() {
		return String.format("猫,编号:%d 月龄:%d 种类:%s 备注:%s",getNum(),getAge(),getType(),getRemarks());
	}

	@Override
	protected Object getDognum() {
		return "empty";
	}

}

狗类(继承动物类)

package animal;

public class Dog extends Animal {//狗类继承动物类

	private String dognum;//养狗证编号
	
	@Override
	public String getDognum() {
		return dognum;
	}
	public void setDognum(String dognum) {
		this.dognum = dognum;
	}
	
	public Dog(int num, int age, String type,String dognum, String remarks) {
		super(num, age, type, remarks);
		this.dognum=dognum;
	}

	@Override
	public String toString() {
		return String.format("狗,编号:%d 月龄:%d 种类:%s 养狗证编号:%s 备注:%s",getNum(),getAge(),getType(),getDognum(),getRemarks());
	}
	
}

测试类

package animal;

import java.util.Scanner; // 导入Scanner类,用于输入参数

public class AnimalTest {
	private static Scanner input = new Scanner(System.in);// input作为Scanner类的对象

	private static Animal[] animals = new Animal[100];// 以动物类为类型的数组

	private static int animals_number = 0; // 记录当的动物数

	public static void Create(String[] arr) {// 新增动物
		if (arr[0].equals("猫")) {
			if (arr.length == 4) {
				if (arr[1].matches("[1-9]+")) // 对月龄的输入进行判断
				{
					animals_number++; // 当前动物数量增加1
					animals[animals_number] = new Cat(animals_number, Integer.parseInt(arr[1]), arr[2], arr[3]);
					System.out.println("创建成功\n"); // 创建完成的提示信息
					System.out.println(animals[animals_number]);// 自动调用toString()方法
					System.out.println();
					return;
				} else {
					System.out.println("第二个参数为月龄,应为整数"); // 错误提示
				}
			} else {
				System.out.println("参数的数目不正确,猫后应有三项参数");
			}
			System.out.println("\n创建不成功\n\n"); // 此处输出创建失败的提示信息
		} else if (arr[0].equals("狗")) {
			if (arr.length == 5) {
				if (arr[1].matches("[0-9]+")) // 对月龄的输入进行判断
				{

					animals_number++; // 当前动物数量增加1
					animals[animals_number] = new Dog(animals_number, Integer.parseInt(arr[1]), arr[2], arr[3], arr[4]);
					System.out.println("创建成功\n"); // 创建完成的提示信息
					System.out.println(animals[animals_number]);
					System.out.println();
					return;
				} else {
					System.out.println("第二个参数为月龄,应为整数");// 错误提示
				}
			} else {
				System.out.println("参数的数目不正确,狗后应有四项参数");
			}
			System.out.println("\n创建不成功\n\n"); // 此处输出创建失败的提示信息
		} else
			System.out.println("第一个应为猫或者狗");
	}

	public static void Search(String[] arr)// 查找动物
	{
		int numbers = 0; // 记录符合要求的动物数
		Animal[] temp = new Animal[100]; // 存放符合要求的动物对象

		if ( arr[0].equals("猫") && arr.length == 4 ) // 检查是否是输入了4个参数
		{
			System.out.println("判断猫\n");
			for (int i = 1; i <= animals_number; i++) {
				if (    animals[i].getDognum().equals("empty")//如果是猫则养狗证编号为empty
						&&(arr[1].equals("null") || Integer.parseInt(arr[1]) == animals[i].getAge())// 判断月龄是否符合条件
						&& (arr[2].equals("null") || arr[2].equals(animals[i].getType()))// 判断类型是否符合条件
						&& (arr[3].equals("null") || arr[3].equals(animals[i].getRemarks()))// 判断备注是否符合条件
				) {
					System.out.println("猫的数目加一\n");
					numbers++;
					temp[numbers] = animals[i];
				}
			}

		} else if (arr[0].equals("狗") && arr.length == 5) // 检查是否是输入了5个参数
		{
			System.out.println("判断狗\n");
			for (int i = 1; i <= animals_number; i++) {
				if (    !animals[i].getDognum().equals("empty")//如果是狗则养狗证编号不为empty
						&&(arr[1].equals("null") || Integer.parseInt(arr[1]) == animals[i].getAge())// 判断月龄是否符合条件
						&& (arr[2].equals("null") || arr[2].equals(animals[i].getType()))// 判断类型是否符合条件
						&& (arr[3].equals("null") || arr[3].equals(animals[i].getDognum()))// 判断类型是否符合条件
						&& (arr[4].equals("null") || arr[4].equals(animals[i].getRemarks()))// 判断备注是否符合条件
				) {
					System.out.println("狗的数目加一\n");
					numbers++;
					temp[numbers] = animals[i];
				}
			}

		} else
			System.out.println("输入的参数数量不匹配");

		if (numbers > 0) // 判断是否有符合要求的动物
		{
			System.out.printf("搜索到%d只动物,信息如下:\n", numbers);
			for (int i = 1; i <= numbers; i++) {
				System.out.println(temp[i].toString());
			}
		} else {
			System.out.println("没有搜索到相应的动物");
		}

	}

	public static void Menu() {// 菜单函数实现与用户的交互,并作出相应的操作
		boolean go = true;
		while (go == true) {
			System.out.printf("%n请输入命令编号:%n" + "1.录入一个新的动物%n" + "2.根据输入信息查询动物%n" + "3.列出所有动物%n" + "4.退出%n");
			String ch;
			ch = input.nextLine();// 以回车作为界定符
			switch (ch) {
			case "1": {
				boolean zhixing = true;// 根据zhixing来判断是否继续循环
				while (zhixing) // 输入Y继续,输入N退出
				{
					System.out.println("请按照以下格式输入:\n" + "   猫 月龄 种类 备注\n" + "   狗 月龄 种类 养狗证编号 备注\n" + "   如:\n"
							+ "   猫 1 波斯猫 脚受伤\n" + "   狗 3 猎犬 BH_12345 棕色\n" + "其中,月龄必须为数字\n");
					String animal_information = input.nextLine();
					String[] arr = animal_information.split("[ ]+");// 以空格分隔字符串
					Create(arr);
					System.out.println("是否继续录入(输入Y则继续,输入N则返回主菜单)\n");
					String yn;
					yn = input.nextLine();
					if (yn.equals("Y") == true)// 输入Y则继续循环
						zhixing = true;
					if (yn.equals("N") == true)// 输入N则跳出循环
						zhixing = false;
				}
			}
				break;
			case "2": {
				System.out.println("请按照以下格式输入查询信息:\n" + "    猫 月龄 种类 备注\n" + "    狗 月龄 种类 养狗证编号 备注\n" + "如:\n"
						+ "    猫 1 波斯猫 脚受伤\n" + "    狗 3 猎犬 BH_12345 棕色\n" + "其中,空的字段均可用 null 代替\n");
				String animal_information = input.nextLine();
				String[] arr = animal_information.split("[ ]+");// 以空格分隔字符串
				Search(arr);
			}
				break;
			case "3": {
				System.out.printf("目前有%d只动物,信息如下:\n", animals_number);
				for (int i = 1; i <= animals_number; i++) {
					System.out.println(i + ": " + animals[i].toString());
				}
			}
				break;
			case "4":
				System.out.println("已成功退出程序");
				go = false;
				break;
			default:
				System.out.println("请输入1-4的整数");
			}
		}
	}

	public static void main(String[] args) {// 主函数
		Menu();
	}
}

  • 2
    点赞
  • 18
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值