Home > Blockchain >  How to simplify this method Java
How to simplify this method Java

Time:04-29

I am currently working on the assignment and my code became too long while working on it. I got multiple inputs and the if statement became too long. is there any way I can simplify it by using another statement such as while?

private void menu() {

    String rentorpurchase = myObj.nextLine();
    if  (whichmovie2.equals("Batman") && rentorpurchase.equals("rent")){
       System.out.println ("How long would you like to rent (days) ?");
       int rentperiod = myObj.nextInt();
       M1.getMovieRP(rentperiod);
    }else if (whichmovie2.equals("Batman") && rentorpurchase.equals("purchase")){
       System.out.println ("Cash or card?");
       String Payoption = myObj.nextLine();
       M1.getMoviePP(Payoption);
    }else if  (whichmovie2.equals("Ironman") && rentorpurchase.equals("rent")){
       System.out.println ("How long would you like to rent (days) ?");
        int rentperiod = myObj.nextInt();
        M2.getMovieRP(rentperiod);
    }else{
        System.out.println ("Cash or card?");
        String Payoption = myObj.nextLine();
        M2.getMoviePP(Payoption);
    }else{
        System.out.println ("which TvShow you would like to rent?");
        String whichmovie2 = myObj.nextLine();
        System.out.println ("would you like to rent or purchase?");
        String rentorpurchase = myObj.nextLine();
    }
    
    if  (whichmovie2.equals("Friends") && rentorpurchase.equals("rent")){
        System.out.println ("How long would you like to rent (days) ?");
        int rentperiod = myObj.nextInt();
        T1.getRP(rentperiod);
    }else if (whichmovie2.equals("Friends") && rentorpurchase.equals("purchase")){
        System.out.println ("Cash or card?");
        String Payoption = myObj.nextLine();
        T1.getPP(Payoption);
    }else if(whichmovie2.equals("Sherlock") && rentorpurchase.equals("rent")){
        System.out.println ("How long would you like to rent (days) ?");
        int rentperiod = myObj.nextInt();
        T2.getRP(rentperiod);
    }else{
        System.out.println ("Cash or card?");
        String Payoption = myObj.nextLine();
        T2.getPP(Payoption);
     }
}

CodePudding user response:

You could use a switch and case statements to select from one of the options above.

CodePudding user response:

Changing from your chain of if/then/else to switch will make the code a bit more readable but it still remains a clunky block. Especially if you want to add more enhancements you will run into problems.

So I suggest for every question you create a method that prints the question and loops for getting input until the input is valid. You need to define what is valid - just any value, or a number, or ...

Then you can have your clunky block make use of these question methods - and here you can potentially reuse stuff you already wrote. This not only makes your code shorter and easier to understand, you can now more easily maintain it.

  • Related