-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathUserDatabase.cpp
89 lines (76 loc) · 1.87 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
88
#include "UserDatabase.h"
#include "User.h"
#include <string>
#include <vector>
#include <iostream>
#include <fstream>
#include <vector>
#include <cctype>
using namespace std;
UserDatabase::UserDatabase()
{
m_loaded = false;
}
bool UserDatabase::load(const string& filename)
{
ifstream infile(filename); // infile is a name of our choosing
if (!infile) // Did opening the file fail?
{
cerr << "Error: Cannot open " << filename << endl;
return false;
}
string s;
string name;
string email;
int numMovies = 0;
vector <string> watch_history;
int count = 0;
while(getline(infile, s))
{
if(s.empty()) // if the current line is empty, add the user to userdatabase
{
User newUser(name, email, watch_history);
userDataBase.insert(email, newUser);
name.clear();
email.clear();
numMovies = 0;
watch_history.clear();
count++;
}
else if(name.empty())
{
name = s;
}
else if(email.empty())
{
email = s;
}
else if(numMovies == 0)
{
numMovies = stoi(s);
}
else
{
watch_history.push_back(s);
}
}
// Add the last user to the database (if there was no empty line after it)
if (!name.empty() && !email.empty() && numMovies > 0)
{
User newUser(name, email, watch_history);
userDataBase.insert(email, newUser);
}
return true;
}
User* UserDatabase::get_user_from_email(const string& email) const
{
TreeMultimap<std::string, User>:: Iterator it = userDataBase.find(email);
if(it.is_valid()) // not nullptr
{
return &it.get_value();
}
else
{
return nullptr;
}
}