java - 尝试从子菜单 Java 返回主菜单时出现问题
问题描述
在我的银行应用程序中返回主菜单时遇到问题。我创建了一个名为 menu 的类,该类具有适用于我的应用程序和驱动程序的功能,其中包含运行程序的主要方法。我给出了退出条件boolean gobackmainMenu
(在类范围内声明),但是一旦我为注销选项输入 0 作为我的选择,程序就会完全退出并且不会返回主菜单。这是我正在使用的方法的代码片段。runMenu()
在驱动程序(主函数)中调用。
public void runMenu() {
printHeader();
while(!exit) {
printMenuMM();
int choice= getInputMainMenu();
loginRegister(choice);
}
}
将菜单打印到控制台
private void printMenuMM() {
System.out.println("\nPlease Make A Selection");
System.out.println("1)Register");
System.out.println("2)Login");
System.out.println("0)Exit");
}
private void printMenuCusAccM() {
System.out.println("\nPlease Make A Selection");
System.out.println("1) Apply for an account");
System.out.println("2) Deposit in account");
System.out.println("3) Withdraw from an account");
System.out.println("4) Transfer Funds");
System.out.println("0) Logout");
}
用于登录和注册账户
private void loginRegister(int choice) {
Scanner s = new Scanner(System.in);
UserService userService = new UserService();
LoginService loginService = new LoginService();
AccountService accService = new AccountService();
AuthenticationService aService= new AuthenticationService();
String username;
String password;
String spassword;
String role1;
String email;
Role role;
String address;
String fullname;
boolean enumTypeExists;
//double amount;
IntUserDAO userDao = new UserDAO();
IAccountDAO accDao = new AccountDAO();
List<User> allUsers =userDao.findAll();
List<Account>allAccounts=accDao.findAll();
switch (choice) {
case 0:
exit=true;
break;
case 1: //Registers a user
System.out.println("Enter full name: ");
fullname= s.nextLine();
System.out.println("Enter email: ");
email= s.nextLine();
System.out.println("Enter username: ");
username= s.nextLine();
boolean a=userService.usernameExists(username);
if(a==true) {
do {
System.out.println("Username already exists enter in different username");
System.out.println("Enter username: ");
username=s.nextLine();
a=userService.usernameExists(username);
}while(a);
}
System.out.println("Enter password: ");
password= s.nextLine();
System.out.println("Enter in account type: ");
role1=s.nextLine();
boolean j=aService.enumTypeExists(role1);
if(j==false) {
do {
System.out.println("Not a valid account type");
System.out.println("Enter in account type : ");
role1=s.nextLine();
j=aService.enumTypeExists(role1);
}while(!j);
}
role= Role.valueOf(role1);
System.out.println("Enter address: ");
address= s.nextLine();
temp = userService.register(username, password, role, email,fullname, address);
break;
case 2:// login a user but do some input validation
System.out.println("Enter login Username: ");
username =s.nextLine();
temp=userDao.findByUsername(username);
boolean k= aService.usernameAuthentication(username);
if(k==false) {
do {
System.out.println("Username does not exist enter in a valid username");
System.out.println("Enter username: ");
username=s.nextLine();
k=aService.usernameAuthentication(username);
}while(!k);
}
System.out.println("Enter login Password: ");
password=s.nextLine();
boolean m= aService.passwordAuthentication(username,password);
if(m==false) {
do {
System.out.println("Password is not associated with user");
System.out.println("Enter password: ");
password=s.nextLine();
m=aService.passwordAuthentication(username,password);
}while(!m);
}
boolean c=userService.login(username, password);
while(c) {
if(temp.getRole().toString()=="Customer") {
while(!gobackmainMenu) {
printMenuCusAccM();
int choice2= getInputCusAccM();
operonCusAcc(choice2);
}
}
if(temp.getRole().toString()=="Employee") {
printMenuEmpAccM();
while(!gobackmainMenu) {
int choice3= getInputEmpAccM();
operonEmpAcc(choice3);
}
}
if(temp.getRole().toString()=="Admin") {
printMenuAdmAccM();
while(!gobackmainMenu) {
int choice4= getInputAdmAccM();
}
}
}
break;
default:
System.out.println("An unknown error has occured");
}
}
用于在选项之间切换
private int getInputMainMenu() {
Scanner s=new Scanner(System.in);
int choice=-1;
while(choice<0 || choice >2) {
try {
System.out.print("\nEnter your choice: ");
choice=Integer.parseInt(s.nextLine());
}catch(NumberFormatException e){
System.out.println("Invalid selection. Please try again");
}
}
return choice;
}
private int getInputCusAccM() {
Scanner s=new Scanner(System.in);
int choice1=-1;
while(choice1<0 || choice1 >4) {
try {
System.out.print("\nEnter your choice: ");
choice1=Integer.parseInt(s.nextLine());
}catch(NumberFormatException e){
System.out.println("Invalid selection. Please try again");
}
}
return choice1;
}
预期产出
***************************************************
Welcome To The Banking
App
***************************************************
Please Make A Selection
1)Register
2)Login
0)Exit
Enter your choice: 2
Enter login Username:
k9
Enter login Password:
k9
Succesful login
Please Make A Selection
1) Apply for an account
2) Deposit in account
3) Withdraw from an account
4) Transfer Funds
0) Logout
Enter your choice: 0
Please Make A Selection
1)Register
2)Login
0)Exit
Enter your choice:
生成的输出
***************************************************
Welcome To The Banking
App
***************************************************
Please Make A Selection
1)Register
2)Login
0)Exit
Enter your choice: 2
Enter login Username:
k9
Enter login Password:
k9
Succesful login
Please Make A Selection
1) Apply for an account
2) Deposit in account
3) Withdraw from an account
4) Transfer Funds
0) Logout
Enter your choice: 0
Please Make A Selection
1)Register
2)Login
0)Exit
终止程序
解决方案
在 operonCusAcc() 或 operonEmpAcc() 方法中发生了一些事情。据我所知,管理员角色没有这样的方法。
如果您需要更多帮助,请分享更多代码。
应该发生这样的事情:
private void operonCusAcc(int choice2) {
switch (choice2) {
case 0: runMenu();
break;
}
}
推荐阅读
- reactjs - React App S3 托管,Url 参数在 S3 上不起作用
- python-3.x - NameError:名称'DatabaseManage'未在python中定义
- php - 在laravel的foreach循环中迭代元素
- latex - tcolorbox 与 tabular、tabularx 兼容吗?
- laravel - 部署中的 Laravel 和 vue js
- linux - 如何使用 awk 从多个文件中搜索字符串?
- python - PYTHON Pandas:在两个数据框中用字符串求和
- sql - 缺少的括号在哪里?
- recursion - 有人能解释一下这个递归吗?谢谢
- reactjs - 警告:React 无法识别 DOM 元素上的 `secondaryFill` 属性