-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathUserDatabase.cpp
87 lines (72 loc) · 1.86 KB
/
UserDatabase.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
#include "UserDatabase.h"
#include "User.h"
#include <string>
#include <vector>
#include <iostream>
#include <fstream>
using namespace std;
UserDatabase::UserDatabase()
{
}
UserDatabase::~UserDatabase()
{
// Delete all dynamically allocated users
vector<User*>::iterator p = m_users.begin();
int n = 0;
while (p != m_users.end())
{
delete *p;
p = m_users.erase(p);
n++;
}
}
bool UserDatabase::load(const string& filename)
{
// If function has already been called on file
if (m_users.size() != 0)
return false;
ifstream infile(filename);
// If file does not exist
if(infile.fail()){
return false;
}
for ( ; ; )
{
// Read in name
string full_name;
getline(infile, full_name);
// Check that we have not reached end of file
if (!infile)
break;
// Read in email
string email;
getline(infile, email);
// Read in count
int count;
infile >> count;
infile.ignore(10000, '\n');
// Add movie IDs to vector
vector<string> watch_history;
for (int i = 0; i < count; i++)
{
string movie;
getline(infile, movie);
watch_history.push_back(movie);
}
// Allocate new user in vector
User* u = new User(full_name, email, watch_history);
m_users.push_back(u);
// Add user to email tree
m_emailTree.insert(email, u);
// Ignore blank line
infile.ignore(10000, '\n');
}
return true;
}
User* UserDatabase::get_user_from_email(const string& email) const
{
TreeMultimap<std::string, User*>::Iterator it = m_emailTree.find(email);
if (!it.is_valid())
return nullptr;
return it.get_value();
}