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<Animal> animals = new ArrayList<>();

? ? public static void main(String[] args) {

? ? ? ? while (true) {

? ? ? ? ? ? System.out.println("=====動(dòng)物園管理系統(tǒng)=====");

? ? ? ? ? ? System.out.println("1.添加動(dòng)物信息");

? ? ? ? ? ? System.out.println("2.注銷一條動(dòng)物信息");

? ? ? ? ? ? System.out.println("3.查詢?nèi)縿?dòng)物信息");

? ? ? ? ? ? System.out.println("4.查詢某種顏色的動(dòng)物信息");

? ? ? ? ? ? System.out.println("5.統(tǒng)計(jì)某種類型的動(dòng)物信息");

? ? ? ? ? ? System.out.println("6.統(tǒng)計(jì)某種類型的動(dòng)物的價(jià)值");

? ? ? ? ? ? System.out.println("7.修改某個(gè)動(dòng)物的基本信息");

? ? ? ? ? ? System.out.println("8.退出");

? ? ? ? ? ? System.out.println("請(qǐng)輸入操作編號(hào):");

? ? ? ? ? ? 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("輸入無(wú)效,請(qǐng)重新輸入!");

? ? ? ? ? ? }

? ? ? ? }

? ? }

? ? private static void addAnimal() {

? ? ? ? System.out.println("請(qǐng)輸入動(dòng)物信息,格式為:ID、類型、顏色、性別、價(jià)格、入園日期(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("請(qǐng)輸入需要注銷的動(dòng)物的ID:");

? ? ? ? String id = scanner.next();

? ? ? ? int index = findIndexById(id);

? ? ? ? if (index == -1) {

? ? ? ? ? ? System.out.println("動(dòng)物不存在!");

? ? ? ? ? ? return;

? ? ? ? }

? ? ? ? Animal animal = animals.get(index);

? ? ? ? animal.setDead(true);

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

? ? }

? ? private static void queryAllAnimals() {

? ? ? ? System.out.println("=======所有動(dòng)物信息=======");

? ? ? ? for (Animal animal : animals) {

? ? ? ? ? ? if (!animal.isDead()) {

? ? ? ? ? ? ? ? System.out.println(animal);

? ? ? ? ? ? }

? ? ? ? }

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

? 繼續(xù)實(shí)現(xiàn)代碼如下:


? ? private static void queryAnimalsByColor() {

? ? ? ? System.out.println("請(qǐng)輸入需要查詢的動(dòng)物顏色:");

? ? ? ? String color = scanner.next();

? ? ? ? System.out.println("=======顏色為 " + color + " 的動(dòng)物=======");

? ? ? ? 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("請(qǐng)輸入需要統(tǒng)計(jì)的動(dòng)物類型:");

? ? ? ? String type = scanner.next();

? ? ? ? int count = 0;

? ? ? ? for (Animal animal : animals) {

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

? ? ? ? ? ? ? ? count++;

? ? ? ? ? ? }

? ? ? ? }

? ? ? ? System.out.println(type + " 的數(shù)量為:" + count);

? ? }


? ? private static void countPriceByType() {

? ? ? ? System.out.println("請(qǐng)輸入需要統(tǒng)計(jì)的動(dòng)物類型:");

? ? ? ? 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 + " 的總價(jià)值為:" + totalPrice);

? ? }


? ? private static void modifyAnimal() {

? ? ? ? System.out.println("請(qǐng)輸入需要修改的動(dòng)物的ID:");

? ? ? ? String id = scanner.next();

? ? ? ? int index = findIndexById(id);

? ? ? ? if (index == -1) {

? ? ? ? ? ? System.out.println("動(dòng)物不存在!");

? ? ? ? ? ? return;

? ? ? ? }

? ? ? ? Animal animal = animals.get(index);

? ? ? ? System.out.println("請(qǐng)輸入需要修改的項(xiàng)編號(hào):");

? ? ? ? System.out.println("1.類型");

? ? ? ? System.out.println("2.顏色");

? ? ? ? System.out.println("3.性別");

? ? ? ? System.out.println("4.價(jià)格");

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

? ? ? ? int choice = scanner.nextInt();

? ? ? ? switch (choice) {

? ? ? ? ? ? case 1:

? ? ? ? ? ? ? ? System.out.println("請(qǐng)輸入新的類型:");

? ? ? ? ? ? ? ? String type = scanner.next();

? ? ? ? ? ? ? ? animal.setType(type);

? ? ? ? ? ? ? ? break;

? ? ? ? ? ? case 2:

? ? ? ? ? ? ? ? System.out.println("請(qǐng)輸入新的顏色:");

? ? ? ? ? ? ? ? String color = scanner.next();

? ? ? ? ? ? ? ? animal.setColor(color);

? ? ? ? ? ? ? ? break;

? ? ? ? ? ? case 3:

? ? ? ? ? ? ? ? System.out.println("請(qǐng)輸入新的性別:");

? ? ? ? ? ? ? ? String sex = scanner.next();

? ? ? ? ? ? ? ? animal.setSex(sex);

? ? ? ? ? ? ? ? break;

? ? ? ? ? ? case 4:

? ? ? ? ? ? ? ? System.out.println("請(qǐng)輸入新的價(jià)格:");

? ? ? ? ? ? ? ? int price = scanner.nextInt();

? ? ? ? ? ? ? ? animal.setPrice(price);

? ? ? ? ? ? ? ? break;

? ? ? ? ? ? case 5:

? ? ? ? ? ? ? ? System.out.println("請(qǐng)輸入新的入園日期(yyyy-MM-dd):");

? ? ? ? ? ? ? ? String buyDate = scanner.next();

? ? ? ? ? ? ? ? animal.setDate(buyDate);

? ? ? ? ? ? ? ? break? ? ? ? ? ? default:

? ? ? ? ? ? ? ? System.out.println("輸入無(wú)效!");

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

? ? }

}

```

?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡(jiǎn)書系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容