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: 3772423 • 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

Note:

The required code class MenuItem and Menu prepared without changing given methods and the given code TestMenuItem.java is used as it is.

Program:

//MenuItem.java

public class MenuItem

{

     //Declare the required variables

     int mid;

     String mlabel;

     int mchoice;

     boolean menabled;

     //Default constructor

     public MenuItem()

     {

     this.mid = 1;

     this.mlabel = "";

     }

     //Constructor with parameter

     public MenuItem(int id, String label)

     {

     this.mid = id;

     this.mlabel = label;

     }

    

     //Access Methods

     public String getLabel()

     {

     return mlabel;

     }

    

     // method to get the id

     public int getID()

     {

     return mid;

     }

    

     // method to get the choice

     public int getChoice()

     {

     return mchoice;

     }

    

     // method to enable

     public boolean getEnabled()

     {

     return menabled;

     }

     //Mutator Methods

     public void setChoice(int choice)

     {

          this.mchoice = choice;

     }

    

     // method to set the enable

     public void setEnabled(boolean on_or_off)

     {

     this.menabled = on_or_off;

     }

}

//Menu.java

// import the required header file

import java.io.*;

import java.util.*;

public class Menu extends MenuItem

{

     // declare the required variables

     public static final char DFLT_UNDERLINE_CHAR = 0;

     String title;

     int mid;

     char underlineChar;

     ArrayList<MenuItem> obj = new ArrayList<MenuItem>();

     // Constructor

     public Menu(String title)

     {

     super();

     this.title = title;

     }

     // Access Methods

     public String getTitle()

     {

     return title;

     }

     // method to get the underline character

     public char getUnderlineChar()

     {

     return underlineChar;

     }

     // method to get the id

     public int getId()

     {

     return mid;

     }

     // Mutator Methods

     // Appends a menu item to this menu

     public void add(MenuItem menuItem) {

     obj.add(menuItem);

     }

     // Inserts a menu item to this menu at a specified position

     public void insert(int pos, MenuItem menuItem) {

     obj.add(pos, menuItem);

     }

     // removes a menu item from this menu

     public void remove(MenuItem menuItem) {

     obj.remove(menuItem);

     }

     // sets the title for this menu

     public void setTitle(String title) {

     this.title = title;

     }

     // sets the character used to underline the menu title

     // Menu Methods

     public void setUnderlineChar(char underlineChar) {

     this.underlineChar = underlineChar;

     }

     // prints this menu to supplied output stream

     public void display(PrintStream out) {

     for (int mi = 0; mi < obj.size(); mi++)

     {

        out.println(obj.get(mi).toString());

     }

     }

     // calls the display() to print this menu and blocks

     // the program until a choice is entered

     public int activate(PrintStream out)

     {

     display(out);

     System.out.print("Enter your choice: ");

     Scanner scnin = new Scanner(System.scnin);

     int option = scnin.nextInt();

     return option;

     }

}

TestMenuItem.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);

      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;

         }

      }

   }

}