-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathBank System(2).cpp
166 lines (152 loc) · 3.41 KB
/
Bank System(2).cpp
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
#include<iostream>
#include<cstring>
using namespace std;
const int NAME_LEN = 20;
class Account
{
private:
char* customerName;
int accountNumber;
int balance;
public:
Account(char* name, int accNum, int bal);
~Account();
int getAccountNumber() const;
int deposit(int money);
int withdraw(int money);
void ViewAccInfo() const;
};
enum { NEW_ACCOUNT = 1, DEPOSIT, WITHDRAW, VIEW_ACCOUNT, EXIT };
Account::Account(char* name, int accNum, int bal) : accountNumber(accNum), balance(bal)
{
customerName = new char[strlen(name) + 1];
for (int i = 0; i < strlen(name); i++)
customerName[i] = name[i];
customerName[strlen(name)] = '\0';
}
Account::~Account()
{
delete[]customerName;
}
int Account::getAccountNumber() const
{
return accountNumber;
}
int Account::deposit(int money)
{
balance += money;
return balance;
}
int Account::withdraw(int money)
{
if (balance > money)
balance -= money;
else
return 0;
return balance;
}
void Account::ViewAccInfo() const
{
cout << "Account number: " << accountNumber << endl;
cout << "Name: " << customerName << endl;
cout << "Balance: " << balance << endl;
}
Account* accountArray[100];
int numOfAccounts = 0;
void displayMenu()
{
cout << "----- Menu -----" << endl;
cout << "1. Open a new bank account" << endl;
cout << "2. Deposit" << endl;
cout << "3. Withdraw" << endl;
cout << "4. View account info" << endl;
cout << "5. Exit the program" << endl;
cout << endl;
}
void newBankAccount()
{
char name[NAME_LEN];
int accountNumber;
int balance;
cout << "[Open a new bank account]" << endl;
cout << "Enter the customer name: "; cin >> name;
cout << "Set account number: "; cin >> accountNumber;
cout << "Enter deposit: "; cin >> balance;
cout << endl;
accountArray[numOfAccounts++] = new Account(name, accountNumber, balance);
}
void depositMoney()
{
int accountNumber;
int money;
cout << "Enter the account number: "; cin >> accountNumber;
cout << "Amount of deposit: "; cin >> money;
for (int i = 0; i < numOfAccounts; i++)
{
if (accountArray[i]->getAccountNumber() == accountNumber)
{
accountArray[i]->deposit(money);
cout << "Deposit completed" << endl;
return;
}
}
cout << "Invalid Account Information." << endl << endl;
}
void withdrawMoney()
{
int accountNumber;
int money;
cout << "Enter the account number: "; cin >> accountNumber;
cout << "Amount of withdraw: "; cin >> money;
for (int i = 0; i < numOfAccounts; i++)
{
if (accountArray[i]->getAccountNumber() == accountNumber)
{
accountArray[i]->withdraw(money);
cout << "Withdraw completed" << endl;
return;
}
}
cout << "Invalid Account Information." << endl << endl;
}
void viewEntireAccountInfo()
{
for (int i = 0; i < numOfAccounts; i++)
{
accountArray[i]->ViewAccInfo();
cout << endl;
}
}
int main()
{
while (true)
{
displayMenu();
int option;
cout << "Select option: "; cin >> option;
cout << endl;
switch (option)
{
case NEW_ACCOUNT:
newBankAccount();
break;
case DEPOSIT:
depositMoney();
break;
case WITHDRAW:
withdrawMoney();
break;
case VIEW_ACCOUNT:
viewEntireAccountInfo();
break;
case EXIT:
for (int i = 0; i < numOfAccounts; i++)
delete accountArray[i];
return 0;
default:
cout << "Illegal selection." << endl;
}
}
system("Pause");
return 0;
}