Class LineItem

  • All Implemented Interfaces:
    Taxable
    Direct Known Subclasses:
    NumberedLineItem

    public class LineItem
    extends java.lang.Object
    implements Taxable
    Represents one entry (or element) for an Order. Central objects that holds all line items related data required for amount calculation. A LineItem is Taxable which means getTotal(Order) is used for tax calculation. This line item holds the basePrice and the numberOfUnits. Additionally the giveAwayCount can be set (10 items are bought, 5 of them are free) and this line item holds a ordered List of LineItemDiscounts and LineItemCharges. The line item must use the same Currency as the linked Order or a CurrenciesAreNotEqualException is thrown.
    • Constructor Detail

      • LineItem

        public LineItem​(Money basePrice)
        Creates a new line item with the given base price and one unit.
      • LineItem

        public LineItem​(Money basePrice,
                        int numberOfUnits)
        Creates a new line item with the given base price and number of units.
    • Method Detail

      • getSubTotal

        public Money getSubTotal()
        Calculates the line item sub total. This is just the bas price multiplied by the number of applicable units (see setGiveAwayUnits(int) for how some units may be excluded).
        See Also:
        getTotal(Order)
      • getTotalDiscount

        public Money getTotalDiscount()
        Calculates the total of all line item discounts.
        See Also:
        getTotalDiscounts()
      • getTotalCharge

        public Money getTotalCharge()
        Calculates the total of all line item charges.
        See Also:
        getTotalCharges()
      • setOrder

        public void setOrder​(Order order)
        Sets the Order to for this line item belongs to. Is needed for the calculation because all line item must have the same currency (Order.getCurrency() is used to check it).
      • getOrder

        public Order getOrder()
        Returns:
        the order this line item belongs to. Can be null but no calculation is then possible!
        Throws:
        MissingCalculationDataException - if order is null and this method or any calculation method (getTotal...) is called.
      • getBasePrice

        public Money getBasePrice()
        Returns the base price of this line item.
      • toString

        public java.lang.String toString()
        {numberOfUnits}x {basePrice} {currency}(free:{giveAwayCount}) discounts:[...] charges:[...]
        Overrides:
        toString in class java.lang.Object
      • getDiscounts

        public java.util.List<LineItemDiscount> getDiscounts()
        Returns all discounts assigned to this line item.
      • addDiscounts

        public void addDiscounts​(LineItemDiscount... discounts)
        Adds multiple discounts to this line item.
        Throws:
        CurrenciesAreNotEqualException - in case at least one discount is absolute and uses a different currency than the enclosing order
      • addDiscounts

        public void addDiscounts​(java.util.List<LineItemDiscount> discounts)
        Adds multiple discounts to this line item.
        Throws:
        CurrenciesAreNotEqualException - in case at least one discount is absolute and uses a different currency than the enclosing order
      • addDiscount

        public void addDiscount​(int index,
                                LineItemDiscount discount)
        Adds a single discount to this line item at a specific position.
        Parameters:
        index - the position to add the discount at
        Throws:
        CurrenciesAreNotEqualException - in case at it's a absolute discount and uses a different currency than the enclosing order
      • assertCurrency

        protected void assertCurrency​(AbstractAmount amount)
      • addDiscount

        public void addDiscount​(LineItemDiscount discount)
        Adds a single discount to this line item.
        Throws:
        CurrenciesAreNotEqualException - in case at it's a absolute discount and uses a different currency than the enclosing order
      • clearDiscounts

        public void clearDiscounts()
        Removes all attached discounts of the current line item.
      • removeDiscount

        public void removeDiscount​(LineItemDiscount discount)
        Removes a single discount from this line item.
      • getCharges

        public java.util.List<LineItemCharge> getCharges()
        Returns all charges assigned to this line item.
      • addCharges

        public void addCharges​(LineItemCharge... lineItemCharges)
        Adds multiple charges to this line item.
        Throws:
        CurrenciesAreNotEqualException - in case there is at least one absolute charge which uses a different currency than the enclosing order
      • addCharges

        public void addCharges​(java.util.List<LineItemCharge> lineItemCharges)
        Adds multiple charges to this line item.
        Throws:
        CurrenciesAreNotEqualException - in case there is at least one absolute charge which uses a different currency than the enclosing order
      • addCharge

        public void addCharge​(LineItemCharge lineItemCharge)
        Adds a single charge to this line item.
        Throws:
        CurrenciesAreNotEqualException - in case at it's a absolute charge and uses a different currency than the enclosing order
      • addCharge

        public void addCharge​(int index,
                              LineItemCharge lineItemCharge)
        Adds a single charge to this line item at a specific position.
        Parameters:
        index - the position to add the charge at
        Throws:
        CurrenciesAreNotEqualException - in case at it's a absolute charge and uses a different currency than the enclosing order
      • clearCharges

        public void clearCharges()
        Removes all attached charges of the current line item.
      • removeCharge

        public void removeCharge​(LineItemCharge charge)
        Removes a charge from this line item.
      • getNumberOfUnitsForCalculation

        public int getNumberOfUnitsForCalculation()
        Returns the number of units that are applicable for calculation. Basically this is getNumberOfUnits() - getGiveAwayUnits().
      • getGiveAwayUnits

        public int getGiveAwayUnits()
        Tells the number of units to be 'for free' meaning that they're excluded from calculation completely. In consequence for calculation this line item appears to hold <numberOfUnits> - <giveAwayCount> units. In case the number of give-away units is greater than the actual available number of units the line item appear to have zero units.
        See Also:
        setNumberOfUnits(int), getNumberOfUnits(), setGiveAwayUnits(int)
      • setGiveAwayUnits

        public final void setGiveAwayUnits​(int giveAwayCount)
        Changes the number of units to be 'for free' meaning that they're excluded from calculation completely. In consequence for calculation this line item appears to hold <numberOfUnits> - <giveAwayCount> units. In case the number of give-away units is greater than the actual available number of units the line item appear to have zero units.
        See Also:
        setNumberOfUnits(int), getNumberOfUnits(), getGiveAwayUnits()
      • getTaxes

        public java.util.Collection<Tax> getTaxes()
        Returns all taxes which are targeting this line item.
        See Also:
        Order.getTaxesFor(Taxable)