-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmoviefactory.cpp
65 lines (60 loc) · 2.22 KB
/
moviefactory.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
/* Aaron Parks
* Prof. Carol Zander
* CSS 343 : Winter 2010
* Lab 4v4 - MOVIE Store */
#include "moviefactory.h"
#include <iostream>
//------------------------------------------------------------------------------
// Constructor
// - Instantiates a new hash factory of Items (Movies)
//------------------------------------------------------------------------------
MovieFactory::MovieFactory()
{
//initialize array
for (int i = 0; i < MOVIEF_SIZE; i++)
factory[i] = NULL;
//allows for only 'defined' genres to produce trees
/* ... can add more as necessary ... */
factory[2] = new Classic(); //classic movie
factory[3] = new Drama(); //drama movie
factory[5] = new Comedy(); //comedy movie
/* ... can add more as necessary ... */
}
//------------------------------------------------------------------------------
// Destructor
// - Will delete functions pointed to by factory array and will reset pointers
// to NULL.
//------------------------------------------------------------------------------
MovieFactory::~MovieFactory()
{
for (int i = 0; i < MOVIEF_SIZE; i++)
{
delete factory[i];
factory[i] = NULL;
}
}
//------------------------------------------------------------------------------
// createMovie
// - Will read one line from parameter file. Genre will be read from file to
// hash to the proper index of 'factory'. The file parameter will then be
// passed to the 'createItem' function of the indexed object where the data
// in the line will be read into a new object of that type.
//------------------------------------------------------------------------------
Item* MovieFactory::createMovie(ifstream& infile)
{
char genre;
infile >> genre;
int index = hashGenre(genre);
if (factory[index] != NULL)
return factory[index]->createItem(infile);
else
return NULL;
}
//------------------------------------------------------------------------------
// hashGenre
// - Will hash parameter and return its integer equivalent.
//------------------------------------------------------------------------------
int MovieFactory::hashGenre(char param)
{
return (param - 'A') % MOVIEF_SIZE;
}