News:

Please note these forums are mostly a testing ground for my SMF work and I don't really use them otherwise.

Main Menu

Paste-1204954238:v:use_geshi-1:v:type-java5

Started by JayBachatero, Mar 08, 2008, 05:30 AM

Previous topic - Next topic

0 Members and 3 Guests are viewing this topic.

JayBachatero

/**
 * This class is a controller to have an array driven menu.
 * @author: Juan J. Hernandez
 * @project: BankTransactions
 */

// We need this library.
import java.util.ArrayList;

public class MenuController
{
   // Start the menu string.
   ArrayList<Object> menu = new ArrayList<Object>();
   // Number of menu items.
   private int totalMenuItems = 0;
   // Current step to fall back to.
   public int currentStep = 1;
   // Did we exit already?
   private boolean menuExit = false;
   // Header and footer.
   private String header = "";
   private String footer = "";

   /**
    * Adds items to the menu.
    * @param item
    */
   public void addMenuItem(String item)
   {
      // Can't add more if the menu already exited.
      if (this.menuExit == false)
      {
         // Increase it.
         this.totalMenuItems++;
         // Set the menu item.
         this.menu.add(item);
         // += "\n" + this.totalMenuItems + ". " + item;
      }
   }

   /**
    * Get method for the menu.
    * @return menu items.
    */
   public ArrayList<Object> getMenuItems()
   {
      return this.menu;
   }

   /**
    * Get method for the menu.
    * @return menu items.
    */
   public Object getMenuItems(int index)
   {
      return this.menu.get(index);
   }

   /**
    * Returns all the items in the menu as a string.
    * @return
    */
   public String getMenuItemsString()
   {
      String menuItems = "";

      // Loop through them and return them.
      for (int i = 0; i < this.menu.size(); i++)
         menuItems += "\n" + (i + 1) + ". " + this.menu.get(i);

      // Add the Header and footer.
      menuItems = this.header + menuItems + this.footer;

      return menuItems;
   }
   /**
    * This method adds the last item in the menu.  That is the exit item.
    * @param exit
    */
   public void addMenuExitItem(String exit)
   {
      if (this.menuExit == false)
      {
         if (exit == "")
            this.addMenuItem("Exit");
         else
            this.addMenuItem(exit);

         // Ok we exited and there is nothing you can do now.
         this.menuExit = true;
      }
   }

   /**
    * Returns the id of the exit step.
    * @return
    */
   public int getExitStep()
   {
      return this.totalMenuItems;
   }

   /**
    * Adds the header value.
    * @param text
    */
   public void addMenuHeader(String text)
   {
      this.header = text;
   }

   /**
    * Gets the header value.
    */
   public String getMenuHeader()
   {
      return this.header;
   }

   /**
    * Adds the footer value.
    * @param text
    */
   public void addMenuFooter(String text)
   {
      this.footer = "\n" + text;
   }

   /**
    * Gets the footer value.
    */
   public String getMenuFooter()
   {
      return this.footer;
   }

   /**
    * Increases the current step.
    */
   public void increaseCurrentStep()
   {
      this.currentStep++;
   }
}