1
import java.util.*;
import java.text.*;

public class DvdRental1 {
    static Scanner input = new Scanner(System.in).useDelimiter("\r\n");
    static  DecimalFormat fmt=new DecimalFormat("0.00");

    public static void main(String[] args) 
    {   


        String [] movies = new String[10];

        movies[0] = "DRAG ME TO HELL";
        movies[1]  = "PARANORMAL ACTIVITY";
        movies[2] =  "SHUTTER";
        movies[3] = "P.S I LOVE YOU";
        movies[4]  = "500 DAYS OF SUMMER";
        movies[5] = "THE NOTE BOOK";
        movies[6] = "2012";
        movies[7] = "THE DAY AFTER TOMORROW";
        movies[8] = "GAMER";


        showmainmenu();
    } // END OF MAIN

    private static void showmainmenu()
    {
        int perday;
        int  mainmenu;
        System.out.println("Welcome to TP DVD Rental Service");
        System.out.println("");
        System.out.println("---DVD RENTAL SYSTEM---");
        System.out.println("-----------------------");
        System.out.println("(1) Rent a DVD");
        System.out.println("(2) Exit");
        System.out.print("Please select your choice(1-2):");
        mainmenu= input.nextInt();



        if(mainmenu ==1 )
        {
            showmenu();             
        }
        else if (mainmenu == 2)
        {
            goExit();
            System.out.println("You have exited");          
        } 
        else 
              System.out.println("Please key in 1,2");
     }
}

This is my first time using Java. I have another question whether this is the right way to make the program run this way, as will the showMenu() method i am using will it cause to program to run endlessly ? Thank you so much. As i have most of my content using methods to do a loop.

4 Answers4

1

Make the array an instance variable, declared outside of any method. In other words, have this:

static Scanner input = new Scanner(System.in).useDelimiter("\r\n");
static  DecimalFormat fmt=new DecimalFormat("0.00");
String[] movies = new String[10];

You can initialize it either outside of any method as an instance variable as follows:

String movies[] = new String[] { "DRAG ME TO HELL", "PARANORMAL ACTIVITY",
        "SHUTTER", "P.S I LOVE YOU", ... };

Or you can just add the elements to the array inside mainmenu() as you are right now.

Then you can access it in showmainmenu() as well as in goExit() and loop through it, printing each entry just before doing System.exit() or whatever you do at the end of goExit();. You can do the latter (printing of the movies array) quite simply like this:

for(int i=0;i<movies.length;i++) {
    System.out.println(movies[i]);
}

Alternatively, you can pass a reference to the array as an argument to the various methods.

Martin Dinov
  • 8,757
  • 3
  • 29
  • 41
0

Either pass movies as a parameter into showmainmenu(), and then in turn pass it onto goExit() or make it a global variable, in which case you can print it wherever you want.

nunchuckNinja
  • 89
  • 1
  • 7
0

You have two options, you could make the following:

String [] movies = new String[10];

A global variable, so you can reference it from anywhere in the class:

static Scanner input = new Scanner(System.in).useDelimiter("\r\n");
static  DecimalFormat fmt=new DecimalFormat("0.00");
String [] movies = new String[10];//add here

Your second option is to pass the array to the showmainmenu() method. It would look as so:

    showmainmenu(movies);
} // END OF MAIN

private static void showmainmenu(String [] movies)
{
    //rest of code

Finally, you in order to print this you must loop through the array and print each value:

    else if (mainmenu == 2)
    {
        for(int i = 0; i<movies.length; i++){
            System.out.println(movies[i]);
        }
        goExit();
        System.out.println("You have exited");          
    } 

Read more about loops over arrays here.

I have another question whether this is the right way to make the program run this way, as will the showMenu() method i am using will it cause to program to run endlessly ?

Yes, I would recommend a do-while loop:

private static void showmainmenu()
{
    int perday;
    int  mainmenu;
    System.out.println("Welcome to TP DVD Rental Service");
    System.out.println("");
    System.out.println("---DVD RENTAL SYSTEM---");
    System.out.println("-----------------------");
    do{
        System.out.println("(1) Rent a DVD");
        System.out.println("(2) Exit");
        System.out.print("Please select your choice(1-2):");
        mainmenu= input.nextInt();
        //code
        else if (mainmenu == 2)
        {
            goExit();
            System.out.println("You have exited");       
            break;//exits do-while   
        } 
        //code
    }while(true)
Community
  • 1
  • 1
0

If I understand it correctly, you can define each movies collection seperatelly as global variable like :

    static String[] thrillerMovies = new String[] {"2012","THE DAY AFTER TOMORROW","GAMER"};
    static String[] loveMovies = new String[] {"P.S I LOVE YOU","500 DAYS OF SUMMER","THE NOTEBOOK"};
    static String[] horrerMovies = new String[] {"DRAG ME TO HELL","PARANORMAL ACTIVITY","SHUTTER"};

and define a method like below to show submenus :

private static void showSubmenu(String SubMenu,String[] movies) {
        System.out.println("");
        System.out.println( SubMenu + "MOVIE SELECTIONS");
        System.out.println("------------------------");
        for(int i=0;i<movies.length;i++) {
            System.out.println("<" +(i+1)+"> " + movies[i]);
        }
        System.out.println("<4> Back to Main Menu");
        System.out.println("------------------------");

        System.out.print("Please select desired movie <1-3>:");

    }

You can print the choices by calling the above method in your if like

if (menu == 1) {
   showSubmenu("HORROR", horrerMovies);

-

else if (menu == 2) {
   showSubmenu("LOVE", loveMovies);

-

else if (menu == 3) {
    showSubmenu("THRILLER", thrillerMovies);

Similarly you can do the same in case of submenu choices if you wish. @Mohammad S. And @Martin Dinov already mentioned some points there.

Asfab
  • 398
  • 6
  • 11