-
Notifications
You must be signed in to change notification settings - Fork 0
/
contestants.cpp
114 lines (99 loc) · 2.15 KB
/
contestants.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
// Parke Lovett
/* contestants.cpp file Class Definition (Implementation of all functions from contestants.h) */
#include "contestants.h"
Contestants::Contestants(){
size = 0;
}
int Contestants::getSize() const{
return size;
}
int Contestants::getMaxSize() const{
return MAX_SIZE;
}
bool Contestants::append(const Player& item_to_append){
if (size < MAX_SIZE){
data[size++] = item_to_append;
return true;
}
else{
return false;
}
}
bool Contestants::erase(const Player& item_to_remove){
int item_Found = find(item_to_remove);
if (item_Found != -1){
erase(item_Found);
return true;
}
else{
std::cout << "Item to remove not found, or index out of range\n";
return false;
}
// call find function then once found, call erase function
}
bool Contestants::erase(int index){
if (index > size || index < 0){
return false;
}
else{
data[index] = data[--size];
return true;
}
}
void Contestants::clearAll(){
size = 0;
}
int Contestants::find(const Player& obj){
int index = -1;
for (int i = 0; i < size; i++){
if (data[i] == obj){
index = i;
break;
}
}
return index;
}
Player Contestants::getItembyIndex(int index){
Player tmpPlayerObj;
if (index >= 0 && index < size){
return data[index];
}
return tmpPlayerObj;
}
bool Contestants::readfile(const std::string& fileName){
std::ifstream inFile_object;
Player tmpPlayerObj;
inFile_object.open(fileName);
if (!inFile_object){
std::cout << "Error opening file\n";
return false;
}
do {
inFile_object >> tmpPlayerObj;
append(tmpPlayerObj);
} while (!inFile_object.eof());
inFile_object.close();
return true;
}
bool Contestants::writefile(const std::string& fileName){
std::ofstream outFile_object;
outFile_object.open(fileName);
if (!outFile_object){
std::cout << "Error opening file\n";
return false;
}
for (int i = 0; i < size; i ++){
outFile_object << data[i];
}
outFile_object.close();
return true;
}
Player Contestants::operator[](int index){
Player default_obj;
if (index < size && index >= 0){
return data[index];
}
else{
return default_obj;
}
}