Home > Software design >  repeat statement from first statement when a specific number is entered
repeat statement from first statement when a specific number is entered

Time:11-22

How do I recall the (Enter 1 - Simple Calculator, Enter 2 - Age Calculator) when number 2 is entered from choice1 and choice2 without having code duplications? I can't seem to find do it unless I duplicate the whole code. Is there no other way unless I duplicate them?

int choice, choice1, choice2;
System.out.print("Enter 1 - Simple Calculator, Enter 2 - Age Calculator: ");
choice = mexam.nextInt();
        
if (choice == 1) {
    do {
        System.out.println("********************Simple Calculator********************");
        System.out.print("Enter first number: ");
        int firstNumber = mexam.nextInt();
        System.out.print("Enter second number: ");
        int secondNumber = mexam.nextInt();
        System.out.print("Enter operator: ");
        String operator = mexam.next();
        int result;
        switch (operator) {
            case " ":
                result = firstNumber   secondNumber;
                System.out.println("Sum: "   result);
                break;
            case "-":
                result = firstNumber - secondNumber;
                System.out.println("Difference: "   result);
                break;
            case "*":
                result = firstNumber * secondNumber;
                System.out.println("Product: "   result);
                break;
            case "/":
                result = firstNumber / secondNumber;
                System.out.println("Quotient: "   result);
                break;
            default:
                System.out.println("Invalid Operator");
        }
System.out.println("*********************************************************");
        System.out.println("*********************************************************");
        System.out.print("Enter 1 - Continue, Enter 2 - Menu, Enter 3 - End: ");
        choice1 = mexam.nextInt();
    } while (choice1 == 1);
} else if (choice == 2) {
    do {
        System.out.println("*********************Age Calculator**********************");
        int year = 2021, result1;
        System.out.print("Enter birth year: ");
        int birthyear = mexam.nextInt();
        result1 = year % birthyear;
        System.out.println("Age: "   result1);
        System.out.println("*********************************************************");
        System.out.println("*********************************************************");
        System.out.print("Enter 1 - Continue, Enter 2 - Menu, Enter 3 - End: ");
        choice2 = mexam.nextInt();
    } while (choice2 == 1);

CodePudding user response:

You could extract those reusable parts into smaller methods and reuse it. Eg:

private void printMainMenu(){
    System.out.print("Enter 1 - Simple Calculator, Enter 2 - Age Calculator: ");
}
private void printSubMenu(){
    System.out.println("*********************************************************");
    System.out.println("*********************************************************");
    System.out.print("Enter 1 - Continue, Enter 2 - Menu, Enter 3 - End: ");
}

etc.... and then call these methods at places where you are repeating them.

Note: these are just hints to start thinking in the right direction. There are a lot of scope for refactoring in the piece of code you shared.

CodePudding user response:

Given your code snippet I assume you have another loop around this so I try to keep your code structure. There are for sure better ways to design the code but since you seem to just have started learning I'll stick to the constructs you seem to know already.

Try moving the calculations and the "sub menu" (continue, menu, end) into separate blocks, e.g. like this (also try to use more meaningful names for your variables):

System.out.print("Enter 1 - Simple Calculator, Enter 2 - Age Calculator: ");
int calculatorChoice = mexam.nextInt();

while( calculatorChoice == 1 || calculatorChoice == 2 ) {
  if( calculatorChoice == 1) {
    //simple calculator
  } else { //calculator can only have value 2 as per while condition
    //age calculator
  }

  //sub menu
  System.out.print("Enter 1 - Continue, Enter 2 - Menu, Enter 3 - End: ");
  int subMenuChoice = mexam.nextInt();

  //subMenuChoice == 1 -> nothing to do, just repeat loop
  if(subMenuChoice == 2) {
    //break out of loop 
  } else if (subMenuChoice == 3) {
    //end program
  }
}

As you can see, your code could be simplified by using methods as per Scary Wombat's comment:

while(calculatorChoice == 1 || calculatorChoice == 2) {
  if( calculatorChoice == 1) {
    simpleCalculator();
  } else { //calculator can only have value 2 as per while condition
    ageCalculator();
  }

  calculatorChoice = subMenu(calculatorChoice );
}

And subMenu(...) could look like this:

public int subMenu(int selectedCalculator) {      
  System.out.print("Enter 1 - Continue, Enter 2 - Menu, Enter 3 - End: ");
  int subMenuChoice = mexam.nextInt();
   
  if(subMenuChoice == 2) {
    return 0; //no calculator selected
  } else if (subMenuChoice == 3) {
    System.exit(0); //end the program
  }

  //neither "menu" nor "end" selected, so keep the current calculator
  return selectedCalculator;
}

Depending on the rest of your program, you could even merge menu and sub menu:

public int menu(final int selectedCalculator) {      
  int calculator = selectedCalculator;      

  //display the sub menu as there already was a calculator selection
  if(  selectedCalculator != 0 ) {
     System.out.print("Enter 1 - Continue, Enter 2 - Menu, Enter 3 - End: ");
     int subMenuChoice = mexam.nextInt();
   
     if (subMenuChoice == 2) {
       //don't do anything but let the program "flow" to the menu
     } else if (subMenuChoice == 3) {
       System.exit(0); //end the program
     } else {
       return selectedCalculator; //assume "continue" was selected so keep the calculator
     }
  }
   
  //if we're here  "selectedCalculator" either was 0 or subMenuChoice was 2
  System.out.print("Enter 1 - Simple Calculator, Enter 2 - Age Calculator: ");
  return mexam.nextInt();
}

Usage:

int calculator = menu(0);

while(calculator != 0) {
  if(calculator == 1) {
    simpleCalculator();
  } else if(calculator == 2) {
    ageCalculator();
  }      

  calculator = menu(calculator);
}
  •  Tags:  
  • java
  • Related