-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathPeripheralDevice.cpp
87 lines (73 loc) · 2.26 KB
/
PeripheralDevice.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
//Student Bshara Haj, 212590186.
//Student Obaeda Khatib, 201278066.
#pragma once
#include "PeripheralDevice.h"
PeripheralDevice::PeripheralDevice(int price, const string& manufacturer, const string& color, bool isWireless)
: Item(price, manufacturer), color(color), isWireless(isWireless) //constructor of the PeripheralDevice class
{
//computerConnectedTo = NULL;
}
string PeripheralDevice::getColor() const //getter for color
{
return color;
}
void PeripheralDevice::setColor(const string& color) //setter for color
{
this->color = color;
}
bool PeripheralDevice::getIsWireless() const //getter for isWireless
{
return isWireless;
}
void PeripheralDevice::setIsWireless(bool isWireless) //setter for isWireless
{
this->isWireless = isWireless;
}
void PeripheralDevice::connect(Computer* computer) //connect function to print connection details //////////// re-arrange
{
if (computerConnectedTo != computer)
{
if (computerConnectedTo)
{
throw ConnectError();
}
else
{
std::cout << operator std::string();
std::cout << " is connecting to computer: " + computer->Computer::operator std::string(); std::cout << std::endl;
for (int i = 0; i < computer->connected.size(); i++)
{
if (typeid(computer->connected[i]) == typeid(*this))
{
throw ConnectError();
}
}
if (computer->connected.size() == computer->getNumOfPorts())
{
throw ConnectError();
}
else
{
computer->connected.push_back(this);
computerConnectedTo = computer;
}
}
}
}
void PeripheralDevice::disconnect() //////////////////////
{
if (computerConnectedTo)
{
std::vector<PeripheralDevice*>::iterator neww;
computerConnectedTo->connected.erase(std::remove(computerConnectedTo->connected.begin(), computerConnectedTo->connected.end(), this));
computerConnectedTo = NULL;
}
}
string PeripheralDevice::toString() const //toString function to return a string representation of the PeripheralDevice
{
return Item::toString() + (this->isWireless ? "Wireless" : "Wired") + ", " + color;
}
PeripheralDevice::operator string() const //string conversion operator for the PeripheralDevice class
{
return toString();
}