java代码写博客能实现管理员系统初始化数据和添加查询删除用户功能和普通用户登录和退出登录、关注的相关功能
时间: 2024-04-03 12:31:16 浏览: 86
java 实现博客的增删改查等功能
5星 · 资源好评率100%
实现管理员系统初始化数据和添加查询删除用户功能可以通过以下java代码实现:
```java
import java.util.ArrayList;
import java.util.List;
public class UserManagementSystem {
private List<User> users;
public UserManagementSystem() {
users = new ArrayList<>();
}
public void initializeData() {
users.add(new User("admin", "admin123", true));
users.add(new User("user1", "user123", false));
users.add(new User("user2", "user123", false));
users.add(new User("user3", "user123", false));
}
public void addUser(String username, String password, boolean isAdmin) {
users.add(new User(username, password, isAdmin));
}
public void deleteUser(String username) {
for (User user : users) {
if (user.getUsername().equals(username)) {
users.remove(user);
break;
}
}
}
public User getUser(String username) {
for (User user : users) {
if (user.getUsername().equals(username)) {
return user;
}
}
return null;
}
}
class User {
private String username;
private String password;
private boolean isAdmin;
public User(String username, String password, boolean isAdmin) {
this.username = username;
this.password = password;
this.isAdmin = isAdmin;
}
public String getUsername() {
return username;
}
public String getPassword() {
return password;
}
public boolean isAdmin() {
return isAdmin;
}
}
```
其中,UserManagementSystem 类为用户管理系统,包含了初始化数据、添加用户、删除用户和查询用户等方法,User 类为用户实体类,包含了用户名、密码和是否为管理员等属性。管理员用户的 isAdmin 属性为 true,普通用户的 isAdmin 属性为 false。
实现普通用户登录和退出登录、关注的相关功能可以通过以下java代码实现:
```java
import java.util.ArrayList;
import java.util.List;
public class UserManagementSystem {
private List<User> users;
public UserManagementSystem() {
users = new ArrayList<>();
}
public void initializeData() {
users.add(new User("admin", "admin123", true));
users.add(new User("user1", "user123", false));
users.add(new User("user2", "user123", false));
users.add(new User("user3", "user123", false));
}
public User login(String username, String password) {
for (User user : users) {
if (user.getUsername().equals(username) && user.getPassword().equals(password)) {
return user;
}
}
return null;
}
public void logout(User user) {
user.setLoggedIn(false);
}
public void follow(User user, User following) {
user.getFollowings().add(following);
}
public void unfollow(User user, User following) {
user.getFollowings().remove(following);
}
}
class User {
private String username;
private String password;
private boolean isAdmin;
private boolean isLoggedIn;
private List<User> followings;
public User(String username, String password, boolean isAdmin) {
this.username = username;
this.password = password;
this.isAdmin = isAdmin;
this.isLoggedIn = false;
this.followings = new ArrayList<>();
}
public String getUsername() {
return username;
}
public String getPassword() {
return password;
}
public boolean isAdmin() {
return isAdmin;
}
public boolean isLoggedIn() {
return isLoggedIn;
}
public void setLoggedIn(boolean loggedIn) {
isLoggedIn = loggedIn;
}
public List<User> getFollowings() {
return followings;
}
}
```
其中,UserManagementSystem 类新增了 login、logout、follow 和 unfollow 等方法,用于实现用户登录、退出登录和关注等功能。User 类新增了 isLoggedIn 和 followings 属性,用于表示用户是否已登录和用户关注的其他用户列表。关注操作可通过 follow 和 unfollow 方法实现,分别用于添加关注和取消关注。
阅读全文