Customer Accounts Write a program that uses a structure to store the following d
ID: 3767441 • Letter: C
Question
Customer Accounts
Write a program that uses a structure to store the following data about a customer account : Name, Address, City, State, ZIP, Telephone Number, Date of Last Payment (all strings) and Account Balance (double) The program should use an array of 10 structures. The program should have a menu-driven user interface containing the following choices.
*Write a function for each step.
*Read customers’ info from file customers.txt to the array.
*Change the account information for a given customer. Identify the customer by name. Ask the user to enter the name and new information for the customer and modify the customer’s info into the entered new values.
*Display all the data stored in the array.
*Search the structure array for a particular customer’s account. It should accept part of the customer’s name as an argument and then search for an account with a name that matches it. All accounts that match should be displayed (including all customer’s information). If no account matches, a message saying so should be displayed.
*Sort the structure array according to the customers’ balances in descending order. Then print the sorted structure array into file balances.txt. No need to submit the output file.
* Sort the structure array according to the customers’ names in ascending order. Then print the sorted structure array into file names.txt. No need to submit the output file.
* Compare the names of two given customers, and replace the address of smaller name with the address of larger name, and return the length of new address. For example, if the two given names are Alex_Humes and Alan_Elliot, the smaller name is Alan_Elliot. Thus its address will be replaced by 213_Twin_Oaks, which is Alex_humes’s address. Both their addresses will be 213_Twin_Oaks, and its length is 13.
*Exit the program.
Explanation / Answer
#include <iostream>
#include <string>
#include <vector>
#include <algorithm>
#include <fstream>
#include <cstdlib>
using namespace std;
struct Account
{
string name,
address,
cityStateZip,
telephone_Number,
date_Last_Payment;
double account_Balance;
};
void ClearScreen()
{//begin ClearScreen
#ifdef _WIN32
system("cls");//windows
#else
system("clear");//linux
#endif
}//end ClearScreen
void mainMenu()
{
ClearScreen();
cout<<"1) Enter New Record"<<endl;
cout<<"2) Change Record"<<endl;
cout<<"3) Display Record"<<endl;
cout<<"4) sort according to balance in descending order"<<endl;
cout<<"5) sort according to names in ascending order"<<endl;
cout<<"6) compare 2 names and assign address of larger one to smaller name"<<endl;
cout<<"7) Exit Program"<<endl;
}
Account newRecord()
{
ClearScreen();
Account newR;
cout<<"New Record"<<endl;
cout<<"Please enter the customer's name ";
getline(cin, newR.name);
while(newR.name.empty())
{
cout<< "Please re-enter the customer's name ";
getline(cin, newR.name);
}
cout<<"Please enter the customer's address ";
getline(cin,newR.address);
while (newR.address.empty())
{
cout<<"Please re-enter the customer's address ";
getline(cin, newR.address);
}
cout<<"Please enter city, State, and zip ";
getline(cin, newR.cityStateZip);
while(newR.cityStateZip.empty())
{
cout<<"Please re-enter the city, state, and zip ";
getline(cin, newR.cityStateZip);
}
cout<<"Please enter phone number ex. 9164578920 ";
getline(cin, newR.telephone_Number);
while(newR.telephone_Number.length() != 10)
{
cout<<"Please re-enter phone number ";
getline(cin, newR.telephone_Number);
}
cout<<"Please enter the account balance of the customer: $";
cin>>newR.account_Balance;
cin.ignore();
while (newR.account_Balance < 0)
{
cout<<"Account balance must be positive to open the account"<<endl;
cout<<"Please re-enter the account balance: $";
cin>>newR.account_Balance;
cin.ignore();
}
cout<<"Please enter the date of the last payment ex 01/01/2005 ";
getline(cin, newR.date_Last_Payment);
while (newR.date_Last_Payment.length() != 10)
{
cout<<"Please re-enter the date of last payment ex 01/01/2005 ";
getline(cin, newR.date_Last_Payment);
}
return newR;
}
Account changeRecord(Account chgR)
{
char response;
do {
ClearScreen();
cout<<"Change Record"<<endl;
cout<<"a) Name"<<endl;
cout<<"b) Address"<<endl;
cout<<"c) City, State, and zip"<<endl;
cout<<"d) Telephone number"<<endl;
cout<<"e) Account balance"<<endl;
cout<<"f) Date of last payment"<<endl;
cout<<"g) Exit"<<endl;
cout<<"Your choice: ";
cin>>response;
cin.ignore();
if (response=='a')
{
cout<<"Please enter the customer's name ";
getline(cin, chgR.name);
while(chgR.name.empty())
{
cout<< "Please re-enter the customer's name ";
getline(cin, chgR.name);
}
}
if (response=='b')
{
cout<<"Please enter the customer's address ";
getline(cin,chgR.address);
while (chgR.address.empty())
{
cout<<"Please re-enter the customer's address ";
getline(cin, chgR.address);
}
}
if (response=='c')
{
cout<<"Please enter city, State, and zip ";
getline(cin, chgR.cityStateZip);
while(chgR.cityStateZip.empty())
{
cout<<"Please re-enter the city, state, and zip ";
getline(cin, chgR.cityStateZip);
}
}
if (response=='d')
{
cout<<"Please enter phone number ex. 9164578920 ";
getline(cin, chgR.telephone_Number);
while(chgR.telephone_Number.length() != 10)
{
cout<<"Please re-enter phone number ";
getline(cin, chgR.telephone_Number);
}
}
if (response=='e')
{
cout<<"Please enter the account balance of the customer: $";
cin>>chgR.account_Balance;
cin.ignore();
while (chgR.account_Balance < 0)
{
cout<<"Account balance must be positive to open the account"<<endl;
cout<<"Please re-enter the account balance: $";
cin>>chgR.account_Balance;
cin.ignore();
}
}
if (response=='f')
{
cout<<"Please enter the date of the last payment ex 01/01/2005 ";
getline(cin, chgR.date_Last_Payment);
while (chgR.date_Last_Payment.length() != 10)
{
cout<<"Please re-enter the date of last payment ex 01/01/2005 ";
getline(cin, chgR.date_Last_Payment);
}
}
} while (response != 'g');
return chgR;
}
void displayRecord(Account customer)
{
cout<<"Name: "<<customer.name<<endl;
cout<<"Address: "<<customer.address<<endl;
cout<<"City,State,Zip: "<<customer.cityStateZip<<endl;
cout<<"Phone number: "<<customer.telephone_Number<<endl;
cout<<"Account Balance: $"<<customer.account_Balance<<endl;
cout<<"Date of last payment: "<<customer.date_Last_Payment<<endl<<endl;
}
int main()
{
Account customer[40];
int index;
char response,ch;
size_t found;
ifstream readFile("ch11input.txt");
vector <string> Record;
string Str;
while (getline(readFile, Str)) Record.push_back(Str);
int j;
j= int(Record.size())/6;
for (int i=0; i<j; i++) {
customer[i].name=Record[i*6];
customer[i].address=Record[i*6+1];
customer[i].cityStateZip=Record[i*6+2];
customer[i].telephone_Number=Record[i*6+3];
customer[i].account_Balance=atof(Record[i*6+4].c_str());
customer[i].date_Last_Payment=Record[i*6+5];
}
int size=j;
do {
mainMenu();
cin>>response;
cin.ignore();
if (response=='1') {
customer[size]=newRecord();
size++;
}
else if (response=='2')
{
cout<<"Which record would you like to change? "<<endl;
cout<<"1) Search by Index"<<endl;
cout<<"2) Search by name"<<endl;
cout<<"Which method would you prefer?"<<endl;
cin>>ch;
cin.ignore();
if (ch=='1') {
cout<<"Enter index # ";
cin>>index;
while (index < 0 || index > size)
{
cout<<"Please re-enter index number";
cin>>index;
}
customer[index]=changeRecord(customer[index]);
}
if (ch=='2') {
cout<<"Enter customer name" <<endl;
cin>>Str;
int hit=0;
for (int i=0; i<size; i++) {
found=customer[i].name.find(Str);
if (found!=string::npos) {
index=i;
hit=1;
}
}
if(hit==0) cout<<"No matches found"<<endl;
else customer[index]=changeRecord(customer[index]);
}
}
else if (response =='3') { ClearScreen();
for (int i=0; i < size; i++) {
cout<<"Account "<<i<<endl;
displayRecord(customer[i]);
}
cout<<"Press any key to continue"<<endl;
fflush(stdin);
getchar();
}
else if (response=='4')
{
Ofstream out_data(“balances.txt”);
int a;
for(int i=0;i<size;i++)
{
for( int j=0;j<size;j++)
{
if (customer[i].account_balance < customer[j].account_balance)
{
a = customer[i].account_balance;
customer[i].account_balance = customer[j].account_balance;
customer[j].account_balance = a;
}
}
}
for( int i=0;i<size;i++)
out_data << customer[i].account_balance;
}
else if (response == ‘5’)
{
ofstream out_data1(“names.txt”);
char temp[15];
for( int i=0;i< size-1;i++)
{
for( int j= i+1; j< size;j++)
{
if(strcmp(customer[i].name , customer[j].name)<0)
{
strcpy(temp, customer[i].name);
strcpy(customer[i].name, customer[j].name);
strcpy(customer[j].name, temp);
}
}
}
for(int i=0;i<size;i++)
out_data1 << customer[i].name;
}
else if (response ==’6’)
{
int len;
for( int i=0;i< size-1;i++)
{
for( int j= i+1; j< size;j++)
{
if(strcmp(customer[i].name , customer[j].name)<0)
{
customer[i].address= customer[j].address;
len = strln(customer[i].address);
cout<<”the length of smaller name is %d”<<len;
}
} while (response != '7');
return 0;
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.