-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path9_2.cpp
More file actions
87 lines (74 loc) · 2.64 KB
/
9_2.cpp
File metadata and controls
87 lines (74 loc) · 2.64 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
#include <iostream>
#include <vector>
#include <string>
#include <iomanip>
using namespace std;
struct Transaction {
string type;
double amount;
string message;
};
class BankAccount {
private:
string holderName;
double balance;
vector<Transaction> history;
public:
BankAccount(const string& name, double initialBalance)
: holderName(name), balance(initialBalance) {
if (initialBalance < 0) {
cout << "Warning: Initial balance is negative. Setting it to 0.\n";
balance = 0;
}
history.push_back({"Account Created", balance, "Initial Balance Set"});
}
void deposit(double amount) {
if (amount <= 0) {
history.push_back({"Deposit", amount, "Failed: Invalid deposit amount"});
cout << "Invalid deposit amount.\n";
return;
}
balance += amount;
history.push_back({"Deposit", amount, "Success"});
cout << "Deposited rupees " << fixed << setprecision(2) << amount << " successfully.\n";
}
void withdraw(double amount) {
if (amount <= 0) {
history.push_back({"Withdraw", amount, "Failed: Invalid withdrawal amount"});
cout << "Withdrawal amount must be positive.\n";
return;
}
if (amount > balance) {
history.push_back({"Withdraw", amount, "Failed: Insufficient balance"});
cout << "Insufficient balance.\n";
return;
}
balance -= amount;
history.push_back({"Withdraw", amount, "Success"});
cout << "Withdrawn rupees " << fixed << setprecision(2) << amount << " successfully.\n";
}
void showBalance() const {
cout << "\nCurrent balance for " << holderName
<< ": rupees " << fixed << setprecision(2) << balance << "\n";
}
void showTransactionLog() const {
cout << "\nTransaction History for " << holderName << ":\n";
cout << "---------------------------------------------\n";
for (const auto& t : history) {
cout << t.type << " of rupees " << fixed << setprecision(2)
<< t.amount << " - " << t.message << "\n";
}
}
};
int main() {
BankAccount account("Pushti", 15000);
account.deposit(6000);
account.withdraw(11000); // should fail
account.withdraw(3500); // should succeed
account.deposit(-150); // should fail
account.withdraw(-40); // should fail
account.showBalance();
account.showTransactionLog();
cout<<endl<<"24CE052_Pushtikansara"<<endl;
return 0;
}