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

edu.pdx.cs.joy.di.BookStore Maven / Gradle / Ivy

The newest version!
package edu.pdx.cs.joy.di;

import java.util.List;

/**
 * A book store that depends on a {@link BookInventory} and a {@link CreditCardService}
 */
public class BookStore
{
    private final BookInventory inventory;

    private final CreditCardService cardService;

    public BookStore( BookInventory inventory, CreditCardService cardService )
    {
        this.inventory = inventory;
        this.cardService = cardService;
    }

    /**
     * Purchases a number of books from this book store
     *
     * @return the total amount of the purchase
     * @throws CreditCardTransactionException
     *         If a problem occurs while paying for the purchase
     */
    public double purchase( List books, CreditCard card) {
        double total = 0.0d;
        for (Book book : books) {
            inventory.remove(book);
            total += book.getPrice();
        }

        CreditTransactionCode code = cardService.debit(card, total);
        if (code == CreditTransactionCode.SUCCESS ) {
            return total;

        } else {
            throw new CreditCardTransactionException(code);
        }
    }

}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy