-
Notifications
You must be signed in to change notification settings - Fork 0
/
call_class.cpp
executable file
·287 lines (233 loc) · 9.28 KB
/
call_class.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
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
#include <iostream>
#include <string>
#include <fstream>
#include "call_class.h"
using namespace std;
/************************************************************************************************************************************/
//Name: default constructor
//Precondition: takes no parameters
//Postcondition: initializes the class "call_class" and reads the data from the file “callstats_data.txt” into the dynamic array call_DB
//Decription: Reads the data file of call information (cell number, relays and call length) into the dynamic array of call record,
//call_DB. If the count because equal to the size the function double_size is called and the memory allocated to call_DB is doubled.
/************************************************************************************************************************************/
call_class::call_class()
{
size = 5;
count = 0;
call_DB = new call_record[size];
ifstream in;
in.open("callstats_data.txt");
double_size();
while (!in.eof())
{
if (is_full() == true)
{
double_size();
}
in >> call_DB[count].firstname >> call_DB[count].lastname
>> call_DB[count].cell_number >> call_DB[count].relays
>> call_DB[count].call_length;
count++;
}
in.close();
}
/************************************************************************************************************************************/
//Name: copy constructor
//Precondition: takes the class to be copied as the only parameter
//Postcondition: makes a deep copy of the class
//Decription: performs a deep copy.
/************************************************************************************************************************************/
call_class::call_class(const call_class & cop)
{
count = cop.count;
size = cop.size;
call_DB = new call_record[size];
for (int i = 0; i < count; i++)
{
call_DB[i] = cop.call_DB[i];
}
}
/***********************************************************************************************************************************/
//Name: empty
//Precondition: takes no parameters
//Postcondition: If count == 0 then true is returned; otherwise false is returned
//Decription: returns true if call_DB is empty
/**********************************************************************************************************************************/
bool call_class::empty()
{
if (count == 0)
{
cout << "record is empty" << endl;
return true;
}
return false;
}
/**********************************************************************************************************************************/
//Name: is_full
//Precondition: takes no parameters
//Postcondition: If count == size then true is return; otherwise false. The size is the capacity which is the total number of cells allocated to call_DB
//Decription: returns true if call_DB is full
/*********************************************************************************************************************************/
bool call_class::is_full()
{
if (count == size)
{
return true;
}
return false;
}
/**********************************************************************************************************************************/
//Name: search
//Precondition: takes no parameters
//Postcondition: returns the location of key in call_DB if it is there; otherwise -1 is returned
//Decription: locates key in call_DB if it is there; otherwise -1 is returned
/*********************************************************************************************************************************/
int call_class::search(const string key)
{
for (int i = 0; i < count; i++)
{
if (call_DB[i].cell_number == key)
{
return i;
}
}
return -1;
}
/*********************************************************************************************************************************/
//Name: add
//Precondition: takes a string variable called "key" in which stores the phone number provided by the user
//Postcondition: prompts the user for the firstname, lastname, relays and call length to add a new record in call_DB
//Decription: add key to call_DB; if call_DB is full, double_size is called to increase the size of call_DB.
/********************************************************************************************************************************/
void call_class::add(const string key)
{
if (is_full() == true)
{
double_size();
}
call_DB[count].cell_number = key;
cout << "Please enter the First Name: ";
cin >> call_DB[count].firstname;
cout << endl;
cout << "Please enter the Last Name: ";
cin >> call_DB[count].lastname;
cout << endl;
cout << "Please enter the Relay: ";
cin >> call_DB[count].relays;
cout << endl;
cout << "Please enter the Call Length: ";
cin >> call_DB[count].call_length;
cout << endl;
count++;
}
/******************************************************************************************************************************/
//Name: double_size
//Precondition: takes no parameters
//Postcondition: doubles the size of the array of records (call_DB)
//Decription: doubles the size (capacity) of call_DB
/******************************************************************************************************************************/
void call_class::double_size()
{
size *= 2;
call_record *temp = new call_record[size];
for (int i = 0; i<count; i++)
{
temp[i] = call_DB[i];
}
delete[] call_DB;
call_DB = temp;
}
/******************************************************************************************************************************/
//Name: process
//Precondition: takes no parameters
//Postcondition: calculate the net cost of a call (net_cost), the tax on a call (call_tax) and the total cost of the call (total_cost)
// using the number of relay stations (relays) and the length in minutes of the call (call_length)
// for all call records stored in call_DB
//Decription: calculate the net cost, tax rate, call tax and total cost for every call record in call_DB.
/*****************************************************************************************************************************/
void call_class::process()
{
for (int i = 0; i < count; i++)
{
call_DB[i].net_cost = call_DB[i].relays / 50.0 * 0.40 * call_DB[i].call_length;
if (call_DB[i].relays <= 5)
{
call_DB[i].tax_rate = 0.01;
}
else if (call_DB[i].relays >= 6 && call_DB[i].relays <= 11)
{
call_DB[i].tax_rate = 0.03;
}
else if (call_DB[i].relays >= 12 && call_DB[i].relays <= 20)
{
call_DB[i].tax_rate = 0.05;
}
else if (call_DB[i].relays >= 21 && call_DB[i].relays <= 50)
{
call_DB[i].tax_rate = 0.08;
}
else if (call_DB[i].relays > 50)
{
call_DB[i].tax_rate = 0.12;
}
call_DB[i].call_tax = call_DB[i].net_cost * call_DB[i].tax_rate;
call_DB[i].total_cost = call_DB[i].net_cost + call_DB[i].call_tax;
}
return;
}
/****************************************************************************************************************************/
//Name: operator<<
//Precondition: takes ostream variable and an object of the class call_class
//Postcondition: overloads the output operator "<<" and prints the output into a file and the screen
//Decription: Overloading operator<< as a friend function. Prints every field of every call_record in call_DB
// formatted to the screen and a file called "stats7_output.txt".
/***************************************************************************************************************************/
ostream & operator<<(ostream & out, call_class & Org)
{
for (int i = 0; i<Org.count; i++)
{
out << Org.call_DB[i].firstname << " "
<< Org.call_DB[i].lastname << " "
<< Org.call_DB[i].cell_number << " "
<< Org.call_DB[i].relays << " "
<< Org.call_DB[i].call_length << " "
<< Org.call_DB[i].net_cost << " "
<< Org.call_DB[i].tax_rate << " "
<< Org.call_DB[i].call_tax << " "
<< Org.call_DB[i].total_cost << endl;
}
ofstream output;
output.open("stats7_output.txt");
if (output.fail())
{
cout << "Unable to open the output file" << endl;
}
else
{
for (int i = 0; i<Org.count; i++)
{
output << Org.call_DB[i].firstname << " "
<< Org.call_DB[i].lastname << " "
<< Org.call_DB[i].cell_number << " "
<< Org.call_DB[i].relays << " "
<< Org.call_DB[i].call_length << " "
<< Org.call_DB[i].net_cost << " "
<< Org.call_DB[i].tax_rate << " "
<< Org.call_DB[i].call_tax << " "
<< Org.call_DB[i].total_cost << endl;
}
}
output.close();
return out;
}
/****************************************************************************************************************************/
//Name: destructor
//Precondition: takes no parameters
//Postcondition: automatically called by the compiler to de-allocate new memory that was allocated to call_DB
//Decription: de-allocates all memory allocated to call_DB. This should be the last function to be called before the program
// is exited.
/***************************************************************************************************************************/
call_class::~call_class()
{
delete[]call_DB;
}