edu.uci.ics.inf111.dvdvendor.app
Class DVDVendor

java.lang.Object
  extended by edu.uci.ics.inf111.dvdvendor.app.DVDVendor
All Implemented Interfaces:
DVDDispenserListener

public class DVDVendor
extends java.lang.Object
implements DVDDispenserListener

The DVDVendor class contains the business logic of the point of sale, and keeps track of the state of the current customer's rental and return checkout. The class contains methods to handle renting DVDs and returning them.


Nested Class Summary
static class DVDVendor.checkOutState
          This enumeration represents the states of the DVDVendor system:
READY means the system is awaiting a new customer
States for Rental:
ADDING means the system is prepared for another item to be added
PAYING means the system is awaiting for payment
DISPENSING means the system is awaiting notification that the items have been dispensed in the DVD Dispenser
States for Returning:
RECEIVING means the system is awaiting for the customer to return the product using the DVD Dispenser
CALCULATING means the system is calculating the charges.
Attempts to add items while PAYING or DISPENSING will result in errors.
 
Field Summary
private  CheckOutCart checkOutCart
          The cart containing items the customer has added.
 DVDDispenser dvdDispenser
          The associated DVDDispenser, which will notify DVDVendor when it detects a status change.
private  PaymentCollector paymentCollector
          An object representing the credit card or debit card accepting device.
private  ProductDB productDB
          The database of products in the point of sale.
private  Transaction transaction
          The transaction that will be persisted in the TransactionDB
private  TransactionDB transactionDB
          The database of transactions in the point of sale.
private  DVDVendor.checkOutState transactionState
          The current state of the system.
 
Constructor Summary
DVDVendor()
          The argument-less constructor makes the necessary utility classes and passes them to the constructor with arguments.
DVDVendor(DVDDispenser dvdDispenser, PaymentCollector payment, ProductDB productDB, TransactionDB transactionDB)
          This is the chief constructor.
 
Method Summary
 Product addItem(BarCode barCode)
          This method accepts a bar code and adds the corresponding product to the customer's cart.
 CheckOutCart calculateChargesForReturn(BarCode barCode)
          This method calculate the charges for the Return Transaction.
 java.lang.String dispenseItems()
          Method that dispense the items in the transaction using the dvdDispenser object from the dvdVendor object.
 DVDDispenser getDVDDispenser()
          An accessor method which returns the DVDDispenser associated with this DVDVendor.
 PaymentCollector getPaymentCollector()
          An accessor method which returns the PaymentCollector associated with this DVDVendor.
 ProductDB getProductDB()
          An accessor method which returns the ProductDB associated with this DVDVendor.
 double getTotalCost()
          This method returns the current total cost of all items in the cart.
 TransactionDB getTransactionDB()
          An accessor method which returns the TransactionDB associated with this DVDVendor.
 DVDVendor.checkOutState getTransactionState()
          An accessor method which returns the Transaction State Useful if the application wants to get the current state of the transaction.
 void initiateRental()
          This method initiates a rental transaction
 void initiateReturn()
          This method initiates a return transaction
 java.util.Enumeration<Product> listItemsInCart()
          This method retrieves an enumeration of all the items currently in the cart and returns it.
 void notifyDVDDispenserEvent(DVDDispenser dd, DVDDispenserEvent event)
          When the DVDDispenser detects a change in its state, this function is called to change the state of the system (transactionState).
 CheckOutCart payForRental(java.lang.String cardNumber)
          This method accepts a credit/debit card number to charge for the rented products.
 void receiveItem(java.lang.String barCode)
          Method that receives the returned item using the dvdDispenser object from the dvdVendor object
 java.lang.String recordTransaction()
          Method that records the transaction in the Transaction file
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

checkOutCart

private CheckOutCart checkOutCart
The cart containing items the customer has added.


dvdDispenser

public DVDDispenser dvdDispenser
The associated DVDDispenser, which will notify DVDVendor when it detects a status change.


paymentCollector

private PaymentCollector paymentCollector
An object representing the credit card or debit card accepting device.


productDB

private ProductDB productDB
The database of products in the point of sale.


transactionDB

private TransactionDB transactionDB
The database of transactions in the point of sale.


transactionState

private DVDVendor.checkOutState transactionState
The current state of the system.


transaction

private Transaction transaction
The transaction that will be persisted in the TransactionDB

Constructor Detail

DVDVendor

public DVDVendor()
          throws java.lang.Exception
The argument-less constructor makes the necessary utility classes and passes them to the constructor with arguments.

Throws:
java.lang.Exception

DVDVendor

public DVDVendor(DVDDispenser dvdDispenser,
                 PaymentCollector payment,
                 ProductDB productDB,
                 TransactionDB transactionDB)
          throws java.lang.Exception
This is the chief constructor. It records the provided DVDDispenser, PaymentCollector, ProductDB, and TransactionDB, and attaches itself to the DVDDispenser so that it receives notifications of DVDDispenserEvents. It also initiate the transaction in state READY

Parameters:
dvdDispenser - dvdDispenser object for the DVDVendor.
payment - paymentCollector object for the DVDVendor.
productDB - productDB object for the DVDVendor.
transactionDB - transactionDB object for the DVDVendor.
Throws:
java.lang.Exception
Method Detail

initiateRental

public void initiateRental()
                    throws IncorrectStateException
This method initiates a rental transaction

Throws:
IncorrectStateException - Thrown when Rental was initiated when the system was not READY

initiateReturn

public void initiateReturn()
                    throws IncorrectStateException
This method initiates a return transaction

Throws:
IncorrectStateException - Thrown when Return was initiated when the system was not READY

addItem

public Product addItem(BarCode barCode)
                throws InvalidProductException,
                       IncorrectStateException,
                       java.text.ParseException,
                       TransactionDBException
This method accepts a bar code and adds the corresponding product to the customer's cart. First, it checks if the transaction is in the appropriate state, then it looks for the item in the ProductDB. If the product is valid, then it will check if the item already exist in the user's cart. If the product is new for the cart, then it verifies if the product is available to be rented. If the item is available, it will be added to the user's cart.

Parameters:
barCode - The bar code of the selected item.
Returns:
Product The Product which is also added to the CheckOutCart.
Throws:
InvalidProductException - Thrown when a product corresponding to the bar code is not found in the ProductDB
IncorrectStateException - Thrown when the user tried to Add an item when the system was in the state DISPENSING (AddWhileDispensingException) or PAYING (AddWhilePayingException).
TransactionDBException - Thrown when there is an exception when looking for the last transactions (rent and return) in which the barCode was involved
java.text.ParseException - Thrown when there is an exception when invoking the method getSecondsDifference

payForRental

public CheckOutCart payForRental(java.lang.String cardNumber)
                          throws PaymentCollectorException,
                                 IncorrectStateException
This method accepts a credit/debit card number to charge for the rented products. It checks if there are items to pay for in the CheckOutCart, then it checks that the payment is not done while DISPENSING. It proceeds with the payment and if everything is ok with it, it will change the status to PAYING and it will set the information in the Transaction object. It updates the status to DISPENSING and returns the CheckOutCart.

Parameters:
cardNumber - String that represents the credit/debit card Number to be charged.
Returns:
the cart corresponding to the paid transaction.
Throws:
PaymentCollectorException - Thrown if there was an error with the payment. It could be that the cardNumber was not valid
IncorrectStateException - Thrown if the user tried to pay when he/she did not select items (PayWithNoItemsException), or when the payment was done while dispensing (PayWhileDispensingException), or the incorrect type of transaction was received

calculateChargesForReturn

public CheckOutCart calculateChargesForReturn(BarCode barCode)
                                       throws PaymentCollectorException,
                                              TransactionDBException,
                                              java.text.ParseException,
                                              IncorrectStateException
This method calculate the charges for the Return Transaction. It checks if a product has been returned using the DVDDispenser, then it checks if the product is expected to be returned (it has been rented). The last Rent Transaction is located and the credit/debit card number is taken from that transaction. It calculate how much need to be charged to the card Number. It proceeds with the payment and if everything is ok with it, it will set the information in the Transaction object. It creates a new empty cart and it updates the status to RECEIVING and returns the CheckOutCart.

Parameters:
BarCode - barCode object that identifies the product to be returned.
Returns:
the cart corresponding to the paid return transaction.
Throws:
PaymentCollectorException - Thrown if there was an error with the payment. It could be that the cardNumber was not valid
TransactionDBException - Thrown when there is an exception when looking for the last transactions (rent and return) in which the barCode was involved
java.text.ParseException - Thrown when there is an exception when invoking the method getSecondsDifference
IncorrectStateException - Thrown if the user tried to calculateCharges when he/she did not select items (CalculateWithNoItemException), or when the product has not been rented (ProductNotRentedException), or the incorrect type of transaction was received

dispenseItems

public java.lang.String dispenseItems()
                               throws IncorrectStateException,
                                      PersistenceException
Method that dispense the items in the transaction using the dvdDispenser object from the dvdVendor object. First, it will check the correct status to dispense, then it will dispense all the products in the cart, and finally it will record the transaction in the Transaction file

Returns:
String that contains the transaction recorded in the format used in the Transaction file
Throws:
IncorrectStateException - Thrown if the dispense was done while adding
PersistenceException - Thrown if there was an error in persisting the transaction in the Transaction file

receiveItem

public void receiveItem(java.lang.String barCode)
                 throws java.lang.Exception
Method that receives the returned item using the dvdDispenser object from the dvdVendor object

Parameters:
barCode - String with the bar code of the returned item
Throws:
java.lang.Exception - Thrown if there are problem with the DVDDispenser

recordTransaction

public java.lang.String recordTransaction()
                                   throws PersistenceException
Method that records the transaction in the Transaction file

Returns:
String that contains the transaction recorded in the format used in the Transaction file
Throws:
PersistenceException - Thrown if there was an error in persisting the transaction in the Transaction file

listItemsInCart

public java.util.Enumeration<Product> listItemsInCart()
This method retrieves an enumeration of all the items currently in the cart and returns it.


getTotalCost

public double getTotalCost()
This method returns the current total cost of all items in the cart.


notifyDVDDispenserEvent

public void notifyDVDDispenserEvent(DVDDispenser dd,
                                    DVDDispenserEvent event)
When the DVDDispenser detects a change in its state, this function is called to change the state of the system (transactionState). If a RECEIVE event is trigger, then the transactionState is updated to CALCULATING. If a DISPENSE event is trigger, then the checkOutCart is restarted and the transactionState is updated to ADDING.

Specified by:
notifyDVDDispenserEvent in interface DVDDispenserListener
Parameters:
dd - The attached DVDDispenser which is sending the event.
event - The DVDDispenserEvent, which includes the type of event and the read bar code.

getDVDDispenser

public DVDDispenser getDVDDispenser()
An accessor method which returns the DVDDispenser associated with this DVDVendor. Useful if the application wants to also receive DVD Dispenser events, for example.


getProductDB

public ProductDB getProductDB()
An accessor method which returns the ProductDB associated with this DVDVendor. Useful if the application wants to add items to the database or to look up items.


getTransactionDB

public TransactionDB getTransactionDB()
An accessor method which returns the TransactionDB associated with this DVDVendor. Useful if the application wants to add items to the database or to look up items.


getPaymentCollector

public PaymentCollector getPaymentCollector()
An accessor method which returns the PaymentCollector associated with this DVDVendor. Useful if the application wants to get payment details such as credit card number.


getTransactionState

public DVDVendor.checkOutState getTransactionState()
An accessor method which returns the Transaction State Useful if the application wants to get the current state of the transaction.