-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathfactory.cpp
More file actions
98 lines (88 loc) · 3.47 KB
/
factory.cpp
File metadata and controls
98 lines (88 loc) · 3.47 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
//----------------------------------------------------------------------------
// Factory.cpp
// Implementation:
// -- Responsible for choosing which Movie class or Trandaction class to
// create. The facotry is just used to create
// Assumptions:
// -- Data files are formatted properly, although data can be incorrect, the
// lower level classes will check the validity of the Transaction methods
// or the Movie type
//----------------------------------------------------------------------------
#include "factory.hpp"
//--------------------------------constructor---------------------------------
// sets all indexes to nullptr, and then assignes indexes to pointers to a
// movide genre. Index is assigned based on hashed value of the char type
Factory::Factory() {
// makes sure all original values are nullptr
for (int i = 0; i < MAX_ITEMS; i++) {
storeMovies[i] = nullptr;
storeTransactions[i] = nullptr;
}
// determinds which type of movie to create
storeMovies[2] = new Classic();
storeMovies[3] = new Drama();
storeMovies[5] = new Comedy();
// determines which type of transaction to create
storeTransactions[1] = new Borrow();
storeTransactions[7] = new History();
storeTransactions[17] = new ReturnItem();
}
//-------------------------------destructor-----------------------------------
// frees all memory
Factory::~Factory() {
for (int i = 0; i < MAX_ITEMS; i++) {
if (storeMovies[i] != nullptr) {
delete storeMovies[i];
storeMovies[i] = nullptr;
}
if (storeTransactions[i] != nullptr) {
delete storeTransactions[i];
storeTransactions[i] = nullptr;
}
}
}
//-----------------------------createMovie------------------------------------
// determins which type of movie object to create based on the hashed value of
// the char type being read in from the movie file. If type is not valid,
// error message will be printed
Movie* Factory::createMovie(char ch, ifstream& movieFile) {
string temp;
int subscript = hash(ch);
if (storeMovies[subscript] == nullptr) {
getline(movieFile, temp, '\n');
return nullptr;
} else {
return storeMovies[subscript]->create();
}
}
//----------------------------createTransaction-------------------------------
// determins which type of transaction object to create based on the hashed
// value of the char type being read in from the transaction file. If type is
// not valid, error message will be printed
Transaction* Factory::createTransaction(char ch, ifstream& commandFile) {
string temp;
int subscript = hash(ch);
if (storeTransactions[subscript] != nullptr) {
return storeTransactions[subscript]->create();
} else {
getline(commandFile, temp, '\n');
return nullptr;
}
}
//--------------------------------Subscript-----------------------------------
// will determine the proper spot in the store inventory list to store movie
// or transaction objects. Movies are stored with only alike movies
// e.g classics with classics (for sorting purposes).
int Factory::subscript(char ch) {
return hash(ch);
}
//----------------------------------hash--------------------------------------
// will return the int hash value of a given char type for a movie or
// transaction
int Factory::hash(char ch) const {
if (ch < 'a') {
return ch - 'A'; // letters are assumed to be upper-case
} else {
return 0;
}
}