-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtempCodeRunnerFile.cpp
More file actions
140 lines (117 loc) · 3.35 KB
/
tempCodeRunnerFile.cpp
File metadata and controls
140 lines (117 loc) · 3.35 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
#include <iostream>
#include <string>
using namespace std;
const int MAX = 100;
class BankAccount {
public:
string name;
float balance;
string transactionType[MAX];
float transactionAmount[MAX];
int txnCount;
BankAccount() {
cout<<"Hi welcome your system .......";
name = "";
balance = 0;
txnCount = 0;
}
void createAccount(string userName, float initialAmount) {
name = userName;
balance = initialAmount;
cout << "Account created for " << name << " with $" << balance << " balance.\n";
}
void deposit(float amount) {
if (amount <= 0) {
throw string("Deposit must be positive!");
}
balance += amount;
transactionType[txnCount] = "Deposit";
transactionAmount[txnCount] = amount;
txnCount++;
cout << "Deposited $" << amount << ". New balance: $" << balance << "\n";
}
void withdraw(float amount) {
if (amount <= 0) {
throw string("Withdrawal must be positive!");
}
if (amount > balance) {
throw string("Insufficient funds!");
}
balance -= amount;
transactionType[txnCount] = "Withdraw";
transactionAmount[txnCount] = amount;
txnCount++;
cout << "Withdrew $" << amount << ". Remaining balance: $" << balance << "\n";
}
void checkBalance() {
cout << "Current Balance: $" << balance << "\n";
}
void showHistory() {
if (txnCount == 0) {
cout << "No transactions made.\n";
return;
}
cout << "\nTransaction History:\n";
for (int i = 0; i < txnCount; i++) {
cout << i + 1 << ". " << transactionType[i] << ": $" << transactionAmount[i] << "\n";
}
}
};
void showError(string function, string error) {
cout << "[ERROR] in " << function << ": " << error << "\n";
}
int main() {
BankAccount acc;
string userName;
float initialAmount;
int choice;
float amount;
cout << "Enter your name: ";
cin >> userName;
cout << "Enter initial deposit: ";
cin >> initialAmount;
acc.createAccount(userName, initialAmount);
menu:
cout << "\n--- Bank Menu ---\n";
cout << "1. Deposit\n";
cout << "2. Withdraw\n";
cout << "3. Check Balance\n";
cout << "4. Show Transaction History\n";
cout << "5. Exit\n";
cout << "Enter your choice: ";
cin >> choice;
cout<<"Thank You ......";
switch (choice) {
case 1:
cout << "Enter amount to deposit: ";
cin >> amount;
try {
acc.deposit(amount);
} catch (string msg) {
showError("deposit", msg);
}
goto menu;
case 2:
cout << "Enter amount to withdraw: ";
cin >> amount;
try {
acc.withdraw(amount);
} catch (string msg) {
showError("withdraw", msg);
}
goto menu;
case 3:
acc.checkBalance();
goto menu;
case 4:
acc.showHistory();
goto menu;
case 5:
cout << "Thank you for using our banking system!\n";
break;
default:
cout << "Invalid choice! Try again.\n";
goto menu;
}
return 0;
}