-
Notifications
You must be signed in to change notification settings - Fork 0
/
Library.cpp
186 lines (155 loc) · 3.99 KB
/
Library.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
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
#include "Library.h"
// default constructor
// nothing loaded into library class
Library::Library() {
}
// main constructor
// version string loaded into constructor for the menu
Library::Library(std::string version) {
m_version = version;
}
// default deconstructor
Library::~Library() {
}
void Library::showMenu() {
std::cout << " Library Control System - Version " << m_version << "\n" << std::endl;
std::cout << "1. Add a Member" << std::endl;
std::cout << "2. Print out Member List" << std::endl;
std::cout << "3. View Member Information" << std::endl;
std::cout << "4. Check out a Book" << std::endl;
std::cout << "5. Add a Book to Inventory" << std::endl;
std::cout << "6. Remove a Book from Inventory" << std::endl;
std::cout << "7. Pay Fine" << std::endl;
std::cout << "8. Increase Date" << std::endl;
std::cout << "9. Save Database" << std::endl;
std::cout << "10. Load Database" << std::endl;
std::cout << "11. Shut Down System" << std::endl;
}
void Library::menu() {
int choice;
do {
showMenu();
std::cout << "Enter menu choice: ";
std::cin >> choice;
switch (choice) {
case 1: { // Add a Member
addMember();
} break;
case 2: { // Print out Member List
printMembers();
} break;
case 3: { // View Member Information
memberInfo();
} break;
case 4: { // Check out a Book
} break;
case 5: { // Add a Book to Inventory
} break;
case 6: { // Remove a book from inventory
} break;
case 7: { // Pay Fine
} break;
case 8: { // Increase Date
} break;
case 9: { // Save Database
} break;
case 10: { // Load Database
} break;
}
} while (choice < 11);
}
void Library::addMember() {
int id = 0,
pos = 0;
bool exists = true;
// Generate random ID # between 100000 and 999999
// then check to see if it exists already
while (exists) {
id = rand() % 899999 + 100000;
pos = searchIDs(id);
if (pos == -1) {
exists = false;
}
}
Member newMember(id);
m_members.push_back(newMember);
if (m_members.size() > 1) {
sortMembers(0, m_members.size() - 1);
}
}
int Library::searchIDs(int id) {
int first = 0,
last = m_members.size() - 1,
mid;
while (first <= last) {
mid = first + ((last - first) / 2);
if (m_members[mid].getID() == id) {
return mid;
}
else if (m_members[mid].getID() > id) {
last = mid - 1;
}
else {
first = mid + 1;
}
}
return -1;
}
void Library::sortMembers(int left, int right) {
// quicksort algorithm
int i = left,
j = right;
Member temp;
int pivot = m_members[(left + right) / 2].getID();
while (i <= j) {
while (m_members[i].getID() < pivot) {
i++;
}
while (m_members[j].getID() > pivot) {
j--;
}
if (i <= j) {
temp = m_members[i];
m_members[i] = m_members[j];
m_members[j] = temp;
i++;
j--;
}
}
if (left < j) {
sortMembers(left, j);
}
if (i < right) {
sortMembers(i, right);
}
}
void Library::printMembers() {
std::cout << std::left << std::setw(7) << "ID";
std::cout << std::left << std::setw(25) << "Name";
std::cout << std::left << std::setw(25) << "Address";
std::cout << std::left << std::setw(20) << "City";
std::cout << std::left << std::setw(6) << "State";
std::cout << std::left << "Zip Code" << std::endl;
for (unsigned int i = 0; i < m_members.size(); i++) {
std::cout << std::left << std::setw(7) << m_members[i].getID();
std::cout << std::left << std::setw(25) << m_members[i].getName();
std::cout << std::left << std::setw(25) << m_members[i].getAddress();
std::cout << std::left << std::setw(20) << m_members[i].getCity();
std::cout << std::left << std::setw(6) << m_members[i].getState();
std::cout << std::left << m_members[i].getZipcode() << std::endl;
}
}
void Library::memberInfo() {
int id,
pos;
std::cout << "Enter in the Member's ID: ";
std::cin >> id;
pos = searchIDs(id);
while (pos == -1) {
std::cout << "Member ID not found." << std::endl;
std::cout << "Please re-enter ID (-1 to quit): ";
std::cin >> id;
pos = searchIDs(id);
}
std::cout << m_members[pos].getName() << std::endl;
}