Advertisement
joxaren

ShittyCalculator

Apr 26th, 2017
36
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.86 KB | None | 0 0
  1. import java.text.DecimalFormat;
  2.  
  3. public class Calc {
  4.  
  5.     void divide(int x, int y) {
  6.         double z = (double) x / y;
  7.         System.out.println("You divided " + x + " by " + y + ". Result is " + new DecimalFormat("##.##").format(z));
  8.     }
  9.  
  10.     void multiply(int x, int y) {
  11.         int z = x * y;
  12.         System.out.println("You multiplied " + x + " by " + y + ". Result is " + z);
  13.     }
  14.  
  15.     void increment(int x, int y) {
  16.         int z = x + y;
  17.         System.out.println("You incremented " + x + " by " + y + ". Result is " + z);
  18.     }
  19.  
  20.     void decrement(int x, int y) {
  21.         int z = x - y;
  22.         System.out.println("You decremented " + x + " by " + y + ". Result is " + z);
  23.     }
  24.  
  25.     public static void main(String[] args) {
  26.         Calc t = new Calc();
  27.         InputParser parser = new InputParser();
  28.         GettingInput h = new GettingInput();
  29.         String userInput = h.getUserInput("What would you like to do? \nYou can divide, multiply, increment or decrement two numbers.");
  30.         if (userInput.equals("*") || userInput.equals("/") || userInput.equals("divide") ||userInput.equals("+") || userInput.equals("-")){
  31.             int firstDigit = parser.getFirstDIgitValue();
  32.             int secondDigit = parser.getSecondDigitValue();
  33.             if (userInput.equals("/")|| userInput.equals("divide")) {
  34.                 t.divide(firstDigit, secondDigit);
  35.             }
  36.             if (userInput.equals("*")) {
  37.                 t.multiply(firstDigit, secondDigit);
  38.             }
  39.             if (userInput.equals("+")){
  40.                 t.increment(firstDigit, secondDigit);
  41.             }
  42.             if (userInput.equals("-")) {
  43.                 t.decrement(firstDigit, secondDigit);
  44.             }
  45.         } else {
  46.             System.out.print("Sorry, you will have to try again.");
  47.         }
  48.     }
  49. }
  50.  
  51. ________________________________________________________--
  52. import java.io.*;
  53.  
  54. public class GettingInput {
  55.  
  56.     public String getUserInput(String prompt) {
  57.         String inputLine = null;
  58.         System.out.print(prompt + " ");
  59.         try {
  60.             BufferedReader is = new BufferedReader(
  61.                     new InputStreamReader(System.in));
  62.             inputLine = is.readLine();
  63.             if (inputLine.length() == 0 ) return null;
  64.         } catch (IOException e) {
  65.             System.out.println("IOException: " + e);
  66.         }
  67.         return inputLine;
  68.     }
  69. }
  70. _________________________________________________________
  71. public class InputParser {
  72.  
  73.     GettingInput h = new GettingInput();
  74.  
  75.     int getFirstDIgitValue() {
  76.         int firstDigit = Integer.parseInt(h.getUserInput("Please enter the first digit."));
  77.         return firstDigit;
  78.     }
  79.  
  80.     int getSecondDigitValue() {
  81.         int secondDigit = Integer.parseInt(h.getUserInput("Please enter the second digit."));
  82.         return secondDigit;
  83.     }
  84. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement