Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Learning Objectives for User Defined Data in C++ (Please code in C++ Thankyou) 1

ID: 3586574 • Letter: L

Question

Learning Objectives for User Defined Data in C++ (Please code in C++ Thankyou)

1. Class design and implementation

2. Encapsulation

3. Working with projects: Makefile and linking

4. Write to file and encrypt content

5. Read from file and decrypt content

6. Redesign an existing project to be OOP

7. Pay attention to code reuse concepts and how to maintain code.

8. Code documentation

Lab Requirement:

You will design a class that will represent the client for a banking project. For this lab, you will not complete the whole banking project!!!!

1. Create a class Client

2. Decide about

a. Encapsulate (private data members)

b. features (interface functions)

3. Write a test application to verify the operability of an object of type Client.

Design Requirements:

1. Be able to save the content (name, account/id and balances to a file)

2. Encrypt the content of the file

3. Decrypt the content of the file back to the running application

4. Add more clients in the file

5. Update balances for any client.

Explanation / Answer

#include <iostream>
#include <string>
#include <fstream>
#include <cstdlib>
#include <cctype>
using namespace std;

//Class Client definition
class Client
{
//Data members
string name;
string accountId;
double balances;
int numberOfRecords;
public:
//Member function prototype
void readFile(Client[]);
void writeFile(Client[]);
void encryptFile(Client[]);
void decryptFile(Client[]);
void displayClient(Client[]);
string getName();
void setName(string);
string getAccountId();
void setAccountId(string);
double getBalance();
void setBalance(double);
};//End of class

//Function to encrypt file contents
void Client::encryptFile(Client c[])
{
int t;
//Read file contents and store it in class data member
c[0].readFile(c);
//Loops till end of records
for(int x = 0; x < numberOfRecords; x++)
{
//Encrypt each character of name
for(t = 0; t < c[x].name.length(); t++)
c[x].name[t] = (c[x].name[t] + '0');
//Encrypt each character of id
for(t = 0; t < c[x].accountId.length(); t++)
c[x].accountId[t] = (c[x].accountId[t] + '0');
//Encrypt account balance
c[x].balances = c[x].balances + 'a';
}//End of for loop
c[0].writeFile(c);
cout<<" ************* Encrypted File Contents **************** ";
//Display the contents
c[0].displayClient(c);
}//End of function

//Decrypt the file contents
void Client::decryptFile(Client c[])
{
int t;
//Read file contents and store it in class data member
c[0].readFile(c);
//Loops till end of records
for(int x = 0; x < numberOfRecords; x++)
{
//Decrypt each character of name
for(t = 0; t < c[x].name.length(); t++)
c[x].name[t] = c[x].name[t] - '0';
//Decrypt each character of id
for(t = 0; t < c[x].accountId.length(); t++)
c[x].accountId[t] = c[x].accountId[t] - '0';
//Decrypt the balance
c[x].balances = c[x].balances - 'a';
}//End of for loop
cout<<" ************* Decrypted File Contents **************** ";
//Display the contents
c[0].displayClient(c);
}//End of function

//Function to return name
string Client::getName()
{
return name;
}//End of function
//Function to set name
void Client::setName(string n)
{
name = n;
}//End of function
//Function to return id
string Client::getAccountId()
{
return accountId;
}//End of function
//Function to sat id
void Client::setAccountId(string a)
{
accountId = a;
}//End of function
//Function to return balance
double Client::getBalance()
{
return balances;
}//End of function
//Function to set balance
void Client::setBalance(double b)
{
balances = b;
}//End of function

//Function to display client information
void Client::displayClient(Client c[])
{
//Loops till number of records
for(int x = 0; x < numberOfRecords; x++)
{
cout<<" Name: "<<c[x].name<<" Account Id: "<<c[x].accountId<<" Balance: "<<c[x].balances;
}//End of for loop
}//End of function

//Function to write client information onto the file
void Client::writeFile(Client c[])
{
//ofstream object created to write
ofstream wFile;
//Open the file for writing
wFile.open("client.txt");
//Loops till end of the record
for(int l = 0; l < numberOfRecords; l++)
{
//Write each data member to the file
wFile<<c[l].getName()<<endl;
wFile<<c[l].getAccountId()<<endl;
wFile<<c[l].getBalance()<<endl;
}//End of function
//Close file
wFile.close();
}//End of function

//Function to read file contents and store it in the data members
void Client::readFile(Client c[])
{
//ifstream object created to read
ifstream rFile;
//Open the file for reading
rFile.open("client.txt");
int x = 0;
string data;
double bal;
//Loops till end of the file
while(!rFile.eof())
{
//Reads the data and stores it in the data members
rFile>>data;
c[x].setName(data);
rFile>>data;
c[x].setAccountId(data);
rFile>>bal;
c[x].setBalance(bal);
//Increase the record counter
x++;
}//End of while loop
//Close file
rFile.close();
numberOfRecords = x;
}//End of function

//Main function definition
int main()
{
//Creates an array of objects
Client client[100];

client[0].encryptFile(client);
client[0].decryptFile(client);
return 0;
}//End of function

Sample Run:


************* Encrypted File Contents ****************

Name: ÇæóÖ Account Id: a`a Balance: 10097
Name: }ƒÿæ Account Id: a`b Balance: 15097
Name: 鿥 Account Id: a`c Balance: 22097
Name: âÑóòúÿ Account Id: a`i Balance: 10297
Name: éæÜòúÿ Account Id: aab Balance: 15597
Name: âæúÖ Account Id: ab` Balance: 29097
************* Decrypted File Contents ****************

Name: Pyari Account Id: 101 Balance: 10000
Name: Mohan Account Id: 102 Balance: 15000
Name: Ram Account Id: 103 Balance: 22000
Name: Suresh Account Id: 109 Balance: 10200
Name: Rajesh Account Id: 112 Balance: 15500
Name: Sasi Account Id: 120 Balance: 29000
Name: 120 Account Id: 120 Balance: 29000

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote