-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathinventory.cpp
More file actions
83 lines (65 loc) · 1.75 KB
/
inventory.cpp
File metadata and controls
83 lines (65 loc) · 1.75 KB
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
#include "inventory.h"
Inventory::Inventory()
{}
Inventory::Inventory(QList <Item*> items, QList <Customer*> customers)
:m_items(items), m_customers(customers)
{}
Inventory::~Inventory(){
for(Item* item : m_items){
delete item;
}
for(Customer* c : m_customers){
delete c;
}
for(Transaction* t : m_transactions){
delete t;
}
}
QList<Customer*> Inventory::getCustomers(){
return m_customers;
}
QList<Item*> Inventory::getItems(){
return m_items;
}
QList<Item*> Inventory::getBackup(){
return m_backup;
}
QList<Transaction*> Inventory::getTransactions(){
return m_transactions;
}
void Inventory::addTransaction(Transaction* t) {
m_transactions.append(t);
}
void Inventory::addItem(Item* item){
m_items.append(item);
}
bool Inventory::processTransaction(QDialog* w, const Transaction& transaction){
int inStock = 0;
//Increase the counter for every item currently in stock
for (Item* item : m_items){
if(item == transaction.getItem())
++inStock;
}
//If there are more items requested than are in stock.
if (inStock < transaction.getQuantity()) {
QMessageBox::warning(w, "Error", "Insufficient Stock");
return false;
}
//If there is enough stock, remove those items from the stock list.
if(inStock>=transaction.getQuantity()){
for (int i=m_items.size() - 1; i>=0 && inStock>0; --i){
if (m_items[i] == transaction.getItem()){
m_items.removeAt(i);
--inStock;
}
}
}
return true;
}
void Inventory::restoreItems(){
for (Item* item : m_items)
delete item;
m_items.clear();
for (Item* b : m_backup)
m_items.append(new Item(*b));
}