用java写一个菜单程序,能够对动物园的动物进行管理 动物类(id、type、color、sex、price、buyDate(入园时间)、isDead=false) 1、添加动物信息进动物园 2、注销一条动物信息(死亡或卖掉) 3、查询全部动物信息 4、查询某种颜色的动物信息 5、统计某种类型的动物信息 6、统计某种类型的动物的价值 7、修改某个动物的基本信息(输入id) 8、退出 注1:注销动物信息,可以通过改变动物的某一个特殊属性来控制。 比如,动物有个属性叫 isDead , 正常情况下是false,当注销的时候改成true,然后其他所有的统计和查询都只列出isDead属性为false的动物来,这样,看起来这个动物就不存在了。 注2:id是用来唯一标识动物信息的,区别动物主要就得靠ID了,所以添加动物信息的时候,id不允许重复 注3:动物信息是指类似下面的信息: ID:3、老虎、白色、雌、370000购买、2012-3-28入园
时间: 2024-02-04 08:03:06 浏览: 26
好的,这是一个比较具体的问题,需要一些具体的代码实现。以下是我提供的代码实现:
```java
import java.util.ArrayList;
import java.util.Scanner;
class Animal {
private int id;
private String type;
private String color;
private String sex;
private double price;
private String buyDate;
private boolean isDead;
public Animal(int id, String type, String color, String sex, double price, String buyDate) {
this.id = id;
this.type = type;
this.color = color;
this.sex = sex;
this.price = price;
this.buyDate = buyDate;
this.isDead = false;
}
public int getId() {
return id;
}
public String getType() {
return type;
}
public String getColor() {
return color;
}
public String getSex() {
return sex;
}
public double getPrice() {
return price;
}
public String getBuyDate() {
return buyDate;
}
public boolean getIsDead() {
return isDead;
}
public void setIsDead(boolean isDead) {
this.isDead = isDead;
}
public String toString() {
return "ID:" + id + "、" + type + "、" + color + "、" + sex + "、" + price + "购买、" + buyDate + "入园";
}
}
public class ZooManager {
private ArrayList<Animal> animalList;
public ZooManager() {
this.animalList = new ArrayList<Animal>();
}
public void addAnimal(Animal animal) {
if (getAnimalById(animal.getId()) == null) {
animalList.add(animal);
System.out.println("添加动物信息成功!");
} else {
System.out.println("添加动物信息失败,该ID已存在!");
}
}
public void removeAnimalById(int id) {
Animal animal = getAnimalById(id);
if (animal != null) {
animal.setIsDead(true);
System.out.println("注销动物信息成功!");
} else {
System.out.println("注销动物信息失败,该ID不存在!");
}
}
public ArrayList<Animal> getAllAnimals() {
ArrayList<Animal> result = new ArrayList<Animal>();
for (Animal animal : animalList) {
if (!animal.getIsDead()) {
result.add(animal);
}
}
return result;
}
public ArrayList<Animal> getAnimalsByColor(String color) {
ArrayList<Animal> result = new ArrayList<Animal>();
for (Animal animal : animalList) {
if (!animal.getIsDead() && animal.getColor().equals(color)) {
result.add(animal);
}
}
return result;
}
public int getAnimalCountByType(String type) {
int count = 0;
for (Animal animal : animalList) {
if (!animal.getIsDead() && animal.getType().equals(type)) {
count++;
}
}
return count;
}
public double getAnimalValueByType(String type) {
double value = 0.0;
for (Animal animal : animalList) {
if (!animal.getIsDead() && animal.getType().equals(type)) {
value += animal.getPrice();
}
}
return value;
}
public void updateAnimalById(int id, String type, String color, String sex, double price, String buyDate) {
Animal animal = getAnimalById(id);
if (animal != null) {
animalList.remove(animal);
animal = new Animal(id, type, color, sex, price, buyDate);
animalList.add(animal);
System.out.println("修改动物信息成功!");
} else {
System.out.println("修改动物信息失败,该ID不存在!");
}
}
private Animal getAnimalById(int id) {
for (Animal animal : animalList) {
if (animal.getId() == id) {
return animal;
}
}
return null;
}
public static void main(String[] args) {
ZooManager manager = new ZooManager();
Scanner scanner = new Scanner(System.in);
while (true) {
System.out.println("请选择操作:");
System.out.println("1、添加动物信息");
System.out.println("2、注销动物信息");
System.out.println("3、查询全部动物信息");
System.out.println("4、查询某种颜色的动物信息");
System.out.println("5、统计某种类型的动物数量");
System.out.println("6、统计某种类型的动物价值");
System.out.println("7、修改某个动物的基本信息");
System.out.println("8、退出");
int choice = scanner.nextInt();
switch (choice) {
case 1:
System.out.println("请输入动物信息(id、type、color、sex、price、buyDate):");
int id = scanner.nextInt();
String type = scanner.next();
String color = scanner.next();
String sex = scanner.next();
double price = scanner.nextDouble();
String buyDate = scanner.next();
manager.addAnimal(new Animal(id, type, color, sex, price, buyDate));
break;
case 2:
System.out.println("请输入要注销的动物的ID:");
int removeId = scanner.nextInt();
manager.removeAnimalById(removeId);
break;
case 3:
ArrayList<Animal> allAnimals = manager.getAllAnimals();
System.out.println("全部动物信息:");
for (Animal animal : allAnimals) {
System.out.println(animal);
}
break;
case 4:
System.out.println("请输入要查询的颜色:");
String queryColor = scanner.next();
ArrayList<Animal> colorAnimals = manager.getAnimalsByColor(queryColor);
System.out.println(queryColor + "颜色的动物信息:");
for (Animal animal : colorAnimals) {
System.out.println(animal);
}
break;
case 5:
System.out.println("请输入要统计的类型:");
String typeCount = scanner.next();
int count = manager.getAnimalCountByType(typeCount);
System.out.println(typeCount + "类型的动物数量:" + count);
break;
case 6:
System.out.println("请输入要统计的类型:");
String typeValue = scanner.next();
double value = manager.getAnimalValueByType(typeValue);
System.out.println(typeValue + "类型的动物价值:" + value);
break;
case 7:
System.out.println("请输入要修改的动物的ID以及新的信息(type、color、sex、price、buyDate):");
int updateId = scanner.nextInt();
String updateType = scanner.next();
String updateColor = scanner.next();
String updateSex = scanner.next();
double updatePrice = scanner.nextDouble();
String updateBuyDate = scanner.next();
manager.updateAnimalById(updateId, updateType, updateColor, updateSex, updatePrice, updateBuyDate);
break;
case 8:
System.out.println("退出程序");
System.exit(0);
break;
default:
System.out.println("请输入正确的操作编号!");
break;
}
}
}
}
```
这段代码实现了菜单程序,能够对动物园的动物进行管理。其中,Animal 类表示动物信息,ZooManager 类表示动物园管理器,实现了添加动物信息、注销动物信息、查询全部动物信息、查询某种颜色的动物信息、统计某种类型的动物数量、统计某种类型的动物价值、修改某个动物的基本信息等功能。在主函数中,通过循环显示菜单,根据用户输入的操作编号执行相应的操作。
相关推荐
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)