Overview A Menu is a collection of items (referred to as \"menu items\") from wh
ID: 3770260 • 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 MenuItem objects.
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 MenuItem classes. You may use this program if you'd like.
TestMenuItem.java
Explanation / Answer
MenuItem.java:
public class MenuItem
{
int id;
String label;
int choice;
boolean enabled;
public MenuItem()
{
}
public MenuItem(int id, String label)
{
this.id = id;
this.label = label;
}
public String getLabel()
{
return label;
}
public int getID()
{
return id;
}
public int getChoice()
{
return choice;
}
public boolean getEnabled()
{
return enabled;
}
public void setChoice(int choice)
{
this.choice = choice;
}
public void setEnabled(boolean enabled)
{
this.enabled = enabled;
}
public String toString()
{
String str=id+" "+label;
return str;
}
}
Menu.java:
import java.util.*;
import java.io.*;
public class Menu extends MenuItem
{
public static final char DFLT_UNDERLINE_CHAR = 0;
String title;
char underlineChar;
ArrayList<MenuItem> MenuArray=new ArrayList<MenuItem>();
public Menu(String title)
{
this.title=title;
}
//Access Methods
public String getTitle()
{
return title;
}
public char getUnderlineChar()
{
return underlineChar;
}
public int getId()
{
return id;
}
//Mutator Methods
public void add(MenuItem menuItem)
{
MenuArray.add(menuItem);
}
public void insert(int pos, MenuItem menuItem)
{
MenuArray.add(pos, menuItem);
}
public void remove(MenuItem menuItem)
{
MenuArray.remove(menuItem);
}
public void setTitle(String title)
{
this.title=title;
}
public void setUnderlineChar(char underlineChar)
{
this.underlineChar=underlineChar;
}
public void display(PrintStream out)
{
for (int i=0;i<MenuArray.size();i++)
{
out.println(MenuArray.get(i).toString());
}
}
public int activate(PrintStream out)
{
display(out);
System.out.print("Enter your choice: ");
Scanner in = new Scanner(System.in);
int option = in.nextInt();
return option;
}
}
TestMenuMenuItem.java:
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.add(diskUsage);
sysAdmin.add(diskFree);
sysAdmin.add(unused);
sysAdmin.add(exit);
int option;
while ((option = sysAdmin.activate(System.out)) != EXIT)
{
switch (option)
{
case ADD:
System.out.println("... adding user");
break;
case DELETE:
System.out.println("... deleting user");
break;
case UPDATE:
System.out.println("... updating user");
break;
case CHANGE:
System.out.println("... changing user");
break;
case DU:
System.out.println("... calculating disk usage");
break;
case DF:
System.out.println("... calculating disk free");
break;
case UNUSED:
System.err.println("... what's this?");
break;
}
}
final int CHANGE_ULINECHAR = 1, CHANGE_ENABLED = 2, CHANGE_TITLE = 3, REMOVE_ITEM = 4, INSERT_ITEM = 5, ADD_ITEM = 6, DONE = 7;
TITLE = "Test Menu Changes";
MenuItem chgULineChar = new MenuItem(CHANGE_ULINECHAR,
"Change Underline Character");
MenuItem chgEnabled = new MenuItem(CHANGE_ENABLED,
"Change "Change Title" Enabled Flag");
MenuItem chgTitle = new MenuItem(CHANGE_TITLE, "Change Title");
MenuItem rmItem = new MenuItem(REMOVE_ITEM, "Remove This Menu Item");
MenuItem insItem = new MenuItem(INSERT_ITEM,
"Insert "Remove" Menu Item");
MenuItem addItem = new MenuItem(ADD_ITEM, "Add This Menu Item");
MenuItem done = new MenuItem(DONE, "Done");
Menu test = new Menu(TITLE);
test.add(chgULineChar);
test.add(chgEnabled);
test.add(chgTitle);
// test.addSeparator();
test.add(rmItem);
test.add(insItem);
test.add(addItem);
// test.addSeparator();
test.add(done);
boolean didAnAdd = false;
while ((option = test.activate(System.out)) != DONE)
{
switch (option)
{
case CHANGE_ULINECHAR:
char ch = test.getUnderlineChar();
if (ch == Menu.DFLT_UNDERLINE_CHAR)
test.setUnderlineChar('*');
else
test.setUnderlineChar(Menu.DFLT_UNDERLINE_CHAR);
break;
case CHANGE_ENABLED:
chgTitle.setEnabled(!chgTitle.getEnabled());
break;
case CHANGE_TITLE:
if (test.getTitle().equals(TITLE))
test.setTitle("Look At This New Title");
else
test.setTitle(TITLE);
break;
case REMOVE_ITEM:
test.remove(rmItem);
break;
case INSERT_ITEM:
test.insert(REMOVE_ITEM - 1, rmItem);
break;
case ADD_ITEM:
if (!didAnAdd)
{
test.add(addItem);
didAnAdd = true;
}
break;
}
}
}
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.