Advertisement
JoshuaStrutt

Calculator Hausaufgabe Projekt - Alpha

Mar 12th, 2015
208
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 4.38 KB | None | 0 0
  1. public class Calculator {
  2.        
  3.         public Calculator(){}
  4.    
  5.         private double r1 = 0;
  6.        
  7.         public double getR1() {
  8.             return r1;
  9.         }
  10.  
  11.         public void setR1(double r1) {
  12.             this.r1 = r1;
  13.         }
  14.  
  15.         private double r2 = 0;
  16.        
  17.         public double getR2() {
  18.             return r2;
  19.         }
  20.  
  21.         public void setR2(double r2) {
  22.             this.r2 = r2;
  23.         }
  24.        
  25.         public void setRegisters(double r1, double r2){
  26.             this.r1 = r1;
  27.             this.r2 = r2;
  28.         }
  29.        
  30.         public void calculate(int operator){
  31.             calculate(Operator.values()[operator]);
  32.         }
  33.        
  34.         public void calculate(Operator operator){
  35.             switch(operator){
  36.                 case ADD:
  37.                     r1 = r1 + r2;
  38.                     break;
  39.                 case SUBSTRACT:
  40.                     r1 = r1 - r2;
  41.                     break;
  42.                 case MULTIPlY:
  43.                     r1 = r1 * r2;
  44.                     break;
  45.                 case DIVIDE:
  46.                     r1 = r1 / r2;
  47.                     break;
  48.                 case SQRT:
  49.                     r1 = Math.sqrt(r1);
  50.                     break;
  51.                 case SQRT_N:
  52.                     r1 = Math.pow(r1, 1/r2);
  53.                     break;
  54.                 case POW:
  55.                     r1 = Math.pow(r1, r2);
  56.                     break;
  57.                 case PERCENT:
  58.                     r1 = r1 * (r2/100);
  59.                     break;
  60.                 case CLEAR:
  61.                     r1 = 0;
  62.                     r2 = 0;
  63.                     break;
  64.             }
  65.         }
  66.        
  67. }
  68.  
  69.  
  70. public enum Operator{
  71.     ADD(0),
  72.     SUBSTRACT(1),
  73.     MULTIPlY(2),
  74.     DIVIDE(3),
  75.     SQRT(4),
  76.     SQRT_N(5),
  77.     POW(6),
  78.     PERCENT(7),
  79.     CLEAR(8);
  80.    
  81.     private int value;
  82.     private Operator(int value){
  83.         this.value = value;
  84.     }
  85.     public int getValue(){
  86.         return this.value;
  87.     }
  88.    
  89. }
  90.  
  91. import java.io.*;
  92. class Keyboard{
  93.  
  94.   public static String readLine(){
  95.       try {
  96.           BufferedReader in = new BufferedReader(new InputStreamReader(System.in));
  97.           return (in.readLine());
  98.       } catch(Exception e) {
  99.           return ("");
  100.       }
  101.   }
  102.  
  103.   public static double readDouble(){
  104.       double enter = Double.parseDouble(readLine());
  105.       return (enter);
  106.   }
  107.  
  108.   public static int readInteger(){
  109.       int enter = Integer.parseInt(readLine());
  110.       return (enter);
  111. }
  112.  
  113.   public static char readChar(){
  114.       char ch =  readLine().charAt(0);
  115.       return (ch);
  116.   }
  117.  
  118.   public static void writeLine(String value){
  119.       System.out.println(value);
  120.   }
  121.  
  122.   public static void writeLine(Double value){
  123.       System.out.println(value);
  124.   }
  125.  
  126.   public static void writeLine(int value){
  127.       System.out.println(value);
  128.   }
  129.  
  130.   public static void writeLine(float value){
  131.       System.out.println(value);
  132.   }
  133.  
  134.   public static void writeLine(Object value){
  135.       System.out.println(value);
  136.   }
  137.  
  138.   public static void writeLine(char[] value){
  139.       System.out.println(value);
  140.   }
  141.  
  142.   public static void writeLine(boolean value){
  143.       System.out.println(value);
  144.   }
  145.  
  146.   public static void writeF(String format, Object args){
  147.       System.out.printf(format,args);
  148.   }
  149.  
  150.  
  151. }
  152.  
  153.  
  154. public class Main {
  155.  
  156.     public static void main(String[] args) {
  157.        
  158.         Calculator calc = new Calculator();
  159.        
  160.         boolean exit = false;
  161.  
  162.         while (!exit){
  163.             Keyboard.writeLine("");
  164.             Keyboard.writeLine("- OPERATION -");
  165.             for (Operator op : Operator.values()) {
  166.                 Keyboard.writeLine(op.getValue() + ". " + op.name());
  167.             }
  168.             Keyboard.writeLine("");
  169.             Keyboard.writeLine("- PARAMETERS -");
  170.             Keyboard.writeLine(Operator.values().length + ". Change the 1st number (VALUE: " + calc.getR1() + ")");
  171.             Keyboard.writeLine((Operator.values().length + 1) + ". Change the 2nd number (VALUE: " + calc.getR2() + ")");
  172.             Keyboard.writeLine("");
  173.             Keyboard.writeLine("- COMMANDS -");
  174.             Keyboard.writeLine((Operator.values().length + 2) + ". Close program");
  175.             Keyboard.writeLine("");
  176.             Keyboard.writeLine("Please, enter the command you wish to execute: ");
  177.             try{
  178.                 int selection = Keyboard.readInteger();
  179.                 if (selection < Operator.values().length && selection >= 0){
  180.                     calc.calculate(selection);
  181.                 }
  182.                 else{
  183.                     selection = selection - Operator.values().length;
  184.                     switch (selection){
  185.                         case 0:
  186.                             Keyboard.writeLine("Please, enter a new value for the 1st number:");
  187.                             double value1 = Keyboard.readDouble();
  188.                             calc.setR1(value1);
  189.                             break;
  190.                         case 1:
  191.                             Keyboard.writeLine("Please, enter a new value for the 2nd number:");
  192.                             double value2 = Keyboard.readDouble();
  193.                             calc.setR2(value2);
  194.                             break;
  195.                         case 2:
  196.                             Keyboard.writeLine("Bye! Bye!");
  197.                             exit = true;
  198.                             break;
  199.                         default:
  200.                             Keyboard.writeLine("Command not found");
  201.                             break;
  202.                     }
  203.                 }
  204.             }
  205.             catch (Exception e){
  206.                 Keyboard.writeLine("Error: " + e.toString());
  207.             }      
  208.         }
  209.        
  210.     }
  211.  
  212. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement