Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Overview A Menu is a collection of items (referred to as \"menu items\") from wh

ID: 3773237 • 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

Class : Menu.java

import java.io.PrintStream;
import java.util.ArrayList;
import java.util.Scanner;


public class Menu
{
   private char UnderlineChar;
   public static char DFLT_UNDERLINE_CHAR='=';
   private String title;
   private ArrayList<MenuItem> menu=new ArrayList<MenuItem>();
  
  
   //Constructor
   public Menu(String title)
   {
       super();
       this.title = title;
   }

   //Accessor
   public char getUnderlineChar() {
       return UnderlineChar;
   }


   public String getTitle() {
       return title;
   }
  
   public int getId()
   {
       System.out.println("Enter Choice : ");
       Scanner sc=new Scanner(System.in);
       return sc.nextInt();
   }

   //Mutator
   public void add(MenuItem menuItem) // appends a menu item to this menu
   {
       menu.add(menuItem);
   }
  
   public void insert(int pos, MenuItem menuItem) // inserts a menu item to this menu at a specified position
   {
       menu.add(pos, menuItem);
   }
  
   public void remove(MenuItem menuItem) // removes a menu item from this menu
   {
       menu.remove(menuItem);
   }
  
   public void setTitle(String title) // sets the title for this menu
   {
       this.title=title;
   }
  
   public void setUnderlineChar(char underlineChar) // sets the character used to underline the menu title
   {
       this.UnderlineChar=underlineChar;
   }
  
   public void display(PrintStream out)// prints this menu to supplied output stream
   {
       out.print(getTitle()+" ");
       for(int i=0;i<15;i++)
       {
           out.print(DFLT_UNDERLINE_CHAR);
       }
       out.println();
       for(MenuItem m:menu)
       {          
           out.print(m.getId()+" ");
           out.print(m.getLabel()+" ");
       }
   }
  
   public int activate(PrintStream out) // calls the display() to print this menu and blocks the program until a choice is entered
   {
       return getId();
   }

}

Class : TestMenuMenuItem.java

import java.util.*;
import java.io.*;

/*
* This program was written to test class Menu and class MenuItem.
* Note: The code for these two classes is not provided.
*
* @creator gdt
* @created 02014.09.25
*/
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);
sysAdmin.display(System.out);
int option;
while ((option = sysAdmin.activate(System.out)) != EXIT) {
   sysAdmin.display(System.out);
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;
}
}
}
}

Class : MenuItem.java


public class MenuItem
{
   private String label;
   private int id;           //an internal identifier that uniquely identifies the MenuItem
   private boolean flag=true;
   private int choice;
   public MenuItem(int id, String label)
   {
       super();
       this.label = label;
       this.id = id;
   }
   public String getLabel() {
       return label;
   }
   public int getId() {
       return id;
   }
   public boolean getEnabled() {
       return flag;
   }
   public int getChoice() {
       return choice;
   }
  
   //Mutators Methods
   public void setEnabled(boolean on_or_off) {
       this.flag = on_or_off;
   }
   public void setChoice(int choice) {
       this.choice = choice;
}
  

}