Advertisement
Guest User

Untitled

a guest
Sep 23rd, 2019
99
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 5.88 KB | None | 0 0
  1. import java.util.Scanner;
  2. public class Calculator{
  3.      
  4.    private String chooseOperand;
  5.  //  private boolean userAnswer;
  6.    private Scanner userKeyboard;
  7.    private boolean askOperand;
  8.    private boolean runAskNumber;
  9.    private boolean wantsUseCalculate;
  10.    private boolean wantsToRerun;
  11.    private int count;
  12.    private double numbOne;
  13.    private double numbTwo;
  14.    private double operationTotal;
  15.    
  16.    public Calculator(){
  17.       userKeyboard = new Scanner(System.in);
  18.       askOperand = true;
  19.       runAskNumber = false;
  20.  
  21.    }
  22.  
  23.    public void printIntro(){
  24.       System.out.println("Hi, Welcome to Calculator");
  25.       System.out.println("Please input what function you would like to do");
  26.       System.out.println("If you would like to add, type: + ");
  27.       System.out.println("If you would like to subtract, type: - ");
  28.       System.out.println("If you would like to multiply, type: * ");
  29.       System.out.println("If you would like to divide, type: / ");
  30.       System.out.println("Then hit enter");
  31.      
  32.    }
  33.    public void calculate(){
  34.         System.out.println("In Calculate Method");
  35.         do {
  36.             while (askOperand == true){
  37.                System.out.println("Please input what function you would like the Calculator to perform: ");
  38.                chooseOperand = userKeyboard.nextLine();
  39.                if (chooseOperand.substring(0).equalsIgnoreCase("*")){
  40.                   System.out.println("You want to multiply");
  41.                   askOperand = false; //ends current loop
  42.                   runAskNumber = true; //starts next loop to ask them for a number
  43.                }
  44.                else if(chooseOperand.substring(0).equalsIgnoreCase("/")){
  45.                   System.out.println("You want to divide");  
  46.                   askOperand = false;  
  47.                   runAskNumber = true;            
  48.                }
  49.                else if(chooseOperand.substring(0).equalsIgnoreCase("+")){
  50.                   System.out.println("You Want to add");
  51.                   askOperand = false;
  52.                   runAskNumber = true;
  53.                }
  54.                
  55.                else if(chooseOperand.substring(0).equalsIgnoreCase("-")){
  56.                   System.out.println("You want to subtract");
  57.                   askOperand = false;
  58.                   runAskNumber = true;
  59.                }
  60.                else{
  61.                   System.out.println("Please input either: * / + -");
  62.                   System.out.println("For example: *");
  63.                }
  64.                
  65.                while (runAskNumber == true){
  66.                   System.out.println("What's your first number?");
  67.                      try{
  68.                         numbOne = userKeyboard.nextDouble();
  69.                         System.out.println("What's your second number?");
  70.                         numbTwo = userKeyboard.nextDouble();
  71.                        
  72.                         runAskNumber = false;
  73.                      }
  74.                      catch (Exception ex) {
  75.                         System.out.println("Please enter a number:");
  76.                         System.out.println("Example: 98");
  77.                         System.exit(1);
  78.                      }
  79.                      if (chooseOperand.substring(0).equalsIgnoreCase("*")){
  80.                         operationTotal = numbOne * numbTwo;
  81.                         System.out.println("Your Total is: " + operationTotal);
  82.                         wantsToRerun = true;
  83.                      }
  84.                      else if (chooseOperand.substring(0).equalsIgnoreCase("/")){
  85.                         operationTotal = numbOne / numbTwo;
  86.                         System.out.println("Your Total is: " + operationTotal);
  87.                         wantsToRerun = true;
  88.                      }
  89.                      else if (chooseOperand.substring(0).equalsIgnoreCase("+")){
  90.                         operationTotal = numbOne + numbTwo;
  91.                         System.out.println("Your Total is: " + operationTotal);
  92.                         wantsToRerun = true;
  93.                      }
  94.                      else {
  95.                         operationTotal = numbOne - numbTwo;
  96.                         System.out.println(operationTotal);
  97.                         wantsToRerun = true;
  98.                      }
  99.                      System.out.println("Would you like to do another calculation?");
  100.                      System.out.println("To do another calculation type: Yes");
  101.                      System.out.println("Quit the program type: No");
  102.                      System.out.println(" ------ ");
  103.                      userKeyboard.nextLine();
  104.                      /* ^ added because nextInt(); reads only one int and doesn't finish the line. So it reads the call after it asked for the 2nd number.
  105.                      https://stackoverflow.com/questions/7877529/java-string-scanner-input-does-not-wait-for-info-moves-directly-to-next-stateme */
  106.                      while (wantsToRerun == true){
  107.                         String userAnswer = userKeyboard.nextLine();
  108.                          if (userAnswer.contains("yes") || userAnswer.contains("Yes")){
  109.                             askOperand = true;
  110.                             wantsToRerun = false;
  111.                          }
  112.                          else if (userAnswer.contains("no") || userAnswer.contains("No")){
  113.                             wantsUseCalculate = false;
  114.                          }
  115.                          else {
  116.                            System.out.println("Please input either yes or no");
  117.                          }
  118.                      }  
  119.                }
  120.             }          
  121.         } while(wantsUseCalculate == true);
  122.    }
  123.    
  124.    
  125.    public void printReport(){
  126.      
  127.    }    
  128.  
  129.    public static void main(String args[]){
  130.       Calculator myCalculator = new Calculator();
  131.       myCalculator.printIntro();
  132.       myCalculator.calculate();
  133.      
  134.          
  135.    }
  136. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement