java动物园信息


import java.text.SimpleDateFormat;
import java.util.*;
public class ajsj {
    private List<Animal> animalList = new ArrayList<>();
       public void start() {
        Scanner scanner = new Scanner(System.in);
        while (true) {
            System.out.println("欢迎来到动物园管理系统,请输入您的选择:");
            System.out.println("1.添加动物信息  2.注销动物信息  3.查询所有动物信息  4.查询某种颜色的动物信息" +
                    "  5.统计某种类型的动物信息  6.统计某种类型的动物价值  7.修改某个动物的基本信息  8.退出");
            int choice = scanner.nextInt();
            switch (choice) {
                case 1:
                    addAnimal(scanner);
                    break;
                case 2:
                    deleteAnimal(scanner);
                    break;
                case 3:
                    queryAllAnimal();
                    break;
                case 4:
                    queryAnimalByColor(scanner);
                    break;
                case 5:
                    countAnimalByType(scanner);
                    break;
                case 6:
                    countAnimalValueByType(scanner);
                    break;
                case 7:
                    modifyAnimal(scanner);
                    break;
                case 8:
                    System.out.println("感谢使用动物园管理系统,再见!");
                    System.exit(0);
                default:
                    System.out.println("输入有误,请重新输入!");
            }
        }
    }

    /**
     * 添加动物信息
     */
    private void addAnimal(Scanner scanner) {
        Animal animal = new Animal();
        // 输入ID,如果已经存在则重新输入
        while (true) {
            System.out.println("请输入动物的ID:");
            int id = scanner.nextInt();
            if (!isIdExist(id)) {
                animal.setId(id);
                break;
            } else {
                System.out.println("该ID已经存在,请重新输入!");
            }
        }
        scanner.nextLine(); // 处理nextInt()后的回车符
        // 输入其他属性
        System.out.println("请输入动物的类型:");
        animal.setType(scanner.nextLine());
        System.out.println("请输入动物的颜色:");
        animal.setColor(scanner.nextLine());
        System.out.println("请输入动物的性别:");
        animal.setSex(scanner.nextLine());
        System.out.println("请输入动物的价格:");
        animal.setPrice(scanner.nextDouble());
        scanner.nextLine(); // 处理nextDouble()后的回车符
        System.out.println("请输入动物的购买日期(格式yyyy-MM-dd):");
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
        Date buyDate = null;
        try {
            buyDate = sdf.parse(scanner.nextLine());
        } catch (Exception e) {
            e.printStackTrace();
        }
        animal.setBuyDate(buyDate);
        // 添加到List中
        animalList.add(animal);
        System.out.println("添加成功!");
    }

    /**
     * 判断ID是否已存在
     */
    private boolean isIdExist(int id) {
        for (Animal animal : animalList) {
            if (animal.getId() == id) {
                return true;
            }
        }
        return false;
    }

    /**
     * 删除动物信息
     */
    private void deleteAnimal(Scanner scanner) {
        System.out.println("请选择删除方式:1.按ID删除  2.按类型删除");
        int choice = scanner.nextInt();
        switch (choice) {
            case 1:
                deleteAnimalById(scanner);
                break;
            case 2:
                deleteAnimalByType(scanner);
                break;
            default:
                System.out.println("输入有误,请重新选择!");
        }
    }

    /**
     * 按ID删除动物信息
     */
    private void deleteAnimalById(Scanner scanner) {
        System.out.println("请输入要删除的动物ID:");
        int id = scanner.nextInt();
        boolean isDeleted = false;
        for (Animal animal : animalList) {
            if (animal.getId() == id && !animal.getIsDead()) {
                animal.setIsDead(true); // 标记为死亡
                isDeleted = true;
                break;
            }
        }
        if (isDeleted) {
            System.out.println("删除成功!");
        } else {
            System.out.println("没有找到该ID对应的动物或该动物已被注销!");
        }
    }

    /**
     * 按类型删除动物信息
     */
    private void deleteAnimalByType(Scanner scanner) {
        System.out.println("请输入要删除的动物类型:");
        String type = scanner.next();
        boolean isDeleted = false;
        for (Animal animal : animalList) {
            if (animal.getType().equals(type) && !animal.getIsDead()) {
                animal.setIsDead(true); // 标记为死亡
                isDeleted = true;
            }
        }
        if (isDeleted) {
            System.out.println("删除成功!");
        } else {
            System.out.println("没有找到该类型对应的动物或所有该类型的动物都已被注销!");
        }
    }

    /**
     * 查询所有动物信息
     */
    private void queryAllAnimal() {
        System.out.println("所有动物信息如下:");
        System.out.println("ID\t类型\t颜色\t性别\t价格\t购买日期");
        for (Animal animal : animalList) {
            if (!animal.getIsDead()) { // 判断是否已死亡
                System.out.println(animal.getId() + "\t" + animal.getType() + "\t" + animal.getColor() + "\t" +
                        animal.getSex() + "\t" + animal.getPrice() + "\t" + new SimpleDateFormat("yyyy-MM-dd").format(animal.getBuyDate()));
            }
        }
    }

    /**
     * 查询某种颜色的动物信息
     */
    private void queryAnimalByColor(Scanner scanner) {
        System.out.println("请输入要查询动物的颜色:");
        String color = scanner.next();
        System.out.println("颜色为" + color + "的动物信息如下:");
        System.out.println("ID\t类型\t颜色\t性别\t价格\t购买日期");
        for (Animal animal : animalList) {
            if (animal.getColor().equals(color) && !animal.getIsDead()) { // 判断是否已死亡
                System.out.println(animal.getId() + "\t" + animal.getType() + "\t" + animal.getColor() + "\t" +
                        animal.getSex() + "\t" + animal.getPrice() + "\t" + new SimpleDateFormat("yyyy-MM-dd").format(animal.getBuyDate()));
            }
        }
    }

    /**
     * 统计某种类型的动物数量
     */
    private void countAnimalByType(Scanner scanner) {
        System.out.println("请输入要统计动物的类型:");
        String type = scanner.next();
        int count = 0;
        for (Animal animal : animalList) {
            if (animal.getType().equals(type) && !animal.getIsDead()) { // 判断是否已死亡
                count++;
            }
        }
        System.out.println("类型为" + type + "的动物数量为" + count + "只。");
    }

    /**
     * 统计某种类型的动物价值
     */
    private void countAnimalValueByType(Scanner scanner) {
        System.out.println("请输入要统计动物的类型:");
        String type = scanner.next();
        double value = 0;
        for (Animal animal : animalList) {
            if (animal.getType().equals(type) && !animal.getIsDead()) { // 判断是否已死亡
                value += animal.getPrice();
            }
        }
        System.out.println("类型为" + type + "的动物总价值为" + value + "元。");
    }

    /**
     * 修改动物信息
     */
    private void modifyAnimal(Scanner scanner) {
        System.out.println("请输入要修改的动物ID:");
        int id = scanner.nextInt();
        boolean isModified = false;
        for (Animal animal : animalList) {
            if (animal.getId() == id && !animal.getIsDead()) { // 判断是否已死亡
                System.out.println("请输入要修改的属性:1.类型 2.颜色 3.性别 4.价格 5.购买日期");
                int choice = scanner.nextInt();
                scanner.nextLine(); // 处理nextInt()后的回车符
                switch (choice) {
                    case 1:
                        System.out.println("请输入新的类型:");
                        animal.setType(scanner.nextLine());
                        isModified = true;
                        break;
                    case 2:
                        System.out.println("请输入新的颜色:");
                        animal.setColor(scanner.nextLine());
                        isModified = true;
                        break;
                    case 3:
                        System.out.println("请输入新的性别:");
                        animal.setSex(scanner.nextLine());
                        isModified = true;
                        break;
                    case 4:
                        System.out.println("请输入新的价格:");
                        animal.setPrice(scanner.nextDouble());
                        scanner.nextLine(); // 处理nextDouble()后的回车符
                        isModified = true;
                        break;
                    case 5:
                        System.out.println("请输入新的购买日期(格式yyyy-MM-dd):");
                        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
                        Date buyDate = null;
                        try {
                            buyDate = sdf.parse(scanner.nextLine());
                        } catch (Exception e) {
                            e.printStackTrace();
                        }
                        animal.setBuyDate(buyDate);
                        isModified = true;
                        break;
                    default:
                        System.out.println("输入有误,请重新选择!");
                }
                break;
            }
        }
        if (isModified) {
            System.out.println("修改成功!");
        } else {
            System.out.println("没有找到该ID对应的动物或该动物已被注销!");
        }
    }

    public static void main(String[] args) {
        ajsj zoo = new ajsj();
        zoo.start(); // 启动动物园管理系统
    }
}

/**
 * 动物类
 */
class Animal {
    private int id; // ID
    private String type; // 类型
    private String color; // 颜色
    private String sex; // 性别
    private double price; // 价格
    private Date buyDate; // 购买日期
    private boolean isDead; // 是否死亡

    public Animal() {
        this.isDead = false; // 默认未死亡
    }

    // getter和setter方法
    // ...

    public int getId() {
        return id;
    }

    public void setId(int id) {
        this.id = id;
    }

    public String getType() {
        return type;
    }

    public void setType(String type) {
        this.type = type;
    }

    public String getColor() {
        return color;
    }

    public void setColor(String color) {
        this.color = color;
    }

    public String getSex() {
        return sex;
    }

    public void setSex(String sex) {
        this.sex = sex;
    }

    public double getPrice() {
        return price;
    }

    public void setPrice(double price) {
        this.price = price;
    }

    public Date getBuyDate() {
        return buyDate;
    }

    public void setBuyDate(Date buyDate) {
        this.buyDate = buyDate;
    }

    public boolean getIsDead() {
        return isDead;
    }

    public void setIsDead(boolean isDead) {
        this.isDead = isDead;
    }
}

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值