letsdoitjava

Breaking It Down

Jun 17th, 2019
164
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 3.38 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.             userInput = getOperandAndValidateThatItIsAValidNumber(scan);
  31.             // storing user input
  32.             operand1 = Integer.parseInt(userInput);
  33.  
  34.             whichNumber = 2;
  35.             System.out.println("Enter operand " + whichNumber);
  36.             userInput = getOperandAndValidateThatItIsAValidNumber(scan);
  37.  
  38.             operand2 = Integer.parseInt(userInput);
  39.  
  40.             answer = doArithmetic(operand1, operand2, operator);
  41.  
  42.             System.out.println(operand1 + " " + operator + " " + operand2 + " = " + answer);
  43.             System.out.println("=======================");
  44.         }
  45.         // this is inside while loop here
  46.         System.out.println("Finished Calculations");
  47.         scan.close();
  48.     }
  49.    
  50.     private static char doCalculation(Scanner scan, char operator) {
  51.         boolean operator_is_good = false;
  52.  
  53.         do {
  54.             System.out.print("Enter an Operator: + - * / q for quit: ");
  55.             String strOperator = scan.nextLine();
  56.             strOperator = strOperator.trim();
  57.             if (strOperator.length() == 0)
  58.                 continue; // Need to try this again with no input
  59.             operator = strOperator.charAt(0);
  60.             operator_is_good = false;
  61.             switch (operator) {
  62.             case 'q':
  63.             case '+':
  64.             case '-':
  65.             case '*':
  66.             case '/':
  67.                 operator_is_good = true;
  68.                 break;
  69.             default:
  70.                 System.out.println("Your operator is bad ... try again:");
  71.                 break;
  72.             }
  73.         } while (!operator_is_good);
  74.         return operator;
  75.     }
  76.  
  77.     private static int doArithmetic(int operand1, int operand2, char operator) {
  78.         int answer;
  79.  
  80.         switch (operator) {
  81.         // identifying each valid operator response from user
  82.         case '+':
  83.             answer = operand1 + operand2;
  84.             break;
  85.         case '-':
  86.             answer = operand1 - operand2;
  87.             break;
  88.         case '*':
  89.             answer = operand1 * operand2;
  90.             break;
  91.         case '/':
  92.             answer = operand1 / operand2;
  93.             break;
  94.         default:
  95.             System.out.println("We shouldn't get here in doArithmentic!!!!");
  96.             answer = -1;
  97.             break;
  98.         }
  99.         return answer;
  100.     }
  101.  
  102.     private static String getOperandAndValidateThatItIsAValidNumber(Scanner scan) {
  103.         boolean operand_is_bad;
  104.         String input;
  105.         do {
  106.             // if user provides bad input
  107.             operand_is_bad = false;
  108.             input = scan.nextLine();
  109.             // trims trailing and leading spaces from user input
  110.             input = input.trim();
  111.             if (input.length() == 0)
  112.                 operand_is_bad = true;
  113.             for (int i = 0; i < input.length(); i++) {
  114.                 char c = input.charAt(i);
  115.                 // when left side is true, evaluate right side
  116.                 if (c < '0' || c > '9') {
  117.                     operand_is_bad = true;
  118.                     // prints when user gives a bad input
  119.                     System.out.println("Your last input was bad, try again");
  120.                 }
  121.             }
  122.         } while (operand_is_bad);
  123.         return input;
  124.     }
  125. }
Advertisement
Add Comment
Please, Sign In to add comment