Click here to Skip to main content
Rate this: bad
good
Please Sign up or sign in to vote.
See more: Java Homework
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
MainMenu mn = new Mainmenu
)?
 
Thank you very much!
Posted 2-May-13 17:48pm
Edited 2-May-13 21:30pm
TorstenH.33.8K
v4

1 solution

Rate this: bad
good
Please Sign up or sign in to vote.

Solution 1

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 ).
  Permalink  

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

  Print Answers RSS
0 BillWoodruff 330
1 Mathew Soji 309
2 Maciej Los 295
3 DamithSL 225
4 Afzaal Ahmad Zeeshan 217
0 OriginalGriff 6,369
1 Sergey Alexandrovich Kryukov 5,973
2 DamithSL 5,183
3 Manas Bhardwaj 4,673
4 Maciej Los 4,025


Advertise | Privacy | Mobile
Web04 | 2.8.1411019.1 | Last Updated 3 May 2013
Copyright © CodeProject, 1999-2014
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100