This struct is provided in department.h . It represents a project which a department can work on.
Question:
This struct is provided in department.h. It represents a project which a department can work on. A project will have a topic represented by a C string that can hold a max of 25 characters. A project also has double that represents its cost.
Your task for part one of workshop 3 is to create a class called "Department" that encapsulates a Project. (in Department.h and Department.cpp).
Attributes
The Department class must keep the following information:
- The departments' name: that is held dynamically in a C string. (char *)
- The departments' projects: that is held dynamically by a Project pointer. (Project *)
- Number of Current Projects: that are all held in an integer. (int)
- Budget of the Department: that is held in a double (double). Should have an initial value of 15345.99.
Public methods
The Department class has the following Public functions:
void updateName(const char* newname)
If the provided name is valid (exists and is not empty), this function will set the name of the department. If the department is already set, it will deallocate the name. It will create enough memory for the name of the department to match the new name and then copies the data of the new name. If the new name is not valid, the department's current name will not change.
void updateBudget(double change)
This function will update the budget of the department by adding a new change to it.
bool addProject(Project& newproject)
This function will add a new project to the department as long as the total cost does not exceed the allocated budget. If the new project cost in addition to the total cost of the current project does not exceed the budget this function will add the new project to the department list of projects by doing the following steps:
- if there are no projects currently in progress:
- creates a new Project and saves the data inside it
- if there are projects currently in progress:
- allocates a temp array of projects with new size = old size + 1.
- copy all projects from the old array to the new one
- add new data to the last position of the temp array
- wipe out the old array
- increase the number of the project by one
- allocate projects with the new size
- copy all projects from the temp array to projects
- wipe out the temp array
- This function will eventually return true if a project was added to the department, and false otherwise.
void createDepartment(const char* newname, Project& newproject, double change);
This function will use functions updateName, addProject and updateBudget to set the name of the department, add a project to it and update its budget.
Project* fetchProjects() const
This function will return the list of projects a department is currently working on.
int fetchNumProjects() const;
This function will return the number of projects a department is currently working on.
double fetchBudget() const
This function will return the current budget of a department.
char* fetchName() const
This function will return the name of a department.
double totalexpenses()
This function will calculate and return the expenses of a department. The total expenses of a department is the total cost of all its current projects.
double remainingBudget()
This function will calculate and return the remaining budget of a department by subtracting the total expenses from the budget.
void clearDepartment()
This function will clear all the dynamic memory allocation in a department.
the departmant.h:
#ifndef SDDS_DEPARTMENT_H_ #define SDDS_DEPARTMENT_H_ namespace sdds {
const int MAX_TOPIC_LENGTH = 25;
struct Project { char m_topic[MAX_TOPIC_LENGTH+1]; double m_cost; };
//class Department does here //helper functions - do not belong inside the class void display(const Project& project); void display(const Department& department);
} #endif // !SDDS_DEPARTMENT_H_
main.cpp:
#include #include "Department.h" using namespace std; using namespace sdds;
int main() {
Project testProject = { "Base",551.55 };
Project myprojects[5] = { {"Synergy", 5041.55}, {"Mecha", 2925.99}, {"Chroma", 3995.45}, {"Force", 6988.45}, {"Oculus", 7441.22} };
Department SDDS{};
//Test1 cout << "**********Create Department TEST START**********" << endl; SDDS.createDepartment("School of Software development", testProject, 1); display(SDDS); cout << "**********Create Department TEST END**********" << endl;
//Test2 cout << endl << "**********Update Name TEST Start**********" << endl; SDDS.updateName("School of Software development and design"); display(SDDS); cout << "**********Update Name TEST END**********" << endl;
//Test3 cout << endl << "**********Update Budget TEST Start**********" << endl; SDDS.updateBudget(5555.99); display(SDDS); cout << "**********Update Budget TEST END**********" << endl;
//Test4 cout << endl << "**********Expenses and Remaining Budget TEST Start**********" << endl; cout << "Our current total Expenses are: " << SDDS.totalexpenses() << endl; cout << "Our remaining budget is: " << SDDS.remainingBudget() << endl; cout << "**********Expenses and Remaining Budget TEST END**********" << endl;
//Test5 cout << endl << "**********Add Project TEST START**********" << endl; for (int i = 0; i < 5 && SDDS.addProject(myprojects[i]); i++); display(SDDS); cout << "**********Add Project TEST END**********" << endl; SDDS.clearDepartment(); return 0; }
department.cpp:
#define _CRT_SECURE_NO_WARNINGS #include #include #include "Department.h"
using namespace std;
namespace sdds {
//fully provided for students to display details of a project void display(const Project& project) { cout << "Project " << project.m_topic << " will cost a total of " << project.m_cost << " C$." << endl; }
//fully provided for students to display details of a department void display(const Department& department) { Project* temp = department.fetchProjects(); int projects = department.fetchNumProjects(); cout << "Department " << department.fetchName() << " details:" << endl; cout << "Budget: " << department.fetchBudget() << " and the number of projects in progress is: " << projects << endl; cout << "Below are the details of the projects we are currently working on: " << endl; for (int i = 0; i < projects; i++) { display(temp[i]); } }
}
Microeconomics An Intuitive Approach with Calculus
ISBN: 978-0538453257
1st edition
Authors: Thomas Nechyba