Click here to Skip to main content
15,885,216 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hello everyone. I want to create a menu for my application. But I have a problem.

This is the code.

class MainMenu{
	Scanner MainMenusc = new Scanner(System.in);
	public int YourChoice;
	public void MainMenu(){
		System.out.println("Welcome");
		System.out.println("1. Quan ly sinh vien");
		System.out.println("2. Quan ly diem");
		System.out.println("3. Quan ly thong ke");
		System.out.println("");
		System.out.println("0. Thoat");		
		System.out.print("Vui long chon chuc nang muon su dung: ");
		YourChoice = MainMenusc.nextInt();
		if(YourChoice==1){
			Menu1 mn1 = new Menu1();
			mn1.Menu1();
		}
	}
}

class Menu1 extends MainMenu{
	Scanner Menu1sc = new Scanner(System.in);
	public int YourChoice;
	public void Menu1(){
		System.out.println("Welcome");
		System.out.println("1. Nhap sinh vien moi");
		System.out.println("2. Sua thong tin sinh vien");
		System.out.println("3. Xoa sinh vien");
		System.out.println("");
		System.out.println("9. Return to main menu");		
		System.out.println("0. Thoat");		
		System.out.print("Vui long chon chuc nang muon su dung: ");
		YourChoice = Menu1sc.nextInt();
		if(YourChoice==9){
			MainMenu.MainMenu();
		}
	}
}

class Menu2 extends MainMenu{
	Scanner Menu2sc = new Scanner(System.in);
	public int YourChoice;
	public void Menu2(){
		System.out.println("Welcome");
		System.out.println("1. Nhap diem");
		System.out.println("2. Sua diem");
		System.out.println("3. Xoa diem");
		System.out.println("4. Sap xep diem");
		System.out.println("");
		System.out.println("9. Return to main menu");		
		System.out.println("0. Thoat");
		System.out.print("Vui long chon chuc nang muon su dung: ");
		YourChoice = Menu2sc.nextInt();
	}
}

class Menu3 extends MainMenu{
	Scanner Menu3sc = new Scanner(System.in);
	public int YourChoice;
	public void Menu3(){
		System.out.println("Welcome");
		System.out.println("1. Thong ke sinh vien");
		System.out.println("2. Thong ke diem");
		System.out.println("");
		System.out.println("9. Return to main menu");		
		System.out.println("0. Thoat");
		System.out.print("Vui long chon chuc nang muon su dung: ");
		YourChoice = Menu3sc.nextInt();
	}
	}
class Menu31 extends Menu3{
	Scanner Menu31sc = new Scanner(System.in);
	public int YourChoice;
	public void Menu31(){
		System.out.println("Welcome");
		System.out.println("1. Tong so sinh vien");
		System.out.println("2. Tong so sinh vien toan");
		System.out.println("3. Tong so sinh vien ly");
		System.out.println("4. Tong so sinh vien hoa");
		System.out.println("");
		System.out.println("9. Ve menu truoc");
		System.out.println("0. Thoat");
		System.out.print("Vui long chon chuc nang muon su dung: ");
		YourChoice = Menu31sc.nextInt();
		}
	}
class Menu32 extends Menu3{
	Scanner Menu32sc = new Scanner(System.in);
	public int YourChoice;
	public void Menu32(){
		System.out.println("Welcome");
		System.out.println("1. So sinh vien gioi ");
		System.out.println("2. So sinh vien kha");
		System.out.println("3. So sinh vien trung binh");
		System.out.println("4. So sinh vien yeu");
		System.out.println("5. Sinh vien co diem cao nhat");
		System.out.println("6. Sinh vien co diem thap nhat");
		System.out.println("");
		System.out.println("9. Ve menu truoc");
		System.out.println("0. Thoat");
		System.out.print("Vui long chon chuc nang muon su dung: ");
		YourChoice = Menu32sc.nextInt();
		}
	}

//Class main

class MainTask{
	public static void main(String args[]){
		/* stm = new StudentManager();
		stm.InputInfo();
		stm.ViewInfo();*/
		MainMenu menu = new MainMenu();
		menu.MainMenu();
	}
}


When I switch back to MainMenu from Menu1, Menu2 or Menu3, how can I call back the MainMenu method (through the object "menu" that I created in main method) without create a new one? (it means not use the "new" keyword, something like
Java
MainMenu mn = new Mainmenu
)?

Thank you very much!
Posted
Updated 2-May-13 20:30pm
v4

1 solution

You created an object menu from type MainMenu.
That variable exists in the scope of your main method.
You can call that object as often as you want.

Currently your application ends after the call of the object menu.

Also: You're extending MainMenu in various types.

I'd recommend to make MainMenu abstract and to use the Scanner from parental object MainMenu in subclasses.
You'd need to create another Top level menu, but will have a much simpler logic without various scanners and reactions (and of course some OOP ).
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

  Print Answers RSS
Top Experts
Last 24hrsThis month


CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900