网吧管理系统中用户实现会员管理,充值,登陆注册代码java
时间: 2023-08-24 22:03:57 浏览: 42
以下是一个简单的网吧管理系统中实现会员管理、充值、登录和注册的Java代码示例:
```
import java.util.Scanner;
public class NetBar {
static Scanner input = new Scanner(System.in);
static String[] usernames = new String[100]; // 假设最多100个用户
static String[] passwords = new String[100];
static double[] balances = new double[100];
static int userCount = 0; // 用户数量
public static void main(String[] args) {
System.out.println("欢迎使用网吧管理系统!");
while (true) {
System.out.println("-----------------------");
System.out.println("请选择您要进行的操作:");
System.out.println("1. 登录");
System.out.println("2. 注册");
System.out.println("3. 退出系统");
int choice = input.nextInt();
switch (choice) {
case 1:
login();
break;
case 2:
register();
break;
case 3:
System.out.println("谢谢使用,再见!");
System.exit(0);
break;
default:
System.out.println("输入有误,请重新选择!");
}
}
}
// 登录方法
public static void login() {
System.out.println("请输入用户名:");
String username = input.next();
System.out.println("请输入密码:");
String password = input.next();
for (int i = 0; i < userCount; i++) {
if (usernames[i].equals(username) && passwords[i].equals(password)) {
System.out.println("登录成功!欢迎您," + username + "。");
showMenu(i);
return;
}
}
System.out.println("登录失败,请检查用户名和密码是否正确!");
}
// 注册方法
public static void register() {
System.out.println("请输入用户名:");
String username = input.next();
System.out.println("请输入密码:");
String password = input.next();
usernames[userCount] = username;
passwords[userCount] = password;
balances[userCount] = 0.0; // 初始余额为0
userCount++;
System.out.println("注册成功!请登录。");
}
// 显示菜单方法
public static void showMenu(int index) {
while (true) {
System.out.println("-----------------------");
System.out.println("请选择您要进行的操作:");
System.out.println("1. 查询余额");
System.out.println("2. 充值");
System.out.println("3. 修改密码");
System.out.println("4. 退出登录");
int choice = input.nextInt();
switch (choice) {
case 1:
System.out.println("当前余额为:" + balances[index]);
break;
case 2:
System.out.println("请输入充值金额:");
double amount = input.nextDouble();
balances[index] += amount;
System.out.println("充值成功!当前余额:" + balances[index]);
break;
case 3:
System.out.println("请输入新密码:");
String newPassword = input.next();
passwords[index] = newPassword;
System.out.println("密码修改成功!");
break;
case 4:
System.out.println("退出登录成功!");
return;
default:
System.out.println("输入有误,请重新选择!");
}
}
}
}
```
在这个示例代码中,我们使用了一个静态的usernames、passwords和balances数组来存储用户的信息,使用userCount变量来记录用户数量。在程序运行时,我们首先会进入一个while循环来不断接受用户的选择。当用户选择1时,会进入login()方法来进行登录操作。当用户选择2时,会进入register()方法来进行注册操作。当用户选择3时,程序会退出。在login()方法中,我们使用一个for循环来遍历所有用户,如果找到了与输入的用户名和密码匹配的用户,就会进入showMenu()方法来显示菜单。在showMenu()方法中,我们使用一个while循环来不断接受用户的选择。当用户选择1时,会输出当前余额。当用户选择2时,会要求用户输入充值金额,并将充值金额加入余额中。当用户选择3时,会要求用户输入新密码,并将新密码更新到密码数组中。当用户选择4时,会退出登录。