Majadul Haque
Majadul Haque

Reputation: 59

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

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("Admin", "1234");
    
    
    System.out.println("**********Welcome to University Management*******");


    //do {  
    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 ( "**** n 5, print student *****");
            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]);
                            
                            //break;
                        }
                        
                        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() == 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);
    //flag = false;
    //break;
    //continue;
    break;
    default:
    break;
                
            }
        }while (true);

    } else {
        //flag = true;
        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() == userId){
                System.out.println("*********Welcome to university Portal***");
                //isStudentRegistered = true;
                total.showInfo();
     }else{
         System.out.println("You are not registered yet");
     }
             
         }
            
        
     
                        
            }
        }while(true);
        
             
          
        
    }
    
    else{
    System.out.println ( "Login failed please re-enter:");
    }
/*} 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<totalDepartmentCount; i++){
        department[i].showInfo();
    }
}

public void showRegisteredInfo(){
    for(int i=0; i<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("student found");
                isStudentRegistered = true;
                total.showInfo();
     }
             
         }
            
        
     
                        
            }
        if(!isStudentRegistered){
            System.out.println("No student found");
        }
        
            
         
     }
     /*
     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("student found");
                isStudentRegistered = true;
                total = null;
                System.out.println("Student removed");
     }
             
         }
            
        
     
                        
            }
        if(!isStudentRegistered){
            System.out.println("No student found");
        }
        
            
         
     }
     */
     

            
     

     }
             
     //courses[num].showInfo();
    


/*public void showCourseInfo(){
    for(int i=0; i<totalCourseCount; i++){
        courses[i].showInfo();
    }
}
*/

Upvotes: 0

Views: 115

Answers (1)

Ruan
Ruan

Reputation: 344

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("**********Please Choose the task you want to perform****");
    System.out.println("1.Admin  Login");
    System.out.println("2.Faculty Login");
    System.out.println("3.Student Login");
    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("**** 1. add students and department *****");
    System.out.println("**** 2. remove students *****");
    System.out.println("**** 3. student Information *****");
    System.out.println("**** 4, return to main menu *****");
    System.out.println("**** 5, exit the system *****");

    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.

Upvotes: 1

Related Questions