In: Computer Science
Write a C++ program that will be an information system for Oregon State University using classes as well as demonstrating a basic understanding of inheritance and polymorphism.
You will create a representation of an Oregon State University information system that will contain information about the university. The university will contain a name of the university, n number of buildings, and m number of people. People can be either a student or an instructor. Every person will have a name and an age. Every student will have also have a GPA, but an instructor will NOT have a GPA. Every instructor will have an instructor rating, but a student will NOT have an instructor rating. Every building will have a name, the size in sqft (preferred the real value which you need to look up), and an address (stored as a string, also preferred to look this up).
People will contain a method called “do_work” that will take in a random integer as a parameter that represents how many hours they will do work for. If the person is a student, a message will be printed to the screen that says “PERSON_NAME did X hours of homework.” If the person is an instructor, a message will be printed to the screen that says “Instructor PERSON_NAME graded papers for X hours.” You will need to fill in the appropriate values.
The student GPA can either be an input from the user or randomized, but it must be between 0.0 and 4.0. It cannot be preset. The instructor rating can either be an input from the user or randomized, but it must be between 0.0 and 5.0. The ages of a person can be randomized or an input, but make it realistic. You can choose whether it is randomized or user input, or both.
The university will contain a method that will print the name and address of all the buildings in its information system and another method that will print the name of all the people. The name of the university MUST be “Oregon State University” (because we are the best).
You will manually instantiate at least 1 student, 1 instructor, and 2 buildings, then give them values and store them appropriately in the university object. You can do this in whatever fashion you wish.
You will have a menu that does at least the following:
1) Prints names of all the buildings
2) Prints names of everybody at the university
3) Choose a person to do work
4) Exit the program
Note that option 3 will require you to print another menu that gives options for each person.
You may create any other functions, methods, member variables, etc. to modularize your code and complete the lab.
Extra Credit Add an option to save the information system to a file, and add an option to read a saved information system from a file so that you can close the program, but not lose information. This will also require you to be able to add people and/or buildings to the program during runtime. This is an all or nothing extra credit (you will not get partial points for partial completion).
Program Screenshot:
Screenshot of the output:
Code to copy:
#include <iostream>
#include <vector>
#include <string>
#include <ctime>
#include <cstdlib>
#include <fstream>
using namespace std;
//define the class Person
class Person
{
private:
//declare the member variables
string personName;
int personAge;
public:
//declare the member functions
Person();
Person(string pname);
Person(string pname, int page);
string getPersonName();
int getPersonAge();
virtual void print() = 0;
virtual void do_work(int randInt) = 0;
};
//definition of the default constructors
Person::Person()
{
personName = "";
personAge = 0;
}
//definition of the constructors
//sets the data variables of the class
Person::Person(string pname)
{
personName = pname;
personAge = 0;
}
//definition of the constructors
//sets the data variables of the class
Person::Person(string pname, int page)
{
personName = pname;
personAge = page;
}
//getter function of the person name
string Person::getPersonName()
{
return personName;
}
//getter function of the person age
int Person::getPersonAge()
{
return personAge;
}
//definition of the clas Student
//which is the subclass of the Person
class Student : public Person
{
private:
//declare the member variables
double studentGPA;
public:
//declare the member functions
Student();
Student(string sname);
Student(string sname, int sage, double sgpa);
double getStudentGPA();
void print();
void do_work(int randInt);
};
//definition of the default constructors
Student::Student() : Person()
{
studentGPA = 0.0;
}
//definition of the constructors
//sets the data variables of the class
Student::Student(string sname) : Person(sname)
{
studentGPA = 0.0;
}
//definition of the constructors
//sets the data variables of the class
Student::Student(string sname, int sage, double sgpa) : Person(sname, sage)
{
studentGPA = sgpa;
}
//getter function of the student gpa.
double Student::getStudentGPA()
{
return studentGPA;
}
//definition of the function print()
//prints the student name, age, and gpa
void Student::print()
{
cout << "Student Name: " << getPersonName() << endl;
cout << "Student Age: " << getPersonAge() << endl;
cout << "Student GPA: " << studentGPA << endl;
}
//definition of the function do_work()
//take in a random integer as a parameter that
//represents how many hours they will do work for.
void Student::do_work(int randInt)
{
cout << getPersonName() << " did " << randInt << " hours of homework." << endl;
}
//definition of the clas Instructor
//which is the subclass of the Person
class Instructor : public Person
{
private:
//declare the member variables
double instructorRating;
public:
//declare the member functions
Instructor();
Instructor(string iname);
Instructor(string iname, int iage, double irating);
double getInstructorRating();
void print();
void do_work(int randInt);
};
//definition of the default constructors
Instructor::Instructor() : Person()
{
instructorRating = 0.0;
}
//definition of the constructors
//sets the data variables of the class
Instructor::Instructor(string iname) : Person(iname)
{
instructorRating = 0.0;
}
//definition of the constructors
//sets the data variables of the class
Instructor::Instructor(string iname, int iage, double irating) : Person(iname, iage)
{
instructorRating = irating;
}
//getter function of the instructor Rating.
double Instructor::getInstructorRating()
{
return instructorRating;
}
//definition of the function print()
//prints the Instructor name, age, and Rating
void Instructor::print()
{
cout << "Instructor Name: " << getPersonName() << endl;
cout << "Instructor Age: " << getPersonAge() << endl;
cout << "Instructor Rating: " << instructorRating << endl;
}
//definition of the function do_work()
//take in a random integer as a parameter that
//represents how many hours they will do work for.
//If the person is an instructor, a message will be
//printed to the screen that says “Instructor PERSON_NAME
//graded papers for X hours
void Instructor::do_work(int randInt)
{
cout << "Instructor " << getPersonName() <<
" graded papers for " << randInt << " hours." << endl;
}
//definition of the clas Building
class Building
{
private:
//declare the member variables
string buildingName;
int buildingSize;
string buildingAddress;
public:
//declare the member functions
Building();
Building(string bname, int bsize, string baddress);
string getBuildingName();
int getBuildingSize();
string getBuildingAddress();
void printBuilding();
};
//definition of the default constructors
Building::Building()
{
buildingName = "";
buildingSize = 0;
buildingAddress = "";
}
//definition of the constructors
//sets the data variables of the class
Building::Building(string bname, int bsize, string baddress)
{
buildingName = bname;
buildingSize = bsize;
buildingAddress = baddress;
}
//getter function to get the building name
string Building::getBuildingName()
{
return buildingName;
}
//getter function to get the building size
int Building::getBuildingSize()
{
return buildingSize;
}
//getter function to get the building address
string Building::getBuildingAddress()
{
return buildingAddress;
}
//definition of the function printBuilding()
//prints the Building name, Size, and Address
void Building::printBuilding()
{
cout << "Building Name: " << buildingName << endl;
cout << "Building Size: " << buildingSize << " sqft" << endl;
cout << "Building Address: " << buildingAddress << endl;
}
//definition of the clas University
//which is the subclass of the Building
class University
{
private:
//declare the member functions
string universityName;
//
vector<Building> buildings;
vector<Person*> people;
public:
University();
University(string uname);
void addBuilding(Building b);
void addPerson(Person* p);
string getUniversityName();
void printNamesOfTwoBuildings();
void printNamesOfAllBuildings();
void printNamesOfAllPeople();
void printAllDetailsOfAllBuildings();
void printAllDetailsOfAllPeople();
void writeDataToFile(string fileName);
void readDataFromFileToConsole(string fileName);
void person_do_work(Person* p);
};
// University class implementation
University::University()
{
universityName = "";
}
University::University(string uname)
{
universityName = uname;
}
void University::addBuilding(Building b)
{
buildings.push_back(b);
}
void University::addPerson(Person* p)
{
people.push_back(p);
}
string University::getUniversityName()
{
return universityName;
}
void University::printNamesOfTwoBuildings()
{
if (buildings.empty())
cout << "No buildings are in the university!" << endl;
else
{
cout << "\nAt most two buildings in the university..." << endl;
for (unsigned int i = 0; i < buildings.size() && i < 2; i++)
{
cout << "Building #" << (i + 1) << ": "
<< buildings.at(i).getBuildingName() << ", "
<< buildings.at(i).getBuildingAddress() << endl;
}
}
}
void University::printNamesOfAllBuildings()
{
if (buildings.empty())
cout << "No buildings are in the university!" << endl;
else
{
cout << "\nAll the buildings in the university..." << endl;
for (unsigned int i = 0; i < buildings.size(); i++)
{
cout << "Building #" << (i + 1) << ": "
<< buildings.at(i).getBuildingName()
<< ", " << buildings.at(i).getBuildingAddress() << endl;
}
}
}
void University::printNamesOfAllPeople()
{
if (people.empty())
cout << "No people are in the university!" << endl;
else
{
cout << "\nAll the people in the university..." << endl;
for (unsigned int i = 0; i < people.size(); i++)
{
cout << "Person #" << (i + 1) << ": " << people.at(i)->getPersonName() << endl;
}
}
}
void University::printAllDetailsOfAllBuildings()
{
if (buildings.empty())
cout << "No buildings are in the university!" << endl;
else
{
cout << "\nAll the buildings in the university..." << endl;
for (unsigned int i = 0; i < buildings.size(); i++)
{
cout << "\nBuilding #" << (i + 1) << endl;
buildings.at(i).printBuilding();
}
}
}
void University::printAllDetailsOfAllPeople()
{
if (people.empty())
cout << "No people are in the university!" << endl;
else
{
cout << "\nAll the people in the university..." << endl;
for (unsigned int i = 0; i < people.size(); i++)
{
cout << "\nPerson #" << (i + 1) << endl;
people.at(i)->print();
}
}
}
void University::writeDataToFile(string fileName)
{
ofstream outfile;
outfile.open(fileName);
outfile << "University Name: " << universityName << endl << endl;
if (buildings.empty())
outfile << "No buildings are in the university!" << endl;
else
{
outfile << "All the buildings in the university..." << endl;
for (unsigned int i = 0; i < buildings.size(); i++)
{
outfile << "\nBuilding #" << (i + 1) << endl;
outfile << "Building Name: " << buildings.at(i).getBuildingName() << endl;
outfile << "Building Size: " << buildings.at(i).getBuildingSize() << " sqft" << endl;
outfile << "Building Address: " << buildings.at(i).getBuildingAddress() << endl;
}
}
outfile << endl;
if (people.empty())
outfile << "No people are in the university!" << endl;
else
{
outfile << "All the people in the university..." << endl;
for (unsigned int i = 0; i < people.size(); i++)
{
outfile << "\nPerson #" << (i + 1) << endl;
if (Student* st = dynamic_cast<Student*>(people.at(i)))
{
outfile << "Student Name: " << st->getPersonName() << endl;
outfile << "Student Age: " << st->getPersonAge() << endl;
outfile << "Student GPA: " << st->getStudentGPA() << endl;
}
else if (Instructor* in = dynamic_cast<Instructor*>(people.at(i)))
{
outfile << "Instructor Name: " << in->getPersonName() << endl;
outfile << "Instructor Age: " << in->getPersonAge() << endl;
outfile << "Instructor Rating: " << in->getInstructorRating() << endl;
}
}
}
outfile.close();
}
void University::readDataFromFileToConsole(string fileName)
{
ifstream infile;
infile.open(fileName);
if (!infile)
{
cout << fileName << " file could not opened!" << endl;
exit(1);
}
string line;
bool empty = true;
cout << endl;
getline(infile, line);
while (infile)
{
cout << line << endl;
empty = false;
getline(infile, line);
}
infile.close();
if (empty)
cout << "No data is saved in the file!" << endl;
}
void University::person_do_work(Person* p)
{
bool found = false;
for (unsigned int i = 0; i < people.size(); i++)
{
if (people.at(i)->getPersonName().compare(p->getPersonName()) == 0)
{
found = true;
int randInt = rand() % 8;
if (Student* st = dynamic_cast<Student*>(p))
{
st->do_work(randInt);
}
else if (Instructor* in = dynamic_cast<Instructor*>(p))
{
in->do_work(randInt);
}
}
}
if (found == false)
cout << "No person is found with the name "
<< p->getPersonName() << " in the university." << endl;
}
// start main function
int main()
{
//create objects of the student
Student st1("Amelia", 36, 3.2);
Student st2("Jack", 27, 3.8);
//create an Instructor object
Instructor in1("Joseph", 37, 4.0);
Instructor in2("Ambuj", 48, 4.5);
//create Building objects
Building bu1("Willis Tower", 3200, "Chicago");
Building bu2("Empire State", 3500, "New York City");
Building bu3("Columbia Center", 3800, "Seattle");
//create University
University uni("Oregon State University");
//add students and instructors to the university
uni.addPerson(&st1);
uni.addPerson(&st2);
uni.addPerson(&in1);
uni.addPerson(&in2);
//add building to the university
uni.addBuilding(bu1);
uni.addBuilding(bu2);
uni.addBuilding(bu3);
//declare the variables
int choice;
int option;
string temp;
string name;
int age;
double gpa;
double rating;
int size;
string address;
string fileName;
Building bu;
Student st;
Instructor in;
do
{
//print the menu options
cout << "\n***** MENU *****" << endl;
cout << "1) Print names of all the buildings" << endl;
cout << "2) Print names of everybody at the university" << endl;
cout << "3) Choose a person to do work" << endl;
cout << "4) Print all details of all the buildings" << endl;
cout << "5) Print all details of all the people" << endl;
cout << "6) Add a building" << endl;
cout << "7) Add a person" << endl;
cout << "8) Save data to the file" << endl;
cout << "9) Read data from the file" << endl;
cout << "0) Exit the program" << endl;
cout << "Enter you choice: ";
//read the choice from the lunch
cin >> choice;
getline(cin, temp);
switch (choice)
{
case 1:
//print all the building names
uni.printNamesOfAllBuildings();
break;
case 2:
//print all the persons names
uni.printNamesOfAllPeople();
break;
case 3:
cout << "\n*** SUBMENU ***" << endl;
cout << "1. Student" << endl;
cout << "2. Instructor" << endl;
cout << "Enter your option: ";
cin >> option;
// read the remaining line to end
getline(cin, temp);
if (option == 1)
{
cout << "\nEnter the student name: ";
getline(cin, name);
st = Student(name);
uni.person_do_work(&st);
}
else if (option == 2)
{
cout << "\nEnter the instructor name: ";
getline(cin, name);
in = Instructor(name);
uni.person_do_work(&in);
}
else
{
cout << "Invalid option!" << endl;
}
break;
case 4:
uni.printAllDetailsOfAllBuildings();
break;
case 5:
uni.printAllDetailsOfAllPeople();
break;
case 6:
cout << "\nEnter the building name: ";
getline(cin, name);
cout << "Enter the building size in sqft: ";
cin >> size;
// read the remaining line to end
getline(cin, temp);
cout << "Enter the building address: ";
getline(cin, address);
bu = Building(name, size, address);
uni.addBuilding(bu);
break;
case 7:
cout << "\n*** SUBMENU ***" << endl;
cout << "1. Student" << endl;
cout << "2. Instructor" << endl;
cout << "Enter your option: ";
cin >> option;
// read the remaining line to end
getline(cin, temp);
if (option == 1)
{
cout << "Enter the student name: ";
getline(cin, name);
do
{
cout << "Enter the student age(1-100): ";
cin >> age;
// read the remaining line to end
getline(cin, temp);
} while (age < 1 || age > 100);
do
{
cout << "Enter the student gpa(0.0-4.0): ";
cin >> gpa;
getline(cin, temp);
} while (gpa < 0 || gpa > 4.0);
st = Student(name, age, gpa);
uni.addPerson(&st);
}
else if (option == 2)
{
cout << "Enter the instructor name: ";
getline(cin, name);
do
{
cout << "Enter the instructor age(1-100): ";
cin >> age;
// read the remaining line to end
getline(cin, temp);
} while (age < 1 || age > 100);
do
{
cout << "Enter the instructor rating(0.0-5.0): ";
cin >> rating;
// read the remaining line to end
getline(cin, temp);
} while (rating < 0 || rating > 5.0);
in = Instructor(name, age, rating);
uni.addPerson(&in);
}
else
{
cout << "Invalid option!" << endl;
}
break;
case 8:
cout << "Enter the output file name: ";
getline(cin, fileName);
uni.writeDataToFile(fileName);
break;
case 9:
cout << "Enter the input file name: ";
getline(cin, fileName);
uni.readDataFromFileToConsole(fileName);
break;
case 0:
cout << "Thank you!" << endl;
break;
default:
cout << "Invalid choice!" << endl;
}
} while (choice != 0);
//system("pause");
return 0;
}