-
Notifications
You must be signed in to change notification settings - Fork 0
/
borrow.cpp
189 lines (169 loc) · 4.19 KB
/
borrow.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
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
// ------------------------------------------------ borrow.cpp ------------------------------------------------------
// Name: Ihar Simanovich & Jason Kozodoy
// Course: CSS343 A
// Creation Date: 12/01/2016
// Last Modification: 12/14/2016
// ------------------------------------------------------------------------------------------------------------------
#include "borrow.h"
#include <sstream>
#include "movie.h"
using namespace std;
//constructor
Borrow::Borrow() {
type = 'B';
}
//destructor
Borrow::~Borrow() {
}
//returns if borrow transaction successful
bool Borrow::isSuccess(){
return success;
}
//does a transaction for borrow
void Borrow::doTransaction(BinTree& tree, char movieType) {
Movie* result = NULL;
switch (movieType) {
case 'F': {
Comedy* target = new Comedy(title, year);
if (tree.retrieve(*target, result)) {
if (result->borrow() == false) {
cout << "OUT OF STOCK: F, " << target->getTitle() << ", "
<< target->getYear() << endl;
cout << endl;
success = false;
}
} else {
cout << "ERROR - Invalid Movie: F, " << target->getTitle() << ", "
<< target->getYear() << endl;
cout << endl;
success = false;
}
delete target;
break;
}
case 'D': {
Drama* target = new Drama(director, title);
if (tree.retrieve(*target, result)) {
if (result->borrow() == false) {
cout << "OUT OF STOCK: D, " << target->getDirector() << ", "
<< target->getTitle() << endl;
cout << endl;
success = false;
}
} else {
cout << "ERROR - Invalid Movie: D, " << target->getDirector()
<< ", " << target->getTitle() << endl;
cout << endl;
success = false;
}
delete target;
break;
}
case 'C': {
Classic* target = new Classic(month, year, actor);
if (tree.retrieve(*target, result)) {
if (result->borrow() == false) {
cout << "OUT OF STOCK: C, " << target->getActor() << ", "
<< target->getMonth() << " " << target->getYear()
<< endl;
cout << endl;
success = false;
}
} else {
cout << "ERROR - Invalid Movie: C, " << target->getActor() << ", "
<< target->getMonth() << " " << target->getYear() << endl;
cout << endl;
success = false;
}
delete target;
break;
}
default:
cout << movieType << " - Invalid Movie Type, Can't Borrow" << endl;
cout << endl;
success = false;
break;
}
//reset data based on performed transaction
if (result != NULL) {
title = result->getTitle();
year = result->getYear();
}
}
//sets the data for a movie that is borrowed
void Borrow::setData(string data, char movieType) {
istringstream dataStream(data); //opening stream
type = 'B';
switch (movieType) {
case 'F': {
string temp;
//reading title
dataStream >> temp;
for (;;) {
//no comma case, adding words to title
if (temp.find(',') == -1) {
title += temp;
title += " ";
dataStream >> temp;
}
//comma case, removing comma and adding to title then break
else {
temp = temp.substr(0, temp.size() - 1);
title += temp;
break;
}
}
//reading year
dataStream >> year;
break;
}
case 'D': {
string temp;
//reading director
dataStream >> temp;
for (;;) {
//no comma case, adding words to title
if (temp.find(',') == -1) {
director += temp;
director += " ";
dataStream >> temp;
}
//comma case, removing comma and adding to title then break
else {
temp = temp.substr(0, temp.size() - 1);
director += temp;
break;
}
}
//reading title
dataStream >> temp;
for (;;) {
//no comma case, adding words to title
if (temp.find(',') == -1) {
title += temp;
title += " ";
dataStream >> temp;
}
//comma case, removing comma and adding to title then break
else {
temp = temp.substr(0, temp.size() - 1);
title += temp;
break;
}
}
break;
}
case 'C': {
string first, last;
dataStream >> month;
dataStream >> year;
dataStream >> first;
dataStream >> last;
actor = first + " " + last;
break;
}
default:
cout << "invalid type of movie, can't set data" << endl;
break;
}
}