Credit Card Simulator

I am new to classes and am having trouble understanding this project. I don't understand how to send the input variables to the class and have them changed when need be and then sent back upon request. Also, I can't seem to understand the process of writing the log very well.

For the part where they would want to try and increase their credit limit, how can I use a random number to do so? Would I just have a random number generator generate only 2 numbers?

Here is what I have so far:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
#pragma once
#include <string>

using namespace std;

class Credit_Card
{
public:
	Credit_Card();
	Credit_Card(int a);
	double getCredit_Limit();
	double getBalance_Due();
	int getAcc_No();
	double Avail_Cred();
	int Credit_Inc();
	void Transaction1();
	int Cred_Inc();
	bool add_charge(double charge_amount, const std::string& description);
	

	~Credit_Card();

private:
	int Ano;
	bool vErr;
	string vE_Msg;
	double vC_Limit;
	double vBal_due;
	void write_status();
	void write_log(string m);
	string CCName, CCLName;
	double vBal_own = vC_Limit;
	double payment;
	double charge;



};



1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
#include "stdafx.h"
#include "Credit_Card.h"
#include <iostream>
#include <fstream>
#include <ctime>
#include <sstream>

using namespace std;



Credit_Card::Credit_Card()
{
	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();
	vC_Limit = 1000;
	vBal_due = 0;
	CCName = fName;
	CCLName = "CCL" + n.str() + ".txt";
	write_status();
	write_log("Account " + n.str() + " opened. ");

}
Credit_Card::Credit_Card(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 >> vC_Limit;
		fin >> vBal_due;
		fin.close();
		CCName = fName;
		CCLName = "CCL" + n.str() + ".txt";
		write_log("Account " + n.str() + " reopened.");

	}
	else
	{
		Ano = 0;
		vC_Limit = 0;
		vBal_due = 0;
		vErr = true;
		vE_Msg = "Account " + n.str() + " could not be opened.";
	}


}
int Credit_Card::getAcc_No()
{
	return Ano;
}
double Credit_Card::getCredit_Limit()
{
	return vC_Limit;
}
double Credit_Card::getBalance_Due()
{
	return vBal_due;
}
double Credit_Card::Avail_Cred()
{
	return vBal_own;
}
void Credit_Card::Transaction1()
{
	
		vBal_own = vBal_own - charge + payment;
		vBal_due = vBal_due + charge - payment;
	
}
/*int Credit_Card::Cred_Inc()
{
	//if statement
}*/

void Credit_Card::write_status()
{
	vErr = false;
	vE_Msg = "";
	ofstream fout;
	fout.open(CCName.c_str());
	if (fout.is_open())
	{
		fout << vC_Limit << endl;
		fout << vBal_due << endl;
		fout.close();
	}
	else
	{
		vErr = true;
		vE_Msg = "Unable to write status file.";
	}
}
void Credit_Card::write_log(string m)
{
	vErr = false;
	vE_Msg = "";
	time_t rawtime;
	time(&rawtime);

	ofstream fout;
	fout.open(CCLName.c_str(), ios_base::app);

	if (fout.is_open())
	{
		fout << m << " on " << ctime(&rawtime);
		fout.close();
	}
	else
	{
		vErr = true;
		vE_Msg = "Unable to write log entry " + m;
	}


}
// write log to send to main, use vector, similar to 6

Credit_Card::~Credit_Card()
{

}


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
#include "stdafx.h"
#include "Credit_Card.h"
#include <iostream>
#include <ctime>

using namespace std;
using namespace System;

int main()
{
	
	srand((unsigned)time(nullptr));

	Credit_Card *cc;
	char choice;
	int acct_no;
	int ans;
	double charge = 0;
	string charge_desc;
	double payment = 0;
	int increase;


	cout << "New or existing account? (N/E): ";
	cin >> choice;
	choice = toupper(choice);

	if (choice == 'N')
	{
		//new account creation
		cc = new Credit_Card();
		if (cc->getAcc_No() == 0)
		{
			cout << "Account couldn't be created.";
			return(1);
		}

		cout << "Credit Account " << cc->getAcc_No() << " was opened. " << endl;
	}
	else
	{
		cout << "Please enter Account Number: ";
		cin >> acct_no;
		cc = new Credit_Card(acct_no);
		if (cc->getAcc_No() == 0)
		{
			cout << "Account doesn't exist.";
			return(2);
		}
		cout << "Credit Account " << cc->getAcc_No() << " was re-opened." << endl;
	}

	do
	{
		cout << "Your Credit Limit is " << cc->getCredit_Limit() << endl;
		cout << "Account: " << cc->getAcc_No() << "\nOutstanding Balance: " << cc->getBalance_Due()
			<< "\nCredit Limit: " << cc->getCredit_Limit() << "\nAvailable Credit: " << cc->Avail_Cred() << endl;
		cout << "\nTransaction Options" << endl;
		cout << "0. Quit \n1. New Charge \n2. Payment \n3. Credit Increase Request \n4. Card History \nChoice: ";
		cin >> ans;
		if (cin.fail())
		{

			cin.clear();
			cin.ignore();
		}

		switch (ans)
		{
		case 0:
			cout << "Thanks for using the credit card simulator!" << endl;
			break;
		case 1:
			cout << "Charge Amount: ";
			cin >> charge;
			if ((charge + cc->getBalance_Due()) < cc->getCredit_Limit())
			{
				cout << "Charge Description: ";
				cin.clear();
				cin.ignore();
				getline(cin, charge_desc);
			}
			else
			{
				cout << "You can't charge that amount. You will exceed your credit limit." << endl;
			}
			break;
		case 2:
			cout << "Payment Amount: ";
			cin >> payment;
			break;
		case 3:
			cout << "Request Increase (increments of 100): ";
			cin >> increase;
			
			break;
		case 4:
			break;

		default:
			cout << "You did not enter a valid selection";
			break;
		}

	} while (ans != 0);


	system("pause");
	return 0;
}
Last edited on
I know I have a lot to do to finish this project, but I was just looking for some hints or examples or pushes in the right directions.
You may want to start by more clearly describing the assignment and asking specific questions.

You posted a whole lot of code but haven't really asked specific questions about that code.

What is the purpose of all that code in main()? Why are you complicating your code by using pointers?

How much of the code did you actually write and how much was provided by the instructor or someone else?
Ok, my questions are: how can I send the input variables from the user in main to the credit card class, have them changed appropriately, and sent back? Also, how can I write the rest of the transactions to the log? I know (think) I need a vector or array somewhere, but where? and how should i implement it?

I am supposed to only output anything from main, not from the credit card class. the class is just supposed to compute everything.

I wrote about half, the instructor set up part of it. He helped with most of the constructors and the writelog and write status methods.
What are the objectives of the assignment? Your code in main() doesn't really make much sense. The code in both the if and the else clause looks almost identical. I also seriously question the use of the pointers, especially since you seem to be very new to classes. Normally using pointers would be saved until you understand "normal" usage of classes.

In this assignment you are to create a program to simulate the operations of a credit card (as explained below); 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.

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:

1) 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”.
2) 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”
3) 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.
4) 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).


Our instructor said we 'had' to use pointers, not sure why. He actually wrote, or atleast instruced us on how, to write the pointers. It is an online course, so it is hard to get good explanations
Last edited on
I'm not asking for anyone to write this for me, I am simply asking for some knowledgeable guidance as to where to go from what I have now?
I would start by reducing some of the code duplication in main():

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
int main()
{
    // This should only be called once, so do it here.
    srand((unsigned)time(nullptr));

    Credit_Card *cc;
    char choice;
    int acct_no;
    int ans;
    double charge = 0;
    string charge_desc;
    double payment = 0;
    double increase;


    cout << "New or existing account? (N/E): ";
    cin >> choice;
    choice = toupper(choice);

    if(choice == 'N')
    {
        //new account creation
        cc = new Credit_Card();
        if(cc->getAcc_No() == 0)
        {
            cout << "Account couldn't be created.";
            return(1);
        }

        cout << "Credit Account " << cc->getAcc_No() << " was opened. " << endl;
    }
    else
    {
        cout << "Please enter Account Number: ";
        cin >> acct_no;
        cc = new Credit_Card(acct_no);
        if(cc->getAcc_No() == 0)
        {
            cout << "Account doesn't exist.";
            return(2);
        }
        cout << "Credit Account " << cc->getAcc_No() << " was re-opened." << endl;
    }

    do
    {
        cout << "Your Credit Limit is " << cc->getCredit_Limit() << endl;
        cout << "Account: " << cc->getAcc_No() << "\nOutstanding Balance: " << cc->getBalance_Due()
             << "\nCredit Limit: " << cc->getCredit_Limit() << "\nAvailable Credit: " << cc->Avail_Cred()<<endl;
        cout << "\nTransaction Options" << endl;
        cout << "0. Quit \n1. New Charge \n2. Payment \n3. Credit Increase Request \n4. Card History \nChoice: ";
        cin >> ans;
        if(cin.fail())
        {

            cin.clear();
            cin.ignore();
        }

        switch(ans)
        {
            case 0:
                cout << "Thanks for using the credit card simulator!" << endl;
                break;
            case 1:
                cout << "Charge Amount: ";
                cin >> charge;
                if((charge + cc->getBalance_Due()) < cc->getCredit_Limit())
                {
                    cout << "Charge Description: ";
                    cin.clear();
                    cin.ignore();
                    getline(cin, charge_desc);
                }
                else
                {
                    cout << "You can't charge that amount. You will exceed your credit limit." << endl;
                }
                break;
            case 2:
                cout << "Payment Amount: ";
                cin >> payment;
                break;
            case 3:
                cout << "Request Increase (increments of 100): ";
                cin >> increase;
// The class should make the decision whether or not to accept this request, so the if statement really should be in the class member function responsible for this feature. 
                //if statement to see if they get increase or not using random number generator

                break;
            case 4:
                break;

            default:
                cout << "You did not enter a valid selection";
                break;
        }

    } while(ans !=0);


    system("pause");
    return 0;
}


Notice that the do/while() and switch() logic only needs to be done once. Because once you successfully open or create a new account all the logic is the same so there is no sense of repeating all that code.

Then next step, IMO, would be to save any modified data so that when you re-run the program with an existing account the changes you made in the previous run would be retained.

Ok, that makes sense. I was being really redundant.

How do i 'save' the data? Also, in the first run of the program on the output of available credit, I am getting a number like -6.46534e etc. Why am I not atleast getting 1000 for initial credit because they should be the same the first time around in a new account?
How do i 'save' the data?

Since the assignment talks about transactions you will probably want to write the data every time you change something.

Also, in the first run of the program on the output of available credit, I am getting a number like -6.46534e etc. Why am I not atleast getting 1000 for initial credit because they should be the same the first time around in a new account?

Did you select a New account the first time? The first time I run the program I get this output:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
bin/Debug/c++homework
New or existing account? (N/E): N
Credit Account 91636 was opened. 
Account: 91636
Outstanding Balance: 0
Credit Limit: 1000
Available Credit: 1000

Transaction Options
0. Quit 
1. New Charge 
2. Payment 
3. Credit Increase Request 
4. Card History 
Choice: 0
Thanks for using the credit card simulator!
sh: pause: command not found

Of course my main() may be different than your's and without your code I'm only guessing.

By the way you seem to have a few variables in your class that don't really seem to be needed. At a quick glance these are the only variables that seem absolutely necessary.

1
2
3
4
5
        int account_number;
        double vC_Limit;
        double vBal_due;
        std::string CCName;   // Account file name.
        std::string CCLName; // Log file name. 

Would i then write the data after every cin in main, or would I do that in each method in the class?

I used my above code and yours and I still don't get the available credit to be 1000.

Do you mean in my class or class header?
Well this is the main() that I used to get the above output.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
#include <iostream>

using namespace std;

int main()
{
    // This should only be called once, so do it here.
    srand((unsigned)time(nullptr));

    Credit_Card *cc;
    char choice;
    int acct_no;
    int ans;
    double charge = 0;

    double payment = 0;
    double increase;


    cout << "New or existing account? (N/E): ";
    cin >> choice;
    choice = toupper(choice);

    if(choice == 'N')
    {
        //new account creation
        cc = new Credit_Card();
        if(cc->getAcc_No() == 0)
        {
            cout << "Account couldn't be created.";
            return(1);
        }

        cout << "Credit Account " << cc->getAcc_No() << " was opened. " << endl;
    }
    else
    {
        cout << "Please enter Account Number: ";
        cin >> acct_no;
        cc = new Credit_Card(acct_no);
        if(cc->getAcc_No() == 0)
        {
            cout << "Account doesn't exist.";
            return(2);
        }
        cout << "Credit Account " << cc->getAcc_No() << " was re-opened." << endl;
    }

    do
    {
        //cout << "Your Credit Limit is " << cc->getCredit_Limit() << endl;
        cout << "Account: " << cc->getAcc_No() << "\nOutstanding Balance: " << cc->getBalance_Due()
             << "\nCredit Limit: " << cc->getCredit_Limit() << "\nAvailable Credit: " << cc->Avail_Cred()<<endl;
        cout << "\nTransaction Options" << endl;
        cout << "0. Quit \n1. New Charge \n2. Payment \n3. Credit Increase Request \n4. Card History \nChoice: ";
        cin >> ans;
        if(cin.fail())
        {

            cin.clear();
            cin.ignore();
        }

        switch(ans)
        {
            case 0:
                cout << "Thanks for using the credit card simulator!" << endl;
                break;
            case 1:
            {
                string description;
                cout << "Charge Amount: ";
                cin >> charge;
                cout << "Charge Description: ";
                getline(cin >> ws, description);
                if(cc->add_charge(charge, description))
                {
                    cout << "New balance is: " << cc->getBalance_Due() << endl;
                }
                else
                {
                    double remaining = cc->getCredit_Limit() - cc->getBalance_Due();
                    cout << "Sorry charge rejected. Current remaining balance is: " << remaining << endl;
                }

                break;
            }
            case 2:
                cout << "Payment Amount: ";
                cin >> payment;
                break;
            case 3:
                cout << "Request Increase (increments of 100): ";
                cin >> increase;
                //if statement to see if they get increase or not using random number generator

                break;
            case 4:
                break;

            default:
                cout << "You did not enter a valid selection";
                break;
        }

    } while(ans !=0);


    system("pause");
    return 0;
}

This is the default constructor.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
Credit_Card::Credit_Card()
{
    string fName;

    ostringstream sin;

    account_number = (rand() % 100000) + 1;
    sin << account_number;
    fName = "CC" + sin.str() + ".txt";  // NOTICE that there are no spaces in the ".txt" part. Your's has a space before the '.' that may cause problems later.       

    ifstream fin(fName);

    vC_Limit = 1000;
    vBal_due = 0;
    CCName = fName;
    CCLName = "CCL" + sin.str() + ".txt";
    write_status("Account " + sin.str() + " opened. ");
}


And this is the class definition:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
#include <string>

class Credit_Card
{
    public:
        Credit_Card();
        Credit_Card(int account);
        double getCredit_Limit();
        double getBalance_Due();
        int getAcc_No();
        double Avail_Cred();
        int Credit_Inc(int amount);
        void Transaction1();
        // I added this to start the add charge item.
        bool add_charge(double charge_amount, const std::string& description);
        ~Credit_Card();

    private:
        int account_number;
        double vC_Limit;
        double vBal_due;
        std::string CCName;
        std::string CCLName;
        void write_status(const std::string& message);
        void write_log(const std::string& message);

};


The write_status() function should probably re-write your "account" file and then possibly call write_log() to log the changes.
How do I implement the add_charge method function into main?

what would i have to add to yours to get it to atleast compile? It doesn't for me. what will the spaces before the .txt do?

also, what does your line 76 mean/do in your main? I haven't seen that before
Last edited on
also, what does your line 76 mean/do in your main? I haven't seen that before

That line is calling the add_charge() member function and using the value returned in the if() statement.

How do I implement the add_charge method function into main?

See above.

what would i have to add to yours to get it to atleast compile?

Did you implement the add_charge() method?

It doesn't for me.

Post your code and the complete error messages, all of them. Then perhaps I or someone else can help you decipher those messages.

what will the spaces before the .txt

Spaces, especially unexpected spaces, can make it hard to see why your strings are not comparing the same. And sometimes spaces in file names can be confusing when looking at directory listings.
Topic archived. No new replies allowed.