All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.wix.restaurants.helpers.PriceCalculator Maven / Gradle / Ivy

There is a newer version: 1.22.0
Show newest version
package com.wix.restaurants.helpers;

import com.openrest.v1_1.OrderItem;

import java.math.BigDecimal;
import java.util.Arrays;
import java.util.List;

public class PriceCalculator {
    public double price(OrderItem orderItem) {
        return priceImpl(orderItem).doubleValue();
    }

    public double price(List orderItems) {
        return priceImpl(orderItems).doubleValue();
    }

    public double price(OrderItem... orderItems) {
        return price(Arrays.asList(orderItems));
    }

    private BigDecimal priceImpl(OrderItem orderItem) {
        BigDecimal choicesTotal = new BigDecimal(0);
        for (List choices : orderItem.variationsChoices) {
            choicesTotal = choicesTotal.add(priceImpl(choices));
        }
        return BigDecimal.valueOf(orderItem.price).movePointLeft(2)
                .add(choicesTotal)
                .multiply(BigDecimal.valueOf(orderItem.count));
    }

    private BigDecimal priceImpl(List orderItems) {
        BigDecimal total = new BigDecimal(0);
        for (OrderItem orderItem : orderItems) {
            total = total.add(priceImpl(orderItem));
        }
        return total;
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy