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 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291
|
#include "stdafx_h"
#define interes 6
Account* a[100],b;
int quantity=0,n;
Account::Account(){
name[0]=NULL;
userID[0]=NULL;
password[0]=NULL;
balance=0;
type=0;
}
Account::Account(char Aname[], char AuserID[], char Apassword[], double Abalance, int Atype){
strcpy(name,Aname);
strcpy(userID,AuserID);
strcpy(password,Apassword);
this->balance=Abalance;
this->type=Atype;
}
void Account::loadDataFromFile(const char *filename){
char Aname[50],AuserID[50],Apassword[50];
double Abalance;
int Atype;
ifstream is;
is.open(filename);
if(is.fail()){
cout << "File not found!" << endl;
return;
}
else
while(1){
is.getline(AuserID,50,'|');
is.getline(Aname,50,'|');
is.getline(Apassword,50,'|');
is >> Abalance;
is >> Atype;
is.ignore();
a[quantity++]=new Account(Aname,AuserID,Apassword,Abalance,Atype);
n=quantity;
if(is.gcount()==0) break;
}
}
void Account::saveDataToFile(const char *filename){
ofstream os(filename);
for(int i=0;i<n;i++){
os<<a[i]->name << "|";
fflush(stdin);
os<<a[i]->userID << "|";
fflush(stdin);
os<<a[i]->password << "|";
fflush(stdin);
if (i==n-1){
os<<a[i]->balance <<" ";
fflush(stdin);
}
else{
os<<a[i]->balance << " ";
fflush(stdin);
}
os << a[i]->type;
fflush(stdin);
os << endl;
}
os.close();
}
int Account::createAnAccount(){
char AuserID[50],Aname[50],Apassword[50];
double Abalance;
int c,Atype;
/*Enter account name*/
do{
cout << "Enter your name : ";
cin.getline(Aname,sizeof(Aname));
if (strlen(Aname)<=50 && strlen(Aname)>=2) break;
cout << "Invalid name format. Try again !"<< endl;
fflush(stdin);
}while(true);
/*Enter account ID*/
do{
cout << "Enter your user ID : ";
cin.getline (AuserID,sizeof(AuserID));
if (strlen(AuserID)<=50 && strlen(AuserID)>=2) break;
cout << "Invalid user ID format. Try again !"<<endl;
fflush(stdin);
}while(true);
/*Check userID exist*/
int k=0;
for(int i=0;i<n;i++)
if(strcmp(a[i]->userID,AuserID)==0)
k++;
if(k!=0){
cout << "\nAccount ID exist! Please try again with other user ID!\n" << endl;
return 0;
}
/*Enter account password*/
do{
cout << "Enter your password : ";
cin.getline(Apassword,sizeof(Apassword));
if (strlen(Apassword)<=50 && strlen(Apassword)>=6) break;
cout << "Invalid password format. Password must have unless 6 character. Try again !"<< endl;
fflush(stdin);
}while(true);
/*Enter account balance*/
do{
cout << "Enter your balance : ";
cin >> Abalance;
if(Abalance > 0) break;
cout << "Balance must be positive. Try again !" << endl;
cin.clear();
fflush(stdin);
}while(true);
/*Enter account type*/
cout << "Choose your account type" <<endl;
cout << "1. Savings Account - 2. Checking Account : ";
cin >> c;
if(c==1)
Atype=1;
else
Atype=2;
a[quantity++]=new Account(Aname,AuserID,Apassword,Abalance,Atype);
n=quantity;
cout << "Successful to create your account." << endl;
saveDataToFile("accounts.txt");
}
void Account::setBalance(double ba){
balance=ba;
}
void main(){
static char filename[50];
char acc[50],pass[50];
SavingAccount sa;
CheckingAccount ca;
int choice,i,flag,chon;
double bi,si;
b.loadDataFromFile("accounts.txt");
while(1){
system("cls");
cout <<"| BANK ACCOUNT MANAGEMENT |"<<endl;
cout <<"|-----------------------------|"<<endl;
cout <<"| 1. Create Account |"<<endl;
cout <<"| 2. Login |"<<endl;
cout <<"| 0. Exit Program |"<<endl;
cout <<"|-----------------------------|"<<endl;
cout <<"| Your seclection <0-->2> : ";
cin >> choice;
cin.sync();
cin.clear();
if(choice==0) break;
switch (choice){
case 1:
system("cls");
cout<<"] Account Register ["<<endl;
cout<<"|================================|"<<endl;
b.createAnAccount();
break;
case 2:
system("cls");
cout<<"] LOGIN ["<<endl;
cout<<"|================================|"<<endl;
cout << "Enter User ID : ";
fflush(stdin);
cin.getline(acc,50);
cout << "Enter Password : ";
fflush(stdin);
cin.getline(pass,50);
for(i=0;i<quantity;i++){
flag=0;
if(strcmp(acc,a[i]->getUserID())==0 && strcmp(pass,a[i]->getPassword())==0){
cout << "Welcome to Bank Account Management !" << endl;
flag=1;
/*Saving Account*/
if(a[i]->getType()==1){
int choose;
double Ainterest,ba;
do{
system("cls");
cout << "|Saving Account Management|" << endl;
cout << "|=========================|" << endl;
cout << "| 1.View Balance |" << endl;
cout << "| 2.View Interest Amount |" << endl;
cout << "| 3.Credit |" << endl;
cout << "| 4.Debit |" << endl;
cout << "| 0.Exit to main menu |" << endl;
cout << "|Your selection (0->2): ";
cin >> choose;
cin.sync();
cin.clear();
if(choose==0) break;
switch (choose){
case 1:{
ba=a[i]->getBalance();
Ainterest=sa.getInterest();
Ainterest=(ba*interes)/100;
cout << "Your current balance is " << ba << endl;
system("pause");
break;
}
case 2:{
ba=a[i]->getBalance();
Ainterest=sa.getInterest();
Ainterest=(ba*interes)/100;
cout << "Your current interest Amount is " << Ainterest << endl;
system("pause");
break;
}
case 3:{
si=sa.getCredit(a[i]->getBalance());
a[i]->setBalance(si);
system("pause");
b.saveDataToFile("accounts.txt");
break;
}
case 4:{
si=sa.getDebit(a[i]->getBalance());
a[i]->setBalance(si);
system("pause");
b.saveDataToFile("accounts.txt");
break;
}
}
}while(1);
}
/*Cheking Account*/
if(a[i]->getType()==2){
do{
system("cls");
cout <<"| Cheking Account Management |" << endl;
cout <<"|============================|" << endl;
cout <<"| 1. View Balance |" << endl;
cout <<"| 2. Credit |" << endl;
cout <<"| 3. Debit |" << endl;
cout <<"| 0. Exit |" << endl;
cout <<"|Your selection (0->3) : ";
cin >> chon;
cin.sync();
cin.clear();
if(chon==0) break;
switch(chon){
case 1:{
cout << "Your current balance is " << a[i]->getBalance() << endl;
system("pause");
break;
}
case 2:{
bi=ca.getCredit(a[i]->getBalance());
a[i]->setBalance(bi);
system("pause");
b.saveDataToFile("accounts.txt");
break;
}
case 3:{
bi=ca.getDebit(a[i]->getBalance());
a[i]->setBalance(bi);
system("pause");
b.saveDataToFile("accounts.txt");
break;
}
}
}while(1);
}break;
}
}
if(flag==0)
cout << "Sorry. Your account doesn't exist." << endl;
break;
default: cout << "Your selection must (0->2). Try again ! ";
}
system("pause");
}
}
|