-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathAccountList.cpp
More file actions
197 lines (188 loc) · 5.57 KB
/
AccountList.cpp
File metadata and controls
197 lines (188 loc) · 5.57 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
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
#ifndef ACCOUNTLIST_CPP
#define ACCOUNTLIST_CPP
#include <fstream>
#include <string>
#include "Account.h"
#include "AccountList.h"
#include <map>
#include <unordered_map>
using namespace std;
//%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
//Reads file and inserts data
AccountList::AccountList(string AccountsFile, BookList myLib)
{
//check if file exists
highestID=0;
vector<string> Accounts;
unsigned int numbooks;
ifstream AccountFile;
string AccountData;
AccountFile.open(AccountsFile);
if (!AccountFile.is_open())
{
cout<< "Could not find file "<<AccountsFile<<". Skipping."<<endl;
}
//If it does, read file, split by | until number of checkouts lines +1
//and push into a vector. Add the account
getline(AccountFile, AccountData);
while(!AccountFile.eof())
{
while(getline(AccountFile,AccountData))
{
stringstream ss(AccountData);
while(getline(ss, AccountData, '|'))//split line by |
{
Accounts.push_back(AccountData);
if (Accounts.size()>=3)//After User info is added
{
if (highestID<atoi(Accounts[0].c_str()))//tracks maxid
{
highestID=atoi(Accounts[0].c_str());
}
numbooks=atoi(Accounts[2].c_str());
if(Accounts.size()==((numbooks+1)*3))//Continues until all books found
{
Account* myAccount= new Account(Accounts, myLib);
AddAccount(myAccount);
Accounts.clear();
}
}
}
}
}
AccountFile.close();
}
//%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
//Inserts accounts
void AccountList::AddAccount(Account* someAccount)
{
ListOfAccounts.insert(std::pair<int,Account*>(someAccount->getUserID(),someAccount));
}
//%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
//Adds new account with given name
void AccountList::AddAccount(string name)
{
std::map<int,Account*>::const_iterator it;
int newID=highestID+1;
highestID++;
Account* someAccount= new Account(name, newID);
ListOfAccounts.insert(std::pair<int,Account*>(newID,someAccount));
cout<<"AccountID#"<<newID<< " successfully created."<<endl;
}
//%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
//deletes account
void AccountList::removeAccount(int AccountID)
{ //checks if user has books, and force returns them before deleting.
if (ListOfAccounts[AccountID]->getCheckouts()>0)
{
ListOfAccounts[AccountID]->forceRemoveBooks();
}
cout<<ListOfAccounts[AccountID]->getName()<<"'s account successfully removed."<<endl;
ListOfAccounts.erase(AccountID);
}
//%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
//Add book to specific account
void AccountList::AddBookToAccount(int UserID, Book* Book)
{
ListOfAccounts[UserID]->CheckoutBook(Book);
}
//%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
//print info if Id exist
void AccountList::printUserInfo(int ID)
{
//check if user exists, and prints if user does
if (ListOfAccounts.find(ID)!=ListOfAccounts.end())
{
ListOfAccounts[ID]->printAccountFull();
}
else
{
cout<<"AccountID# "<<ID<<" not found."<<endl;
}
}
//%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
//print all accounts
void AccountList::PrintAll(string criteria)
{
if (criteria=="name" or criteria=="accountid" or criteria=="checkouts")
{
vector<Account*> AccountsToSort=order(criteria);
for (unsigned int i = 0; i < AccountsToSort.size(); ++i)
{
cout<<i+1<<". ";
AccountsToSort[i]->printAccount();
}
}
else
{
cout<<"Invalid value."<<endl;
}
}
//%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
//Output file with account data
void AccountList::OutputAccounts(string accountsfile)
{
ofstream sendfile;
sendfile.open(accountsfile);
if (ListOfAccounts.size()>0)
{
sendfile<<ListOfAccounts.size()<<endl;
}
for (auto it = ListOfAccounts.begin(); it !=ListOfAccounts.end() ; ++it)
{
sendfile << it->second->outputAccount();
}
sendfile.close();
}
//%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
//returns account with userid
Account* AccountList::findAccount(int UserID)
{
return ListOfAccounts[UserID];
}
//%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
//retruns number od Overdue accounts
int AccountList::OverdueAccounts()
{
int count;
for (auto it = ListOfAccounts.begin(); it !=ListOfAccounts.end() ; ++it)
{
if (it->second->hasOverdueBooks())
{
count++;
}
}
return count;
}
// //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
//Order Accounts by criteria
vector<Account*> AccountList::order(string criteria)
{
//copy accountss into a vector, sort using vector sort function, and a cpmarator
vector<Account*> AccountsToSort;
for (auto it = ListOfAccounts.begin(); it!=ListOfAccounts.end() ; ++it)
{
AccountsToSort.push_back(it->second);
}
if (criteria=="name")
{
sort (AccountsToSort.begin(), AccountsToSort.end(), [](Account* const &a, Account* const &b) { return a->getName() < b->getName(); });
}
if (criteria=="accountid")
{
sort (AccountsToSort.begin(), AccountsToSort.end(), [](Account* const &a, Account* const &b) { return a->getUserID() < b->getUserID(); });
}
if(criteria=="checkouts")
{
sort (AccountsToSort.begin(), AccountsToSort.end(), [](Account* const &a, Account* const &b) { return a->getCheckouts() > b->getCheckouts(); });
}
return AccountsToSort;
}
//%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
//Check if account exists
bool AccountList::hasAccount(int ID)
{
return (ListOfAccounts.find(ID)!=ListOfAccounts.end());
}
//%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
#endif