Please HELP with Credit Card Simulator coding using C++ My coding is below the i
ID: 3859669 • Letter: P
Question
Please HELP with Credit Card Simulator coding using C++
My coding is below the instructions,
Below is what I would like the calculator to do:
The program should be constructed using classes. Account operations include opening the account with an established limit, posting charges and payments, requesting a limit increase, and displaying account history. Data is stored in text files by account number so that you may run the program multiple times with the same account.
A sample/session might be (items in bold are user inputs):
Welcome to the Credit Card simulator!
Existing account or new (E/N): N
Your account number will be: 999999999
Your Credit Limit: $1000.00
Account: 99999999
Outstanding Balance: 0.00
Credit Limit: 1000
Available Credit: 1000
Transaction Options:
Quit
New Charge
Payment
Credit Increase Request
Card History
Choice: 1
Charge Amount: 100.00
Charge Description: Gas
Account: 99999999
Outstanding Balance: 100.00
Credit Limit: 1000.00
Available Credit: 900.00
Transaction Options:
Quit
New Charge
Payment
Credit Increase Request
Card History
Choice: 2
Payment Amount: 25
Account: 99999999
Outstanding Balance: 75.00
Credit Limit: 1000
Available Credit: 925.00
Transaction Options:
Quit
New Charge
Payment
Credit Increase Request
Card History
Choice: 3
Requested Increase: 250
Sorry, but your credit increase cannot be granted at this time.
Account: 99999999
Outstanding Balance: 75.00
Credit Limit: 1000
Available Credit: 925.00
Transaction Options:
Quit
New Charge
Payment
Credit Increase Request
Card History
Choice: 4
Transaction Log History:
Account 99999999 created, 8/1/2008 10:00:00am
Charge: 100, Gas, 8/1/2008 10:05:00am
Payment: 25, 8/5/2008 4:00:00pm
Limit Increase: 250, declined, 8/6/2008 8:00:00
Account: 99999999
Outstanding Balance: 75.47
Credit Limit: 1000
Available Credit: 924.53
Transaction Options:
Quit
New Charge
Payment
Credit Increase Request
Card History
Choice: 0
Thanks for using the credit card simulator!
The CREDIT CARD class should handle the normal activities related to a credit card: a) creating the account, b) making charges and payments, and c) updating the credit balance and credit limits. In addition, the Credit Card class will keep account status and transaction histories in text files.
Develop the Credit Card class according to the following guidelines:
The class should either create a new account or ‘load’ an old account if a valid account number is given. This would involve 2 constructors: one that takes no account number (and generates a new account) or one that accepts an account number and reads in the current values for the account. In the case of a new account, the Credit Card number should be a randomly generated value while the starting credit limit will be $1000. In the case of an existing account, you must verify that the account actually exists (i.e., there is already a non-empty account status file). The account status (only) data should be stored in a text file named as follows: “CC” + Account Number + “.txt”.
Methods for processing transactions must be included. These increase and decrease the credit card balance along with the remaining credit. Make sure that the operator cannot exceed his/her credit limit with any given charge amount. Each transaction should be stored in a credit card log file, with name as follows: “CCL” + account number “+ “.txt”
Allow for a request for a credit increase; increases are granted in $100 increments. Use a random number generator to determine whether a credit increase request is granted or denied.
The display of the transaction log is to be done in the ‘view’ program (the program with main()) – there should not be any screen output done by the Credit Card Class. Each transaction log entry is time-stamped when posted to the file (as seen from the sample run).
A list of likely class properties for the Credit Card Class:
- 2 Constructors (with and without Account number);
- account number (returns current account number);
- credit limit (returns current limit);
- credit increase (returns code indicating success/failure of increase request);
- available credit (return value);
- balance (return value);
- transaction (processes transactions and returns error status codes);
- history log (returns an appropriate structure containing account history)
Other class members may be created as you see fit.
This is what I have so far by using VS C++:
CreditCard.h
#pragma once
#include
using namespace std;
class CreditCard
{
public:
CreditCard(void);
CreditCard(int a);
double getCreditLimit();
double getBalanceDue();
int getAcctNo();
~CreditCard(void);
private:
int Ano;
bool vErr;
string vEMsg;
double vCLimit, vBalDue;
void writestatus();
void writelog(string m);
string CCName, CCLName;
};
CreditCard.cpp
#include "stdafx.h"
#include "CreditCard.h"
#include
#include
#include
#include
using namespace std;
CreditCard::CreditCard(void)
{
string fName;
ifstream fin;
ostringstream n;
srand((unsigned)time(0));
Ano = (rand() % 100000) + 1;
n << Ano << flush;
fName = "CC" + n.str() + ".txt";
fin.open(fName.c_str());
while (fin.is_open())
{
fin.close();
Ano = (rand() % 100000) + 1;
n << Ano << flush;
fName = "CC" + n.str() + ".txt";
fin.open(fName.c_str());
}
fin.close();
vCLimit = 1000;
vBalDue = 0;
CCName = fName;
CCLName = "CCL" + n.str() + ".txt";
writestatus();
writelog("Account " + n.str() + " opened.");
}
CreditCard::CreditCard(int a)
{
string fName;
ifstream fin;
ostringstream n;
vErr = false;
n << a << flush;
fName = "CC" + n.str() + ".txt";
fin.open(fName.c_str());
if (fin.is_open())
{
Ano = a;
fin >> vCLimit;
fin >> vBalDue;
fin.close();
CCName = fName;
CCLName = "CCL" + n.str() + ".txt";
writelog("Account " + n.str() + " reopened.");
}
else
{
Ano = 0;
vCLimit = 0;
vBalDue = 0;
vErr = true;
vEMsg = "Account " + n.str() + " could not be opened.";
}
}
int CreditCard::getAcctNo()
{
return Ano;
}
double CreditCard::getCreditLimit()
{
return vCLimit;
}
double CreditCard::getBalanceDue()
{
return vBalDue;
}
void CreditCard::writestatus()
{
vErr = false;
vEMsg = "";
ofstream fout;
fout.open(CCName.c_str());
if (fout.is_open())
{
fout << vCLimit << endl;
fout << vBalDue << endl;
fout.close();
}
else
{
vErr = true;
vEMsg = "Unable to write status file.";
}
}
void CreditCard::writelog(string m)
{
vErr = false;
vEMsg = "";
time_t rawtime;
time(&rawtime);
ofstream fout;
fout.open(CCLName.c_str(), ios_base::app);
if (fout.is_open())
{
fout << m << " on " << ctime(&rawtime) << endl;
fout.close();
}
else
{
vErr = true;
vEMsg = "Unable to write log entry: " + m;
}
}
CreditCard::~CreditCard()
{
}
CreditCardSim.cpp
// CreditCardSim.cpp : main project file.
#include "stdafx.h"
#include "CreditCard.h"
#include
using namespace std;
using namespace System;
int main()
{
CreditCard *cc;
char choice;
int acctno;
cout << "New or Existing Account? (N/E): ";
cin >> choice;
if (choice == 'N')
{
//new account creation
cc = new CreditCard();
cout << "Credit account " << cc->getAcctNo() << " opened." << endl;
}
else
{
cout << "Enter account number: ";
cin.ignore(1000, ' ');
cin >> acctno;
cc = new CreditCard(acctno);
cout << "Credit account " << cc->getAcctNo() << " was re-opened." << endl;
cout << "Limit = " << cc->getCreditLimit() << endl;
cout << "Balance Due = " << cc->getBalanceDue() << endl;
}
system("Pause");
return 0;
}
Explanation / Answer
CreditCardSim.cpp
#include "stdafx.h"
#include "CreditCard.h"
#include <iostream>
#include <vector>
using namespace std;
using namespace System;
char choose(string);
int getValue(string);
void requestIncrease(bool);
void transact(bool);
string getDescription(string);
void printHistory(vector<string>);
int main()
{
CreditCard *cc;
char accntType;
cout << "Welcome to the credit card simulator" << endl;
accntType = choose("Will this be a new or existing account? (n/e): ");
if (accntType == 'e' || accntType == 'E') {
cc = new CreditCard(getValue("Enter account number: "));
}
if (cc == NULL || cc->getAccountNum() == 0) {
cout << "Opening new account..." << endl;
cc = new CreditCard();
}
cout << " Your account number: " << cc->getAccountNum() << endl;
cout << "Your credit limit: " << cc->getCreditLimit() << endl << endl;
int transType;
do {
cout << "Transaction Options:" << endl << "0. Quit" << endl << "1. New Charge" << endl << "2. Payment" << endl << "3. Credit Increase Request" << endl << "4. Card History" << endl << endl;
transType = getValue("Choice: ");
switch (transType) {
case 1:
transact(cc->processTransaction(getDescription("Charge Description: "), getValue("Charge Amount: ")));
cout << endl;
break;
case 2:
transact(cc->processTransaction(getValue("Payment Amount: ")));
cout << endl;
break;
case 3:
requestIncrease(cc->increaseCreditLimit(getValue("Requested Increase: ")));
cout << endl;
break;
case 4:
printHistory(cc->readLog());
cout << endl;
break;
}
cout << " Account Number: " << cc->getAccountNum() << endl << "Outstanding Balance: " << cc->getBalanceDue() << endl << "Your credit limit: " << cc->getCreditLimit() << endl << "Availale Credit: " << cc->getAvailCredit() << endl << endl;
} while (transType > 0 && transType < 5);
cout << "Thank you for using the credit card simulator!" << endl;
system("Pause");
return 0;
}
char choose(string message) {
char choice;
cout << message;
cin >> choice;
if (!cin.good()) {
cin.clear();
cin.ignore(1000, ' ');
choice = 'n';
}
return choice;
}
int getValue(string message) {
int value;
cout << message;
cin >> value;
if (!cin.good()) {
cin.clear();
cin.ignore(1000, ' ');
value = 0;
}
return value;
}
string getDescription(string message) {
cin.ignore(numeric_limits<streamsize>::max(), ' ');
string desc;
cout << message;
getline(cin, desc);
return desc;
}
void requestIncrease(bool granted) {
if (granted) {
cout << "The request was granted.";
}
else {
cout << "The request was declined. Please try again another time.";
}
}
void transact(bool approved) {
if (approved) {
cout << "Your transaction was approved.";
}
else {
cout << "The transaction was declined. Please check the amount and try again.";
}
}
void printHistory(vector<string> lines) {
for (unsigned int i = 0; i < lines.size(); i++) {
cout << " " << lines[i] << endl;
}
cout << endl;
}
CreditCard.cpp
#include "stdafx.h"
#include "CreditCard.h"
#include <iostream>
#include <fstream>
#include <ctime>
#include <sstream>
#include <cstring>
#include <vector>
using namespace std;
using namespace System;
CreditCard::CreditCard()
{
string filename;
ifstream fin;
ostringstream n;
srand((unsigned)time(0));
do {
if (fin.is_open()) {
fin.close();
}
accountNum = (rand() % 100000) + 1;
n << accountNum << flush;
filename = "CC" + n.str() + ".txt";
fin.open(filename.c_str());
} while (fin.is_open());
fin.close();
vCreditLimit = 1000;
vBalanceDue = 0;
availCredit = 1000;
CCName = filename;
CCLName = "CCL" + n.str() + ".txt";
writeStatus();
writeLog("Account " + n.str() + " opened.");
}
CreditCard::CreditCard(int anum)
{
string filename;
ifstream fin;
ostringstream n;
vError = false;
n << anum << flush;
filename = "CC" + n.str() + ".txt";
fin.open(filename.c_str());
if (fin.is_open()) {
accountNum = anum;
fin >> vCreditLimit;
fin >> vBalanceDue;
fin >> availCredit;
fin.close();
CCName = filename;
CCLName = "CCL" + n.str() + ".txt";
writeLog("Account " + n.str() + " re-opened.");
}
else {
accountNum = 0;
vCreditLimit = 0;
vBalanceDue = 0;
vError = true;
vErrorMsg = "Account " + n.str() + " could not be opened.";
}
}
double CreditCard::getCreditLimit()
{
return vCreditLimit;
}
double CreditCard::getBalanceDue()
{
return vBalanceDue;
}
double CreditCard::getAvailCredit()
{
return availCredit;
}
int CreditCard::getAccountNum()
{
return accountNum;
}
bool CreditCard::increaseCreditLimit(double amt)
{
srand((unsigned)time(0));
if (rand() % 2 == 1) {
vCreditLimit += ((int)(amt / 100) * 100);//get a whole number for the increase amount in case they enter a number such as 250
availCredit = vCreditLimit - vBalanceDue;
writeStatus();
writeLog("Limit Increase: " + to_string(amt) + ", approved.");
return true;
}
writeStatus();
writeLog("Limit Increase: " + to_string(amt) + ", declined.");
return false;
}
bool CreditCard::processTransaction(double amt)
{
if (vBalanceDue - amt < 0) {
writeStatus();
writeLog("Payment declined. Must be less than " + to_string(vBalanceDue) + ". " + to_string(amt) + " not accepted.");
return false;
}
availCredit += amt;
vBalanceDue = vCreditLimit - availCredit;
writeStatus();
writeLog("Payment: " + to_string(amt));
return true;
}
bool CreditCard::processTransaction(string desc, double amt)
{
if (amt + vBalanceDue > vCreditLimit) {
writeStatus();
writeLog("Charge: " + to_string(amt) + " declined. Must be less than " + to_string(availCredit) + ".");
return false;
}
availCredit -= amt;
vBalanceDue = vCreditLimit - availCredit;
writeStatus();
writeLog("Charge: " + to_string(amt) + ", " + desc);
return true;
}
void CreditCard::writeStatus()
{
vError = false;
vErrorMsg = "";
ofstream fout;
fout.open(CCName.c_str());
if (fout.is_open()) {
fout << vCreditLimit << endl;
fout << vBalanceDue << endl;
fout << availCredit << endl;
fout.close();
}
else {
vError = true;
vErrorMsg = "Unable to write status file.";
}
}
void CreditCard::writeLog(string message)
{
vError = false;
vErrorMsg = "";
time_t rawtime;
time(&rawtime);
ofstream fout;
fout.open(CCLName.c_str(),ios_base::app);
if (fout.is_open()) {
fout << message << " Occurred " << ctime(&rawtime);
fout.close();
}
else {
vError = true;
vErrorMsg = "Unable to write log entry: " + message;
}
}
vector<string> CreditCard::readLog()
{
ifstream fin;
vector<string> lines;
fin.open(CCLName.c_str());
if (fin.is_open()) {
string row;
while (getline(fin, row)) {
lines.push_back(row);
}
fin.close();
}
else {
vError = true;
vErrorMsg = "Unable to read log.";
}
return lines;
}
CreditCard::~CreditCard()
{
}
CreditCard.h
#pragma once
#include <string>
#include <vector>
using namespace std;
class CreditCard
{
public:
CreditCard(void);
CreditCard(int);
double getCreditLimit();
double getBalanceDue();
double getAvailCredit();
int getAccountNum();
bool increaseCreditLimit(double);
bool processTransaction(double);
bool processTransaction(string,double);
vector<string> readLog();
~CreditCard(void);
private:
int accountNum;
bool vError;
double vBalanceDue;
double vCreditLimit;
double availCredit;
string vErrorMsg;
string CCName;
string CCLName;
void writeStatus();
void writeLog(string);
};