实现一个“CSDN 网站会员管理中心”,至少实现的功能有:用户登录、购买会员、会员 充值、会员权益查询、个人信息。用java实现给出具体能运行的代码
时间: 2024-06-08 21:06:10 浏览: 156
Java项目:基于SSM框架实现的会员积分管理系统【ssm+B/S架构+源码+数据库+毕业论文】
以下是一个基本的会员管理中心的 Java 代码示例。由于涉及到数据库操作,这里使用了 JDBC 进行数据持久化。
1. User.java
```
public class User {
private int id;
private String username;
private String password;
private String email;
private boolean isMember;
// getter 和 setter 方法省略
}
```
2. UserDao.java
```
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
public class UserDao {
private Connection conn;
public UserDao(Connection conn) {
this.conn = conn;
}
public User getUserByUsername(String username) throws SQLException {
String sql = "SELECT * FROM users WHERE username = ?";
PreparedStatement stmt = conn.prepareStatement(sql);
stmt.setString(1, username);
ResultSet rs = stmt.executeQuery();
if (rs.next()) {
User user = new User();
user.setId(rs.getInt("id"));
user.setUsername(rs.getString("username"));
user.setPassword(rs.getString("password"));
user.setEmail(rs.getString("email"));
user.setMember(rs.getBoolean("is_member"));
return user;
} else {
return null;
}
}
public void updateUserMembership(int userId, boolean isMember) throws SQLException {
String sql = "UPDATE users SET is_member = ? WHERE id = ?";
PreparedStatement stmt = conn.prepareStatement(sql);
stmt.setBoolean(1, isMember);
stmt.setInt(2, userId);
stmt.executeUpdate();
}
public void updateUserBalance(int userId, double amount) throws SQLException {
String sql = "UPDATE users SET balance = balance + ? WHERE id = ?";
PreparedStatement stmt = conn.prepareStatement(sql);
stmt.setDouble(1, amount);
stmt.setInt(2, userId);
stmt.executeUpdate();
}
}
```
3. MembershipService.java
```
import java.sql.Connection;
import java.sql.SQLException;
public class MembershipService {
private UserDao userDao;
public MembershipService(Connection conn) {
userDao = new UserDao(conn);
}
public boolean login(String username, String password) throws SQLException {
User user = userDao.getUserByUsername(username);
if (user != null && user.getPassword().equals(password)) {
return true;
} else {
return false;
}
}
public void purchaseMembership(int userId) throws SQLException {
userDao.updateUserMembership(userId, true);
}
public void rechargeBalance(int userId, double amount) throws SQLException {
userDao.updateUserBalance(userId, amount);
}
public void checkMembershipBenefits(int userId) throws SQLException {
User user = userDao.getUserById(userId);
if (user.isMember()) {
System.out.println("您是会员,可以享受以下权益:");
// TODO: 列出会员权益
} else {
System.out.println("您不是会员,无法查看会员权益。");
}
}
public void showPersonalInfo(int userId) throws SQLException {
User user = userDao.getUserById(userId);
System.out.println("用户名:" + user.getUsername());
System.out.println("邮箱:" + user.getEmail());
System.out.println("余额:" + user.getBalance());
}
}
```
4. 使用示例
```
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
try {
String url = "jdbc:mysql://localhost:3306/csdn";
String user = "root";
String password = "";
Connection conn = DriverManager.getConnection(url, user, password);
MembershipService service = new MembershipService(conn);
Scanner scanner = new Scanner(System.in);
System.out.print("请输入用户名:");
String username = scanner.nextLine();
System.out.print("请输入密码:");
String password = scanner.nextLine();
if (service.login(username, password)) {
System.out.println("登录成功!");
while (true) {
System.out.println("请选择要执行的操作:");
System.out.println("1. 购买会员");
System.out.println("2. 充值余额");
System.out.println("3. 查看会员权益");
System.out.println("4. 查看个人信息");
System.out.println("5. 退出");
int choice = scanner.nextInt();
switch (choice) {
case 1:
service.purchaseMembership(userId);
System.out.println("购买会员成功!");
break;
case 2:
System.out.print("请输入充值金额:");
double amount = scanner.nextDouble();
service.rechargeBalance(userId, amount);
System.out.println("充值成功!");
break;
case 3:
service.checkMembershipBenefits(userId);
break;
case 4:
service.showPersonalInfo(userId);
break;
case 5:
System.out.println("再见!");
System.exit(0);
default:
System.out.println("无效的选择,请重新输入。");
break;
}
}
} else {
System.out.println("用户名或密码错误!");
}
conn.close();
} catch (SQLException ex) {
ex.printStackTrace();
}
}
}
```
上述代码仅为示例,实际应用中可能还需要进行更多的输入验证和异常处理。
阅读全文