Project Three Adaptor Pattern
- Insert UML Here
Employee Header (Employee.h)
#ifndef EMPLOYEE_H
#define EMPLOYEE_H
#include <string>
using std::string;
#include <iostream>
using std::cout;
using std::endl;
class Employee{
protected:
string First_Name;
string Last_Name;
unsigned short salary_;
unsigned short hire_year;
public:
// General Constructor
Employee(string firstName, string lastName, unsigned short salary, unsigned short hireYear)
{
First_Name = firstName;
Last_Name = lastName;
salary_ = salary;
hire_year = hireYear;
};
// Constructor if nothing is passed
Employee(){
First_Name = "";
Last_Name = "";
hire_year = 0;
}
virtual ~Employee(){} // Deconstructor
// Getters and Setters
string get_First_Name(){ return First_Name; }
string get_Last_Name(){ return Last_Name;}
unsigned short get_Salary(){ return salary_;}
unsigned short getHireYear(){return hire_year;}
void set_First_Name(const string first_name){First_Name = first_name;}
void set_Last_Name(const string last_name){ Last_Name = last_name;}
void set_Salary(const unsigned short sal){salary_ = sal;}
void set_HireYear(const unsigned short hire_y){ hire_year = hire_y;}
virtual void DisplayEmployee()const{
cout << First_Name << " " << Last_Name << ": " << salary_ << "; " << hire_year << endl;
}
};
#endif /* EMPLOYEE_H */
Manager Header (Manager.h)
#ifndef MANAGER_H
#define MANAGER_H
#include "Employee.h"
#include <vector>
class Manager: public Employee{
protected:
vector<Employee*> group; // Vector of pointers to group of Employees
string department;
public:
// Default Constructor
Manager(){
cout << "Using Constructor 1 to construct a Manager " << endl;
First_Name = "Default_First_Name";
Last_Name = "Default_last_Name";
hire_year = 2002;
salary_ = 42;
department = "CSE";
}
Manager(string f_n, string l_n, string dep, int sal, short int h_y) :Employee(f_n, l_n, sal, h_y){
cout << "Using Constructor 2 to Construct a Manager" << endl;
First_Name = f_n;
Last_Name = l_n;
hire_year = h_y;
salary_ = sal;
department = dep;
}
Manager(Manager &other){
cout << "Using Copy Constructor for Manager" << endl;
// Copy Constructor
First_Name = other.First_Name;
Last_Name = other.Last_Name;
salary_ = other.salary_;
hire_year = other.hire_year;
department = other.department;
//group = other.group; // Maybe??
}
Manager& operator=(Manager &other){
// Operator Overloaded
cout << "Using Operator Overloader for Manager " << endl;
if(this!=&other){
this->First_Name = other.First_Name;
this->Last_Name = other.Last_Name;
this->salary_ = other.salary_;
this->hire_year = other.hire_year;
this->department = other.department;
// this->m_group = other.group;
}
return *this; // Return Pointer to this new "object"
}
virtual ~Manager(){} // Deconstructor
string getDepartment()const{return department;}
void setDepartment(string d){department = d;}
vector<Employee*> getGroup()const{return group;}
void setGroup(vector<Employee*> &g){group = g;}
virtual void addEmployee(Employee* emp){group.push_back(emp);}
virtual Employee getEmployee(int i){return *(group[i]);}
// Displays Empolyee, Const is so nothing is changed
virtual void DisplayEmployee()const{
cout << First_Name << " " << Last_Name << ": " << salary_ << "; " << hire_year << "; "<< department << endl;
}
};
#endif /* MANAGER_H */
EmployeeDataBase Header (EmployeeDataBase.h)
#ifndef EMPLOYEEDATABASE_H
#define EMPLOYEEDATABASE_H
#include "Employee.h"
#include "Manager.h"
#include <string>
using std::string;
#include <algorithm>
#include "SortableVector.h"
class EmployeeDatabase : public SortableVector{
protected:
string name; // Name of the DataBase
vector<Employee*> database; // the actual vector of employees in the Database
public:
EmployeeDatabase(){} // Constructor
EmployeeDatabase(const vector<Employee*>& g) // Constructor if a database is initiated with a vector of employees
{
database = g;
}
virtual ~EmployeeDatabase(){
// Virtual Deconstructor to destroy the database, to loop the vector and delete each Element
for(int i = 0; i < database.size(); i++){
delete database[i];
}
}
virtual unsigned int getSize() const {
return database.size(); // Returns size of Database(aka size of the vector) (const so can't change db)
}
virtual bool smaller(int i, int j) const{ // compares the size of two databases (const so nothing is changed in the database with this method)
return database[i] > database[j];
}
virtual void swap(int i, int j){
// Swapping the contents of data
Employee* temp = database[i];
database[i] = database[j];
database[j] = temp;
}
void AddRecord(Employee *newAdd){ // Add New Employee to the Database
database.push_back(newAdd);
}
Employee* getRecord(int i)
{
return database[i]; // Return Employee at certain index
}
void DisplayRecords(){
// Loops over DB and Prints vector
for(int i = 0; i < database.size(); i++){
database[i]->DisplayEmployee();
}
}
};
#endif /* EMPLOYEEDATABASE_H */
EmployeeDatabaseComparisons Header (EmployeeDatabaseComparisons.h)
#ifndef EMPLOYEEDATABASECOMPARISONS_H
#define EMPLOYEEDATABASECOMPARISONS_H
class EmployeeDatabaseComparisons{
virtual unsigned int getSize() const = 0;
virtual bool compare_first_name(int, int) const = 0;
virtual bool compare_last_name(int, int) const = 0;
virtual bool compare_salary(int, int) const = 0;
virtual bool compare_hire_year(int, int) const = 0;
};
#endif
EmployeeDatabaseAdapter Header (EmployeeDatabaseAdapter.h)
#ifndef EMPLOYEEDATABASEADAPTER_H
#define EMPLOYEEDATABASEADAPTER_H
#include "EmployeeDatabase.h"
#include "EmployeeDatabaseComparisons.h"
class EmployeeDatabaseAdapter : public EmployeeDatabase, public EmployeeDatabaseComparisons {
public:
EmployeeDatabaseAdapter(){}
EmployeeDatabaseAdapter(const vector<Employee*>& g){
database = g;
}
virtual unsigned int getSize() const {
return database.size();
}
virtual bool compare_first_name(int i, int j) const {
return database[i]->get_First_Name() > database[j]->get_First_Name();
}
virtual bool compare_last_name(int i, int j) const {
return database[i]->get_Last_Name() > database[j]->get_Last_Name();
}
virtual bool compare_salary(int i, int j) const {
return database[i]->get_Salary() > database[j]->get_Salary();
}
virtual bool compare_hire_year(int i, int j) const {
return database[i]->getHireYear() > database[j]->getHireYear();
}
};
#endif /* EMPLOYEEDATABASEADAPTER_H */
Bubble Sort (BubbleSort.h)
#ifndef BUBBLESORT_H
#define BUBBLESORT_H
#include "SortableVector.h"
#include "EmployeeDatabaseAdapter.h"
class BubbleSortInterface{
public:
// Method for Sorting Datbase
void Sort(EmployeeDatabaseAdapter* empDatabase){
bool sorted = false;
int n = empDatabase->getSize();
while(!sorted){
sorted = true;
for(int i = 1; i < n; i++){
if(compare(empDatabase, i, i-1)){
empDatabase->swap(i-1, i);
sorted = false;
}
}
n--;
}
}
protected:
virtual bool compare(EmployeeDatabaseAdapter* empDatabase, int i, int j) const = 0;
};
// Implementing Sorting algorithms based on the BubbleSort interface Above!!!!!
//
class BubbleSortFirstName : public BubbleSortInterface{
protected:
virtual bool compare(EmployeeDatabaseAdapter* empDatabase, int i, int j) const {
return !empDatabase->compare_first_name(i, j);
}
};
class BubbleSortFirstNameReverse : public BubbleSortInterface{
protected:
virtual bool compare(EmployeeDatabaseAdapter* empDatabase, int i, int j) const {
return empDatabase->compare_first_name(i, j);
}
};
class BubbleSortLastName : public BubbleSortInterface{
protected:
virtual bool compare(EmployeeDatabaseAdapter* empDatabase, int i, int j) const {
return !empDatabase->compare_last_name(i, j);
}
};
class BubbleSortLastNameReverse : public BubbleSortInterface{
protected:
virtual bool compare(EmployeeDatabaseAdapter* empDatabase, int i, int j) const {
return empDatabase->compare_last_name(i, j);
}
};
class BubbleSortSalary : public BubbleSortInterface{
protected:
virtual bool compare(EmployeeDatabaseAdapter* empDatabase, int i, int j) const {
return !empDatabase->compare_salary(i, j);
}
};
class BubbleSortSalaryReverse : public BubbleSortInterface{
protected:
virtual bool compare(EmployeeDatabaseAdapter* empDatabase, int i, int j) const {
return empDatabase->compare_salary(i, j);
}
};
class BubbleSortYear : public BubbleSortInterface{
protected:
virtual bool compare(EmployeeDatabaseAdapter* empDatabase, int i, int j) const {
return !empDatabase->compare_hire_year(i, j);
}
};
class BubbleSortYearReverse : public BubbleSortInterface{
protected:
virtual bool compare(EmployeeDatabaseAdapter* empDatabase, int i, int j) const {
return empDatabase->compare_hire_year(i, j);
}
};
#endif /* BUBBLESORT_H */
Main File (main.cpp)
- Lets run our file and see what happens!!!!
Output:
*********************** Before Sorting *******************
Using Constructor 2 to Construct a Manager
Using Constructor 2 to Construct a Manager
Display Record
Lebron James: 10000; 2011
Kobe Bryant: 20000; 2009
Michael Jordan: 30000; 2013
Tom Anderson: 40000; 2000; Gender Studies
Jimmy Reder: 15000; 2010
Austin Adams: 25000; 2011
Devin Powers: 35000; 2012
Aaron Schreck: 9000; 2009
Michael Jackson: 45000; 2000; Human Resources
_________________________________________________________________
*********************** After Sorting By First Name Alphabetical *********************
Aaron Schreck: 9000; 2009
Austin Adams: 25000; 2011
Devin Powers: 35000; 2012
Jimmy Reder: 15000; 2010
Kobe Bryant: 20000; 2009
Lebron James: 10000; 2011
Michael Jackson: 45000; 2000; Human Resources
Michael Jordan: 30000; 2013
Tom Anderson: 40000; 2000; Gender Studies
*********************** After Sorting By First Name Reverse Alphabetical ***********************
Tom Anderson: 40000; 2000; Gender Studies
Michael Jackson: 45000; 2000; Human Resources
Michael Jordan: 30000; 2013
Lebron James: 10000; 2011
Kobe Bryant: 20000; 2009
Jimmy Reder: 15000; 2010
Devin Powers: 35000; 2012
Austin Adams: 25000; 2011
Aaron Schreck: 9000; 2009
*********************** After Sorting By Last Name Alphabetical ***********************
Austin Adams: 25000; 2011
Tom Anderson: 40000; 2000; Gender Studies
Kobe Bryant: 20000; 2009
Michael Jackson: 45000; 2000; Human Resources
Lebron James: 10000; 2011
Michael Jordan: 30000; 2013
Devin Powers: 35000; 2012
Jimmy Reder: 15000; 2010
Aaron Schreck: 9000; 2009
*********************** After Sorting By Salary Increasing ***********************
Aaron Schreck: 9000; 2009
Lebron James: 10000; 2011
Jimmy Reder: 15000; 2010
Kobe Bryant: 20000; 2009
Austin Adams: 25000; 2011
Michael Jordan: 30000; 2013
Devin Powers: 35000; 2012
Tom Anderson: 40000; 2000; Gender Studies
Michael Jackson: 45000; 2000; Human Resources
*********************** After Sorting By Salary Decreasing ***********************
Michael Jackson: 45000; 2000; Human Resources
Tom Anderson: 40000; 2000; Gender Studies
Devin Powers: 35000; 2012
Michael Jordan: 30000; 2013
Austin Adams: 25000; 2011
Kobe Bryant: 20000; 2009
Jimmy Reder: 15000; 2010
Lebron James: 10000; 2011
Aaron Schreck: 9000; 2009
*********************** After Sorting By Hire year Increasing ***********************
Michael Jackson: 45000; 2000; Human Resources
Tom Anderson: 40000; 2000; Gender Studies
Aaron Schreck: 9000; 2009
Kobe Bryant: 20000; 2009
Jimmy Reder: 15000; 2010
Lebron James: 10000; 2011
Austin Adams: 25000; 2011
Devin Powers: 35000; 2012
Michael Jordan: 30000; 2013
*********************** After Sorting By Hire year Decreasing ***********************
Michael Jordan: 30000; 2013
Devin Powers: 35000; 2012
Lebron James: 10000; 2011
Austin Adams: 25000; 2011
Jimmy Reder: 15000; 2010
Aaron Schreck: 9000; 2009
Kobe Bryant: 20000; 2009
Michael Jackson: 45000; 2000; Human Resources
Tom Anderson: 40000; 2000; Gender Studies