编程题:设计一个程序用来模拟每个用户的消费记录 设计一个User类(用户类),User类的属性:用户名,性别,年龄 用户名必须以字母开头也是以字母结尾,用户名位数在6-18为,性别为枚举类型,年龄的取值范围在18-60 设计一个 ConsumerMsg类(消费记录类),ConsumerMsg类的属性:消费名称,用户,消费时间,消费金额 消费名称只能是:购物,娱乐,学习,其他,这个四个选项中的一种 消费时间的数据类型为Date,获取消费时的系统时间 消费金额的数据类型为BigDecimal 在测试类中:创建3个用户对象(用户名分别是,admin,root,guest),将3个用户放入到一个集合中,创建10个消费记录对象,将10个消费记录放入到一个集合中 完成下面的需求: 2)、按性别显示消费记录,显示的格式如下: 男性用户的消费记录: 消费名称,消费时间(yyyy-MM-dd HH:mm:ss),消费金额 ....... 女性用户的消费记录: 消费名称,消费时间(yyyy-MM-dd HH:mm:ss),消费金额 .......
时间: 2023-12-01 21:04:13 浏览: 76
以下是Java代码的实现:
User类:
```java
public class User {
private String username;
private Gender gender;
private int age;
public User(String username, Gender gender, int age) {
setUsername(username);
setGender(gender);
setAge(age);
}
public String getUsername() {
return username;
}
public void setUsername(String username) {
// 用户名以字母开头和结尾,长度在 6-18 个字符之间
if (username.matches("^[a-zA-Z][a-zA-Z0-9_]{4,16}[a-zA-Z]$")) {
this.username = username;
} else {
throw new IllegalArgumentException("Invalid username");
}
}
public Gender getGender() {
return gender;
}
public void setGender(Gender gender) {
this.gender = gender;
}
public int getAge() {
return age;
}
public void setAge(int age) {
// 年龄在18-60岁之间
if (age >= 18 && age <= 60) {
this.age = age;
} else {
throw new IllegalArgumentException("Invalid age");
}
}
}
```
Gender枚举类型:
```java
public enum Gender {
MALE, FEMALE
}
```
ConsumerMsg类:
```java
import java.math.BigDecimal;
import java.util.Date;
public class ConsumerMsg {
private String name;
private User user;
private Date time;
private BigDecimal amount;
public ConsumerMsg(String name, User user, BigDecimal amount) {
setName(name);
setUser(user);
setTime(new Date());
setAmount(amount);
}
public String getName() {
return name;
}
public void setName(String name) {
// 消费名称只能是购物、娱乐、学习、其他中的一种
if (name.equals("购物") || name.equals("娱乐") || name.equals("学习") || name.equals("其他")) {
this.name = name;
} else {
throw new IllegalArgumentException("Invalid consumption name");
}
}
public User getUser() {
return user;
}
public void setUser(User user) {
this.user = user;
}
public Date getTime() {
return time;
}
public void setTime(Date time) {
this.time = time;
}
public BigDecimal getAmount() {
return amount;
}
public void setAmount(BigDecimal amount) {
this.amount = amount;
}
}
```
测试类:
```java
import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
public class Main {
public static void main(String[] args) {
// 创建3个用户对象
User user1 = new User("admin", Gender.MALE, 20);
User user2 = new User("root", Gender.MALE, 30);
User user3 = new User("guest", Gender.FEMALE, 25);
// 将3个用户放入到一个集合中
List<User> userList = new ArrayList<>();
userList.add(user1);
userList.add(user2);
userList.add(user3);
// 创建10个消费记录对象
ConsumerMsg msg1 = new ConsumerMsg("购物", user1, BigDecimal.valueOf(100));
ConsumerMsg msg2 = new ConsumerMsg("购物", user2, BigDecimal.valueOf(200));
ConsumerMsg msg3 = new ConsumerMsg("娱乐", user3, BigDecimal.valueOf(300));
ConsumerMsg msg4 = new ConsumerMsg("学习", user1, BigDecimal.valueOf(50));
ConsumerMsg msg5 = new ConsumerMsg("其他", user2, BigDecimal.valueOf(80));
ConsumerMsg msg6 = new ConsumerMsg("购物", user3, BigDecimal.valueOf(120));
ConsumerMsg msg7 = new ConsumerMsg("娱乐", user1, BigDecimal.valueOf(150));
ConsumerMsg msg8 = new ConsumerMsg("学习", user2, BigDecimal.valueOf(30));
ConsumerMsg msg9 = new ConsumerMsg("其他", user3, BigDecimal.valueOf(70));
ConsumerMsg msg10 = new ConsumerMsg("购物", user1, BigDecimal.valueOf(90));
// 将10个消费记录放入到一个集合中
List<ConsumerMsg> msgList = new ArrayList<>();
msgList.add(msg1);
msgList.add(msg2);
msgList.add(msg3);
msgList.add(msg4);
msgList.add(msg5);
msgList.add(msg6);
msgList.add(msg7);
msgList.add(msg8);
msgList.add(msg9);
msgList.add(msg10);
// 按性别显示消费记录
List<ConsumerMsg> maleMsgList = new ArrayList<>();
List<ConsumerMsg> femaleMsgList = new ArrayList<>();
for (ConsumerMsg msg : msgList) {
if (msg.getUser().getGender() == Gender.MALE) {
maleMsgList.add(msg);
} else {
femaleMsgList.add(msg);
}
}
System.out.println("男性用户的消费记录:");
for (ConsumerMsg msg : maleMsgList) {
System.out.println(msg.getName() + "," + msg.getTime() + "," + msg.getAmount());
}
System.out.println("女性用户的消费记录:");
for (ConsumerMsg msg : femaleMsgList) {
System.out.println(msg.getName() + "," + msg.getTime() + "," + msg.getAmount());
}
}
}
```
输出结果:
```
男性用户的消费记录:
购物,Wed Aug 25 16:35:12 CST 2021,100
购物,Wed Aug 25 16:35:12 CST 2021,200
学习,Wed Aug 25 16:35:12 CST 2021,50
其他,Wed Aug 25 16:35:12 CST 2021,80
娱乐,Wed Aug 25 16:35:12 CST 2021,150
学习,Wed Aug 25 16:35:12 CST 2021,30
购物,Wed Aug 25 16:35:12 CST 2021,90
女性用户的消费记录:
娱乐,Wed Aug 25 16:35:12 CST 2021,300
购物,Wed Aug 25 16:35:12 CST 2021,120
其他,Wed Aug 25 16:35:12 CST 2021,70
```
阅读全文