// Sell an item to a customer, if quantity of stock is available and SKU exists.
// Reduce stock by quantity, increase balance by quantity*price, and return true if stock available.
// If partial stock (less than quantity) available, sell the available stock and return true.
// If no stock, sku does not exist, or quantity is negative, return false.
bool StockSystem::Sell(int itemsku, int quantity)
{
	bool existence;
	StockItem item = StockItem(itemsku, "", 0);
	StockItem* temp = records.Retrieve(item);
	if (temp == nullptr)
		existence = false;
	else
		existence = true;
	
	if (existence && temp->GetStock() > quantity)
	{
		temp->SetStock(temp->GetStock() - quantity);
		balance = balance + quantity*(temp->GetPrice());
		return true;
	}
	else if ((0 <= quantity < temp->GetStock()) && existence)
	{
		balance = balance + (temp->GetStock())*(temp->GetPrice());
		temp->SetStock(0);
		return true;
	}
	else
		return false;
}
示例#2
0
// Sell an item to a customer, if quantity of stock is available and SKU exists.
// Reduce stock by quantity, increase balance by quantity*price, and return true if stock available.
// If partial stock (less than quantity) available, sell the available stock and return true.
// If no stock, sku does not exist, or quantity is negative, return false.
bool StockSystem::Sell(int itemsku, int quantity)
{
	int size = records.Size();
	StockItem* arr = records.Dump(size);

	if (quantity < 0) { return false; }

	for (int i = 0; i < size; i++)
	{
		if (arr[i].GetSKU() == itemsku)
		{
			StockItem *item = records.Retrieve(arr[i]);

			int number = item->GetStock() - quantity;
			if (number <= 0) { number = 0; }
			if (item->GetStock() <= 0) { return false; }

			if (number == 0)
			{
				this->balance = balance + ( item->GetStock()*item->GetPrice());
			}
			else
			{
				this->balance = balance + (quantity*item->GetPrice());
			}
			delete[] arr;
			item->SetStock(number);
			return true;
		}
	}
	delete[] arr;
	return false;
}
// Sell an item to a customer, if quantity of stock is available and SKU exists.
// Reduce stock by quantity, increase balance by quantity*price, and return true if stock available.
// If partial stock (less than quantity) available, sell the available stock and return true.
// If no stock, sku does not exist, or quantity is negative, return false.
bool StockSystem::Sell(int itemsku, int quantity){
    StockItem inputItem =  StockItem(itemsku, "ignore this message", 0.00);
    //search this identical item, if it is found return a pointer to it
    StockItem* valueRetreived = records.Retrieve(inputItem);
    //if the pointer is null, must be that retrieve failed
    if (valueRetreived == NULL || quantity <=0 || valueRetreived->GetStock()<=0) {
        return false;
    }else{
        if (valueRetreived->GetStock()<=quantity) {
            balance = balance + (valueRetreived->GetStock())*(valueRetreived->GetPrice());
            valueRetreived->SetStock(0);
            return true;
        }else{
            valueRetreived->SetStock(valueRetreived->GetStock()-quantity);
            balance = balance + quantity*(valueRetreived->GetPrice());
            return true;
        }
    }
}
// Locate the item with key itemsku and update its description field.
// Return false if itemsku is not found or retailprice is negative.
bool StockSystem::EditStockItemPrice(int itemsku, double retailprice){
    //init an stockitem
    StockItem inputItem =  StockItem(itemsku, "ignore this message", retailprice);
    //search this identical item, if it is found return a pointer to it
    StockItem* valueRetreived = records.Retrieve(inputItem);
    //if the pointer is null, must be that retrieve failed
    if (valueRetreived == NULL) {
        return false;
    }else{
        //check negative pricing
        if (valueRetreived->GetPrice() < 0) {
            return false;
        }else{
            valueRetreived->SetPrice(retailprice);
            return true;
        }
    }
}