letsdoitjava

Too Much In Main

Jun 23rd, 2019
166
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 3.75 KB | None | 0 0
  1. // Breaking it down problem, too much was in main, broke it down
  2. // Renee Waggoner
  3. // June 24, 2019
  4. // Special Requirements: None
  5.  
  6. package breaking_it_down;
  7. import java.util.Scanner;
  8.  
  9. public class TooMuchInMain {
  10.     public static void main(String[] args) {
  11.         Scanner scan = new Scanner(System.in);
  12.         int operand1, operand2, answer;
  13.         char operator='q';
  14.         // highest priority for single calculation
  15.         boolean keepCalculating = true;
  16.         while (keepCalculating) {
  17.  
  18.             // calling operator for calculation with do calculation method with parameters
  19.             operator = doCalculation(scan, operator);
  20.  
  21.             //  if true do this
  22.             if (operator == 'q') {
  23.                 break;
  24.             }
  25.             int whichNumber;
  26.             // assigning a value
  27.             whichNumber = 1;
  28.             System.out.println("Enter an operator: + - * / q for quit: " + whichNumber);
  29.             String userInput;
  30.             // has to match below private static
  31.             userInput = getNumber(scan);
  32.             // storing user input, parseInt for throwing exceptions
  33.             operand1 = Integer.parseInt(userInput);
  34.  
  35.             whichNumber = 2;
  36.             System.out.println("Enter operand " + whichNumber);
  37.             // has to match below private static
  38.             userInput = getNumber(scan);
  39.  
  40.             operand2 = Integer.parseInt(userInput);
  41.             // matching below private static int
  42.             answer = doArithmetic(operand1, operand2, operator);
  43.  
  44.             System.out.println(operand1 + " " + operator + " " + operand2 + " = " + answer);
  45.             System.out.println("=======================");
  46.         } // end of while (continue_calculating)
  47.         System.out.println("Finished Calculations");
  48.         scan.close();
  49.     }       // end of main
  50.  
  51.     private static char doCalculation(Scanner scan, char operator) {
  52.         // Top level routine for doing a single calculation
  53.         boolean operator_is_good = false;
  54.  
  55.         do {
  56.             System.out.print("Enter an Operator: + - * / q for quit: ");
  57.             String strOperator = scan.nextLine();
  58.             strOperator = strOperator.trim();
  59.             if (strOperator.length() == 0)
  60.                 continue; // Need to try this again with no input
  61.             operator = strOperator.charAt(0);
  62.             operator_is_good = false;
  63.             switch (operator) {
  64.             case 'q':
  65.             case '+':
  66.             case '-':
  67.             case '*':
  68.             case '/':
  69.                 operator_is_good = true;
  70.                 break;
  71.             default:
  72.                 System.out.println("Your operator is bad ... try again:");
  73.                 break;
  74.             }
  75.         } while (!operator_is_good);
  76.         return operator;
  77.     }
  78.  
  79.     private static int doArithmetic(int operand1, int operand2, char operator) {
  80.         int answer;
  81.  
  82.         switch (operator) {
  83.         // identifying each valid operator response from user
  84.         case '+':
  85.             answer = operand1 + operand2;
  86.             break;
  87.         case '-':
  88.             answer = operand1 - operand2;
  89.             break;
  90.         case '*':
  91.             answer = operand1 * operand2;
  92.             break;
  93.         case '/':
  94.             answer = operand1 / operand2;
  95.             break;
  96.         default:
  97.             System.out.println("We shouldn't get here in doArithmentic!!!!");
  98.             answer = -1;
  99.             break;
  100.         }
  101.         return answer;
  102.     }
  103.     // validating user number
  104.     private static String getNumber(Scanner scan) {
  105.         boolean operand_is_bad;
  106.         String input;
  107.         do {
  108.             // if user provides bad input
  109.             operand_is_bad = false;
  110.             input = scan.nextLine();
  111.             // trims trailing and leading spaces (whitespace) from user input, doesn't change original string
  112.             input = input.trim();
  113.             // setting allowed length of user response, won't accept an empty response from user
  114.             // (just repeats request for operator if they hit enter)
  115.             if (input.length() == 0)
  116.                 operand_is_bad = true;
  117.             for (int i = 0; i < input.length(); i++) {
  118.                 // getting user input
  119.                 char c = input.charAt(i);
  120.                 // when left side is true, evaluate right side
  121.                 if (c < '0' || c > '9') {
  122.                     operand_is_bad = true;
  123.                     // prints when user gives a bad input
  124.                     System.out.println("Your last input was bad, try again");
  125.                 }
  126.             }
  127.         } while (operand_is_bad);
  128.         return input;
  129.     }
  130. }
Advertisement
Add Comment
Please, Sign In to add comment