使用Java写一个(一)系统功能说明 1、 用户管理系统主要功能:普通用户和管理员用户功能。 2、 普通用户:可以查看全部用户信息,按用户年龄查看个人信息、修改自己的信息;可以尝试增加按照名字查看个人信息。 3、 管理员:删除用户、增加用户、修改用户、按ID查看用户、查看全部信息。可以尝试增加按照名字查看个人信息。 4、 用户的实体类的特征:用户ID编号、用户名、用户密码、家庭地址、年龄、电话等 (二)实现说明: 1.定义User类,用户ID编号、用户名、用户密码、家庭地址、年龄、电话、是否管理员。定义有参和无参的两个构造方法,定义toString()方法方便打印对象信息。 2.系统中定义UserDAO类,定义密码错误次数属性成员;定义以下成员方法:查看全部用户信息、按用户年龄查看个人信息、按名字查看用户信息、按照ID查看信息、根据用户名以及密码修改用户的信息、根据用户编号修改信息、删除用户、增加用户、根据用户名和密码登陆的功能。 3、模拟数据存储使用类Memoery中的userlist数组实现。该类中定义长度为100的User类型的静态数据成员,用于存放数据信息;定义用于描述数组中元素个数的静态数据成员;为了模拟实现数据存储,定义不含参数的构造函数,实现通过定义若干用户对象初始化数组成员中的一部分,其余成员也初始化为user对象,仅为对象是否管理员赋值为false。 4.定义Test类,在main方法中实现功能操作。要注意能循环操作,即输入用户名和密码一次,能进行多种功能。使用循环语句实现。
时间: 2024-01-01 11:05:29 浏览: 79
好的,以下是Java实现:
User类:
```java
public class User {
private int id;
private String name;
private String password;
private String address;
private int age;
private String phone;
private boolean isAdmin;
public User() {
this.id = -1;
this.name = "";
this.password = "";
this.address = "";
this.age = -1;
this.phone = "";
this.isAdmin = false;
}
public User(int id, String name, String password, String address, int age, String phone, boolean isAdmin) {
this.id = id;
this.name = name;
this.password = password;
this.address = address;
this.age = age;
this.phone = phone;
this.isAdmin = isAdmin;
}
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
public String getAddress() {
return address;
}
public void setAddress(String address) {
this.address = address;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
public String getPhone() {
return phone;
}
public void setPhone(String phone) {
this.phone = phone;
}
public boolean isAdmin() {
return isAdmin;
}
public void setAdmin(boolean isAdmin) {
this.isAdmin = isAdmin;
}
@Override
public String toString() {
return "User{" +
"id=" + id +
", name='" + name + '\'' +
", password='" + password + '\'' +
", address='" + address + '\'' +
", age=" + age +
", phone='" + phone + '\'' +
", isAdmin=" + isAdmin +
'}';
}
}
```
UserDAO类:
```java
public class UserDAO {
private static int passwordErrorCount = 0;
private static int elementCount = 0;
private static final int MAX_SIZE = 100;
private static User[] userList = new User[MAX_SIZE];
static {
userList[0] = new User(0, "admin", "admin", "北京市", 30, "123456789", true);
userList[1] = new User(1, "user1", "123456", "上海市", 20, "987654321", false);
userList[2] = new User(2, "user2", "123456", "广州市", 25, "123123123", false);
elementCount = 3;
}
public static void showAllUsers() {
for (int i = 0; i < elementCount; i++) {
System.out.println(userList[i]);
}
}
public static void showUserByAge(int age) {
for (int i = 0; i < elementCount; i++) {
if (userList[i].getAge() == age) {
System.out.println(userList[i]);
}
}
}
public static void showUserByName(String name) {
for (int i = 0; i < elementCount; i++) {
if (userList[i].getName().equals(name)) {
System.out.println(userList[i]);
}
}
}
public static void showUserById(int id) {
for (int i = 0; i < elementCount; i++) {
if (userList[i].getId() == id) {
System.out.println(userList[i]);
return;
}
}
System.out.println("User not found.");
}
public static boolean login(String name, String password) {
for (int i = 0; i < elementCount; i++) {
if (userList[i].getName().equals(name) && userList[i].getPassword().equals(password)) {
passwordErrorCount = 0;
return true;
}
}
passwordErrorCount++;
System.out.println("Wrong username or password. You have " + (3 - passwordErrorCount) + " chances left.");
return false;
}
public static void modifyUserByNameAndPassword(String name, String password, String address, int age, String phone) {
for (int i = 0; i < elementCount; i++) {
if (userList[i].getName().equals(name) && userList[i].getPassword().equals(password)) {
userList[i].setAddress(address);
userList[i].setAge(age);
userList[i].setPhone(phone);
System.out.println("User info modified successfully.");
return;
}
}
System.out.println("Wrong username or password.");
}
public static void modifyUserById(int id, String name, String password, String address, int age, String phone) {
for (int i = 0; i < elementCount; i++) {
if (userList[i].getId() == id) {
userList[i].setName(name);
userList[i].setPassword(password);
userList[i].setAddress(address);
userList[i].setAge(age);
userList[i].setPhone(phone);
System.out.println("User info modified successfully.");
return;
}
}
System.out.println("User not found.");
}
public static void deleteUserById(int id) {
for (int i = 0; i < elementCount; i++) {
if (userList[i].getId() == id) {
for (int j = i; j < elementCount - 1; j++) {
userList[j] = userList[j + 1];
}
userList[elementCount - 1] = null;
elementCount--;
System.out.println("User deleted successfully.");
return;
}
}
System.out.println("User not found.");
}
public static void addUser(String name, String password, String address, int age, String phone) {
if (elementCount == MAX_SIZE) {
System.out.println("User list is full, cannot add more users.");
return;
}
userList[elementCount] = new User(elementCount, name, password, address, age, phone, false);
elementCount++;
System.out.println("User added successfully.");
}
}
```
Memoery类:
```java
public class Memoery {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
String name = "";
String password = "";
while (true) {
System.out.print("Please enter your name: ");
name = scanner.nextLine();
System.out.print("Please enter your password: ");
password = scanner.nextLine();
if (UserDAO.login(name, password)) {
break;
}
if (UserDAO.passwordErrorCount == 3) {
System.out.println("Too many password errors, program exits.");
return;
}
}
while (true) {
System.out.println("Please select the function you want to use:");
System.out.println("1. Show all users");
System.out.println("2. Show user by age");
System.out.println("3. Show user by name");
System.out.println("4. Show user by ID");
System.out.println("5. Modify user info by name and password");
System.out.println("6. Modify user info by ID");
System.out.println("7. Delete user by ID");
System.out.println("8. Add user");
System.out.println("0. Exit");
System.out.print("Your choice: ");
int choice = scanner.nextInt();
scanner.nextLine();
switch (choice) {
case 1:
UserDAO.showAllUsers();
break;
case 2:
System.out.print("Please enter the age: ");
int age = scanner.nextInt();
scanner.nextLine();
UserDAO.showUserByAge(age);
break;
case 3:
System.out.print("Please enter the name: ");
String userName = scanner.nextLine();
UserDAO.showUserByName(userName);
break;
case 4:
System.out.print("Please enter the ID: ");
int id = scanner.nextInt();
scanner.nextLine();
UserDAO.showUserById(id);
break;
case 5:
System.out.print("Please enter the address: ");
String address = scanner.nextLine();
System.out.print("Please enter the age: ");
int userAge = scanner.nextInt();
scanner.nextLine();
System.out.print("Please enter the phone: ");
String phone = scanner.nextLine();
UserDAO.modifyUserByNameAndPassword(name, password, address, userAge, phone);
break;
case 6:
System.out.print("Please enter the ID: ");
int userId = scanner.nextInt();
scanner.nextLine();
System.out.print("Please enter the name: ");
String userName2 = scanner.nextLine();
System.out.print("Please enter the password: ");
阅读全文