In this assignment, you will be creating a class that defines the object User th
ID: 3855963 • Letter: I
Question
In this assignment, you will be creating a class that defines the object User that will keep track of a person’s details and the details of their physical activities. You will be able to use your code from Assignment 4, but you will need to create the User.h and User.cpp files as well. If your Assignment 4 didn’t use the arrays, then you may have to change the implementation. You should use the same activitydata.csv file to access the file information.
Your User class should have the following:
Private variables:
- int userID
- string username
- int numRuns = 0 //a counter that keeps track of the number of run distances that have been added to the runDistances array
- int numSkiErgs = 0 //a counter that keeps track of the number of skierg distances that have been added to the skiErgDistances array
- int numErgs = 0 //a counter that keeps track of the number of erg distances that have been added to the ergDistances array
- double runDistances[10] = {}
- double skiErgDistances[10] = {}
- double ergDistances[10] ={}
Public methods:
- User Constructor: The constructor should initialize userID to 0 and userName to an empty string (two quotation marks).
- User Destructor: The destructor doesn’t need to do anything at this time.
- void setUserName(string newUserName): This method should set the variable userName to the newUserName.
- void setUserID(int newUserID):
This method should set the variable userID to the newUserID.
- int getUserID(): This method should return the value stored in the variable userID.
- string getUserName(): This method should return the value stored in the variable username.
- void processData(string fileName): This method will read activity data from the activitydata.csv file with name fileName and call the method addActivity (described below) within this function to process the data properly.
- double getActivityDetails(string activityType, string reportType): This method returns the distance details for a given activity (options: run, erg, skierg, all) based on the specified type of report (options: avg or total). Please note: this method should provide the same output as the getDistance function that you wrote in Assignment 4.
Private method:
Explanation / Answer
// I have given full class code of your requirement. You can test it using main function.
#include <cstdlib>
#include <iostream>
#include<fstream>
#include<sstream>
using namespace std;
class User
{
private:
int userID;
string userName;
int numRuns;
int numSkiErgs;
int numErgs;
double runDistances[10];
double skiErgDistances[10];
double ergDistances[10];
void addActivity(string activityType, double activityDistance)
{
if(activityType=="run")
{
runDistances[numRuns++]=activityDistance;
}
if(activityType=="ski")
{
ergDistances[numErgs++]=activityDistance;
}
if(activityType=="skierg")
{
skiErgDistances[numSkiErgs++]=activityDistance;
}
}
public:
User()
{
userID=0;
userName="";
numRuns=0;
numSkiErgs=0;
numErgs=0;
}
//destructor
void setUserName(string newUserName)
{
userName=newUserName;
}
void setUserID(int newUserID)
{
userID=newUserID;
}
int getUserID()
{
return userID;
}
string getUserName()
{
return userName;
}
void processData(string fileName)
{
ifstream infile;
infile.open(fileName.c_str());
string line;
while(getline(infile, line))
{
istringstream iss(line);
string activity;
double distance;
iss>>activity>>distance;
addActivity(activity,distance);
}
}
double getActivityDetails(string activityType, string reportType)
{
double total_run=0;
double total_ski=0;
double total_ergs=0;
double total_all=0;
if(reportType=="avg")
{
if(activityType=="run")
{
for(int i=0;i<numRuns;i++)
total_run=total_run+ runDistances[i];
return total_run/ numRuns;
}
if(activityType=="ski")
{
for(int i=0;i<numErgs;i++)
total_ergs=total_ergs+ ergDistances[i];
return total_ergs/ numErgs;
}
if(activityType=="skierg")
{
for(int i=0;i<numSkiErgs;i++)
total_ski=total_ski+ skiErgDistances[i];
return total_ski/ numSkiErgs;
}
if(activityType=="all")
{
for(int i=0;i<numSkiErgs;i++)
total_all=total_all+ skiErgDistances[i];
for(int i=0;i<numErgs;i++)
total_all=total_all+ ergDistances[i];
for(int i=0;i<numRuns;i++)
total_all=total_all+ runDistances[i];
return total_all/ (numSkiErgs+numErgs+numRuns);
}
}
if(reportType=="total")
{
if(activityType=="run")
{
for(int i=0;i<numRuns;i++)
total_run=total_run+ runDistances[i];
return total_run;
}
if(activityType=="ski")
{
for(int i=0;i<numErgs;i++)
total_ergs=total_ergs+ ergDistances[i];
return total_ergs;
}
if(activityType=="skierg")
{
for(int i=0;i<numSkiErgs;i++)
total_ski=total_ski+ skiErgDistances[i];
return total_ski;
}
if(activityType=="all")
{
for(int i=0;i<numSkiErgs;i++)
total_all=total_all+ skiErgDistances[i];
for(int i=0;i<numErgs;i++)
total_all=total_all+ ergDistances[i];
for(int i=0;i<numRuns;i++)
total_all=total_all+ runDistances[i];
return total_all;
}
}
}
};