Bank Account System in C++ using Classes and Data Encapsulation
Hi everyone,
I have run into problems with this program involving a customer bank account management system on C++ using a class.
Here is a description of what the program should do:
In a header file, I need to hold the definition of a class. This class will store information about a customer at a bank. The class must contain data members for Customer Name, Street Address, City, Postal Code and account balance. I need to write a Constructor function that will initialize each of the data members. I need to add methods to: define the data for the account, allow money to be deposited into the account, allow money to be withdrawn from the account and show all data stored for the account (name, address, balance). Validate the input from the user. If bad data is entered, prompt again. The postal code must be in the form L#L#L#. I need some assistance on checking for invalid postal codes as well.
Then, in a separate .cpp file I will have the main routine that will test the class in the header file. The program should be able to handle up to 10 customers which is why I've made an array of 10 instances as you'll see below.
First problem was that I was receiving bad information on the first run through of displaying the user-inputted data. Second problem I had was that the program would crash after entering information for one of the five private data members.
Here is my code up to this point. I've put the whole thing together even though the main routine is supposed to be in a .cpp file and the class definition in a .h header file. I commented out a main menu which will eventually allow the user to choose an option and then go through only that option and then return to the main menu once complete but first I want to make sure all the class methods work OK and currently they are not.
Here is my code so far:
Code:
#include <iostream>
#include <conio.h>
#include <stdlib.h>
class Customer {
private: // data members (properties)
char *name;
char *address;
char *city;
char *pcode;
float acc_bal;
public:
Customer:: Customer() { // constructor
name = NULL;
address = NULL;
city = NULL;
pcode = NULL;
acc_bal = 0.00;
}
void addcust() { // add customer method
cout << "\nEnter your name: "; cin >> name;
cout << "\nEnter your address: "; cin >> address;
cout << "\nEnter your city: "; cin >> city;
cout << "\nEnter your postal code: "; cin >> pcode;
cout << "\nEnter current account balance: "; cin >>
acc_bal;
}
void deposit(float bal) { // deposit money method
float dep;
cout << "\nEnter amount to be deposited: "; cin >> dep;
bal = bal + dep;
acc_bal = bal;
}
void withdraw(float bal) { // withdraw money method
float wdraw;
cout << "\nEnter amount to be withdrawn: "; cin >>
wdraw;
bal = bal - wdraw;
acc_bal = bal;
}
void showdata() { // show data method
cout << "Name: " << name;
cout << "\nAddress: " << address;
cout << "\nCity: " << city;
cout << "\nPostal Code: " << pcode;
cout << "\nAccount Balance: $" << acc_bal << endl;
}
};
int main() {
/*
cout << "\t\t\n\n" << "Main Menu";
cout << "\t\n\n" << "Select by letter:";
cout << "\t\n" << "a - Add a customer.";
cout << "\t\n" << "d - Deposit money.";
cout << "\t\n" << "w - Withdraw money.";
cout << "\t\n" << "s - Show Account Information.";
cout << "\t\n" << "q - Quit Application.\n\n";
cout << "\t" << "Choice: ";
*/
Customer cust[10]; // array of 10 instances of customer
int i;
for (i = 0; i < 10; i++) {
cust[i].getdata();
cust[i].showdata();
}
}
Basically, I'm trying to find out why the program is crashing on me and how I can change the methods to reflect the correct data because I believe there is something incorrect about my logic here.
Any help or assistance would be greatly appreciated. Thanks.
Re: Postal Code Validation
Well my bank program using classes is working a lot better now.
I need some help though on how to validate a Canadian postal code entry to ensure that it's in 'letter-number-letter-number-letter-number' format in C++. I think I know that I need to check the postal code char by char but I'm not sure of how to actually code it.
Here is my current program:
Code:
#include <iostream>
#include <conio.h>
#include <stdlib.h>
class Customer {
private:
char name[20];
char address[30];
char city[20];
char pcode[6];
float acc_bal;
public:
Customer:: Customer() { // constructor
acc_bal = 0.00;
}
void getdata() {
cout << "\nEnter your name: "; cin >> name;
cout << "\nEnter your address: "; cin >> address;
cout << "\nEnter your city: "; cin >> city;
cout << "\nEnter your postal code: "; cin >> pcode;
cout << "\nEnter current account balance: "; cin >> acc_bal;
}
void deposit() {
float dep;
cout << "\nEnter amount to be deposited: ";
cin >> dep;
acc_bal += dep;
}
void withdraw() {
float wdraw;
cout << "\nEnter amount to be withdrawn: "; cin >> wdraw;
acc_bal -= wdraw;
}
void showdata() {
cout << "Name: " << name;
cout << "\nAddress: " << address;
cout << "\nCity: " << city;
cout << "\nPostal Code: " << pcode;
cout << "\nAccount Balance: $" << acc_bal << endl;
}
};
int main() {
char choice;
int flag = 0;
int count = 0;
int recnum;
Customer cust[10];
while (flag == 0) {
cout << "\t\t\n\n" << "Main Menu";
cout << "\t\n\n" << "Select by letter:";
cout << "\t\n" << "a - Add a customer.";
cout << "\t\n" << "d - Deposit money.";
cout << "\t\n" << "w - Withdraw money.";
cout << "\t\n" << "s - Show Account Information.";
cout << "\t\n" << "q - Quit Application.\n\n";
cout << "\t" << "Choice: ";
choice = getche();
switch(choice) {
case 'a':
system("cls");
if (count > 10) {
cout << "Can't add anymore records. Press any key to return to main menu.";
getche();
break;
}
count += 1;
cust[count].getdata();
system("cls");
break;
case 'd':
system("cls");
cout << "\nEnter customer number: ";
cin >> recnum;
cust[recnum].deposit();
system("cls");
break;
case 'w':
system("cls");
cout << "\nEnter customer number: ";
cin >> recnum;
cust[recnum].withdraw();
system("cls");
break;
case 's':
system("cls");
cout << "\nEnter customer number: ";
cin >> recnum;
cust[recnum].showdata();
getche();
system("cls");
break;
case 'q':
flag = 1;
break;
default:
cout << "\nInvalid selection. Press a key to return to main menu.";
getche();
}
if (flag == 1) {
break;
}
}
return 0;
}
So if anyone has any advice on how to validate a postal code then please post a message on this thread. It would be much appreciated.
Thanks.