tags:

views:

112

answers:

2

I wrote a switch statement that I have to put with another but I'm not sure how to. This is the one I'm trying to put with the other switch statement.

   import java.util.Scanner;
   public class Main

   public static void main(String[] args) {
       String input;
       char name;


    Scanner keyboard = new Scanner(System.in);



    System.out.println("Are you a Customer or Owner?");
    System.out.print("Press C for customer or O for owner:");
    input = keyboard.nextLine();
    name = input.charAt(0);

    switch (name)
    {
        case 'C':
            System.out.println("You entered customer.");
            System.out.println("Welcome!");
            break;
        case 'O':
            System.out.println("You entered owner.");
            System.out.println("What would you like to do?");
            break;
        default:
            System.out.println("That's not Customer or Owner");
    }

   }
}

I'm trying to put in this class

import java.util.Scanner;

public class Menu {
private Inventory database;
private char menuItem;
private Scanner input;


public Menu(Inventory database)
{
    this.database = database;
    menuItem      = 'N';
    input = new Scanner(System.in);
}

private void showMenu()
{
    System.out.println();
    System.out.println("------------------");
    System.out.println("Display Movies : D");
    System.out.println("Rent a Movie   : R");
    System.out.println("Reserve a Movie: S");
    System.out.println("Exit           : E");
    System.out.println("------------------");
    System.out.println();
    System.out.print("Please make your selection: ");

}

private void rentMovie(int productID)
{
    int index = database.getIndex(productID);
    if( index == -1)
    {
        System.out.println("There is not such a code.");
    }
    else
    {
        if( database.getMovie(index).getIsRented())
        {
            System.out.println("You cannot rent " +  database.getMovie(index).getTitle() + ". It is already rented.");

        }
        else
        {
            database.getMovie(index).setIsRented(true);
            System.out.println("Please take your movie.");

        }
    }

}


private void reserveMovie(int productID)
{
    int index = database.getIndex(productID);
    if( index == -1)
    {
        System.out.println("There is not such a code.");
    }
    else
    {
        if( database.getMovie(index).getIsReserved() )
        {
            System.out.println("You cannot reserve " +  database.getMovie(index).getTitle() + ". It is already reserved.");
        }
        else
        {
            if( database.getMovie(index).getIsRented())
            {
                database.getMovie(index).setIsReserved(true);
                System.out.println( database.getMovie(index).getTitle() + " is reserved for you." );
            }
            else
            {
                System.out.println( database.getMovie(index).getTitle() + " is available. You can rent it if you like.");
            }
        }
    }

}

public void run()
{

    while(true)
    {

        switch(menuItem)
        {
            case 'N':
                showMenu();
                menuItem = input.next().charAt(0);
                break;
            case 'D':
                database.print();
                showMenu();
                menuItem = input.next().charAt(0);
                break;
            case 'R':
                System.out.print("Please enter product code:");
                rentMovie( input.nextInt() );
                showMenu();
                menuItem = input.next().charAt(0);
                break;
            case 'S':
                System.out.print("Please enter product code:");
                reserveMovie( input.nextInt() );
                showMenu();
                menuItem = input.next().charAt(0);
                break;
            case 'E':
                System.out.print("Program terminated.");
                System.exit(0);
                break;
            default :
                showMenu();
                menuItem = input.next().charAt(0);
        }

    }
 }

}

A: 

I think what you're looking for is a way to have your Main class use your menu code. If that's the case, you should create a Menu object in your Main.main() method and call the new object's run() method.

RD
A: 

I'd go with some kind of command pattern here instead of a switch statement.

The command woul be like: (Sorry that it's C# but should be close enough to Java)

public interface IConsoleCommand {
  string Code { get; }
  void Execute(string args);
}

Then you'd simply instantiate all classes that implement this interface and instead of a switch statement you have a list / hashmap of those IConsoleCommands. You then check if the input equals their code and if that is the case, you call their Execute method. A nice benefit is that you could nest those commands for example, your CustomerConsoleCommand could have DisplayMovies / RentMovie as sub commands,

Maxem