4
  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)));
        }
    }
  1. I have the below problem which I am trying to solve in Java 8 using BinaryOperator.There are three inputs to this application [mealCost(double), tipPercent(int),taxPercent(int)].
  2. I am trying to calculate the below values :

    tip = (mealCost*tipPercent)/100;
    tax = (mealCost*taxPercent)/100;
    TotalCost = mealCost+tip +tax;   
    
  3. I am unable to pass an integer input to the apply method of BinaryOperator. Also the calculated value into biList is not proper. Below is my code

1
  • you also might want to use DoubleBinaryOperator which uses primitive values instead of the Double wrapper Commented Jul 26, 2017 at 7:03

3 Answers 3

2

The other answers have already told you the root cause of the error in calculation. Regarding the second part of your question:

I am unable to pass an integer input to the apply method of BinaryOperator

This is because you have declared that your BinaryOperator takes Double as the input parameter type and return type. BinaryOperator takes only one Type as parameter and which is the type of both the input parameters as well as the return type, so if you have Double as method parameters and Double as the return type also, you can decide to use BinaryOperator. If you have multiple types as parameters and return type, you can consider using BiFunction.

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

Here we are saying that the input parameters are Double and Inetger corresponding to the mealCost and taxPercent and the return type is a Double.

You can even define your own functional interface with more number of parameters like in example below:

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);
    }
Sign up to request clarification or add additional context in comments.

Comments

1

You are putting the same key twice in a Map, so the second value overrides the first value. I don't think a Map is appropriate for these calculations. You can use a List<SomePairType> instead.

Or keep mealCost in one variable and the other values in a 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)));
    }

Comments

0

You are putting the values for the same key in the map. So the initial value was getting overridden by the new value.

Try the code below

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)));
    }
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.