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

java.lang.Object
  extended by edu.uci.ics.inf111.dvdvendor.app.TransactionDB

public class TransactionDB
extends java.lang.Object

The TransactionDB class encapsulates the list of all products sold in the store. In a real system, this would likely be a wrapper around a database of products which would be managed elsewhere. In our sample system, we have a method which can provide a sample DB, and the capability to add items to the DB using an addItem() method. The TransactionDB is persisted in a file.


Field Summary
private  Persistence persistence
          Persistence object that will help persist the database information in a file
private  java.util.ArrayList<Transaction> transactions
          This ArrayList of Transactions is the core of our sample DB.
 
Constructor Summary
TransactionDB()
          Constructs an empty database.
 
Method Summary
 void addItem(Transaction transaction)
          This method is called to add items directly to the database in our example.
 RentTransaction findLastRentTransaction(BarCode barCode)
          This method finds the last Rent Transaction in the database for a specific BarCode.
 ReturnTransaction findLastReturnTransaction(BarCode barCode)
          This method finds the last Return Transaction in the database for a specific BarCode.
 void initializeTestDB()
          This test method constructs the transaction database from a txt file that contains all the transactions.
 java.util.ArrayList<Transaction> listAll()
          This method returns a copy of the TransactionDB ArrayList.
 java.lang.String persistTransaction(Transaction transaction)
          This method is called to persist a 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

transactions

private java.util.ArrayList<Transaction> transactions
This ArrayList of Transactions is the core of our sample DB. In a real implementation, the actual data would likely be in a separate database, which we would access using database queries. In this case, the transactions are read from a txt file and stored in the same txt file. Each line in the file is represented as one entry in this ArrayList.


persistence

private Persistence persistence
Persistence object that will help persist the database information in a file

Constructor Detail

TransactionDB

public TransactionDB()
Constructs an empty database.

Method Detail

initializeTestDB

public void initializeTestDB()
                      throws java.lang.Exception
This test method constructs the transaction database from a txt file that contains all the transactions. Each Transaction will be read from the file and added to this class using the addItem() method.

Throws:
java.lang.Exception

listAll

public java.util.ArrayList<Transaction> listAll()
This method returns a copy of the TransactionDB ArrayList. If we provided the original, external code could modify the DB directly.


findLastRentTransaction

public RentTransaction findLastRentTransaction(BarCode barCode)
                                        throws TransactionDBException
This method finds the last Rent Transaction in the database for a specific BarCode.

Parameters:
barCode - The bar code of the product involved in the Rent Transaction
Returns:
RentTransacion The class of the corresponding transaction, or null if no such transaction.
Throws:
TransactionDBException

findLastReturnTransaction

public ReturnTransaction findLastReturnTransaction(BarCode barCode)
                                            throws TransactionDBException
This method finds the last Return Transaction in the database for a specific BarCode.

Parameters:
barCode - The bar code of the product involved in the Return Transaction
Returns:
ReturnTransacion The class of the corresponding transaction, or null if no such transaction.
Throws:
TransactionDBException

addItem

public void addItem(Transaction transaction)
This method is called to add items directly to the database in our example. In a real implementation, this would likely be done directly to the product database using a separate piece of software.

Parameters:
transaction - The transaction to be added.

persistTransaction

public java.lang.String persistTransaction(Transaction transaction)
                                    throws PersistenceException
This method is called to persist a Transaction in the Transaction file.

Parameters:
transaction - The transaction to be persisted in the file.
Returns:
line The String representation of the Transaction using the format in the Transaction file
Throws:
PersistenceException - If the transaction passed as a parameter is null or if there is an error while writing the file.