-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathBorrow.cpp
More file actions
84 lines (75 loc) · 2 KB
/
Borrow.cpp
File metadata and controls
84 lines (75 loc) · 2 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
84
#include "Borrow.h"
Borrow::Borrow()
{
}
Borrow::~Borrow()
{
}
void Borrow::doAction(string trans, map<string, Movie*>& movies,
myHashTable<Customer>& accounts)
{
vector<string> tokens(5);
/*tester tes;
tes.getTokens(tokens, trans);*/
//takes input string and fills vector with relevant movie tokens
ActionFactory::getTokens(tokens, trans);
if (tokens[0].length() != 4)
{
return; //get tokens method found invalid input, do nothing
}
//valid id, find genre of movie to retreive from db
string index;
char ch = tokens[1].at(0); //genre element
switch (ch)
{
case 'C':
{
index = CLASSIC;
if (tokens[3].length() == 1)
{
tokens[3] = "0" + tokens[3];
} //appends tokens to recreate movie search index
index.append(tokens[2]).append(tokens[3]).append(tokens[4]);
}
break;
case 'D':
{
index = DRAMA;//appends tokens to recreate movie search index
index.append(tokens[2]).append(tokens[3]);
}
break;
case 'F':
{
index = COMEDY;//appends tokens to recreate movie search index
index.append(tokens[2]).append(tokens[3]);
}
break;
default: return;
}
Movie* m;
//search for movie
map<string, Movie*>::const_iterator iter = movies.find(index);
if (iter == movies.end()) {
//unable to find movie
cout << "Error --Movie information does not match..." << endl
<< "check title/actor/director etc and try again." << endl;
return;
}
m = iter->second;
//movie found, search client by id number
Customer* client = accounts.getEntry(stoi(tokens[0]));
if (client == NULL) //invalid customer id
{
cout << "Error --Invalid Customer ID, sign up today!" << endl;
return;
}
//movie and customer ok, available in stock?
if (m->getRemaining() <= 0)
{
cout << "All copies rented, could we suggest Top Gun?" << endl;
return;
}
//all checks pass, rent to customer and list on trans history
m->rent(); //decrease remaining by 1
client->addTransaction(m->transDisplay()); //add transaction with movie
}