Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
package com.Anna_west2_work01.animalShop;

public abstract class Animal {
protected String name;
protected int age;
protected String gender;
protected double inPrice;
protected double outPrice;

public Animal() {}

public Animal(String name, int age, String gender, double inPrice, double outPrice) {
this.name = name;
this.age = age;
this.gender = gender;
this.inPrice = inPrice;
this.outPrice = outPrice;
}

public abstract String toString();

public String getName() {
return name;
}

public void setName(String name) {
this.name = name;
}

public int getAge() {
return age;
}

public void setAge(int age) {
this.age = age;
}

public String getGender() {
return gender;
}

public void setGender(String gender) {
this.gender = gender;
}

public double getInPrice() {
return inPrice;
}

public void setInPrice(double inPrice) {
this.inPrice = inPrice;
}

public double getOutPrice() {
return outPrice;
}

public void setOutPrice(double outPrice) {
this.outPrice = outPrice;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
package com.Anna_west2_work01.animalShop;

import com.Anna_west2_work01.animalShop.customer.Customer;
import com.Anna_west2_work01.animalShop.exception.InsufficientBalanceException;

public interface AnimalShop {
void buyAnimal(Animal animal) throws InsufficientBalanceException;

void serveCustomer(Customer customer, Animal animal) throws InsufficientBalanceException;

void close();

void open();
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,92 @@
package com.Anna_west2_work01.animalShop;

import com.Anna_west2_work01.animalShop.customer.Customer;
import com.Anna_west2_work01.animalShop.exception.AnimalNotFoundException;
import com.Anna_west2_work01.animalShop.exception.InsufficientBalanceException;

import java.time.LocalDateTime;
import java.util.ArrayList;

public class MyAnimalShop implements AnimalShop {
protected double balance;
protected ArrayList<Animal> AnimalList = new ArrayList<>();
protected ArrayList<Customer> CustomerList = new ArrayList<>();
protected boolean isOpen;
protected double todayNetProfit;
protected LocalDateTime lastCloseTime; // 新增:记录上次关店时间

public MyAnimalShop() {
this.lastCloseTime = LocalDateTime.now(); // 初始化关店时间
}

public MyAnimalShop(double balance, ArrayList<Animal> animalList, ArrayList<Customer> customerList, boolean isOpen, double todayNetProfit) {
this.balance = balance;
AnimalList = animalList;
CustomerList = customerList;
this.isOpen = isOpen;
this.todayNetProfit = todayNetProfit;
this.lastCloseTime = LocalDateTime.now();
}

public double getBalance() {
return balance;
}

public void buyAnimal(Animal animal) {
if(this.balance < animal.inPrice) {
throw new InsufficientBalanceException("余额不足以购买哦");
} else {
System.out.println("恭喜,已成功购买该宠物!");
System.out.println("该宠物的具体信息如下:");
System.out.println(animal.toString());
AnimalList.add(animal);
balance -= animal.inPrice;
todayNetProfit -= animal.inPrice;
}
}

public void serveCustomer(Customer customer, Animal animal) {
if(AnimalList.isEmpty()) {
throw new AnimalNotFoundException("");
}
if(!isOpen) {
System.out.println("抱歉哦,宠物店今日已打烊,请明日再光临!");
return;
}
CustomerList.add(customer);
System.out.println("恭喜,已成功售出该宠物!");
System.out.println("该宠物的具体信息如下:");
System.out.println(animal.toString());
AnimalList.remove(animal);
todayNetProfit += animal.outPrice;
balance += animal.outPrice;
}

public void open() {
this.isOpen = true;
System.out.println("开始今天的营业啦!~");
this.todayNetProfit = 0;
// 清空前一天顾客名单
CustomerList.clear();
}

public void close() {
LocalDateTime time = LocalDateTime.now();

if(time.getHour() < 9 || time.getHour() > 20) {
this.isOpen = false;
this.lastCloseTime = time; // 更新关店时间
System.out.println("结束今天的营业!~");
System.out.println("今天宠物店一共接待了" + CustomerList.size() + "位顾客哦");
System.out.println("以下为今日接待所有顾客的信息:");
for (int i = 0; i < CustomerList.size(); i++) {
System.out.println("第" + (1 + i) + "位顾客的信息为: " + CustomerList.get(i));
}
System.out.println("今天宠物商店营业的净利润为:" + todayNetProfit + "元!");
System.out.println("宠物商店的总余额为" + balance + "元");
System.out.println("辛苦啦,明天见!");
} else {
System.out.println("还没到歇业时间哦,请继续努力~");
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
package com.Anna_west2_work01.bonus;

import java.util.Scanner;

public class email {
private static boolean isEmail(String email){
if(email == null){
return false;
}
return email.matches("^[a-zA-Z0-9_-]+@[a-zA-Z0-9_-]+(\\.[a-zA-Z0-9_-]+)+$");
}
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.println("请输入一个邮箱:");
String email = sc.nextLine();
if(isEmail(email)){
System.out.println("邮箱格式正确!");
}else{
System.out.println("邮箱格式错误!");
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
package com.Anna_west2_work01.bonus;

import java.util.Scanner;

public class intThread {
// 控制线程交替进行的锁
private static final Object lock = new Object();
// 标记当前是哪个数组
private static int turn = 0;
private static int index1 = 0;
private static int index2 = 0;
public static void main(String[] args) {
// int[] arr1 = {1,3,5,7,9};
// int[] arr2 = {2,4,6,8,10};
int count = 5;
int[] arr1 = new int[count];
int[] arr2 = new int[count];
Scanner sc = new Scanner(System.in);
System.out.println("输入数组1的元素:");
for (int i = 0; i < arr1.length; i++) {
arr1[i] = sc.nextInt();
}
System.out.println("输入数组2的元素:");
for (int i = 0; i < arr2.length; i++) {
arr2[i] = sc.nextInt();
}
sc.close();
// 控制线程交替进行的锁

Thread t1 = new Thread(new Runnable() {
@Override
public void run() {
synchronized (lock){
while (index1 < arr1.length){
if(turn != 0){
// 不是该线程输出时
try {
lock.wait();
} catch (InterruptedException e) {
throw new RuntimeException(e);
}
}
System.out.print(arr1[index1++] + " ");
turn = 1;
lock.notifyAll();
}
}
}
});
Thread t2 = new Thread(new Runnable() {
@Override
public void run() {
synchronized (lock){
while (index2 < arr2.length){
if (turn != 1){
// 不是该线程输出
try {
lock.wait();
} catch (InterruptedException e) {
throw new RuntimeException(e);
}
}
System.out.print(arr2[index2++] + " ");
turn = 0;
lock.notifyAll();
}
}
}
});
t1.start();
t2.start();
}
}
71 changes: 71 additions & 0 deletions work1/anna2333/src/main/test/Test.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
import com.Anna_west2_work01.animalShop.Animal;
import com.Anna_west2_work01.animalShop.MyAnimalShop;
import com.Anna_west2_work01.animalShop.animals.Birds;
import com.Anna_west2_work01.animalShop.animals.CRDogs;
import com.Anna_west2_work01.animalShop.animals.Cats;
import com.Anna_west2_work01.animalShop.customer.Customer;

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

public class Test {
public static void main(String[] args) {
//初始化动物、顾客、商店基本信息
System.out.println("[Hint:正在尝试初始化您的宠物店……]");
ArrayList<Animal> AnimalList = new ArrayList<>();
Birds bird1 = new Birds("黄鹂", 1, "公", 50, 100);
Cats cat1 = new Cats("金渐层", 1, "母", 200, 400);
CRDogs dog1 = new CRDogs("土狗", 2, "公", 100, 180, true);
AnimalList.add(bird1);
AnimalList.add(cat1);
AnimalList.add(dog1);
ArrayList<Customer> CustomerList = new ArrayList<>();
Customer customer1 = new Customer("小陈", 3, LocalDate.now());
Customer customer2 = new Customer("小李", 5, LocalDate.now());
Customer customer3 = new Customer("小张", 2, LocalDate.now());
Customer customer4 = new Customer("小王", 1, LocalDate.now());
MyAnimalShop AnimalShop = new MyAnimalShop(10000.0, AnimalList, CustomerList, true, 0.0);

System.out.println("\n" + "[Hint:宠物店信息初始化成功!]");
showAnimalShopInformation(AnimalList, AnimalShop);

//测试购买动物功能
System.out.println("\n" + "[Hint:请为您的宠物商店购入一批宠物!]");
System.out.println("当前宠物店余额为" + AnimalShop.getBalance() + "元!");
System.out.println("当前有这些宠物可供购买:");
Birds bird2 = new Birds("老鹰", 1, "公", 50, 100);
Cats cat2 = new Cats("银渐层", 1, "公", 200, 400);
CRDogs dog2 = new CRDogs("哈士奇", 2, "公", 100, 180,true);
System.out.println("bird2——老鹰、1岁、公、50元");
System.out.println("cat2——银渐层、1岁、公、200元");
System.out.println("dog2——哈士奇、2岁、公、100元");
System.out.println("\n" + "[Hint:尝试购买三只动物中……]" + "\n");
AnimalShop.buyAnimal(bird2);
AnimalShop.buyAnimal(cat2);
AnimalShop.buyAnimal(dog2);
//输出采购宠物后宠物店现有的宠物
showAnimalShopInformation(AnimalList, AnimalShop);

//测试服务顾客功能
System.out.println("\n" + "[Hint:您的商店正在招待顾客!]");
System.out.println("\n" + "[Hint:尝试售出三只动物中……]" + "\n");
AnimalShop.serveCustomer(customer1, cat1);
AnimalShop.serveCustomer(customer2, bird2);
AnimalShop.serveCustomer(customer3, bird1);

//测试商店歇业功能
System.out.println("\n" + "[Hint:您的商店正尝试停止今日份营业中……]");
AnimalShop.close();
}

private static void showAnimalShopInformation(ArrayList<Animal> animalList, MyAnimalShop animalShop) {
System.out.println("- - - - - - - - - - - - - - - ");
System.out.println("当前宠物店有" + animalList.size() + "只宠物!");
for (int i = 0; i < animalList.size(); i++) {
System.out.println("第" + (i + 1) + "只小动物的信息为:");
System.out.println(animalList.get(i).toString());
}
System.out.println("当前宠物店余额为" + animalShop.getBalance() + "元!");
System.out.println("- - - - - - - - - - - - - - - ");
}
}
Loading