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 288 289 290 291 292 293 294
|
// Preprocessor directives
#include <cstdlib>
#include <iostream>
#include <string>
// Avoid name conflicts.
using namespace std;
/*---------------------------- CLASS DECLARATIONS ----------------------------*/
class Person
{
public:
Person();
Person(int ID, string firstName, string lastName);
string getFirstName();
string getLastName();
int getID();
bool equals(const Person& theOtherPerson);
private:
string firstName;
string lastName;
int ID;
};
class Student: public Person
{
public:
Student();
Student(int ID, string firstName, string lastName, string major, int collegeYear);
private:
string major;
int collegeYear;
};
class Vector
{
public:
Vector();
int getCapacity();
int getSize();
int getIndex(int ID);
Student get(int index);
void print();
bool add(Student *newStudent);
int remove(int productID);
private:
Student *list[];
int capacity;
int size;
};
class Database
{
public:
Database();
bool add(int ID, string firstName, string lastName, string major, int collegeYear);
int remove(int ID);
Student get(int index);
int getIndex(int ID);
void print();
private:
Vector *list;
};
/*----------------------------- 'main' FUNCTION ------------------------------*/
int main ()
{
Database *testDatabase = new Database();
testDatabase->remove(2);
testDatabase->add(1, "John", "Doe", "Computer Science", 4);
testDatabase->add(2, "Jane", "Doe", "Computer Science", 3);
testDatabase->add(3, "Joe", "Shmoe", "Computer Science", 2);
testDatabase->print();
testDatabase->add(3, "Joe", "Shmoe", "Computer Science", 2);
testDatabase->remove(2);
testDatabase->print();
system("PAUSE");
return 0;
}
/*--------------------------- FUNCTION DEFINITIONS ---------------------------*/
Person::Person()
{
}
Person::Person(int ID, string firstName, string lastName)
{
this->firstName = firstName;
this->lastName = lastName;
this->ID = ID;
}
string Person::getFirstName()
{
return firstName;
}
string Person::getLastName()
{
return lastName;
}
int Person::getID()
{
return ID;
}
bool Person::equals(const Person& theOtherObject)
{
if(!(this->firstName.compare(theOtherObject.firstName)) && !(this->lastName.compare(theOtherObject.lastName)) && (this->ID == theOtherObject.ID))
{
return true;
}
else
{
return false;
}
}
Student::Student()
{
}
Student::Student(int ID, string firstName, string lastName, string major, int collegeYear):Person(ID, firstName, lastName)
{
this->major = major;
this->collegeYear = collegeYear;
}
Vector::Vector():capacity(2), size(0)
{
*list = new Student[capacity];
}
int Vector::getCapacity()
{
return capacity;
}
int Vector::getSize()
{
return size;
}
int Vector::getIndex(int ID)
{
int index = -1;
for(int i=0; i<size; i++)
{
if(list[i]->getID() == ID)
{
index = i;
}
}
return index;
}
Student Vector:: get(int index)
{
return *list[index];
}
void Vector::print()
{
for(int i=0; i<size; i++)
{
cout << list[i]->getID() << " " << list[i]->getFirstName() << " " << list[i]->getLastName() << "\n";
}
}
bool Vector::add(Student *newStudent)
{
bool result = true;
int index = getIndex(newStudent->getID());
if(index == -1)
{
if(size < capacity)
{
*list[size] = *newStudent;
size++;
}
else //grow
{
capacity *= 2;
Student *tmpList = new Student[capacity];
for(int i=0; i<size; i++)
{
tmpList[i] = *list[i];
*list = tmpList;
*list[size] = *newStudent;
size++;
}
}
}
else
{
result = false;
}
return result;
}
int Vector::remove(int productID)
{
int result = 1;
int index = getIndex(productID);
if(index == -1)
{
result = index;
}
else
{
for(int i = index; i < size-1; i++)
{
*list[i] = *list[i+1];
size -= 1;
}
if(size*2 == capacity) //shrink
{
capacity /= 2;
Student *tmpList = new Student[capacity];
for(int i=0; i<size; i++)
{
tmpList[i] = *list[i];
}
*list = tmpList;
size = capacity;
}
}
return result;
}
Database::Database()
{
Vector *list = new Vector();
}
bool Database::add(int ID, string firstName, string lastName, string major, int collegeYear)
{
bool result = list->add(new Student(ID, firstName, lastName, major, collegeYear));
if(!result)
{
cout << "This student(" << ID << ") is already in the database.\n";
}
else
{
cout << ID << " has been been added.\n";
}
return result;
}
int Database::remove(int ID)
{
int result = list->remove(ID);
if(result == -1)
{
cout << "This student (" << ID << ") is not in the database.\n";
}
else
{
cout << "This student (" << ID << ") has been deleted.\n";
}
return result;
}
Student Database::get(int index)
{
return list->get(index);
}
int Database::getIndex(int ID)
{
return list->getIndex(ID);
}
void Database::print()
{
return list->print();
}
| |