-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathActionFactory.cpp
More file actions
100 lines (93 loc) · 2.07 KB
/
ActionFactory.cpp
File metadata and controls
100 lines (93 loc) · 2.07 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
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
#include "ActionFactory.h"
//constructor
ActionFactory::ActionFactory()
{
}
//destructor
ActionFactory::~ActionFactory()
{
}
/*uses char at 0 of input string to determine
action type to use
*/
Action * ActionFactory::create(string input)
{
Action* a;
switch (input.at(0))
{
case 'B': a = new Borrow();
break;
case 'H': a = new History();
break;
case 'I': a = new Inventory();
break;
case 'R': a = new Return();
break;
default: a = new Error();
break;
}
return a;
}
/*
getTokens()
param info: string with action request, containing all transaction info
breaks down info to correct tokens based on the genre, checking for incorrect
input in genre or format, if so, replacing the token for customer id with '%'
as a flag to the calling class
*/
void ActionFactory::getTokens(vector<string>& token, string info)
{
int month, year;
string title, actor, director;
string id = info.substr(2, 4); //customer id
token[0] = id;
char media = info.at(7); //media char
char genre = info.at(9); //genre char
token[1] = genre;
string remain = info.substr(11); //remaining substring
stringstream ss(remain); //string stream of remaining string
if (media != 'D') //valid media test
{
cout << "Error - No such Media Exists!" << endl;
token[0] = "%"; //fail flag
return;
}
//determine genre for relevant tokens, fail if not correct genre
switch (genre)
{
case 'C': //classic genre
{
ss >> month;
ss >> year;
ss.ignore(); //ignore whitespace
getline(ss, actor);
token[2] = to_string(year);
token[3] = to_string(month);
token[4] = actor;
return;
}
break;
case 'D': //drama genre
{
getline(ss, director, ',');
ss.ignore(); //ignore whitespace
getline(ss, title);
token[2] = director;
token[3] = title;
return;
}
break;
case 'F': //comedy genre
{
getline(ss, title, ',');
ss >> year;
token[2] = title;
token[3] = year;
return;
}
break;
default: cout << "Error - Invalid Genre" << endl;
token[0] = "%"; //fail flag
return;
}
}