-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathstocksystem.cpp
88 lines (69 loc) · 1.88 KB
/
stocksystem.cpp
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
#include "stocksystem.h"
StockSystem::StockSystem() : records(), balance(100000)
{
}
double StockSystem::GetBalance()
{
return balance;
}
bool StockSystem::StockNewItem(StockItem item)
{
return records.Insert(item);
}
bool StockSystem::EditStockItemDescription(int itemsku, std::string desc)
{
StockItem requested_item(itemsku, "225", 0);
if (records.Contains(requested_item))
{
StockItem* found_item = records.Retrieve(requested_item);
return found_item->SetDescription(desc);
}
return false;
}
bool StockSystem::EditStockItemPrice(int itemsku, double retailprice)
{
StockItem requested_item(itemsku, "225", 0);
if (records.Contains(requested_item) && retailprice >= 0)
{
StockItem* found_item = records.Retrieve(requested_item);
return found_item->SetPrice(retailprice);
}
return false;
}
bool StockSystem::Restock(int itemsku, int quantity, double unitprice)
{
if (quantity < 0 || unitprice < 0)
{
return false;
}
double cost = quantity*unitprice;
StockItem requested_item(itemsku, "225", 0);
if (records.Contains(requested_item) && (balance - cost) >= 0)
{
StockItem* found_item = records.Retrieve(requested_item);
balance = balance - cost;
return found_item->SetStock(found_item->GetStock() + quantity);
}
return false;
}
bool StockSystem::Sell(int itemsku, int quantity)
{
StockItem requested_item(itemsku, "225", 0);
if (records.Contains(requested_item) && quantity >= 0)
{
StockItem* found_item = records.Retrieve(requested_item);
int current_stock = found_item->GetStock();
if (quantity > current_stock)
{
balance = balance + current_stock*found_item->GetPrice();
current_stock = 0;
}
else
{
balance = balance + quantity*found_item->GetPrice();
current_stock = current_stock - quantity;
}
return found_item->SetStock(current_stock);
}
return false;
}