001package edu.pdx.cs410J.di;
002
003import java.util.List;
004
005/**
006 * A book store that depends on a {@link BookInventory} and a {@link CreditCardService}
007 */
008public class BookStore
009{
010    private final BookInventory inventory;
011
012    private final CreditCardService cardService;
013
014    public BookStore( BookInventory inventory, CreditCardService cardService )
015    {
016        this.inventory = inventory;
017        this.cardService = cardService;
018    }
019
020    /**
021     * Purchases a number of books from this book store
022     *
023     * @return the total amount of the purchase
024     * @throws CreditCardTransactionException
025     *         If a problem occurs while paying for the purchase
026     */
027    public double purchase( List<Book> books, CreditCard card) {
028        double total = 0.0d;
029        for (Book book : books) {
030            inventory.remove(book);
031            total += book.getPrice();
032        }
033
034        CreditTransactionCode code = cardService.debit(card, total);
035        if (code == CreditTransactionCode.SUCCESS ) {
036            return total;
037
038        } else {
039            throw new CreditCardTransactionException(code);
040        }
041    }
042
043}