In C++ Beginning of a shopping program. Ask the user for an item and price, and
ID: 3908866 • Letter: I
Question
In C++
Beginning of a shopping program. Ask the user for an item and price, and continue until the user says "stop" for the item name.
After stop, ask the user for payment information. This will work like a credit card, information should include - Name, Credit Card number, Exp date, and CVV (the 3 digits on the back of card). The credit card information needs to be encrypted and saved to a binary file. All objects should be deleted from memory. Then load the file, decrypt the data and display the information back onto the screen. Include the total cost at the end of display.
Create a class for Item, Credit Card and Record. Record will contain a vector of Items and one CreditCard.
Keep class variables private. Create the instance of Record on the heap and delete it before the program ends.
Make sure the credit card information is completely indistinguishable in the binary file.
Any comments in the code is greatly appreciated. Thank you.
Explanation / Answer
here is your program : ------------->>>>>>>>>>>
#include<iostream>
#include<fstream>
#include<cstring>
#include<vector>
using namespace std;
class CreditCard{
char name[100];
char number[100];
long expDate;
char cvv[4];
void encrypt(char *array, int array_size){
int i;
char secret[13] = { 22, 53, 44, 71, 66, 177, 253, 122,234,9,28,67,78};
for(i = 0; i < array_size; i++)
array[i] ^= secret[i%13];
}
public:
CreditCard(){
name[0] = '';
number[0] = '';
expDate = 0000;
cvv[0] = '';
}
~CreditCard(){
}
void setName(string n){
strcpy(name,n.c_str());
}
void setNumber(string num){
strcpy(number,num.c_str());
}
void setExpDate(int d){
expDate = d;
}
void setCVV(string cvv){
strcpy(this->cvv,cvv.c_str());
}
string getName(){
return string(name);
}
string getNumber(){
return string(number);
}
int getExpDate(){
return expDate;
}
string getCVV(){
return string(cvv);
}
void write(ofstream &ou){
char *msg = (char *)this;
encrypt(msg,sizeof(CreditCard));
ou.write(msg,sizeof(CreditCard));
}
void read(ifstream &ifs){
ifs.read((char *)this,sizeof(CreditCard));
encrypt((char *)this,sizeof(CreditCard));
}
};
class Item{
string name;
double price;
public:
Item(){
name = "";
price = 0.0;
}
void setName(string n){
name = n;
}
void setPrice(double p){
price = p;
}
double getPrice(){
return price;
}
string getName(){
return name;
}
};
class Record{
vector<Item> items;
CreditCard *card;
void getCreditInfo(){
card = new CreditCard;
string data;
int date;
cout<<" Enter Name On Card : ";
cin>>data;
card->setName(data);
cout<<" Card Number : ";
cin>>data;
card->setNumber(data);
cout<<" Expiry Date : ";
cin>>date;
card->setExpDate(date);
cout<<" CVV : ";
cin>>data;
card->setCVV(data);
ofstream ofs;
ofs.open("credit_info.dba");
card->write(ofs);
ofs.close();
delete card;
card = NULL;
}
public:
Record(){
card = NULL;
}
void getInfo(){
string check = "yes";
check.reserve(100);
Item temp;
double pr = 0;
while(true){
cout<<" Enter Item Name : (Enter stop to quit) ";
cin>>check;
if(check == "stop"){
break;
}
cout<<" Enter Item Price : ";
cin>>pr;
temp.setName(check);
temp.setPrice(pr);
items.push_back(temp);
}
getCreditInfo();
}
void displayInfo(){
double total = 0.0;
for(int i = 0;i<items.size();i++){
total += items[i].getPrice();
}
card = new CreditCard;
ifstream ifs;
ifs.open("credit_info.dba");
card->read(ifs);
ifs.close();
cout<<" Name : "<<card->getName();
cout<<" Number : "<<card->getNumber();
cout<<" Exp Date : "<<card->getExpDate();
cout<<" CVV : "<<card->getCVV();
cout<<" Total Cost : Rs. "<<total;
}
};
int main(){
Record *r = new Record;
r->getInfo();
r->displayInfo();
delete r;
return 0;
}