写一个菜单程序,能够对动物园的动物进行管理动物类(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入园
时间: 2023-07-10 10:17:43 浏览: 43
以下是一个简单的菜单程序,实现了对动物园动物的管理,包括添加、注销、查询、统计、修改等功能:
```java
import java.util.*;
class Animal {
private int id;
private String type;
private String color;
private String sex;
private double price;
private Date buyDate;
private boolean isDead;
public Animal(int id, String type, String color, String sex, double price, Date 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 Date getBuyDate() {
return buyDate;
}
public boolean isDead() {
return isDead;
}
public void setDead(boolean isDead) {
this.isDead = isDead;
}
public String toString() {
return "ID:" + id + "、" + type + "、" + color + "、" + sex + "、" + price + "购买、" + buyDate + "入园";
}
}
public class ZooManager {
private Set<Animal> animals;
private Scanner scanner;
public ZooManager() {
animals = new HashSet<>();
scanner = new Scanner(System.in);
}
public void run() {
while (true) {
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:
addAnimal();
break;
case 2:
removeAnimal();
break;
case 3:
listAnimals();
break;
case 4:
listAnimalsByColor();
break;
case 5:
countAnimalsByType();
break;
case 6:
countAnimalsValueByType();
break;
case 7:
updateAnimal();
break;
case 8:
return;
default:
System.out.println("无效的选择,请重新输入");
break;
}
}
}
private void addAnimal() {
System.out.println("请输入动物ID:");
int id = scanner.nextInt();
if (findAnimalById(id) != null) {
System.out.println("该ID已经存在,请重新输入");
return;
}
System.out.println("请输入动物类型:");
String type = scanner.next();
System.out.println("请输入动物颜色:");
String color = scanner.next();
System.out.println("请输入动物性别:");
String sex = scanner.next();
System.out.println("请输入动物价格:");
double price = scanner.nextDouble();
System.out.println("请输入动物购买日期(格式:yyyy-MM-dd):");
String buyDateString = scanner.next();
Date buyDate = parseDate(buyDateString);
Animal animal = new Animal(id, type, color, sex, price, buyDate);
animals.add(animal);
System.out.println("动物信息添加成功");
}
private void removeAnimal() {
System.out.println("请输入动物ID:");
int id = scanner.nextInt();
Animal animal = findAnimalById(id);
if (animal == null) {
System.out.println("该ID不存在");
return;
}
animal.setDead(true);
System.out.println("动物信息注销成功");
}
private void listAnimals() {
System.out.println("所有动物信息如下:");
for (Animal animal : animals) {
if (!animal.isDead()) {
System.out.println(animal);
}
}
}
private void listAnimalsByColor() {
System.out.println("请输入颜色:");
String color = scanner.next();
System.out.println("颜色为" + color + "的动物信息如下:");
for (Animal animal : animals) {
if (!animal.isDead() && animal.getColor().equals(color)) {
System.out.println(animal);
}
}
}
private void countAnimalsByType() {
System.out.println("请输入类型:");
String type = scanner.next();
int count = 0;
for (Animal animal : animals) {
if (!animal.isDead() && animal.getType().equals(type)) {
count++;
}
}
System.out.println("类型为" + type + "的动物数量为:" + count);
}
private void countAnimalsValueByType() {
System.out.println("请输入类型:");
String type = scanner.next();
double value = 0;
for (Animal animal : animals) {
if (!animal.isDead() && animal.getType().equals(type)) {
value += animal.getPrice();
}
}
System.out.println("类型为" + type + "的动物价值为:" + value);
}
private void updateAnimal() {
System.out.println("请输入动物ID:");
int id = scanner.nextInt();
Animal animal = findAnimalById(id);
if (animal == null) {
System.out.println("该ID不存在");
return;
}
System.out.println("请输入动物类型(原值为" + animal.getType() + "):");
String type = scanner.next();
System.out.println("请输入动物颜色(原值为" + animal.getColor() + "):");
String color = scanner.next();
System.out.println("请输入动物性别(原值为" + animal.getSex() + "):");
String sex = scanner.next();
System.out.println("请输入动物价格(原值为" + animal.getPrice() + "):");
double price = scanner.nextDouble();
System.out.println("请输入动物购买日期(原值为" + animal.getBuyDate() + ",格式:yyyy-MM-dd):");
String buyDateString = scanner.next();
Date buyDate = parseDate(buyDateString);
animal = new Animal(id, type, color, sex, price, buyDate);
animals.remove(animal);
animals.add(animal);
System.out.println("动物信息修改成功");
}
private Animal findAnimalById(int id) {
for (Animal animal : animals) {
if (animal.getId() == id) {
return animal;
}
}
return null;
}
private Date parseDate(String dateString) {
try {
return new SimpleDateFormat("yyyy-MM-dd").parse(dateString);
} catch (ParseException e) {
System.out.println("日期格式错误");
return null;
}
}
public static void main(String[] args) {
ZooManager manager = new ZooManager();
manager.run();
}
}
```
其中,Animal类表示动物信息,包括id、type、color、sex、price、buyDate和isDead属性。ZooManager类是主程序,包括了菜单和各种操作函数,使用HashSet来存储动物信息。注销动物信息是通过将isDead属性设置为true来实现的。其他操作都比较直观,可以直接看代码实现。
相关推荐
![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)