-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathstructBankAcct.cpp
More file actions
192 lines (176 loc) · 6.86 KB
/
Copy pathstructBankAcct.cpp
File metadata and controls
192 lines (176 loc) · 6.86 KB
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
#include <iostream>
#include <string>
#include <stdexcept>
#include <vector>
using namespace std;
const int MAX_ACCTS = 10;
struct BankAcct {
int id;
string name;
float balance;
void display() {
cout << name << " has a balance of $" << balance << endl;
}
void deposit(float amount) {
balance += amount;
}
void withdraw(float amount) {
if (balance < amount) {
cout << "Insufficient funds\n";
} else {
balance -= amount;
}
}
};
void printAccounts(BankAcct accounts[], int count) {
float total = 0;
cout << "ID\tAccount\tBalance\n";
for (int i = 0; i < count; i++) {
printf("%d\t%s\t$%.2f\n", accounts[i].id, accounts[i].name.c_str(), accounts[i].balance);
total += accounts[i].balance;
}
printf("TOTAL\t\t\t$%.2f\n", total);
}
int main() {
BankAcct accounts[MAX_ACCTS] = {
{1, "WECU", 3.50},
{2, "BECU", 3.50},
{3, "Chase", 2.50},
{4, "US Bank", 1.50}
};
int count = 4; // Number of accounts
int next_id = 5; // Next available account ID
char selection;
try {
do {
printAccounts(accounts, count);
cout << "(D) Deposit, (W) Withdraw, (T) Transfer, (A) Add Account, (R) Remove Account, (Q) Quit: ";
cin >> selection;
switch (selection) {
case 'D':
case 'd': { // Deposit case
cout << "Deposit\n";
cout << "Enter account id: ";
int id;
cin >> id;
cout << "Enter deposit amount: ";
float amount; // Deposit amount variable float
cin >> amount;
bool found = false; // Boolean variable to check if account is found
for (int i = 0; i < count; i++) { // Loop through accounts
if (accounts[i].id == id) {
accounts[i].deposit(amount); // Deposit amount
found = true;
break;
}
}
if (!found) { // If account is not found print message
cout << "Account not found.\n";
}
break;
}
case 'W': // Withdraw case
case 'w': {
cout << "Withdraw\n";
cout << "Enter account id: ";
int id;
cin >> id;
cout << "Enter withdrawal amount: ";
float amount;
cin >> amount;
bool found = false;
for (int i = 0; i < count; i++) { // Loop through accounts
if (accounts[i].id == id) {
accounts[i].withdraw(amount); // Withdraw amount
found = true;
break;
}
}
if (!found) {
cout << "Account not found.\n";
}
break;
}
case 'A':
case 'a': {
if (count == MAX_ACCTS) { // Check if maximum number of accounts is reached
cout << "Maximum number of accounts reached. Cannot add more.\n";
continue;
}
else if (count > MAX_ACCTS) { // Check if maximum number of accounts is exceeded
throw(std::runtime_error( "What are you trying to do? Overload my memory? See ya!"));
}
string name;
float balance;
cout << "Enter account name: ";
cin >> name;
cout << "Enter initial balance: ";
cin >> balance;
accounts[count] = {next_id++, name, balance};
count++;
break;
}
case 'R':
case 'r': {
cout << "Remove\n";
cout << "Enter account id to remove: ";
int id;
cin >> id;
bool found = false;
for (int i = 0; i < count; i++) { // Loop through accounts
if (accounts[i].id == id) {
for (int j = i; j < count - 1; j++) { // Shift accounts
accounts[j] = accounts[j + 1];
}
count--; // Decrement count
found = true;
break;
}
}
if (!found) {
cout << "Account not found.\n";
}
break;
}
case 'T':
case 't': { // Transfer money from one account to another
cout << "Transfer\n";
cout << "Enter account id to transfer from: ";
int idFrom;
cin >> idFrom;
cout << "Enter account id to transfer to: ";
int idTo;
cin >> idTo;
cout << "Enter transfer amount: ";
float amount;
cin >> amount;
bool fromFound = false, toFound = false;
for (int i = 0; i < count; i++) { // Loop through accounts
if (accounts[i].id == idFrom) {
accounts[i].withdraw(amount); // Withdraw amount
fromFound = true;
}
if (accounts[i].id == idTo) {
accounts[i].deposit(amount); // Deposit amount
toFound = true;
}
}
if (!fromFound || !toFound) { // If one or both accounts are not found print message
cout << "One or both accounts not found.\n";
}
break;
}
case 'Q':
case 'q': {
cout << "Goodbye!\n";
break;
}
default:
cout << "Invalid selection\n";
}
} while (selection != 'Q' && selection != 'q');
} catch (const std::runtime_error& e) {
cout << e.what() << endl;
}
return 0;
}