宠物管理系统(1):Bean设计
byd学校期末搞实训,做一个宠物管理系统,没有数据库,纯靠文件存储数据,并且还要保证数据的一致性,十分麻烦,但是还是做出来了。先介绍一下系统中bean的设计:
User类
package com.wzb.bean;
import java.io.Serializable;
import java.time.LocalDate;
import java.time.LocalDateTime;
import java.util.ArrayList;
public class User implements Serializable {
// id作为用户的唯一标识符
private final Integer id;
// 用户名
private final String username;
// 密码
private String password;
// 手机号
private String phoneNumber;
// 用户的账户余额
private Integer money;
// 用户的地址
private String address;
// 用户状态
private Boolean status;
// 用户持有的宠物
private ArrayList<Integer> petList;
// 用户的购物车
private ArrayList<Integer> shopCar;
// 用户的订单表
private ArrayList<Order> orderList;
// 注册时间
private LocalDate registerDate;
// 修改时间
private LocalDateTime updateTime;
// 用户注册凭证:手机号注册
public User(Integer id, String username, String password, String phoneNumber) {
this.id = id;
this.username = username;
this.password = password;
this.phoneNumber = phoneNumber;
// 用户账户默认没钱
this.money = 0;
// 用户状态默认可用
this.status = true;
// 用户默认无宠物
this.petList = new ArrayList<>();
// 用户购物车
this.shopCar = new ArrayList<>();
// 用户订单表
this.orderList = new ArrayList<>();
// 注册时间
this.registerDate = LocalDate.now();
// 修改时间
this.updateTime = LocalDateTime.now();
}
// 创建管理员用户
public User(Integer id, String username, String password, String phoneNumber, int money) {
this.id = id;
this.username = username;
this.password = password;
this.phoneNumber = phoneNumber;
this.money = money;
this.address = "重庆文理学院wzb";
this.status = true;
this.petList = new ArrayList<>();
petList.add(0);
// 用户购物车
this.shopCar = new ArrayList<>();
// 用户订单表
this.orderList = new ArrayList<>();
// 注册时间
this.registerDate = LocalDate.now();
// 修改时间
this.updateTime = LocalDateTime.now();
}
public Integer getId() {
return id;
}
public String getUsername() {
return username;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
public Integer getMoney() {
return money;
}
public void setMoney(Integer money) {
this.money = money;
}
public String getPhoneNumber() {
return phoneNumber;
}
public void setPhoneNumber(String phoneNumber) {
this.phoneNumber = phoneNumber;
}
public String getAddress() {
return address;
}
public void setAddress(String address) {
this.address = address;
}
public Boolean getStatus() {
return status;
}
public void setStatus(Boolean status) {
this.status = status;
}
public ArrayList<Integer> getPetList() {
return petList;
}
public void setPetList(ArrayList<Integer> petList) {
this.petList = petList;
}
public ArrayList<Integer> getShopCar() {
return shopCar;
}
public void setShopCar(ArrayList<Integer> shopCar) {
this.shopCar = shopCar;
}
public ArrayList<Order> getOrderList() {
return orderList;
}
public void setOrderList(ArrayList<Order> orderList) {
this.orderList = orderList;
}
public LocalDate getRegisterDate() {
return registerDate;
}
public void setRegisterDate(LocalDate registerDate) {
this.registerDate = registerDate;
}
public LocalDateTime getUpdateTime() {
return updateTime;
}
public void setUpdateTime(LocalDateTime updateTime) {
this.updateTime = updateTime;
}
public void addMoney(Integer money) {
this.money += money;
}
@Override
public String toString() {
return "User{" +
"id=" + id +
", username='" + username + '\'' +
", password='" + password + '\'' +
", phoneNumber='" + phoneNumber + '\'' +
", money=" + money +
", address='" + address + '\'' +
", status=" + status +
", petList=" + petList +
", shopCar=" + shopCar +
", orderList=" + orderList +
", registerDate=" + registerDate +
", updateTime=" + updateTime +
'}';
}
public void showOrders() {
System.out.println("当前用户" + username + "的订单内容为");
for (int i = 0; i < orderList.size(); i++) {
System.out.println("这是第" + (i + 1) + "张订单");
System.out.println(orderList.get(i));
}
}
}
Pet类
package com.wzb.bean;
import java.io.Serializable;
public class Pet implements Serializable {
private final Integer id;
private String name;
private Integer age;
private Double weight;
private String food;
private Integer price;
private Integer brand;
// 通过构造器传入 ID,而不是通过静态初始化获取
public Pet(Integer id, String name, Integer age, Double weight, String food, Integer price, Integer brand) {
this.id = id;
this.name = name;
this.age = age;
this.weight = weight;
this.food = food;
this.price = price;
this.brand = brand;
}
// Getter 和 Setter 方法
public Integer getId() {
return id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public Integer getAge() {
return age;
}
public void setAge(Integer age) {
this.age = age;
}
public Double getWeight() {
return weight;
}
public void setWeight(Double weight) {
this.weight = weight;
}
public String getFood() {
return food;
}
public void setFood(String food) {
this.food = food;
}
public Integer getPrice() {
return price;
}
public void setPrice(Integer price) {
this.price = price;
}
public Integer getBrand() {
return brand;
}
public void setBrand(Integer brand) {
this.brand = brand;
}
@Override
public String toString() {
return "Pet{" +
"id=" + id +
", name='" + name + '\'' +
", age=" + age +
", weight=" + weight +
", food='" + food + '\'' +
", price=" + price +
'}';
}
}
Order类
package com.wzb.bean;
import com.wzb.service.PetService;
import com.wzb.service.impl.PetServiceImpl;
import java.io.Serializable;
import java.util.ArrayList;
public class Order implements Serializable {
private static final PetService petService = new PetServiceImpl();
private final ArrayList<Integer> pets;
private final Integer sum;
public Order(ArrayList<Integer> pets, Integer sum) {
this.pets = pets;
this.sum = sum;
}
public ArrayList<Integer> getPets() {
return pets;
}
@Override
public String toString() {
String result = "{Order: Pets{";
for (Integer id : pets) {
result = result + "{";
result += petService.getById(id);
result += "}";
}
result += "}, ";
result += "Sum{" + sum + "}}";
return result;
}
}
Cat类和Dog类
package com.wzb.bean;
public class Cat extends Pet{
public Cat(Integer id, String name, Integer age, Double weight, String food, Integer price, Integer brand) {
super(id, name, age, weight, food, price, brand);
}
public void catchMouse() {
System.out.println("小猫" + this.getName() + "能够抓老鼠");
}
}
package com.wzb.bean;
public class Dog extends Pet{
public Dog(Integer id, String name, Integer age, Double weight, String food, Integer price, Integer brand) {
super(id, name, age, weight, food, price, brand);
}
public void fight() {
System.out.println("小狗" + this.getName() + "能够看家");
}
}