java实现电商平台用户数据模拟
时间: 2023-12-09 14:05:33 浏览: 61
import java.util.ArrayList;
import java.util.List;
public class UserDataGenerator {
public static void main(String[] args) {
List<User> users = generateUsers(100);
System.out.println(users);
}
/**
* 生成指定数量的用户数据
* @param count 用户数量
* @return 用户列表
*/
public static List<User> generateUsers(int count) {
List<User> users = new ArrayList<>();
for (int i = 0; i < count; i++) {
User user = new User();
user.setId(i + 1);
user.setUserName("user" + (i + 1));
user.setGender(i % 2 == 0 ? "男" : "女");
user.setAge((int) (Math.random() * 50 + 18));
user.setPhone("1" + (int) (Math.random() * 900000000 + 100000000));
user.setEmail("user" + (i + 1) + "@example.com");
user.setAddress("北京市海淀区上地信息路" + (int) (Math.random() * 100 + 1) + "号");
users.add(user);
}
return users;
}
}
class User {
private int id;
private String userName;
private String gender;
private int age;
private String phone;
private String email;
private String address;
// 省略 getter 和 setter 方法
@Override
public String toString() {
return "User{" +
"id=" + id +
", userName='" + userName + '\'' +
", gender='" + gender + '\'' +
", age=" + age +
", phone='" + phone + '\'' +
", email='" + email + '\'' +
", address='" + address + '\'' +
'}';
}
}
阅读全文