Adding in Java 8 using BinaryOperator

  package com.operators;

    import java.util.ArrayList;
    import java.util.HashMap;
    import java.util.List;
    import java.util.Map;
    import java.util.Scanner;
    import java.util.function.BinaryOperator;

    public class TotalCost {

        public static void main(String[] args) {
            Scanner scan = new Scanner(System.in);
            double mealCost = scan.nextDouble(); // original meal price
            int tipPercent = scan.nextInt(); // tip percentage
            int taxPercent = scan.nextInt(); // tax percentage
            scan.close();

            Map<Double,Double> map = new HashMap<>();
            map.put(mealCost, (double)tipPercent);
            map.put(mealCost, (double)taxPercent);

            BinaryOperator<Double> opPercent = (t1,t2) -> (t1*t2)/100;
            BinaryOperator<Double> opSum = (t1,t2) -> (t1+t2);   
            calculation(opPercent,map);
        }

        public static void calculation(BinaryOperator<Double> opPercent , Map<Double,Double> map) {
            List<Double> biList = new ArrayList<>();
            map.forEach((s1,s2)-> biList.add(opPercent.apply(s1, s2)));
        }
    }
  • I have the following problem that I am trying to solve in Java 8 using BinaryOperator. This application includes three inputs [mealCost (double), tipPercent (int), taxPercent (int)].
  • I am trying to calculate the values ​​below:

    tip = (mealCost*tipPercent)/100;
    tax = (mealCost*taxPercent)/100;
    TotalCost = mealCost+tip +tax;   
    
  • I cannot pass integer input to the BinaryOperator application method. Also, the calculated value in biList is not correct. Below is my code

+4
source share
3 answers

You put the same key twice on the card, so the second value overrides the first value. I do not think that is suitable for these calculations Map. You can use instead List<SomePairType>.

mealCost , List<Double>:

    public static void calculation(BinaryOperator<Double> opPercent, Double cost, List<Double> rates) {
        List<Double> biList = new ArrayList<>();
        rates.forEach(d-> biList.add(opPercent.apply(cost, d)));
    }
0

. :

apply BinaryOperator

, , BinaryOperator Double . BinaryOperator "" , , , Double Double , BinaryOperator. , BiFunction.

BiFunction<Double, Integer, Double> opPercent = (t1,t2) -> (t1*t2)/100;

, Double Inetger mealCost taxPercent, - Double.

, :

public class TotalCost {

   public static void main(String[] args) {
      Scanner scan = new Scanner(System.in);
      double mealCost = scan.nextDouble(); // original meal price
      int tipPercent = scan.nextInt(); // tip percentage
      int taxPercent = scan.nextInt(); // tax percentage
      scan.close();

      TriFunction<Double, Integer, Integer, Double> calcCost = (cost, tipPct, taxPcnt) -> 
                                        (cost + (cost * tipPct/100) + (cost * taxPcnt/100));
      Double totalBill = calculation(calcCost, mealCost, tipPercent, taxPercent);
      System.out.println(totalBill);
   }

    public static Double calculation(TriFunction<Double, Integer, Integer, Double> calcCost , 
                                       Double mealCost, Integer tipPct, Integer taxPct) {
       return calcCost.apply(mealCost, tipPct, taxPct);
        }
  }

    @FunctionalInterface
    interface TriFunction<T,U,V,R> {
        R apply(T t, U u, V v);
    }
+2

. , .

package com.operators;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Scanner;
import java.util.function.BinaryOperator;

public class TotalCost {

    public static void main(String[] args) {
        Scanner scan = new Scanner(System.in);
        double mealCost = scan.nextDouble(); // original meal price
        int tipPercent = scan.nextInt(); // tip percentage
        int taxPercent = scan.nextInt(); // tax percentage
        scan.close();

        Map<Double,Double> map = new HashMap<>();

        map.put(mealCost, (double)taxPercent + (double)tipPercent);

        BinaryOperator<Double> opPercent = (t1,t2) -> (t1*t2)/100;
        BinaryOperator<Double> opSum = (t1,t2) -> (t1+t2);   
        calculation(opPercent,map);
    }

    public static void calculation(BinaryOperator<Double> opPercent , Map<Double,Double> map) {
        List<Double> biList = new ArrayList<>();
        map.forEach((s1,s2)-> biList.add(opPercent.apply(s1, s2)));
    }
}
0
source

Source: https://habr.com/ru/post/1682294/


All Articles