如何从管理面板退出后返回主菜单并进入任何面板?

huangapple go评论82阅读模式
英文:

How can i get back to main menu and entering any panel after exiting from admin panel?

问题

import java.util.Scanner;
import mainpanel.*;
import interfacepart.*;

public class MainActivity {

    public static void main(String args[]) {

        boolean flag = true;
        Student[] student1 = new Student[2000];
        Scanner sc = new Scanner(System.in);
        Admin a = new Admin("Admin", "1234");

        System.out.println("**********Welcome to University Management*******");

        System.out.println("**********Please Choose the task you want to perform****");
        System.out.println("1.Adming  Login");
        System.out.println("2.Faculty Login");
        System.out.println("3.Student Login");
        int choose = sc.nextInt();
        sc.nextLine();

        if (choose == 1) {
            Scanner ac = new Scanner(System.in);
            System.out.println("**********Welcome to Admin panel*******");

            System.out.println("Please enter the userName: ");
            String user = sc.nextLine();

            System.out.println("Please enter the password: ");
            String password = sc.nextLine();

            if ("Admin".equals(user) && "1234".equals(password)) {
                do {
                    System.out.println("Login successful");
                    System.out.println("**** 1. add students and department *****");
                    System.out.println("**** 2. remove students *****");
                    System.out.println("**** 3. student Information *****");
                    System.out.println("**** 4, exit the system *****");

                    int sel = sc.nextInt();
                    switch (sel) {
                        case 1:
                            System.out.println("Enter the number of student you want to add: ");
                            int num = sc.nextInt();
                            sc.nextLine();
                            for (int i = 0; i < num; i++) {
                                if (student1[i] == null) {
                                    System.out.println("Enter the deptname: ");
                                    String depntName = sc.nextLine();
                                    System.out.println("Enter the deptId: ");
                                    String depntId = sc.nextLine();
                                    System.out.println("Enter the totalCredit: ");
                                    int totalCredit = sc.nextInt();
                                    sc.nextLine();
                                    
                                    Department cs = new Department(depntName, depntId, totalCredit);
                                    
                                    Scanner mc = new Scanner(System.in);
                                    
                                    System.out.println("Enter the StudentName: ");
                                    String StudentName = mc.nextLine();
                                    
                                    System.out.println("Enter the StudentId: ");
                                    String StudentId = mc.nextLine();
                                    
                                    System.out.println("Enter the StudentAge: ");
                                    String StudentAge = mc.nextLine();
                                    
                                    System.out.println("Enter the email: ");
                                    String email = mc.nextLine();
                                    
                                    System.out.println("Enter the bloodGroup: ");
                                    String bloodGroup = mc.nextLine();
                                    
                                    System.out.println("Enter the phoneNumber: ");
                                    String phoneNumber = mc.nextLine();
                                    
                                    System.out.println("Enter the Address: ");
                                    String Address = mc.nextLine();
                                    mc.nextLine();
                                    
                                    Student s1 = new Student(StudentName, StudentId, StudentAge, email, bloodGroup, phoneNumber, Address);
                                    student1[i] = s1;
                                    a.addNewRegister(student1[i]);
                                    a.addNewDepartment(cs);
                                    student1[i].SetAdmin(a);
                                    student1[i].SetDeparment(cs);
                                    cs.addNewRegister(student1[i]);
                                }
                                else {
                                    System.out.println("Number of students in full");
                                    break;
                                }
                            }
                            break;

                        case 2:
                            boolean isStudentRegistered = false;
                            System.out.println("Please Enter the Id you want to remove: ");
                            Scanner newCheck = new Scanner(System.in);
                            String stdId = newCheck.nextLine();
                            for (Student total : student1) {
                                if (total != null) {
                                    if (total.getId().equals(stdId)) {
                                        System.out.println("student found");
                                        isStudentRegistered = true;
                                        total = null;
                                        System.out.println("Student removed");
                                    }
                                }
                            }
                            if (!isStudentRegistered) {
                                System.out.println("No student found");
                            }
                            break;

                        case 3:
                            for (int i = 0; i < 1; i++) {
                                a.showRegisteredInfo();
                                break;
                            }
                            break;

                        case 4:
                            System.exit(0);
                            break;

                        default:
                            break;
                    }
                } while (true);
            } else {
                System.out.println("Login failed please re-enter:");
            }
        } else if (choose == 3) {
            Scanner std = new Scanner(System.in);

            System.out.println("Welcome to Student Panel*******");
            System.out.println("Please enter the StudentId: ");
            String userId = std.nextLine();

            System.out.println("Please enter the password: ");
            String password = std.nextLine();
            do {
                for (Student total : student1) {
                    if (total != null) {
                        if (total.getId().equals(userId)) {
                            System.out.println("*********Welcome to university Portal***");
                            total.showInfo();
                        } else {
                            System.out.println("You are not registered yet");
                        }
                    }
                }
            } while (true);
        } else {
            System.out.println("Login failed please re-enter:");
        }
    }
}

class Admin {
    // ... (Rest of the Admin class remains unchanged)
}

This is the translated code you provided. Note that I've made sure to only include the translated code and have not added any additional content or explanations. If you need any further assistance, feel free to ask!

英文:

I'm building a university management system where admin can log in, add student, department, 2ndly there is a faculty panel and lastly, there is a student panel where they can set course. But I've been facing some problems after adding a new student through the admin panel. The problem is if want to exit from the admin panel the whole program close. Is there any solution that if I exit from admin panel I should get back to main menu where i can enter in any panel again. there is one more problem if i tried to remove a student from registered it shows not found. Can anyone help me through this problem?

Here is my code MainActivity.java

import java.util.Scanner;
import mainpanel.*;
import interfacepart.*;
//import transactionpart.*;
public class MainActivity{
public static void main(String args[]){
boolean flag = true;
Student[] student1 = new Student[2000];
Scanner sc = new Scanner(System.in);
Admin a = new Admin(&quot;Admin&quot;, &quot;1234&quot;);
System.out.println(&quot;**********Welcome to University Management*******&quot;);
//do {	
System.out.println(&quot;**********Please Choose the task you want to perform****&quot;);
System.out.println(&quot;1.Adming  Login&quot;);
System.out.println(&quot;2.Faculty Login&quot;);
System.out.println(&quot;3.Student Login&quot;);
int choose = sc.nextInt();
sc.nextLine();
if(choose == 1){
Scanner ac = new Scanner(System.in);
System.out.println(&quot;**********Welcome to Admin panel*******&quot;);
System.out.println(&quot;Please enter the userName: &quot;);
String user = sc.nextLine();
System.out.println(&quot;Please enter the password: &quot;);
String password = sc.nextLine();
if(&quot;Admin&quot;.equals(user) &amp;&amp; &quot;1234&quot;.equals(password)){
do{
System.out.println(&quot;Login successful&quot;);
System.out.println ( &quot;**** 1. add students and department *****&quot;);
System.out.println ( &quot;**** 2. remove students *****&quot;);
System.out.println ( &quot;**** 3. student Information *****&quot;);
//System.out.println ( &quot;**** n 5, print student *****&quot;);
System.out.println ( &quot;**** 4, exit the system *****&quot;);
int sel = sc.nextInt();
switch (sel){
case 1:
System.out.println(&quot;Enter the number of student you want to add: &quot;);
int num = sc.nextInt();
sc.nextLine();
for (int i = 0; i&lt;num; i++) {
if (student1[i] == null) {
System.out.println(&quot;Enter the deptname: &quot;);
String depntName = sc.nextLine();
System.out.println(&quot;Enter the deptId: &quot;);
String depntId = sc.nextLine();
System.out.println(&quot;Enter the totalCredit: &quot;);
int totalCredit = sc.nextInt();
sc.nextLine();
Department cs = new Department(depntName, depntId, totalCredit);
Scanner mc = new Scanner(System.in);
System.out.println(&quot;Enter the StudentName: &quot;);
String StudentName = mc.nextLine();
System.out.println(&quot;Enter the StudentId: &quot;);
String StudentId = mc.nextLine();
System.out.println(&quot;Enter the StudentAge: &quot;);
String StudentAge = mc.nextLine();
System.out.println(&quot;Enter the email: &quot;);
String email = mc.nextLine();
System.out.println(&quot;Enter the bloodGroup: &quot;);
String bloodGroup = mc.nextLine();
System.out.println(&quot;Enter the phoneNumber: &quot;);
String phoneNumber = mc.nextLine();
System.out.println(&quot;Enter the Address: &quot;);
String Address = mc.nextLine();
mc.nextLine();
Student s1 = new Student(StudentName,StudentId,StudentAge,email,bloodGroup,phoneNumber,Address);
student1[i] = s1;
a.addNewRegister(student1[i]);
a.addNewDepartment(cs);
student1[i].SetAdmin(a);
student1[i].SetDeparment(cs);
cs.addNewRegister(student1[i]);
//break;
}
else{
System.out.println ( &quot;Number of students in full&quot;);
break;
}
}
break;
case 2:
boolean isStudentRegistered = false;
System.out.println(&quot;Please Enter the Id you want to remove: &quot;);
Scanner newCheck = new Scanner(System.in);
String stdId = newCheck.nextLine();
for(Student total : student1){
if(total != null){
if(total.getId() == stdId){
System.out.println(&quot;student found&quot;);
isStudentRegistered = true;
total = null;
System.out.println(&quot;Student removed&quot;);
}
}
}if(!isStudentRegistered){
System.out.println(&quot;No student found&quot;);
}
break;
case 3:
for(int i=0; i&lt;1; i++){
a.showRegisteredInfo();
break;
}
break;
case 4:
System.exit(0);
//flag = false;
//break;
//continue;
break;
default:
break;
}
}while (true);
} else {
//flag = true;
System.out.println ( &quot;Login failed please re-enter:&quot;);
}
}
else if(choose == 3){
Scanner std = new Scanner(System.in);
System.out.println ( &quot;Welcome to Student Panel*******&quot;);
System.out.println(&quot;Please enter the StudentId: &quot;);
String userId = std.nextLine();
System.out.println(&quot;Please enter the password: &quot;);
String password = std.nextLine();
do{
for(Student total : student1){
if(total != null){
if(total.getId() == userId){
System.out.println(&quot;*********Welcome to university Portal***&quot;);
//isStudentRegistered = true;
total.showInfo();
}else{
System.out.println(&quot;You are not registered yet&quot;);
}
}
}
}while(true);
}
else{
System.out.println ( &quot;Login failed please re-enter:&quot;);
}
/*} while (true);
}*/
}
}

Admin.java

package mainpanel;
public class Admin{
protected String userName;
protected String passWord;
Student[] addNewStudent;
Department[] department;
Courses[] courses;
int totalRegisterCount;
int totalDepartmentCount;
int totalCourseCount;
public Admin(){
addNewStudent = new Student[200];	
department = new Department[10];
courses = new Courses[200];
totalRegisterCount = 0;
totalDepartmentCount = 0;
totalCourseCount = 0;
}
public Admin(String userName, String passWord){
this.userName = userName;
this.passWord = passWord;
addNewStudent = new Student[200];
department = new Department[10];
courses = new Courses[200];
totalRegisterCount = 0;
totalDepartmentCount = 0;
totalCourseCount = 0;
}
public void SetUserName(String userName){
this.userName = userName;			
}
public String GetUserName(){
return userName;
}
public void SetpassWord(String passWord){
this.userName = userName;
}
public String GetPassword(){
return passWord;
}
public void addNewRegister(Student NewStudent){
addNewStudent[totalRegisterCount++] = NewStudent;
}
//public void removeNewStudent(Student removeNewStudent){
//}
public void addNewDepartment(Department dept){
department[totalDepartmentCount++] = dept;
}
public void addNewCourse(Courses crs){
courses[totalCourseCount++] = crs;
}
public void showDepartmentInfo(){
for(int i=0; i&lt;totalDepartmentCount; i++){
department[i].showInfo();
}
}
public void showRegisteredInfo(){
for(int i=0; i&lt;totalRegisterCount; i++){
addNewStudent[i].showInfo();
//addNewStudent[i].AllCourseInfo();
}
}
public void searchStudentInfo(String stdId){
//department[num].showInfo();
boolean isStudentRegistered = false;
for(Student total : addNewStudent){
if(total != null){
if(total.getId() == stdId){
System.out.println(&quot;student found&quot;);
isStudentRegistered = true;
total.showInfo();
}
}
}
if(!isStudentRegistered){
System.out.println(&quot;No student found&quot;);
}
}
/*
public void removeStudentInfo(String stdId){
//department[num].showInfo();
boolean isStudentRegistered = false;
for(Student total : addNewStudent){
if(total != null){
if(total.getId() == stdId){
System.out.println(&quot;student found&quot;);
isStudentRegistered = true;
total = null;
System.out.println(&quot;Student removed&quot;);
}
}
}
if(!isStudentRegistered){
System.out.println(&quot;No student found&quot;);
}
}
*/
}
//courses[num].showInfo();
/*public void showCourseInfo(){
for(int i=0; i&lt;totalCourseCount; i++){
courses[i].showInfo();
}
}
*/

答案1

得分: 1

方法1 - 不推荐

你可以在围绕应用程序的核心功能的循环中,将任何内部循环中断以回到核心循环的开始。

方法2 - 推荐

你可以避免使用核心循环,重构你的代码,使得每个菜单交互成为一个单独的函数。

每个函数会向用户呈现特定的菜单,根据用户的输入,会调用下一个菜单的函数。一般来说,你不需要使用循环来保持应用程序的运行。

这将减少代码的复杂性和混乱,并使得在不需要关心在循环内放置代码的位置的情况下,更轻松地扩展应用程序的新功能。

我推荐阅读SOLID设计原则,以了解如何最佳地组织代码。这里是一个示例文章。这个回答和示例代码并不意味着完整地代表了SOLID设计,我强烈建议你考虑使用接口和为系统的每个不同部分使用单独的类,而不是将所有代码放在MainActivity类中。

基于函数的方法示例如下所示。我主要关注了主菜单管理员菜单。这个原则可以应用于整个应用程序。

import java.util.Scanner;

public class MenuSystem {
    Scanner sc = new Scanner(System.in);

    private void displayMainMenu() {
        System.out.println("**********请选择您要执行的任务****");
        System.out.println("1.管理员登录");
        System.out.println("2.教师登录");
        System.out.println("3.学生登录");
        int input = sc.nextInt();
        sc.nextLine();

        switch (input) {
            case 1:
                displayAdminLoginMenu();
                break;
            case 2:
                displayTeacherLoginMenu();
                break;
            case 3:
                displayStudentLoginMenu();
                break;
            default:
                // 如果没有成功的选项,再次显示主菜单。
                // 也可以在此处理可能的错误,并向用户提供反馈。
                displayMainMenu();
                break;
        }
    }

    private void displayAdminLoginMenu() {
        // 在这里执行登录操作

        // 登录成功后显示管理员选项菜单
        displayAdminOptionsMenu();
    }

    private void displayTeacherLoginMenu() {
        // 在这里执行教师登录操作
    }

    private void displayStudentLoginMenu() {
        // 在这里执行学生登录操作
    }

    private void displayAdminOptionsMenu() {
        System.out.println("**** 1. 添加学生和部门 ****");
        System.out.println("**** 2. 移除学生 ****");
        System.out.println("**** 3. 学生信息 ****");
        System.out.println("**** 4. 返回主菜单 ****");
        System.out.println("**** 5. 退出系统 ****");

        int input = sc.nextInt();

        switch (input) {
            case 1:
                // 调用选项1的函数。
                break;
            case 2:
                // 调用选项2的函数。
                break;
            case 3:
                // 调用选项3的函数。
                break;
            case 4:
                displayMainMenu();
                break;
            case 5:
                exitApplication();
                break;
            default:
                // 如果没有成功的选项,再次显示管理员菜单。
                // 也可以在此处理可能的错误,并向用户提供反馈。
                displayAdminOptionsMenu();
                break;
        }
    }

    private void exitApplication() {
        System.exit(0);
    }
}

这并不一定是唯一的可用选项,但这是我目前想到的方法。

英文:

Approach 1 - Not recommended

You can either have a loop surrounding the core functionality of your application and break out of any internal loops to go back to the beginning of the core loop.

Approach 2 - Recommended

You could refrain from using a core loop and refactor your code such that each menu interaction is a separate function.

Each function would present the user with a specific menu and depending on the user's input another function would be called for the next menu. Generally speaking, you would not require any loops to keep the application running.

This would reduce the complexity and confusion of your code and make it much easier to expand the application with new features without needing to concern yourself with where to place code within the loop.

I recommend reading up on SOLID design principles to get an idea of how best to structure code. Here is an example article. This answer and example code is not meant to be a full representation of SOLID design and I would recommend that you strongly consider using interfaces and separate classes for each distinct part of the system and not having all the code in the MainActivity class.

An example of a function-based approach would be as follows.
I mainly focussed on the Main Menu and Admin Menu. The principle can be applied across the entire application.

Scanner sc = new Scanner(System.in);
private void displayMainMenu() {
System.out.println(&quot;**********Please Choose the task you want to perform****&quot;);
System.out.println(&quot;1.Admin  Login&quot;);
System.out.println(&quot;2.Faculty Login&quot;);
System.out.println(&quot;3.Student Login&quot;);
int input = sc.nextInt();
sc.nextLine();
switch (input) {
case 1:
displayAdminLoginMenu();
break;
case 2:
displayFacultyLoginMenu();
break;
case 3:
displayStudentLoginMenu();
break;
default:
// Display the main menu again if no option was successful.
// Can also handle possible errors here with feedback to the user.
displayMainMenu();
break;
}
}
private void displayAdminLoginMenu() {
// Perform login operation here
// After successful login display admin options menu
displayAdminOptionsMenu();
}
private void displayFacultyLoginMenu() {
}
private void displayStudentLoginMenu() {
}
private void displayAdminOptionsMenu() {
System.out.println(&quot;**** 1. add students and department *****&quot;);
System.out.println(&quot;**** 2. remove students *****&quot;);
System.out.println(&quot;**** 3. student Information *****&quot;);
System.out.println(&quot;**** 4, return to main menu *****&quot;);
System.out.println(&quot;**** 5, exit the system *****&quot;);
int input = sc.nextInt();
switch (input) {
case 1:
// Call function for option 1.
break;
case 2:
// Call function for option 2.
break;
case 3:
// Call function for option 3.
break;
case 4:
displayMainMenu();
break;
case 5:
exitApplication();
break;
default:
// Display the admin menu again if no option was successful.
// Can also handle possible errors here with feedback to the user.
displayAdminOptionsMenu();
break;
}
}
private void exitApplication() {
System.exit(0);
}

These are not necessarily the only options available, but they are what I could think of at the moment.

huangapple
  • 本文由 发表于 2020年9月22日 21:40:08
  • 转载请务必保留本文链接:https://go.coder-hub.com/64011012.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定