Can someone modify this code for me to do the following? Replace the structs in
ID: 3772667 • Letter: C
Question
Can someone modify this code for me to do the following? Replace the structs in your solution to the program with classes. Replace the array of class objects with a vector of class objects Modify the program to copy the contents of a file into the vector of class objects Modify the program to copy the vector of class objects to a file.
#include "stdafx.h"
#include <iostream>
#include <iomanip>
#include <string>
struct DataOfCustomerAccount {
std::string nameOfCustomer;
std::string addressOfCustomer;
std::string cityOfCustomer;
std::string stateOfCustomer;
int ZIP_Code;
double telephoneNumber;
double accountBalance;
std::string dateOfLastPayment;
};
DataOfCustomerAccount getDetails(DataOfCustomerAccount &);
int getCustomer(DataOfCustomerAccount[], int, std::string);
void displayDetails(DataOfCustomerAccount[], int);
void SearchforAccount(DataOfCustomerAccount[], int, std::string);
int main() {
const int SIZE = 20;
DataOfCustomerAccount customers[SIZE];
DataOfCustomerAccount customer;
int userChoice;
int count = 0;
std::string fullName;
int modify;
do {
std::cout << std::endl;
std::cout << "MENU" << std::endl;
std::cout << "1. Add the details of new customer" << std::endl;
std::cout << "2. Change the details of a customer" << std::endl;
std::cout << "3. Display the details of all customer" << std::endl;
std::cout << "4. Search the details of all customer" << std::endl;
std::cout << "5. Exit" << std::endl;
std::cout << "Please enter your choice: ";
std::cin >> userChoice;
switch (userChoice) {
case 1:
customers[count] = getDetails(customer);
count++;
break;
case 2:
std::cout << std::endl << "Please enter full name of customer to be modified: ";
std::cin >> fullName;
modify = getCustomer(customers, count, fullName);
if (modify == -1) {
std::cout << "Wrong customer name to modify" << std::endl;
} else {
customers[modify] = getDetails(customer);
if (customers[modify].nameOfCustomer != fullName) {
std::cout << std::endl << "Unable to change the name." << std::endl;
customers[modify].nameOfCustomer = fullName;
}
}
break;
case 3:
displayDetails(customers, count);
break;
case 4:
std::cout << std::endl << "Please enter name of customer to be searched: ";
std::cin >> fullName;
SearchforAccount(customers, count, fullName); break;
case 5:
return 0;
default:
std::cout << "Please enter 1,2,3 or 4 only" << std::endl;
}
} while (userChoice != 5 && count<SIZE);
return 0;
}
DataOfCustomerAccount getDetails(DataOfCustomerAccount &cust) {
std::cout << std::endl;
std::cout << "Please enter the name of customer: ";
std::cin >> cust.nameOfCustomer;
std::cout << "Please enter the address of customer: ";
std::cin >> cust.addressOfCustomer;
std::cout << "Please enter the city of customer: ";
std::cin >> cust.cityOfCustomer;
std::cout << "Please enter the state of customer: ";
std::cin >> cust.stateOfCustomer;
std::cout << "Please enter the ZIP code: ";
std::cin >> cust.ZIP_Code;
std::cout << "Please enter the telephone number: ";
std::cin >> cust.telephoneNumber;
std::cout << "Please enter the account balance: $";
std::cin >> cust.accountBalance;
while (cust.accountBalance<0)
{
std::cout << "Please enter a positive number: $";
std::cin >> cust.accountBalance;
}
std::cout << "Please enter the date of last payment (dd/mm/yyyy): ";
std::cin >> cust.dateOfLastPayment;
return cust;
}
int getCustomer(DataOfCustomerAccount cust[], int size, std::string name) {
int customer = -1;
for (int i = 0; i<size; i++) {
if (cust[i].nameOfCustomer == name) {
customer = 1;
break;
}
}
return customer;
}
void displayDetails(DataOfCustomerAccount cust[], int size) {
if (size == 0)
std::cout << std::endl << "No customers in the array to display." << std::endl;
else {
for (int i = 0; i<size; i++) {
std::cout << std::endl;
std::cout << "The name of customer: " << cust[i].nameOfCustomer << std::endl;
std::cout << "The address of customer: " << cust[i].addressOfCustomer << std::endl;
std::cout << "The city of customer: " << cust[i].cityOfCustomer << std::endl;
std::cout << "The state of customer: " << cust[i].stateOfCustomer << std::endl;
std::cout << "The ZIP code: " << std::setprecision(10) << cust[i].ZIP_Code << std::endl;
std::cout << "The telephone number: " << std::setprecision(15) << cust[i].telephoneNumber << std::endl;
std::cout << "The account balance: $" << std::setprecision(15) << cust[i].accountBalance << std::endl;
std::cout << "The date of last payment: " << cust[i].dateOfLastPayment << std::endl;
}
}
}
void SearchforAccount(DataOfCustomerAccount cust[], int size, std::string nameToSearch) {
bool is_found = false;
for (int i = 0; i<size; i++) {
if (cust[i].nameOfCustomer.find(nameToSearch) != std::string::npos) {
std::cout << std::endl;
std::cout << "The name of customer: " << cust[i].nameOfCustomer << std::endl;
std::cout << "The address of customer: " << cust[i].addressOfCustomer << std::endl;
std::cout << "The city of customer: " << cust[i].cityOfCustomer << std::endl;
std::cout << "The state of customer: " << cust[i].stateOfCustomer << std::endl;
std::cout << "The ZIP code: " << std::setprecision(10) << cust[i].ZIP_Code << std::endl;
std::cout << "The telephone number: " << std::setprecision(15) << cust[i].telephoneNumber << std::endl;
std::cout << "The account balance: $" << std::setprecision(15) << cust[i].accountBalance << std::endl;
std::cout << "The date of last payment: " << cust[i].dateOfLastPayment << std::endl;
is_found = true;
}
}
if (!is_found) {
std::cout << "No customer found with this name:" << std::endl;
}
}
Explanation / Answer
Here is the script. Please make sure to include algorithm header file to make sure that the find function of the vector is working. This would work great :)
#include "stdafx.h"
#include <iostream>
#include <iomanip>
#include <string>
#include <algorithm>
using namespace std;
class DataOfCustomerAccount {
public:
string nameOfCustomer;
string addressOfCustomer;
string cityOfCustomer;
string stateOfCustomer;
int ZIP_Code;
double telephoneNumber;
double accountBalance;
string dateOfLastPayment;
DataOfCustomerAccount getDetails(DataOfCustomerAccount &);
DataOfCustomerAccount getCustomer(vector<DataOfCustomerAccount>, string fullName);
void displayDetails(vector<DataOfCustomerAccount> cust);
void SearchforAccount(vector<DataOfCustomerAccount> cust,string nameToSearch) {
bool is_found = false;
for (int i = 0; i<cust.size(); i++) {
if (cust[i].nameOfCustomer.find(nameToSearch) != string::npos) {
cout << endl;
cout << "The name of customer: " << cust[i].nameOfCustomer << endl;
cout << "The address of customer: " << cust[i].addressOfCustomer << endl;
cout << "The city of customer: " << cust[i].cityOfCustomer << endl;
cout << "The state of customer: " << cust[i].stateOfCustomer << endl;
cout << "The ZIP code: " << setprecision(10) << cust[i].ZIP_Code << endl;
cout << "The telephone number: " << setprecision(15) << cust[i].telephoneNumber << endl;
cout << "The account balance: $" << setprecision(15) << cust[i].accountBalance << endl;
cout << "The date of last payment: " << cust[i].dateOfLastPayment << endl;
is_found = true;
}
}
if (!is_found) {
cout << "No customer found with this name:" << endl;
}
void displayDetails(vector<DataOfCustomerAccount> cust) {
if (cust.size == 0)
cout << endl << "No customers in the array to display." << endl;
else {
for (int i = 0; i<cust.size; i++) {
cout << endl;
cout << "The name of customer: " << cust[i].nameOfCustomer << endl;
cout << "The address of customer: " << cust[i].addressOfCustomer << endl;
cout << "The city of customer: " << cust[i].cityOfCustomer << endl;
cout << "The state of customer: " << cust[i].stateOfCustomer << endl;
cout << "The ZIP code: " << setprecision(10) << cust[i].ZIP_Code << endl;
cout << "The telephone number: " << setprecision(15) << cust[i].telephoneNumber << endl;
cout << "The account balance: $" << setprecision(15) << cust[i].accountBalance << endl;
cout << "The date of last payment: " << cust[i].dateOfLastPayment << endl;
}
}
}
DataOfCustomerAccount getDetails(DataOfCustomerAccount &cust) {
cout << endl;
cout << "Please enter the name of customer: ";
cin >> cust.nameOfCustomer;
cout << "Please enter the address of customer: ";
cin >> cust.addressOfCustomer;
cout << "Please enter the city of customer: ";
cin >> cust.cityOfCustomer;
cout << "Please enter the state of customer: ";
cin >> cust.stateOfCustomer;
cout << "Please enter the ZIP code: ";
cin >> cust.ZIP_Code;
cout << "Please enter the telephone number: ";
cin >> cust.telephoneNumber;
cout << "Please enter the account balance: $";
cin >> cust.accountBalance;
while (cust.accountBalance<0)
{
cout << "Please enter a positive number: $";
cin >> cust.accountBalance;
}
cout << "Please enter the date of last payment (dd/mm/yyyy): ";
cin >> cust.dateOfLastPayment;
return cust;
}
DataOfCustomerAccount getCustomer(vector<DataOfCustomerAccount> cust,string name) {
if ((cust.begin(), cust.end(), name) != vector.end() )
return cust;
else return null;
}
};
int main() {
const int SIZE = 20;
// DataOfCustomerAccount customers[SIZE];
//Use this for Vector
vector<DataOfCustomerAccount> customers[SIZE];
DataOfCustomerAccount customer;
int userChoice;
int count = 0;
string fullName;
int modify;
do {
cout << endl;
cout << "MENU" << endl;
cout << "1. Add the details of new customer" << endl;
cout << "2. Change the details of a customer" << endl;
cout << "3. Display the details of all customer" << endl;
cout << "4. Search the details of all customer" << endl;
cout << "5. Exit" << endl;
cout << "Please enter your choice: ";
cin >> userChoice;
switch (userChoice) {
case 1:
DataOfCustomerAccount cust = getDetails(customer);
customers.insert(count,cust);
count++;
break;
case 2:
cout << endl << "Please enter full name of customer to be modified: ";
cin >> fullName;
DataOfCustomerAccount cust = getCustomer(customers);
if (cust == null) {
cout << "Wrong customer name to modify" << endl;
} else {
DataOfCustomerAccount cust2 = getDetails(cust);
if (cust2.nameOfCustomer != fullName) {
cout << endl << "Unable to change the name." << endl;
cust2.nameOfCustomer = fullName;
}
}
break;
case 3:
displayDetails(customers, count);
break;
case 4:
cout << endl << "Please enter name of customer to be searched: ";
cin >> fullName;
SearchforAccount(customers, count, fullName); break;
case 5:
return 0;
default:
cout << "Please enter 1,2,3 or 4 only" << endl;
}
} while (userChoice != 5 && count<SIZE);
return 0;
}
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.