Example #1
0
//--------------------------------Execute---------------------------------//
//this method performs the actions for the Borrow object, using the
//store as a parameter to access the inventory and alter accordingly
//it also uses the file stream to read in the information
//according to formatting specifications, setting it for this specified
//derived transaction class
bool Borrow::execute(ifstream& file, Store& st)
{
	int custIdx;
	file >> custIdx;
	Customer* cust = st.getCustomer(custIdx);
	//read the custtomer index and get the customer at that index
	//from the store, if the store doesn't have the customer at that
	//spot then the customer has not been created and we cannot do anything
	if (cust == NULL)
	{
		cout << "Invalid Customer ID. Transaction could not be executed.\n";
		//no need for clean up since Customer is still NULL
		return false;
	}
	else
	{
		char format;
		file >> format;
		//read in the format, the format is specified to only be DVDs
		if (format != 'D')
		{
			cout << "Invalid format. Transaction could not be executed.\n";
			//no need for clean up since Formaat is not dynamic
			return false;
		}
		else
		{
			char genre;
			file >> genre;
			//read in the genre and then use the stores movie factory
			//to create a temporary movie of this genre,
			//however, if the genre did not correspond to an appropriate
			//movie, return false
			Movie* tempMov = st.getMovieFact().createIt(genre);
			if (tempMov == NULL)
			{
				cout << "Invalid movie. Transaction could not be "
					<< "executed.\n";
				//no need for clean up since tempMov is still NULL
				return false;
			}
			else
			{
				//everything worked, now we try to set the data
				//from the format specifications of a borrow
				//movie, and if this didn't work, the movie was 
				//created so we must clean this up. otherwise, we try
				//to borrow the movie from the inventory
				if (!tempMov->readTrans(file))
				{
					delete tempMov;
					tempMov = NULL;
					return false;
				}
				else
				{
					Movie* mov = NULL;
					//use a movie locator and a movie return value
					//to try to borrow the movie from the stores
					//inventory, if the borrow couldn't occur
					//we must delete the temporary movie
					if (!st.getInventory().borrowMovie(mov, tempMov))
					{
						cout << "Invalid movie. Transaction could not be "
							<< "executed.\n";
						delete tempMov;
						tempMov = NULL;

						return false;
					}
					else
					{
						//otherwise everything has worked out so far
						//so we set the data of the borrow transaction
						//because we already know the movie
						//and the customer is accurate
						setData(cust, mov);
						//if we couldn't borrow we must clean up
						if (!cust->borrowMovie(mov))
						{
							cout << "Invalid movie." <<
								"Transaction could not be "
								<< "executed.\n";
							delete tempMov;
							tempMov = NULL;
							return false;
						}
						//otherwise everything worked out perfectly
						//so the data is set and the customer
						//adds this transaction to his/her trans hist
						//and clean up our temp mov
						cust->addTransaction(this);
						delete tempMov;
						tempMov = NULL;
						return true;
					}
				}
			}
		}
	}
}
//-----------------------------------------------------------------------------
// processTransactions(ifstream& infile) const
// Process a list of transactions from an input file
//-----------------------------------------------------------------------------
void MovieStore::processTransactions(ifstream& infile)
{
    while (true)
    {
        if (infile.eof() || !infile.good())
        {
            break;
        }

        char c;
        infile >> c;
        string key;
        stringstream ss;
        Customer target;

        switch (c)
        {
            // borrow and return
            case 'B':
            case 'R':
                int custId;
                char format;
                char genre;
                char sz[256];
                Customer* pCust;
                Movie* pMovie;

                infile >> custId >> format >> genre;
                infile.get();
                infile.get(sz, ARRAYSIZE(sz));
                key = sz;

                ss << custId;
                target.setKey(ss.str());

                // Clean up
                infile.clear();
                infile.ignore(numeric_limits<streamsize>::max(), '\n');

                // Validate genre
                if (!m_movieManager.isValidGenre(genre))
                {
                    cerr << "Invalid genre '" << genre << "'" << endl;
                    continue;
                }

                // Retrieve customer
                pCust = dynamic_cast<Customer*>(m_customersHash.retrieve(target));

                // Retrieve movie
                pMovie = m_movieManager.find(genre, key);

                // Validate customer
                if (NULL == pCust)
                {
                    cerr << "Customer '" << custId << "' not found" << endl;
                    continue;
                }
                // Validate movie
                else if (NULL == pMovie)
                {
                    cerr << "Movie with key '" << key << "' not found" << endl;
                    continue;
                }
                // Validate checkout
                else if (! toggleCheckoutState(('B' == c), pMovie, custId, format))
                {
                    continue;
                }
                else
                {
                    // Add transaction record to customer
                    Transaction* pTrans = new Transaction(pMovie, c, format);
                    pCust->addTransaction(pTrans);
                }

                break;

            // show movie inventory
            case 'S':
                m_movieManager.print();
                break;

            // Display transaction history by customer
            case 'H':
                cout << m_customers;
                break;

            default:
                cout << "Invalid transaction code " << c << endl;

                // Clean up
                infile.clear();
                infile.ignore(numeric_limits<streamsize>::max(), '\n');

                break;
        }
    }
}