目录
(1)登录菜单showLoginMenu()方法参考如下代码。
(2)主菜单showMainMenu()方法中的关键代码如下。
(3) 真情回馈showSendGMenu()方法中的关键代码如下。
12.3.2 技能训练
上机练习2
练习——定义管理员类
需求说明
- 编写管理员类Manager,使用show()方法返回管理员信息。
程序运行结果如图12.6所示。
图12.6上机练习2的运行结果
提示:show()方法使用return语句实现信息的返回。
上机练习3
指导——实现菜单的级联效果
训练要点
- 方法的定义。
- 方法的调用。
- 循环结构。
需求说明
- 实现"我行我素购物管理系统"菜单,输入菜单项编号,可以自由切换各个菜单。
- 菜单的级联关系如图12.7所示,程序运行结果如图12.8所示。
图12.7上机练习3的菜单级联关系
图12.8上机练习3的运行结果
实现思路及关键代码
(1)创建菜单类Menu。
(2)分别编写方法,实现以下功能。
- showLoginMenuf()方法:实现显示登录菜单。
- showMainMenu()方法:实现显示主菜单。
- showCustMenuf()方法:实现显示客户信息管理菜单。
- showSendGMenu()方法:实现显示真情回馈菜单。
(3) 编写测试类TestMenu,进行验证。
参考解决方案
(1)登录菜单showLoginMenu()方法参考如下代码。
package cn.jbit.classandobject;
import java.util.*;
/**
* Menu1.java
*/
public class Menu {
/**
* 显示登录菜单
*/
public void showLoginMenu(){
System.out.println("\n\t欢迎使用我行我素购物管理系统\n");
System.out.println("\t\t 1. 登 录 系 统\n");
System.out.println("\t\t 2. 退 出\n");
System.out.println ("* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *");
System.out.print("请选择,输入数字:");
}
(2)主菜单showMainMenu()方法中的关键代码如下。
public void showMainMenu(){
System.out.println("\n\t我行我素购物管理系统主菜单\n");
System.out.println("* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *\n");
System.out.println("\t\t 1. 客 户 信 息 管 理\n");
System.out.println("\t\t 2. 真 情 回 馈\n");
System.out.println("* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *");
System.out.print("请选择,输入数字或按0返回上一级菜单:");
boolean con;
do{
con = false;
/*输入数字,选择菜单*/
Scanner input = new Scanner(System.in);
int no = input.nextInt();
if (no == 1){
showCustMMenu();
}else if (no == 2){
showSendGMenu();
}else if (no == 0){
showLoginMenu();
}else{
System.out.print("输入错误, 请重新输入数字:");
con = true;
}
}while(con);
}
(3) 真情回馈showSendGMenu()方法中的关键代码如下。
public void showSendGMenu(){
System.out.println("\n\t我行我素购物管理系统 > 真情回馈");
System.out.println("* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *\n");
System.out.println("\t\t 1. 幸 运 大 放 送\n");
System.out.println("\t\t 2. 幸 运 抽 奖\n");
System.out.println("\t\t 3. 生 日 问 候\n");
System.out.println("* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *");
System.out.print("请选择,输入数字或按0返回上一级菜单:");
boolean con;
do{
con = false;
/*输入数字,选择菜单*/
Scanner input = new Scanner(System.in);
int no = input.nextInt();
if(no == 1){
System.out.println("执行幸运大放送");
}else if(no == 2){
System.out.println("执行幸运抽奖");
}else if(no == 3){
System.out.println("执行生日问候");
}else if(no == 0){
showMainMenu(); // 返回主菜单
}else{
System.out.print("输入错误,请重新输入数字:");
con = true;
}
}while(con);
}
(4) 测试类TestMenu中的关键代码如下。
package cn.jbit.dlc1;
import java.util.Scanner;
/**
* TestMenu.java
*/
public class TestMenu {
/**
* 系统程序入口
*/
public static void main(String[] args) {
boolean con=true;
do{
/*显示登录菜单*/
Menu menu = new Menu();
menu.showLoginMenu();
/*实现菜单*/
Scanner input = new Scanner(System.in);
int choice = input.nextInt();
switch(choice){
case 1:
menu.showMainMenu();
break;
case 2:
System.out.println("谢谢您的使用!");
con=false;
break;
}
}while(con);
}
}