-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathBranch.cpp
110 lines (94 loc) · 2.61 KB
/
Branch.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
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
//Student Bshara Haj, 212590186.
//Student Obaeda Khatib, 201278066.
#pragma once
#include "Branch.h"
#include "Computer.h"
#include "PeripheralDevice.h"
//Constructor
Branch::Branch(string location, int capacity) : location(location), capacity(capacity)
{
//I need to initialize its values?
}
Branch::Branch() : location("~"), capacity(0)
{
}
//Destructor
Branch::~Branch()
{
std::vector<Item*>::iterator i; // Declare iterator before the for loop
for (i = catalog.begin() ; i != catalog.end(); ++i)
{
delete* i;
}
}
//Copy constructor
Branch::Branch(const Branch& other) : location(other.location), capacity(other.capacity)
{
}
//Function to get the catalog of items in the branch . also returns the quantity of items
std::vector<Item*> Branch::getCatalog() const /////////// changed it so it fit the one in the main file, added const also
{
return catalog;
}
// to get the location of the branch
string Branch::getLocation() const
{
return location;
}
//method to add an item
void Branch::addItem(Item* item)
{
if (find(catalog.begin(), catalog.end(), item) != catalog.end())
{
throw ExistingItemError();
}
if (catalog.size() >= capacity)
{
throw FullCatalogError();
}
catalog.push_back(item);
if (typeid(*item) == typeid(Computer))
{
computers.push_back(dynamic_cast<Computer*>(item));
}
}
//method that remove item by Id
Item* Branch::removeItem(int id)
{
std::vector<Item*>::iterator i;
Item* itemToRemove = nullptr;
for (i = catalog.begin(); i != catalog.end(); ++i)
{
if ((*i)->getId() == id)
{
itemToRemove = *i;
catalog.erase(i);
break;
}
}
if (itemToRemove == nullptr)
{
throw NonExistingItemError();
}
else
{
int k = 0;
for (; k < computers.size(); ++k)
{
for (int i = 0; i < computers[k]->connected.size(); i++) {
computers[k]->connected.erase(std::remove(computers[k]->connected.begin(), computers[k]->connected.end(), itemToRemove));
}
}
return itemToRemove;
}
}
int Branch::getItemsSum() const
{
int sum = 0;
std::vector<Item*>::const_iterator i;
for (i = catalog.begin(); i != catalog.end(); ++i)
{
sum += (*i)->getPrice();
}
return sum;
}