I am writing a program which has these require ments
1.Program must be command-line based and interactive, allowing the user to control its operation by entering commands.
2. ability to accept new data
3. ability to search for specific previously entered data
4. ability to save data to a file
5. ability to retrieve data from a file
6. at least 5 classes
7. must show inheritance and polymorphism
8. must read and write a file
I am writing a program to store a basketball league. I have two classes already called personType and PlayerType, one of which is inherited from the other.
I am now brainstorming for the part of my program which will store 3 different divisions. Each division will have 5 teams and each team will have 5 players.
I was going to have the division class be the base class and team class will be derived from division and player's derived from team.
Would it be best to have division object which have a string for the division name, an array for the team names and each team would have an array of players?
#ifndef H_playerType
#define H_playerType
#include <fstream>
#include "personType.h"
class playerType : public personType
{
public:
void print();
//Function to print player's first and last name
//and also their number and position
void printFile(ostream& outF);
//Function to print the player information.
//This function sends the player information to the
//output device specified by the parameter outF. If the
//actual parameter to this function is the object cout,
//then the output is shown on the standard output device.
//If the actual parameter is an ofstream variable, say
//outFile, then the output goes to the file specified by
//outFile.
void setPlayerNum(int num);
//Function to set the Player's number
//Postcondition: playerNum = num;
int getNum() const;
//Function to get player's number
//Postcondition: return playerNum
void setPlayerPos(string pos);
//Function to set the Player's position
//Postcondition: playerPos = pos;
string getPos() const;
playerType(string first = "", string last = "", int num = 0, string pos = "");
//Constructor
//sets the parameters specified by the user
//defaults first and last name and pos to null strings, num = 0
//Postcondition: personType name = first, last; playerNum = num; playerPos = pos;
private:
personType name; //variable to hold player's first and last name
int playerNum; //variable to store the player's Number
string playerPos; //variable to store the player's Position
};
#endif
#include <string>
#include <iostream>
#include <fstream>
#include <iomanip>
using namespace std;
#include "playerType.h"
playerType::playerType(string first, string last, int num, string pos)
: personType(first, last)
{
playerNum = num;
playerPos = pos;
} // end function playerType
void playerType::setPlayerNum(int num)
{
try //Exception Handling
{
if(num < 0 )
throw num;
playerNum = num;
}
catch (int num)
{
cout << "Exception: Player's number must be positive. ";
}
} //end function setPlayerNum
int playerType::getNum() const
{
return playerNum;
} //end function getNum
void playerType::setPlayerPos(string pos)
{
try //Exception handling
{
if (pos.length() > 2)
throw string("Player's position must be PG, SG, G, SF, PF, F,, FC or C!");
playerPos = pos;
}
catch (string str)
{
cout << "Exception handler: " << str << endl << endl;
}
} //end function setPlayerPos
string playerType::getPos() const
{
return playerPos;
} //end getNum function
void playerType::print()
{
personType::print();
cout << "Player's Number: " << playerNum << endl
<< "Player's Position: " << playerPos << endl << endl;
} //end function print
void playerType::printFile(ostream& outF)
{
outF << left;
outF << "Player Name:" << " " << "Number:" << " " << "Position:\n";
outF << setw(9) << getFirstName();
outF << setw(10) << getLastName();
outF << setw(12) << getNum();
outF << setw(12) << getPos();
}
#ifndef H_personType
#define H_personType
#include <string>
using namespace std;
class personType
{
friend ostream& operator<<(ostream& , const personType& );
friend istream& operator>>(istream& , personType& );
public:
void print() const;
//Function to output the first name and last name
//in the form firstName lastName.
void setName(string first, string last);
//Function to set firstName and lastName according
//to the parameters.
//Postcondition: firstName = first; lastName = last
string getFirstName() const;
//Function to return the first name.
//Postcondition: The value of the firstName is returned.
string getLastName() const;
//Function to return the last name.
//Postcondition: The value of the lastName is returned.
virtual float pay(float hoursWorked) const;
//Function exhibiting Polymorphism which calculates pay
//Postcondition: The value of the person's pay is returned.
personType(string first = "", string last = "", float personPay = 2500.00);
//constructor
//Sets firstName and lastName according to the parameters.
//The default values of the parameters are empty strings.
//Postcondition: firstName = first; lastName = last
private:
string firstName; //variable to store the first name
string lastName; //variable to store the last name
float payRate; //variable to store the person's payRate
};
#endif
#include <iostream>
#include <string>
#include "personType.h"
using namespace std;
//Overloaded extration << operator
ostream& operator<<(ostream& osObject, const personType& name)
{
osObject << name.firstName << " " << name.lastName << endl;
return osObject;
}
//Overloaded insertion >> operator
istream& operator>>(istream& isObject, personType& name)
{
cout << "Please enter Person's first and last names: " << endl;
isObject >> name.firstName >> name.lastName;
return isObject;
}
void personType::print() const
{
cout << "Name: " << firstName << " " << lastName << endl;
}
void personType::setName(string first, string last)
{
firstName = first;
lastName = last;
}
string personType::getFirstName() const
{
return firstName;
}
string personType::getLastName() const
{
return lastName;
}
float personType::pay(float hoursWorked) const
{
return hoursWorked * payRate;
}
//constructor
personType::personType(string first, string last, float personPay)
{
firstName = first;
lastName = last;
payRate = personPay;
}
#include <iostream>
#include <string>
#include <fstream>
#include "playerType.h"
#include "personType.h"
using namespace std;
int main()
{
ifstream inData;
ofstream outData;
inData.open("Player.txt");
outData.open("PlayerOutput.txt");
personType person("Jim", "Webb");
playerType player("Ray", "Allen", 34, "SG");
cout << "*_*_*_*_*_*_*_*_*_*_*_*_*_*_*_*_*_*_*_*_*_*_*_*_*_\n"
"Please choose a menu option: \n\n"
cout << "1. to add a player to a team." << endl
<< "2. to search for a player on a team." << endl
<< "3. to save list of players." << endl
<< "X. to EXIT the program." << endl;
player.printFile(outData);
inData.close();
outData.close();
}