bank.cpp (4280B)
1 #include <stdio.h> 2 #include <stdlib.h> 3 #include <iostream> 4 using namespace std; 5 6 class BBVA { 7 char name[20]; 8 int accountNumber; 9 char accountType[10]; 10 float balance; 11 public: 12 void assign(int); 13 void deposit(float); 14 void withdraw(float); 15 void display(int); 16 }; 17 18 void BBVA::assign(int x) { 19 cout << "\nEnter the name, account type and balance for account number " << x << " :\t"; 20 cin >> name >> accountType >> balance; 21 accountNumber = x; 22 fflush(stdin); 23 } 24 25 void BBVA::deposit(float x) { 26 if(x < 0) { 27 cout << "Invalid deposit sum.\n"; 28 return; 29 } 30 balance+=x; 31 cout << "\nSuccessfully deposited " << x << ". New balance is: " << balance << endl; 32 } 33 34 void BBVA::withdraw(float x) { 35 if(x < 0 || x > balance) { 36 cout << "\nInsufficient balance.\n"; 37 return; 38 } 39 balance-=x; 40 cout << "\nSuccessfully withdrawn " << x << " from the account. Current balance is: " << balance << endl; 41 } 42 43 void BBVA::display(int x) { 44 cout << "\nAccount number: " << accountNumber << endl << "Name: " << name << endl << "Account: " << accountType << endl << "Balance: " << balance << endl; 45 } 46 47 int main() { 48 int max, maxNew, input = 7, index; 49 float sum; 50 do { 51 cout << "\nEnter the number of accounts\nYour input> "; 52 cin >> max; 53 // cout << endl; 54 if(max < 1) 55 cout << "\nInvalid input, try again.\n\n"; 56 } while(max < 1); 57 // BBVA *a = new BBVA[max]; 58 BBVA *a = (BBVA *) malloc(max * sizeof(BBVA)); 59 if(a == NULL) { 60 cout << "\nInsufficient memory.\n"; 61 return 0; 62 } 63 BBVA *b = a; 64 do{ 65 sum = 0; 66 cout << "\nEnter number corrosponding to their label\n\n1. Enter the details\t2. Deposit money\n3. Withdraw money\t4. Display details\n5. Add more accounts\t6. Exit\n\nYour input> "; 67 cin >> input; 68 fflush(stdin); 69 switch (input) { 70 case 1: 71 cout << "\nEnter the account number. (Between 0 and " << max - 1 << ")\nYour input> "; 72 cin >> index; 73 if(index < 0 || index >= max) { 74 cout << "\nAccount not found. Can not store the information.\n"; 75 break; 76 } 77 a[index].assign(index); 78 break; 79 case 2: 80 cout << "\nEnter the account number. (Between 0 and " << max - 1 << ")\nYour input> "; 81 cin >> index; 82 if(index < 0 || index >= max) { 83 cout << "\nAccount not found. Can not deposit money.\n"; 84 break; 85 } 86 cout << "\nEnter amount to be deposited.\nYour input> "; 87 cin >> sum; 88 a[index].deposit(sum); 89 break; 90 case 3: 91 cout << "\nEnter the account number. (Between 0 and " << max - 1 << ")\nYour input> "; 92 cin >> index; 93 if(index < 0 || index >= max) { 94 cout << "\nAccount not found. Can not withdraw money.\n"; 95 break; 96 } 97 cout << "\nEnter amount to be withdrawn.\nYour input> "; 98 cin >> sum; 99 a[index].withdraw(sum); 100 break; 101 case 4: 102 cout << "\nEnter the account number. (Between 0 and " << max - 1 << ")\nYour input> "; 103 cin >> index; 104 if(index < 0 || index >= max){ 105 cout << "\nAccount not found. Can not display details.\n"; 106 break; 107 } 108 a[index].display(index); 109 break; 110 case 5: 111 cout << "\nEnter new number of bank accounts\nYour input> "; 112 cin >> maxNew; 113 if(maxNew > max) { 114 a = (BBVA *) realloc(a, maxNew * sizeof(BBVA)); 115 if(a == NULL) { 116 cout << "Insufficient memory.\n"; 117 a = b; 118 } 119 max = maxNew; 120 cout << "\nNumber of accounts has been updated to " << max << ".\n"; 121 } 122 else cout << "\nInvalid input. New size should be greater than " << max << ".\n"; 123 break; 124 case 6: 125 cout << "\nProgramme has ended.\n"; 126 break; 127 default: 128 cout << "\nInvalid choice. Try again.\n\n"; 129 break; 130 } 131 } while(input != 6); 132 return 0; 133 }