-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathLibrary.cpp
More file actions
125 lines (105 loc) · 3.62 KB
/
Library.cpp
File metadata and controls
125 lines (105 loc) · 3.62 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
#include <fstream>
#include <string>
#include <cstdlib>
#include <algorithm>
#include <ctime>
#include "Library.h"
#include "StringTokenizer.h"
#include "HelperFunctions.h"
using namespace std;
Library::Library(Date current) :currentDate(current){}
// if the periodical was passed to another employee, that employee is returned
void Library::ReturnToLibraryandPassOn(Periodical& p, Employee& e, Date currentDate, int daysLate)
{//Jordan
e.removeBookFromList(p.getBarcode());
e.updateReliability(currentDate, p.getCheckOutDate(), p.getMaxCheckoutDuration());
if (p.morePeopleInQueue() && p.passToNextEmployee(currentDate, daysLate, employees)) return;
else {
p.setCheckedOut(false);
p.setArchiveDate(currentDate);
ArchivePeriodical(p);
}
}
void Library::SimulateEmployeeAction(ostream& outputStream)
{
int randDaysToAdd;
for (map<string, Employee>::iterator iter = employees.begin(); iter != employees.end(); iter++)
{
if (iter->second.hasNoBooks()) continue;
if (iter->second.isLazy()) randDaysToAdd = rand() % 6;
else randDaysToAdd = rand() % 2;
string barcode = iter->second.getTopBookFromList();
outputStream << iter->second.getName() << " returned " << circulatingPeriodicals[barcode].getName() << " after " << randDaysToAdd << " days." << endl;
ReturnToLibraryandPassOn(circulatingPeriodicals[barcode], iter->second, currentDate, randDaysToAdd);
}
}
void Library::ExecuteSimulator() // Jordan's function
{
ofstream simulatorFile("SimulatorData.txt");
while (!circulatingPeriodicals.empty())
{
currentDate.add_days(7);
SimulateEmployeeAction(simulatorFile);
}
employees.begin()->second.PrintEmployeeDataBeforeNextSim(simulatorFile, employees);
simulatorFile.close();
}
void Library::ReadPeriodicalsFromFile()
{//Evan w/ Brenton debug
ifstream fin("Periodicals.txt");
if (fin)
{
string line, aName, barcode;
while (getline(fin, line))
{
String_Tokenizer st(line, ",");
aName = trim(st.next_token());
barcode = trim(st.next_token());
circulatingPeriodicals[barcode] = Periodical(aName, barcode);
}
}
fin.close();
}
void Library::ReadEmployeesFromFile()
{//Evan w/ Brenton debug
ifstream fin("Employees.txt");
if (fin)
{
string line, empName, startVacation, endVacation, lazyString;
int lateDays, theWaitingTime;
bool lazyBool;
while (getline(fin, line))
{
String_Tokenizer st(line, ",");
empName = trim(st.next_token());
lateDays = stoi(trim(st.next_token()));
theWaitingTime = stoi(trim(st.next_token()));
startVacation = trim(st.next_token());
endVacation = trim(st.next_token());
lazyString = trim(st.next_token());
transform(lazyString.begin(), lazyString.end(), lazyString.begin(), ::toupper); // convert to all uppercase
if (lazyString == "LAZY")
lazyBool = true;
else if (lazyString == "NOT LAZY")
lazyBool = false;
else
throw::exception("Invalid lazy input from employees.txt file");
employees[empName] = Employee(lateDays, empName, Date(startVacation, DateFormat::US), Date(endVacation, DateFormat::US), theWaitingTime, lazyBool);
}
}
fin.close();
}
void Library::buildPriorityQueues(){
//Brenton
for (map<string,Periodical>::iterator itr = circulatingPeriodicals.begin(); itr != circulatingPeriodicals.end(); itr++){
itr->second.generateEmpQueue(employees);
itr->second.passToNextEmployee(currentDate, 0, employees);
}
}
void Library::ArchivePeriodical(Periodical p) // Evan
{
if (circulatingPeriodicals.empty())
throw::exception("All periodicals have been archived");
circulatingPeriodicals.erase(p.getBarcode());
archivedPeriodicals[p.getBarcode()] = p;
}