-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathAccountManager.cpp
More file actions
175 lines (154 loc) · 5.1 KB
/
AccountManager.cpp
File metadata and controls
175 lines (154 loc) · 5.1 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
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
#include "AccountManager.h"
/* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
*
* Constructor defaults all variables to NULL.
*
* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
AccountManager::AccountManager() {
userAccounts = NULL;
tailAccount = NULL;
loggedIn = NULL;
size = 0;
}
/* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
*
* Destructor
*
* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
AccountManager::~AccountManager() {
}
/* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
*
* login() : search for user in linked list, and compare passwords. If user
* is not found, then return false. If user is found, but password does not
* match, return false. Otherwise, return true, and set loggedIn user.
*
* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
bool AccountManager::login(string user, string pw) {
User * loginUser = search(user);
if (loginUser == NULL) return false;
else if (pw.compare(loginUser->getPassword()) != 0) return false;
else loggedIn = loginUser;
return true;
}
void AccountManager::logout() {
loggedIn = NULL;
}
/* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
*
* newAccount() : [before being called, password much be verified twice,
* and have restrictions to username and password (length limit, char type,
* etc.)] function will take in a user name and password parameters. User
* will be added in alphabetical order.
*
* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
bool AccountManager::newAccount(string user, string pw,int score) {
// if: username exists in the list, return false.
if (search(user) != NULL) return false;
User * newAccount = new User(user, pw);
newAccount->setHighScore(score);
User * ptr = userAccounts;
// if: username comes after tail, point tail next to new user, and tail
// pointer equal to new user.
// elseif: username comes before head, point new user new to head, and
// head pointer equal to new user.
// else: increment pointer to node that new user comes after. Set next
// of new user to next of pointer. Set next of pointer to new user.
if (userAccounts == NULL) {
userAccounts = newAccount;
tailAccount = newAccount;
} else if (user.compare(tailAccount->getUserName()) > 0) {
tailAccount->setNext(newAccount);
tailAccount = newAccount;
} else if (user.compare(userAccounts->getUserName()) < 0) {
newAccount->setNext(userAccounts);
userAccounts = newAccount;
} else {
while (user.compare(ptr->next()->getUserName()) > 0)
ptr = ptr->next();
newAccount->setNext(ptr->next());
ptr->setNext(newAccount);
}
return true;
}
/* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
*
* loadData() : read userAccounts from sudoku_save file. If non-existent,
* [...]
*
* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
bool AccountManager::loadData() {
string fileName = "sudoku_save.txt"; // file type? .txt? .bin? ...?
// read userAccounts from file
ifstream file;
file.open(fileName);
string line, username, password;
int temp1=0, temp2=0, score;
if(!file.is_open()) return false;
else{
while(getline(file,line)){
//parse user data
temp1 = line.find("/");
temp2 = line.find("/",temp1);
username = line.substr(0,temp1);
password = line.substr(temp1+1,temp2);
score = atoi(line.substr(temp2+1).c_str());
//add account to linked list
this->newAccount(username,password,score);
}
}
file.close();
return true;
}
/* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
*
* saveData() : write userAccounts to sudoku_save file.
*
* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
bool AccountManager::saveData() {
string fileName = "sudoku_save.txt"; // " " " "
// write userAccounts to a file
ofstream file;
file.open(fileName);
User * ptr = userAccounts;
while (ptr != NULL) {
file << ptr->getUserName() << "/" <<ptr->getPassword() <<"/"
<< ptr->getHighScore() << "\n";
ptr = ptr->next();
}
file.close();
return true;
}
/* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
*
* search() : simple iteration search returns user if found, otherwise
* return NULL.
*
* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
User * AccountManager::search(string user) {
User * ptr = userAccounts;
// while: ptr exists and they don't equal,
while (ptr != NULL && user.compare(ptr->getUserName()) != 0)
ptr = ptr->next();
return ptr;
}
/*
void AccountManager::saveGame(Board * game) {
loggedIn->saveGame(game);
}*/
string AccountManager::getName() {
return (loggedIn != NULL) ? loggedIn->getUserName() : "";
}
void AccountManager::setHiScore(int hs) {
loggedIn->setHighScore(hs);
}
int AccountManager::getHiScore() {
return (loggedIn != NULL) ? loggedIn->getHighScore() : -1;
}
void AccountManager::printList() {
User * ptr = userAccounts;
while (ptr != NULL) {
ptr->printUser();
ptr = ptr->next();
}
}