 nsCelin - 3 years ago 103
Java Question

# Addition 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<>();
}
}
``````

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 Eran
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) {