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?
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
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?
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 forusing 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.
#include <iostream>
usingnamespace 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. ");
}
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.