-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathborrowCommand.cpp
73 lines (67 loc) · 2.12 KB
/
borrowCommand.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
/**
* BorrowCommand.cpp
* BorrowCommand is a child of Command class.
* Borrow (denoted as ‘B’): (stock – 1) for each item borrowed.
*
* @author Olga Kuriatnyk
*/
#include "borrowCommand.h"
// explicit constructor
BorrowCommand::BorrowCommand(const char &commandType) {
this->commandType = commandType;
this->mediaType = ' ';
this->movieType = ' ';
this->customerID = 0;
}
// destructor
BorrowCommand::~BorrowCommand() = default;
// read the command string load into customerID and key
void BorrowCommand::read(istream &is) {
is >> this->customerID;
is >> this->mediaType;
is >> this->movieType;
string str = readNextItem(is);
this->key = ItemKey(str);
}
// process borrow command
void BorrowCommand::process() {
Customer *currCustomer =
CustomerContainer::getInstance().retrieve(this->customerID);
// check if the customerID is valid, if not print ERROR
if (currCustomer == nullptr) {
cerr << "ERROR: invalid customer ID" << endl;
return;
}
// check if the media type is valid, if not print ERROR
if (this->mediaType != 'D') {
cerr << "ERROR: invalid media type" << endl;
return;
}
// check if the movie type is valid, if not print ERROR
if (movieTypes.count(movieType) == 0) {
cerr << "ERROR: not valid movie type" << endl;
return;
}
// find this item by itemKey, if invalid key print ERROR
Movie *currMovie =
Inventory::getInstance().retrieve(this->movieType, this->key);
if (currMovie == nullptr) {
cerr << "ERROR: invalid movie" << endl;
return;
}
// check if item is in stock, if not print ERROR
if (!currMovie->haveInStock()) {
cerr << "ERROR: do not have this movie in stock" << endl;
return;
}
// decrese the stock and add history to customerHistory
currMovie->decreaseStock();
// conver char to string
string cT(1, this->commandType), mediaT(1, this->mediaType),
movieT(1, this->movieType);
string history =
cT + " " + mediaT + " " + movieT + " " + this->key.getItemKey();
currCustomer->insertHistory(history);
}
// creating the object registers the type at runtime
BorrowCommandFactory anonymousBorrowCommandFactory;