1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101
|
#include "factory.h"
#include <algorithm>
#include <iostream>
using namespace std;
Factory::Factory ()
: numOrders(0), maxOrders(4)
{
orders = new Order[maxOrders];
}
Factory::Factory (std::string nm)
: numOrders(0), maxOrders(4), name(nm)
{
orders = new Order[maxOrders];
}
// Clear all orders for this factory
void Factory::clearOrders()
{
numOrders = 0;
}
// Place an order for some number of items.
// If a prior order has been placed for these, this additional
// order adds to that prior one. The Item is also updated to reflect
// the change in total number of that item ordered by all factories.
void Factory::orderItems (Item* itemToOrder, int numToOrder)
{
int k = 0;
while (k < numOrders && orders[k].item->name != itemToOrder->name) //while k is less then numOrders and the order item name is not the itemToOrder name// /Bacically, while the number of orders
++k; //add one to k Hasnt been updated and while you arent on the
if (k < numOrders && orders[k].item == itemToOrder) //If k is less than the number of orders and the order item is the item to order right item anyways
{
// This factory already has an order for some of these items
// cerr<<"Orders Amount = "<<orders[k].amount;
orders[k].amount += numToOrder; //then add the number of the order to the orders that have already been requested
}
else //or else
{
// A new item for this factory - add it
if (numOrders >= maxOrders) //if the numOrders is greater then or equal to the maxOrders
{
Order* newOrders = new Order[2*maxOrders]; //then create a new order
copy (orders, orders+numOrders, newOrders); ///and copy the old orders into a new order CHECK THIS OUT MAYBE OPERATOR OVERLOADING?
delete [] orders; //delete the old orders
orders = newOrders; //put the neworders into the orders array (using operator overloading)
maxOrders *= 2; //maxOrders = maxOrders * 2
}
orders[numOrders] = Order(itemToOrder, numToOrder); //
int k = numOrders; //k equals the number of orders
while (k > 0 && orders[k].item->name < orders[k-1].item->name) //while k is greater than zero and
{
swap (orders[k], orders[k-1]);
--k;
}
++numOrders;
}
// Update the total ordered of this item
itemToOrder->amount += numToOrder ;
//itemToOrder->amount;
}
// Access to Orders (in alphabetic order by item name)
Factory::OrderPosition Factory::begin() const
{
return orders;
}
Factory::OrderPosition Factory::end() const
{
return orders+numOrders;
}
Factory::OrderPosition Factory::find(Item* it) const
{
OrderPosition pos = begin();
while (pos != end() && pos->item != it)
++pos;
return pos;
}
///Copy constructor for linked list
Factory::Factory (const Factory& b)
: name(b.name), maxOrders(b.maxOrders), numOrders(b.numOrders)
{
for (OrderPosition p = b.begin(); p != b.end(); ++p)
orderItems(*p, end());
//orderItems(end(), *p);
}
|