Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // Breaking it down problem, too much was in main, broke it down
- // Renee Waggoner
- // June 24, 2019
- // Special Requirements: None
- package breaking_it_down;
- import java.util.Scanner;
- public class TooMuchInMain {
- public static void main(String[] args) {
- Scanner scan = new Scanner(System.in);
- int operand1, operand2, answer;
- char operator='q';
- // highest priority for single calculation
- boolean keepCalculating = true;
- while (keepCalculating) {
- // calling operator for calculation with do calculation method with parameters
- operator = doCalculation(scan, operator);
- // if true do this
- if (operator == 'q') {
- break;
- }
- int whichNumber;
- // assigning a value
- whichNumber = 1;
- System.out.println("Enter an operator: + - * / q for quit: " + whichNumber);
- String userInput;
- // has to match below private static
- userInput = getNumber(scan);
- // storing user input, parseInt for throwing exceptions
- operand1 = Integer.parseInt(userInput);
- whichNumber = 2;
- System.out.println("Enter operand " + whichNumber);
- // has to match below private static
- userInput = getNumber(scan);
- operand2 = Integer.parseInt(userInput);
- // matching below private static int
- answer = doArithmetic(operand1, operand2, operator);
- System.out.println(operand1 + " " + operator + " " + operand2 + " = " + answer);
- System.out.println("=======================");
- } // end of while (continue_calculating)
- System.out.println("Finished Calculations");
- scan.close();
- } // end of main
- private static char doCalculation(Scanner scan, char operator) {
- // Top level routine for doing a single calculation
- boolean operator_is_good = false;
- do {
- System.out.print("Enter an Operator: + - * / q for quit: ");
- String strOperator = scan.nextLine();
- strOperator = strOperator.trim();
- if (strOperator.length() == 0)
- continue; // Need to try this again with no input
- operator = strOperator.charAt(0);
- operator_is_good = false;
- switch (operator) {
- case 'q':
- case '+':
- case '-':
- case '*':
- case '/':
- operator_is_good = true;
- break;
- default:
- System.out.println("Your operator is bad ... try again:");
- break;
- }
- } while (!operator_is_good);
- return operator;
- }
- private static int doArithmetic(int operand1, int operand2, char operator) {
- int answer;
- switch (operator) {
- // identifying each valid operator response from user
- case '+':
- answer = operand1 + operand2;
- break;
- case '-':
- answer = operand1 - operand2;
- break;
- case '*':
- answer = operand1 * operand2;
- break;
- case '/':
- answer = operand1 / operand2;
- break;
- default:
- System.out.println("We shouldn't get here in doArithmentic!!!!");
- answer = -1;
- break;
- }
- return answer;
- }
- // validating user number
- private static String getNumber(Scanner scan) {
- boolean operand_is_bad;
- String input;
- do {
- // if user provides bad input
- operand_is_bad = false;
- input = scan.nextLine();
- // trims trailing and leading spaces (whitespace) from user input, doesn't change original string
- input = input.trim();
- // setting allowed length of user response, won't accept an empty response from user
- // (just repeats request for operator if they hit enter)
- if (input.length() == 0)
- operand_is_bad = true;
- for (int i = 0; i < input.length(); i++) {
- // getting user input
- char c = input.charAt(i);
- // when left side is true, evaluate right side
- if (c < '0' || c > '9') {
- operand_is_bad = true;
- // prints when user gives a bad input
- System.out.println("Your last input was bad, try again");
- }
- }
- } while (operand_is_bad);
- return input;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment