Overview A Menu is a collection of items (referred to as \"menu items\") from wh
ID: 3772394 • Letter: O
Question
Overview
A Menu is a collection of items (referred to as "menu items") from which a user makes a selection. Menus are used by many console applications to handle user interaction.
In this assignment you will create the classes: Menu and MenuItem. In addition, you will produce a driver program that will be used to unit test your classes.
Specification
Design and implement a class Menu that contains zero or more class MenuItemobjects.
Your program is not allowed to use any arrays or Vector objects; instead, you're required to use an ArrayList object.
The rest of the document provides the class MenuItem and class Menu APIs.
class MenuItem
The MenuItem class contains the following instance variables.
a label
an internal identifier that uniquely identifies the MenuItem
the choice value that the MenuItem is assigned when the Menu is displayed by the Menu display() method.
a flag to indicate if the MenuItem is enabled
Constructors
A MenuItem object is created using the following constructor.
A MenuItem is enabled by default.
Access Methods
Access is provided to all of the instance variables. The following accessor methods must be implemented:
Mutator Methods
There are two mutator methods.
class Menu
A Menu object consists of zero or more MenuItem objects.
Each menu object consists of the following instance variables:
title
character used to underline the title
an ArrayList of MenuItem objects
The following is an example menu:
The default title underline character is the equal sign ('=').
When the user enters a choice, the choice is checked for correctness. If an invalid choice is entered, then the following error message is printed:
and the menu is redisplayed and the program blocks until another choice is entered.
The id of the selected MenuItem is returned to the caller when a valid choice is entered.
A disabled MenuItem is listed with an asterisk and cannot be selected.
Constructors
The following Menu constructor must be implemented:
Access Methods
Mutator Methods
Menu Methods
The following two methods are used to display (print) the menu and get user input.
The display() method assigns to each MenuItem the choice value that is printed with the MenuItem label.
The activate() method returns the MenuItem id that corresponds to the user entered choice.
Driver Program
You must create a driver program that is used to test your classes. The driver program does not need to implement a meaningful application.
TestMenuItem.java is a driver program that I wrote to test my Menu and MenuItemclasses. You may use this program if you'd like.
TestMenuItem.java
Explanation / Answer
You can use it as it is..
import java.util.*;
import java.io.*;
public class TestMenuMenuItem {
public static void main(String[] argv) {
final int ADD = 1, DELETE = 2, UPDATE = 3, CHANGE = 4,
DU = 5, DF = 6, UNUSED = 7, EXIT = 8;
String TITLE = "System Administration Menu";
MenuItem addUser = new MenuItem(ADD, "Add User");
MenuItem delUser = new MenuItem(DELETE, "Delete User");
MenuItem updUser = new MenuItem(UPDATE, "Update User");
MenuItem chgUser = new MenuItem(CHANGE, "Change User");
MenuItem diskUsage = new MenuItem(DU, "Disk Usage");
MenuItem diskFree = new MenuItem(DF, "Disk Free");
MenuItem unused = new MenuItem(UNUSED, "Testing Only");
MenuItem exit = new MenuItem(EXIT, "Exit");
unused.setEnabled(false);
Menu sysAdmin = new Menu(TITLE);
sysAdmin.add(addUser);
sysAdmin.add(delUser);
sysAdmin.add(updUser);
sysAdmin.add(chgUser);
//sysAdmin.addSeparator();
sysAdmin.add(diskUsage);
sysAdmin.add(diskFree);
sysAdmin.add(unused);
//sysAdmin.addSeparator();
sysAdmin.add(exit);
int option;
while ((option = sysAdmin.activate(System.out)) != EXIT) {
switch (option) {
case ADD:
add(item);
break;
case DELETE:
remove(item);
break;
case UPDATE:
update(item);
break;
}
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.