Animal类

import java.time.LocalDate;

public class Customer {
private String name;
private int visitCount;
private LocalDate latestVisitDate;

public Customer(String name, int visitCount, LocalDate latestVisitDate) {
this.name = name;
this.visitCount = visitCount;
this.latestVisitDate = latestVisitDate;
}

@Override
public String toString() {
return "顾客 [name=" + name + ", visitCount=" + visitCount + ", latestVisitDate=" + latestVisitDate + "]";
}
}

AnimalNotFoundException

import java.time.LocalDate;

public class Customer {
private String name;
private int visitCount;
private LocalDate latestVisitDate;

public Customer(String name, int visitCount, LocalDate latestVisitDate) {
this.name = name;
this.visitCount = visitCount;
this.latestVisitDate = latestVisitDate;
}

@Override
public String toString() {
return "顾客 [name=" + name + ", visitCount=" + visitCount + ", latestVisitDate=" + latestVisitDate + "]";
}
}

AnimalShop

import java.time.LocalDate;

public interface AnimalShop {
void buyAnimal(Animal animal) throws InsufficientBalanceException;
void serveCustomer(Customer customer);
void closeShop(LocalDate date);
}

Cat

public class Cat extends Animal {
public Cat(String name, int age, String gender) {
super(name, age, gender, 200.0);
}

@Override
public String toString() {
return "猫猫 [name=" + name + ", age=" + age + ", gender=" + gender + ", price=" + price + "]";
}
}

ChineseRualDog

public class ChineseRuralDog extends Animal {
private boolean isVaccineInjected;

public ChineseRuralDog(String name, int age, String gender, boolean isVaccineInjected) {
super(name, age, gender, 100.0); // 价格100元
this.isVaccineInjected = isVaccineInjected;
}

@Override
public String toString() {
return "中华田园犬 [name=" + name + ", age=" + age + ", gender=" + gender +
", isVaccineInjected=" + isVaccineInjected + ", price=" + price + "]";
}
}

Customer

import java.time.LocalDate;

public class Customer {
private String name;
private int visitCount;
private LocalDate latestVisitDate;

public Customer(String name, int visitCount, LocalDate latestVisitDate) {
this.name = name;
this.visitCount = visitCount;
this.latestVisitDate = latestVisitDate;
}

@Override
public String toString() {
return "顾客 [name=" + name + ", visitCount=" + visitCount + ", latestVisitDate=" + latestVisitDate + "]";
}
}

InsufficientBalanceException

public class InsufficientBalanceException extends Exception{
public InsufficientBalanceException(String message){
super(message);
}
}

MyAnimalShop

import java.time.LocalDate;
import java.util.ArrayList;
import java.util.List;

public class MyAnimalShop implements AnimalShop {
private double balance;
private List<Animal> animalList;
private List<Customer> customerList;
private boolean isOpen;

public MyAnimalShop(double initialBalance) {
this.balance = initialBalance;
this.animalList = new ArrayList<>();
this.customerList = new ArrayList<>();
this.isOpen = true;
}

@Override
public void buyAnimal(Animal animal) throws InsufficientBalanceException {
if (!isOpen) {
throw new IllegalStateException("商店已歇业!");
}
if (balance < animal.price) {
throw new InsufficientBalanceException("余额不足,无法购买此动物!");
}
animalList.add(animal);
balance -= animal.price;
}

@Override
public void serveCustomer(Customer customer) {
if (!isOpen) {
throw new IllegalStateException("商店已歇业!");
}
customerList.add(customer);

if (animalList.isEmpty()) {
throw new AnimalNotFoundException("店内没有动物可供购买!");
}
Animal animal = animalList.remove(0); // 出售第一只动物
balance += animal.price;
}

@Override
public void closeShop(LocalDate date) {
if (!isOpen) {
throw new IllegalStateException("商店已歇业!");
}
isOpen = false;
System.out.println("店内客户信息:");
for (Customer customer : customerList) {
System.out.println(customer);
}
System.out.println("今天的利润是: " + balance);
customerList.clear(); // 清空客户列表
}
}

Rabbit

public class Rabbit extends Animal {
public Rabbit(String name, int age, String gender) {
super(name, age, gender, 150.0);
}

@Override
public String toString() {
return "兔子 [name=" + name + ", age=" + age + ", gender=" + gender + ", price=" + price + "]";
}

}

Test

import java.time.LocalDate;

public class Test {
public static void main(String[] args) {
try {
MyAnimalShop shop = new MyAnimalShop(500.0);

// 买入动物
shop.buyAnimal(new ChineseRuralDog("旺财", 3, "男", true));
shop.buyAnimal(new Cat("喵喵", 2, "女"));
shop.buyAnimal(new Rabbit("小兔", 1, "中"));

// 招待客户
shop.serveCustomer(new Customer("张三", 1, LocalDate.now()));
shop.serveCustomer(new Customer("李四", 2, LocalDate.now()));

// 关闭商店
shop.closeShop(LocalDate.now());

} catch (Exception e) {
e.printStackTrace();
}
}
}