pw_

Interfejsy

pw_
Jan 14th, 2020
146
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 1.55 KB | None | 0 0
  1. import java.util.Scanner;
  2.  
  3. public class Main {
  4.     public static void main(String[] args) {
  5.         Main main = new Main();
  6.         Computation computation;
  7.  
  8.         if (main.shouldMultiply()) {  
  9.             computation = new Multiplication();
  10.         }
  11.         else {
  12.             computation = new Addition();
  13.         }
  14.  
  15.         double argument1 = main.getArgument();
  16.         double argument2 = main.getArgument();
  17.  
  18.         double result = computation.compute(argument1, argument2);
  19.         System.out.println("Wynik: " + result);
  20.    
  21. }
  22.     private boolean shouldMultiply() {
  23.         System.out.println("Co chcesz zrobic? 1. Mnożenie 2. Dodawanie");
  24.         Scanner scanner = new Scanner(System.in);
  25.         int x = scanner.nextInt();
  26.         switch (x) {
  27.         case 1:
  28.             return true;
  29.         case 2:
  30.             return false;
  31.        
  32.         default: throw new IllegalArgumentException("illegal");
  33.         }
  34.     }
  35.  
  36.     private double getArgument() {
  37.         System.out.println("Podaj liczbe: ");
  38.         Scanner scanner = new Scanner(System.in);
  39.         double x = scanner.nextDouble();
  40.         return x;
  41.     }
  42. }
  43.  
  44. public class Multiplication implements Computation {
  45.     public double compute(double argument1, double argument2) {
  46.     return argument1 * argument2;
  47.     }
  48. }
  49.  
  50. public class Addition implements Computation {
  51.     public double compute(double argument1, double argument2) {
  52.     return argument1 + argument2;
  53.     }
  54. }
  55.  
  56. public interface Computation {
  57.     double compute(double argument1, double argument2);
  58. }
Advertisement
Add Comment
Please, Sign In to add comment