2023-05-29

```java

import java.util.ArrayList;

import java.util.Scanner;

class Animal {

    private String id;

    private String type;

    private String color;

    private String sex;

    private int price;

    private String buyDate;

    private boolean isDead;

    public Animal(String id, String type, String color, String sex, int price, String buyDate) {

        this.id = id;

        this.type = type;

        this.color = color;

        this.sex = sex;

        this.price = price;

        this.buyDate = buyDate;

        this.isDead = false;

    }

    // getters and setters

    public String getId() {

        return id;

    }

    public void setId(String 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 int getPrice() {

        return price;

    }

    public void setPrice(int price) {

        this.price = price;

    }

    public String getBuyDate() {

        return buyDate;

    }

    public void setDate(String buyDate) {

        this.buyDate = buyDate;

    }

    public boolean isDead() {

        return isDead;

    }

    public void setDead(boolean dead) {

        isDead = dead;

    }

    @Override

    public String toString() {

        return "ID:" + id + "\t" + type + "\t" + color + "\t" + sex + "\t" + price + "\t" + buyDate;

    }

}

public class ZooManager {

    static Scanner scanner = new Scanner(System.in);

    static ArrayList animals = new ArrayList<>();

    public static void main(String[] args) {

        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.退出");

            System.out.println("请输入操作编号:");

            int choice = scanner.nextInt();

            switch (choice) {

                case 1:

                    addAnimal();

                    break;

                case 2:

                    cancelAnimal();

                    break;

                case 3:

                    queryAllAnimals();

                    break;

                case 4:

                    queryAnimalsByColor();

                    break;

                case 5:

                    countAnimalsByType();

                    break;

                case 6:

                    countPriceByType();

                    break;

                case 7:

                    modifyAnimal();

                    break;

                case 8:

                    System.out.println("谢谢使用,再见!");

                    return;

                default:

                    System.out.println("输入无效,请重新输入!");

            }

        }

    }

    private static void addAnimal() {

        System.out.println("请输入动物信息,格式为:ID、类型、颜色、性别、价格、入园日期(yyyy-MM-dd):");

        String id = scanner.next();

        if (isIdExist(id)) {

            System.out.println("该ID已存在!");

            return;

        }

        String type = scanner.next();

        String color = scanner.next();

        String sex = scanner.next();

        int price = scanner.nextInt();

        String buyDate = scanner.next();

        Animal animal = new Animal(id, type, color, sex, price, buyDate);

animals.add(animal);

        System.out.println("添加成功!");

    }

    private static void cancelAnimal() {

        System.out.println("请输入需要注销的动物的ID:");

        String id = scanner.next();

        int index = findIndexById(id);

        if (index == -1) {

            System.out.println("动物不存在!");

            return;

        }

        Animal animal = animals.get(index);

        animal.setDead(true);

        System.out.println("注销成功!");

    }

    private static void queryAllAnimals() {

        System.out.println("=======所有动物信息=======");

        for (Animal animal : animals) {

            if (!animal.isDead()) {

                System.out.println(animal);

            }

        }

        System.out.println("=========================");

  继续实现代码如下:


    private static void queryAnimalsByColor() {

        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);

            }

        }

        System.out.println("===========================");

    }


    private static 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 static void countPriceByType() {

        System.out.println("请输入需要统计的动物类型:");

        String type = scanner.next();

        int totalPrice = 0;

        for (Animal animal : animals) {

            if (!animal.isDead() && animal.getType().equals(type)) {

                totalPrice += animal.getPrice();

            }

        }

        System.out.println(type + " 的总价值为:" + totalPrice);

    }


    private static void modifyAnimal() {

        System.out.println("请输入需要修改的动物的ID:");

        String id = scanner.next();

        int index = findIndexById(id);

        if (index == -1) {

            System.out.println("动物不存在!");

            return;

        }

        Animal animal = animals.get(index);

        System.out.println("请输入需要修改的项编号:");

        System.out.println("1.类型");

        System.out.println("2.颜色");

        System.out.println("3.性别");

        System.out.println("4.价格");

        System.out.println("5.入园日期");

        int choice = scanner.nextInt();

        switch (choice) {

            case 1:

                System.out.println("请输入新的类型:");

                String type = scanner.next();

                animal.setType(type);

                break;

            case 2:

                System.out.println("请输入新的颜色:");

                String color = scanner.next();

                animal.setColor(color);

                break;

            case 3:

                System.out.println("请输入新的性别:");

                String sex = scanner.next();

                animal.setSex(sex);

                break;

            case 4:

                System.out.println("请输入新的价格:");

                int price = scanner.nextInt();

                animal.setPrice(price);

                break;

            case 5:

                System.out.println("请输入新的入园日期(yyyy-MM-dd):");

                String buyDate = scanner.next();

                animal.setDate(buyDate);

                break            default:

                System.out.println("输入无效!");

                break;

        }

        System.out.println("修改成功!");

    }


    private static boolean isIdExist(String id) {

        for (Animal animal : animals) {

            if (animal.getId().equals(id)) {

                return true;

            }

        }

        return false;

    }


    private static int findIndexById(String id) {

        for (int i = 0; i < animals.size(); i++) {

            if (animals.get(i).getId().equals(id)) {

                return i;

            }

        }

        return -1;

    }

}

```

你可能感兴趣的:(2023-05-29)