#include <iostream.
h>
#include <conio.h>
#include <stdlib.h>
#include <string.h>
struct Transaction {
char type[10];
float amount;
Transaction *next;
};
struct Customer {
int id;
char name[50];
float balance;
Customer *next;
Transaction *transactions; // Pointer to store transaction history
};
Customer *head = NULL; // Head of the linked list for customers
void addCustomer(int id, const char *name, float balance) {
Customer *newCustomer = new Customer;
newCustomer->id = id;
strcpy(newCustomer->name, name);
newCustomer->balance = balance;
newCustomer->next = head;
newCustomer->transactions = NULL; // Initialize transaction list
head = newCustomer;
}
void displayCustomers() {
Customer *temp = head;
while (temp != NULL) {
cout << "ID: " << temp->id << ", Name: " << temp->name << ", Balance: " <<
temp->balance << endl;
temp = temp->next;
}
}
void pushTransaction(Customer *customer, const char *type, float amount) {
Transaction *newTransaction = new Transaction;
strcpy(newTransaction->type, type);
newTransaction->amount = amount;
newTransaction->next = customer->transactions;
customer->transactions = newTransaction;
}
void deposit(int id, float amount) {
Customer *temp = head;
while (temp != NULL) {
if (temp->id == id) {
temp->balance += amount;
pushTransaction(temp, "Deposit", amount);
cout << "Deposited: " << amount << ", New Balance: " << temp->balance
<< endl;
return;
}
temp = temp->next;
}
cout << "Customer not found!" << endl;
}
void withdraw(int id, float amount) {
Customer *temp = head;
while (temp != NULL) {
if (temp->id == id) {
if (temp->balance >= amount) {
temp->balance -= amount;
pushTransaction(temp, "Withdraw", amount);
cout << "Withdrew: " << amount << ", New Balance: " << temp-
>balance << endl;
} else {
cout << "Insufficient balance!" << endl;
}
return;
}
temp = temp->next;
}
cout << "Customer not found!" << endl;
}
// Updated to display all transactions for the customer
void viewTransactionHistory(int id) {
Customer *temp = head;
while (temp != NULL) {
if (temp->id == id) {
Transaction *trans = temp->transactions;
cout << "Transaction History for " << temp->name << ":" << endl;
if (trans == NULL) {
cout << "No transactions found." << endl;
return;
}
while (trans != NULL) {
cout << trans->type << ": " << trans->amount << endl;
trans = trans->next;
}
return;
}
temp = temp->next;
}
cout << "Customer not found!" << endl;
}
float calculateCompoundInterest(float principal, float rate, int time) {
if (time == 0)
return principal;
return calculateCompoundInterest(principal * (1 + rate / 100), rate, time - 1);
}
float convertToFloat(const char *input) {
char temp[20];
strcpy(temp, input);
// Replace comma with point for decimal
for (int i = 0; temp[i]; i++) {
if (temp[i] == ',') {
temp[i] = '.';
}
}
return atof(temp); // Convert string to float
}
void main() {
clrscr();
int choice, id, time;
char name[50];
char amountStr[20], principalStr[20], rateStr[20];
do {
cout << "\nBank Management System Menu:\n";
cout << "1. Add New Customer\n";
cout << "2. Display Customers\n";
cout << "3. Deposit Money\n";
cout << "4. Withdraw Money\n";
cout << "5. View Transaction History\n";
cout << "6. Calculate Compound Interest\n";
cout << "0. Exit\n";
cout << "Enter your choice: ";
cin >> choice;
switch (choice) {
case 1:
cout << "Enter ID, Name, Initial Balance: ";
cin >> id >> name >> amountStr;
addCustomer(id, name, convertToFloat(amountStr));
break;
case 2:
displayCustomers();
break;
case 3:
cout << "Enter Customer ID and Amount to Deposit: ";
cin >> id >> amountStr;
deposit(id, convertToFloat(amountStr));
break;
case 4:
cout << "Enter Customer ID and Amount to Withdraw: ";
cin >> id >> amountStr;
withdraw(id, convertToFloat(amountStr));
break;
case 5:
cout << "Enter Customer ID to view transaction history: ";
cin >> id;
viewTransactionHistory(id); // No longer prompts for number of
transactions
break;
case 6:
cout << "Enter Principal, Rate of Interest, Time: ";
cin >> principalStr >> rateStr >> time;
float totalAmount =
calculateCompoundInterest(convertToFloat(principalStr), convertToFloat(rateStr),
time);
cout << "Total Amount after interest: " << totalAmount << endl;
break;
case 0:
exit(0);
default:
cout << "Invalid choice! Please try again.\n";
}
} while (choice != 0);
getch();
}