Final Programming c++

Need your ASSIGNMENT done? Use our essay writing service to score better and meet your deadline.


Order a Similar Paper Order a Different Paper

 

//Classes(or Libraries)

#include <iostream>

#include <string>

#include <fstream>

#include <sstream>

#include <iomanip>

//Used to shorten console commands

using namespace std;

 

//Programmer defined encapsulation of data and functions

class Department

{

private:

string  DepartmentName, DepartmentHeadName, DepartmentID;

 

public:

// Constructors

    Department()

{

 

}

Department(string id, string name)

{

DepartmentID = id;

DepartmentName = name;

DepartmentHeadName = “”;

}

Department(string id, string name, string hn)

{

DepartmentID = id;

DepartmentName = name;

DepartmentHeadName = hn;

}

 

    //Setter and getters(Accsesors)

    string getDepId()

{

return DepartmentID;

}

string getDepartmentName()

{

return DepartmentName;

}

string getDepHeadName()

{

return DepartmentHeadName;

}

 

 

void setDepId(string DepId)

{

 

DepartmentID = DepId;

}

 

void setDepName(string DepName)

{

 

DepartmentName = DepName;

}

void setDepHead(string DepHead)

{

 

DepartmentHeadName = DepHead;

 

}

 

 

 

};

 

//Programmer defined encapsulation of data and functions 

class Employee

{

private:

string employeename, employeeID, employeeDepartmentID;

int employeeage;

doubleemployeesalary;

public:

 

    // constructors

    Employee() {}

Employee(string id, string name)

{

employeeID = id;

employeename = name;

}

Employee(string id, string name, int age)

{

employeeID = id;

employeename = name;

employeeage = age;

}

Employee(string id, string name, int age, double salary)

{

employeeID = id;

employeename = name;

employeeage = age;

employeesalary = salary;

 

}

Employee(string id, string name, int age, double salary, string departmentid)

{

employeeID = id;

employeename = name;

employeeage = age;

employeesalary = salary;

employeeDepartmentID = departmentid;

}

 

// Accessors

    string getEmpID()

{

return employeeID;

}

string getEmpName()

{

return employeename;

}

 

string getEmpDepID()

{

return employeeDepartmentID;

}

int getEmpAge()

{

return employeeage;

}

double getEmpSal()

{

return employeesalary;

}

void setEmpID(string empId)

{

 

employeeID = empId;

}

void setEmpName(string empName)

{

 

employeename = empName;

}

void setEmpDepID(string empDepId)

{

 

employeeDepartmentID = empDepId;

}

void setEmpAge(int empAge)

{

 

employeeage = empAge;

}

void setEmpSalary(double empsal)

{

 

employeesalary = empsal;

}

 

 

};

 

//Function prototypes

void ShowMenu();

void choices();

 

int main()

{

 

ShowMenu();

choices();

 

return 0;

}

 

//Display Function

void ShowMenu()

{

    cout << “nnn”;

cout << “1-Create Departmentn”;

cout << “2-Creat employeen”;

cout << “3-Write the data to the filen”;

cout << “4-Retrive the data from the filen”;

cout << “5-Display Reportn”;

cout << “6-Exitn n n”;

}

 

// Choice function

void choices()

{

// Creating array objects

    Department dep[3];

Employee emp[7];

 

    string depid, empid, empdepid, depname, dephead, empname, depnamefile, depdeadfile;

int empage; 

double empsalary;

int choice;

ifstream file_in, file_in2;

ofstream file_out, file_out2;

char again; //Used to loop the entire program

int counter, empcounter, depcounter;

bool id3 = false;

int a; //used for getting age from file

double s; //used for getting salarry from file

string info; // getting lines from file

size_t pos; //position

double sal1, sal2, sal3; //Salaries for calculation

 

 

do {

cout << “Please enter a number from the menu:”;

cin >> choice;

 

 

switch (choice)

{

case 1:

 

cout << “Please enter Department ID:”;

cin >> depid;

 

            //Loop to not get a duplicate

            for (int i = 0; i < 3; i++)

{

while (dep[i].getDepId() == depid)

{

cout << “The Department ID that you entered exist. Try again:”;

cin >> depid;

 

}

}

 

                //Condition to make sure not over arrays

if (depcounter < 3)

{

cout << “Please enter the department name:”;

cin >> depname;

 

cout << “Please enter the department head:”;

cin >> dephead;

 

dep[depcounter].setDepId(depid);

dep[depcounter].setDepName(depname);

dep[depcounter].setDepHead(dephead);

depcounter++;

}

else

cout << “Array is full.n”;

 

ShowMenu();

 

break;

 

case 2:

 

 

cout << “Please enter the employee ID:”;

cin >> empid;

 

            //Loop to not get duplicate

            for (int i = 0; i < 7; i++)

{

while (emp[i].getEmpID() == empid)

{

cout << “The Employee ID that you entered exist. Try again:”;

cin >> empid;

}

}

 

                //condition to not go over arrays sizes

if (empcounter < 7) {

 

cout << “Please enter the employee department ID:”;

cin >> empdepid;

for (int i = 0; i < 7; i++)

{

if (dep[i].getDepId() == empdepid)

{

id3 = true;

break;

}

 

}

if (id3)

{

cout << “Please enter the employee Name:”;

cin >> empname;

 

cout << “Please enter the employee Age:”;

cin >> empage;

 

cout << “Please enter the employee salary:”;

cin >> empsalary;

 

emp[empcounter].setEmpID(empid);

emp[empcounter].setEmpName(empname);

emp[empcounter].setEmpDepID(empdepid);

emp[empcounter].setEmpAge(empage);

emp[empcounter].setEmpSalary(empsalary);

empcounter++;

}

else

cout << “Department not foundn”;

}

else

cout << “Array is full.n”;

 

 

ShowMenu();

break;

case 3:

 

            //condition to mmake sure objects are compelted, the write to file

                

            if (depcounter == 3 && empcounter == 7)

{

file_out.open(“dep.txt”);

 

for (int counter = 0; counter < 3; counter++)

{

file_out << “Department ID: ” << dep[counter].getDepId() << endl;

file_out << “Department Name: ” << dep[counter].getDepartmentName() << endl;

file_out << “Department Head Name: ” << dep[counter].getDepHeadName() << endl;

file_out << endl << endl;

 

}

 

file_out.close();

 

file_out2.open(“emp.txt”);

 

for (int counter = 0; counter < 7; counter++)

{

file_out2 << “Employee ID: ” << emp[counter].getEmpID() << endl;

file_out2 << “Employee Name: ” << emp[counter].getEmpName() << endl;

file_out2 << “Employee Age: ” << emp[counter].getEmpAge() << endl;

file_out2 << “Employee Salary: ” << emp[counter].getEmpSal() << endl;

file_out2 << “Employee Department ID: ” << emp[counter].getEmpDepID() << endl;

file_out2 << endl << endl;

 

}

 

 

                //Making sure user doesn’t close the program without saving data

cout << “File not saved. Do you want to save the file? (Y/N)n”;

cin >> again;

if (toupper(again) == ‘Y’)

file_out.close();

file_out2.close();

}

else

cout << “Arrays not complete. Can’t write into files.n” << endl << endl;

 

 

ShowMenu();

break;

case 4:

 

 

file_in.open(“dep.txt”);

 

 

 

if (file_in)

{

int counter;

// getting department data from file

                while (getline(file_in, info) && counter < 3)

{

pos = info.find(“: “);

dep[counter].setDepId(info.substr(pos + 2));

getline(file_in, info);

pos = info.find(“: “);

dep[counter].setDepName(info.substr(pos + 2));

getline(file_in, info);

pos = info.find(“: “);

dep[counter].setDepHead(info.substr(pos + 2));

getline(file_in, info);

getline(file_in, info);

counter++;

}

file_in.close();

 

}

else cout << “File Dep not found.” << endl;

 

 

 

file_in2.open(“emp.txt”);

 

 

if (file_in2)

{

int counter;

//getting employee data from file

                while (getline(file_in2, info) && counter < 7)

{

pos = info.find(“: “);

emp[counter].setEmpID(info.substr(pos + 2));

getline(file_in2, info);

pos = info.find(“: “);

emp[counter].setEmpName(info.substr(pos + 2));

getline(file_in2, info);

pos = info.find(“: “);

istringstream flow(info.substr(pos + 2));

flow >> a;

emp[counter].setEmpAge(a);

getline(file_in2, info);

pos = info.find(“: “);

istringstream flow1(info.substr(pos + 2));

flow1 >> s;

emp[counter].setEmpSalary(s);

getline(file_in2, info);

pos = info.find(“: “);

emp[counter].setEmpDepID(info.substr(pos + 2));

getline(file_in2, info);

getline(file_in2, info);

counter++;

}

file_in2.close();

}

else cout << “File Emp not found.” << endl;

 

ShowMenu();

break;

 

case 5:

 

 

            //Linear search through arrays

for (int i = 0; i < 7; i++)

{

if (emp[i].getEmpDepID() == dep[0].getDepId())

{

sal1 = sal1 + emp[i].getEmpSal();

}

if (emp[i].getEmpDepID() == dep[1].getDepId())

{

sal2 = sal2 + emp[i].getEmpSal();

}

if (emp[i].getEmpDepID() == dep[2].getDepId())

{

sal3 = sal3 + emp[i].getEmpSal();

}

 

}

//displaying report

            cout << showpoint << fixed << setprecision(2);

            cout << “DepartmenttSalaryreportn”;

cout << dep[0].getDepartmentName() << “tt” << sal1 << “n”;

cout << dep[1].getDepartmentName() << “tt” << sal2 << “n”;

cout << dep[2].getDepartmentName() << “tt” << sal3 << “n”;

 

ShowMenu();

break;

 

case 6:

 

cout << “Exiting the program. Thank you.” << endl;

 

break;

 

default:

cout << “Invalid selection. Please Try again” << endl;

 

ShowMenu();

break;

 

 

 

 

 

 

}

} while (choice != 6);

}

 

 

 

The purpose of this project is to take your Midterm project  and  implement it using Random Access Binary Files.  

As you recall, the Midterm project used text files to store the data. Here in the final exam project, 

you will be storing the data in Random Access Binary File. 

 

Also, in the Midterm project you used Arrays to temporarily hold the data in the memory until the user

decides to write the data to file. Here you will not be using Arrays and instead writing the 

data directly to Random Access Binary File. Please read the chapter Advance File and I/O operations 

before attempting this. 

 

Here is the full description of the Final exam project. 

 

Modify your Midterm Exam Project to:

 

1. Replace  Employee and Department classes with Employee and Department Structures.

 

2. Inside each structure, replace all string variables with  array of characters.

 

3. Make Employee and Department editable. That means, the user should be able to edit a given Employee and Department. 

 

4. Do not allow the user to edit the Employee ID and Department ID. 

 

5. Use Random Access Files to store the data in Binary Form. This means, you should not use an Arrays to

store the data in the memory. Instead, when the user wants to create a new Employee/Department, 

you write it to the file right away. Also when the user says he/she wants to edit 

an Employee/Department, ask the user to enter EmployeeID/DepartmentID. 

Read the data from the file and display it to the user. 

Allow the user to enter new data and write it back to the file in the same position inside the file. 

Please read the chapter . Advance File/IO operations which has examples on how to do this.