Your task is to modify your array of structs program (prog2) that maintains a li
ID: 3764717 • Letter: Y
Question
Your task is to modify your array of structs program (prog2) that maintains a list of Insurance clients and convert it to an array of objects. You will use a class called Client that contains a name, policy. vears as a client and premium as members. The program will again read command codes and data from the keyboard to process the list just like you did in prog2. In fact, except for using temporary variables for input before you populate the object, your program interaction will be exactly the same. Here is the same list of the functionality your program will implement add one client to the list - update the policy and/or premium of a client - a listing of the clients a listing of all of the clients with a given policy (Auto, Home, Boat) - a listing of all of the loyal clients You should implement a solution to this problem using three files, client,h.client.cpp and prog4.cpp 1. client.h should contain the class definition as follows: class Client{ private string name; string policy int yrs; int prem: // Consist of the Name of the client // Policies include Auto, Home or Boat // Years as a client / Premium publi Default Constructor Contructor to handle all parameters Getter and setters for every member (setters for yrs and premium must not allow negative values) displayClient (prints one client in a tabular format on one line) 2. client.cpp should contain the member functions 3. prog4.cpp should still contain the same functionality from prog2. You will need to use the getter and setter functions along with the displayclient function to print the data. A. Here is the description for prog2. Your main function should do the following a. b. Declare an array of 30 objects to hold the data. Set NumClients to zeroExplanation / Answer
client.h
-----------------------------------------
#ifndef CLIENT_H
#define CLIENT_H
#include <iostream>
#include <iomanip>
using namespace std;
class Client
{
private:
string name; //Consist of the name of the client
string policy; //Policies include Home, Loan or Boat
int yrs; //Years as a client
int prem; //Premium
public:
Client(); //default constructor
Client(string,string,int, int); //parameterized constructor
//getter and setter functions for data members
string GetName() const;
void SetName(string name);
string GetPolicy() const;
void SetPolicy(string policy);
int GetPrem() const;
void SetPrem(int prem);
int GetYrs() const;
void SetYrs(int yrs);
void displayClient(); //displays a client information
};
#endif /* CLIENT_H */
-------------------------------------------
client.cpp:
---------------------------------------------
//Implementation of Client class
#include "client.h"
//Default constructor
Client::Client() {
name="";
policy="";
yrs=0;
prem=0;
}
//parameterized constructor
Client::Client(string name, string policy, int yrs, int prem) {
this->name=name;
this->policy=policy;
this->yrs=yrs;
this->prem=prem;
}
string Client::GetName() const {
return name;
}
void Client::SetName(string name) {
this->name = name;
}
string Client::GetPolicy() const {
return policy;
}
void Client::SetPolicy(string policy) {
this->policy = policy;
}
int Client::GetYrs() const {
return yrs;
}
void Client::SetYrs(int yrs) {
if (yrs > 0) {
this->yrs = yrs;
}
}
int Client::GetPrem() const {
return prem;
}
void Client::SetPrem(int prem) {
if (prem > 0) {
this->prem = prem;
}
}
void Client::displayClient() {
cout<<this->name<<setw(30)<<this->policy<<setw(10)<<this->yrs<<setw(10)<<this->prem<<endl;
}
----------------------------------------------
prog4.cpp:
-----------------------------------------------
#include <iostream>
#include <cstdlib>
#include "client.h"
#define MAXCLIENTS 30
using namespace std;
//trims leading and trailing spaces
string trim(string& str)
{
str.erase(0, str.find_first_not_of(' '));
str.erase(str.find_last_not_of(' ')+1);
return str;
}
//Linear Search for a client in client list
int LinearSearch(Client CList[], int NumClients, string name)
{
for(int i=0;i<NumClients;i++)
{
int loc=i; //location of client in list
string client_name=CList[loc].GetName();
if(client_name.compare(name)==0)
return loc;
}
return -1;
}
//Adds client to client list
void AddClient(Client CList[], int NumClients)
{
string name,policy;
int yrs,prem;
//Get client information
cout<<"Enter client name:";
cin.ignore();
getline(cin,name);
name=trim(name);
cout<<name<<endl;
cout<<"Enter Policy (any of Home, Loan, Boat):";
cin>>policy;
cout<<policy<<endl;
cout<<"Enter years:";
cin>>yrs;
cout<<yrs<<endl;
cout<<"Enter premium:";
cin>>prem;
cout<<prem<<endl;
//Create a new client
Client client(name,policy,yrs,prem);
//Adds client to list
CList[NumClients]=client;
}
//Updates an existing client information
void UpdateClient(Client CList[], int NumClients)
{
string name;
//Ask for client to update
cout<<"Enter client's name:";
cin.ignore();
getline(cin,name);
cout<<name<<endl;
//search for client in list
int loc=LinearSearch(CList,NumClients,name);
//updates client information, if found
if(loc!=-1)
{
cout<<"Client not found." <<endl;
}
else
{
string policy;
cout<<"Update Policy or enter S to skip:";
cin>>policy;
cout<<policy<<endl;
if(policy.compare("S")!=0)
{
CList[loc].SetPolicy(policy);
}
int prem;
cout<<"Update Premium or enter -1 to skip:";
cin>>prem;
cout<<prem<<endl;
if(prem!=-1)
{
CList[loc].SetPrem(prem);
}
}
}
//Prints all clients information
void ClientList(Client CList[], int NumClients)
{
cout<<"Name"<<setw(30)<<"Policy"<<setw(10)<<"Years"<<setw(10)<<"Premium"<<endl;
for(int i=0;i<NumClients;i++)
{
CList[i].displayClient();
}
}
//Prints all clients with a specific policy type (Auto, Home or Boat)
void PolicyList(Client CList[], int NumClients)
{
string policy;
cout<<"Enter policy type (Auto, Home or Boat):";
cin>>policy;
cout<<policy<<endl;
cout<<"Name"<<setw(30)<<"Policy"<<setw(10)<<"Years"<<setw(10)<<"Premium"<<endl;
for(int i=0;i<NumClients;i++)
{
Client client=CList[i];
string pol=client.GetPolicy();
if(pol.compare(policy)==0)
{
client.displayClient();
}
}
}
void LoyalList(Client CList[], int NumClients)
{
cout<<"Name"<<setw(30)<<"Policy"<<setw(10)<<"Years"<<setw(10)<<"Premium"<<endl;
for(int i=0;i<NumClients;i++)
{
Client client=CList[i];
int yrs=client.GetYrs();
if(yrs>=10)
{
client.displayClient();
}
}
}
//menu function
int menu()
{
int choice;
//Display a menu
cout<<"1. Enter new client information:"<<endl;
cout<<"2. Update client information:"<<endl;
cout<<"3. Display all account information:"<<endl;
cout<<"4. Display by policy:"<<endl;
cout<<"5. Display loyal clients:"<<endl;
cout<<"0. Exit the program."<<endl<<endl;
//Get the user's choice
cout<<"Enter your choice: ";
cin>>choice;
cout<<choice<<endl;
return choice;
}
/*
* main() function using Client class
*/
int main(int argc, char** argv) {
Client CList[MAXCLIENTS]; //List of clients
int NumClients=0; //Number of clients in the list
//loop to get users choice
while(1)
{
int choice=menu();
if(choice==1)
{
if(NumClients < MAXCLIENTS)
{
cout<<"Adding a new client..."<<endl;
AddClient(CList,NumClients);
NumClients++;
cout<<"Client added!"<<endl;
cout<<"---------------------"<<endl;
}
}
else if(choice==2)
{
if(NumClients!=0)
{
cout<<"Updating an existing client..."<<endl;
UpdateClient(CList,NumClients);
cout<<"Client updated!"<<endl;
cout<<"---------------------"<<endl;
}
}
else if(choice==3)
{
cout<<"Displaying client list..."<<endl;
ClientList(CList,NumClients);
cout<<"List complete!"<<endl;
cout<<"---------------------"<<endl;
}
else if(choice==4)
{
cout<<"Displaying client list with a given policy type..."<<endl;
PolicyList(CList,NumClients);
cout<<"List complete!"<<endl;
cout<<"---------------------"<<endl;
}
else if(choice==5)
{
cout<<"Displaying loyal client list(years 10 or more)..."<<endl;
LoyalList(CList,NumClients);
cout<<"List complete!"<<endl;
cout<<"---------------------"<<endl;
}
else if(choice==0)
{
cout<<"Exiting program..."<<endl;
exit(1);
}
}
return 0;
}
---------------------------------------
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.