-
Notifications
You must be signed in to change notification settings - Fork 21
Create fork.cpp #43
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
VarunVoruganti15
wants to merge
1
commit into
gitsofaryan:main
Choose a base branch
from
VarunVoruganti15:main
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Create fork.cpp #43
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,342 @@ | ||
| #include <iostream> | ||
| #include <iomanip> | ||
| #include <vector> | ||
| #include <string> | ||
|
|
||
| using namespace std; | ||
|
|
||
| // Base class | ||
| class Account { | ||
| private: | ||
| static int nextAccNum; | ||
| int AccNo; | ||
| string AccHolderName; | ||
| double balance; // Keep balance private | ||
| string password; | ||
|
|
||
| public: | ||
| Account(const string &holderName, double initBalance, const string &pwd) | ||
| : AccHolderName(holderName), balance(initBalance), password(pwd) { | ||
| AccNo = nextAccNum++; | ||
| } | ||
|
|
||
| virtual double calculateInterest() = 0; // Pure virtual function for polymorphism | ||
|
|
||
| void deposit(double amount) { | ||
| if (amount > 0) { | ||
| balance += amount; | ||
| cout << "Successfully deposited: Rs" << amount << endl; | ||
| } else { | ||
| cout << "Invalid deposit amount!" << endl; | ||
| } | ||
| } | ||
|
|
||
| void withdraw(double amount, double overdraftLimit) { | ||
| const double withdrawalFee = 5.00; | ||
| const double minBalance = 100.00; | ||
|
|
||
| if (amount > 0 && (balance + overdraftLimit) >= (amount + withdrawalFee)) { | ||
| if ((balance - amount) < minBalance) { | ||
| cout << "Withdrawal denied! Minimum balance of Rs100.00 is required." << endl; | ||
| return; | ||
| } | ||
| balance -= (amount + withdrawalFee); | ||
| cout << "Successfully withdrew: Rs" << amount << " (with a fee of Rs" << withdrawalFee << ")" << endl; | ||
| } else { | ||
| cout << "Withdrawal amount exceeds balance and overdraft limit!" << endl; | ||
| } | ||
| } | ||
|
|
||
| virtual void displayAccDetails() const { // Marked as virtual | ||
| cout << fixed << setprecision(2); | ||
| cout << "\n---------------------------------------\n"; | ||
| cout << " Account Details \n"; | ||
| cout << "---------------------------------------\n"; | ||
| cout << setw(20) << "Account Number:" << setw(11) << AccNo << endl; | ||
| cout << setw(20) << "Account Holder:" << setw(11) << AccHolderName << endl; | ||
| cout << setw(20) << "Balance:" << setw(9) << "Rs " << balance << endl; | ||
| } | ||
|
|
||
| bool authenticate(const string &enteredPassword) const { | ||
| return enteredPassword == password; | ||
| } | ||
|
|
||
| int getAccNo() const { | ||
| return AccNo; | ||
| } | ||
|
|
||
| protected: // Change access to protected | ||
| double getBalance() const { | ||
| return balance; // Getter for balance | ||
| } | ||
|
|
||
| void addBalance(double amount) { | ||
| balance += amount; // Setter to modify balance directly | ||
| } | ||
| }; | ||
|
|
||
| int Account::nextAccNum = 10001; | ||
|
|
||
| // Derived class for Savings Account | ||
| class SavingsAccount : public Account { | ||
| private: | ||
| double interestRate; | ||
|
|
||
| public: | ||
| SavingsAccount(const string &holderName, double initBalance, const string &pwd) | ||
| : Account(holderName, initBalance, pwd), interestRate(0.05) {} | ||
|
|
||
| double calculateInterest() override { | ||
| double interest = getBalance() * interestRate; // Use getter for balance | ||
| addBalance(interest); // Add interest to balance | ||
| cout << "Interest of Rs" << interest << " added to your Savings Account." << endl; | ||
| return interest; | ||
| } | ||
|
|
||
| void displayAccDetails() const override { // Now correctly overrides | ||
| Account::displayAccDetails(); | ||
| cout << setw(20) << "Account Type:" << setw(13) << "Savings" << endl; | ||
| cout << setw(20) << "Interest Rate:" << setw(10) << interestRate * 100 << "%" << endl; | ||
| cout << setw(20) << "Overdraft Limit:" << setw(9) << "N/A" << endl; // Savings accounts do not have overdraft | ||
| cout << "---------------------------------------\n"; | ||
| } | ||
| }; | ||
|
|
||
| // Derived class for Current Account | ||
| class CurrentAccount : public Account { | ||
| private: | ||
| double overdraftLimit; | ||
|
|
||
| public: | ||
| CurrentAccount(const string &holderName, double initBalance, const string &pwd) | ||
| : Account(holderName, initBalance, pwd), overdraftLimit(5000.0) {} | ||
|
|
||
| double calculateInterest() override { | ||
| double interest = getBalance() * 0.04; // Use getter for balance | ||
| addBalance(interest); // Add interest to balance | ||
| cout << "Interest of Rs" << interest << " added to your Current Account." << endl; | ||
| return interest; | ||
| } | ||
|
|
||
| void displayAccDetails() const override { // Now correctly overrides | ||
| Account::displayAccDetails(); | ||
| cout << setw(20) << "Account Type:" << setw(13) << "Current" << endl; | ||
| cout << setw(20) << "Interest Rate:" << setw(10) << "4.00%" << endl; // Current accounts fixed interest | ||
| cout << setw(20) << "Overdraft Limit:" << setw(9) << "Rs " << overdraftLimit << endl; | ||
| cout << "---------------------------------------\n"; | ||
| } | ||
|
|
||
| double getOverdraftLimit() const { | ||
| return overdraftLimit; | ||
| } | ||
| }; | ||
|
|
||
| class Bank { | ||
| private: | ||
| vector<Account*> accounts; // Use pointers for polymorphism | ||
|
|
||
| public: | ||
| void createAcc() { | ||
| string holderName, password, AccType; | ||
| double initBalance; | ||
|
|
||
| cout << "Enter Account Holder Name: "; | ||
| cin.ignore(); | ||
| getline(cin, holderName); | ||
|
|
||
| cout << "Enter Initial Balance: "; | ||
| cin >> initBalance; | ||
|
|
||
| cout << "Enter Account Type (Savings/Current): "; | ||
| cin >> AccType; | ||
|
|
||
| cout << "Set your account password: "; | ||
| cin >> password; | ||
|
|
||
| Account* newAccount = nullptr; | ||
| if (AccType == "Savings") { | ||
| newAccount = new SavingsAccount(holderName, initBalance, password); | ||
| } else if (AccType == "Current") { | ||
| newAccount = new CurrentAccount(holderName, initBalance, password); | ||
| } else { | ||
| cout << "Invalid account type!" << endl; | ||
| return; | ||
| } | ||
|
|
||
| accounts.push_back(newAccount); | ||
| cout << "Account created successfully!" << endl; | ||
| } | ||
|
|
||
| void deleteAccount(int accNum) { | ||
| auto it = accounts.begin(); | ||
| while (it != accounts.end()) { | ||
| if ((*it)->getAccNo() == accNum) { | ||
| string enteredPassword; | ||
| cout << "Enter password to delete account: "; | ||
| cin >> enteredPassword; | ||
|
|
||
| if ((*it)->authenticate(enteredPassword)) { | ||
| delete *it; // Free memory | ||
| it = accounts.erase(it); | ||
| cout << "Account deleted successfully!" << endl; | ||
| return; | ||
| } else { | ||
| cout << "Incorrect password! Account deletion denied." << endl; | ||
| return; | ||
| } | ||
| } else { | ||
| ++it; | ||
| } | ||
| } | ||
| cout << "Account not found!" << endl; | ||
| } | ||
|
|
||
| void transactionManagement() { | ||
| int accNum; | ||
| cout << "Enter Account Number: "; | ||
| cin >> accNum; | ||
|
|
||
| for (Account* account : accounts) { | ||
| if (account->getAccNo() == accNum) { | ||
| string enteredPassword; | ||
| cout << "Enter password for transaction: "; | ||
| cin >> enteredPassword; | ||
|
|
||
| if (account->authenticate(enteredPassword)) { | ||
| int choice; | ||
| double amount; | ||
|
|
||
| cout << "1. Deposit\n2. Withdraw\nChoose an option: "; | ||
| cin >> choice; | ||
|
|
||
| switch (choice) { | ||
| case 1: | ||
| cout << "Enter amount to deposit: "; | ||
| cin >> amount; | ||
| account->deposit(amount); | ||
| break; | ||
| case 2: | ||
| if (dynamic_cast<CurrentAccount*>(account)) { | ||
| cout << "Enter amount to withdraw: "; | ||
| cin >> amount; | ||
| account->withdraw(amount, dynamic_cast<CurrentAccount*>(account)->getOverdraftLimit()); | ||
| } else { | ||
| cout << "Enter amount to withdraw: "; | ||
| cin >> amount; | ||
| account->withdraw(amount, 0); // Savings account has no overdraft | ||
| } | ||
| break; | ||
| default: | ||
| cout << "Invalid option!" << endl; | ||
| } | ||
| } else { | ||
| cout << "Incorrect password! Transaction denied." << endl; | ||
| } | ||
| return; | ||
| } | ||
| } | ||
| cout << "Account not found!" << endl; | ||
| } | ||
|
|
||
| void AccBalanceInquiry() { | ||
| int accNum; | ||
| cout << "Enter Account Number: "; | ||
| cin >> accNum; | ||
|
|
||
| for (Account* account : accounts) { | ||
| if (account->getAccNo() == accNum) { | ||
| string enteredPassword; | ||
| cout << "Enter password to view details: "; | ||
| cin >> enteredPassword; | ||
|
|
||
| if (account->authenticate(enteredPassword)) { | ||
| account->displayAccDetails(); | ||
| } else { | ||
| cout << "Incorrect password! Access denied." << endl; | ||
| } | ||
| return; | ||
| } | ||
| } | ||
| cout << "Account not found!" << endl; | ||
| } | ||
|
|
||
| void interestCal() { | ||
| int accNum; | ||
| cout << "Enter Account Number: "; | ||
| cin >> accNum; | ||
|
|
||
| for (Account* account : accounts) { | ||
| if (account->getAccNo() == accNum) { | ||
| string enteredPassword; | ||
| cout << "Enter password to calculate interest: "; | ||
| cin >> enteredPassword; | ||
|
|
||
| if (account->authenticate(enteredPassword)) { | ||
| account->calculateInterest(); | ||
| } else { | ||
| cout << "Incorrect password! Access denied." << endl; | ||
| } | ||
| return; | ||
| } | ||
| } | ||
| cout << "Account not found!" << endl; | ||
| } | ||
|
|
||
| void displayAllAcc() { | ||
| cout << "---------------------------------------\n"; | ||
| cout << " All Accounts in Indian Bank \n"; | ||
| cout << "---------------------------------------\n"; | ||
| for (Account* account : accounts) { | ||
| account->displayAccDetails(); | ||
| } | ||
| cout << "---------------------------------------\n"; | ||
| } | ||
|
|
||
| ~Bank() { | ||
| for (Account* account : accounts) { | ||
| delete account; // Free memory | ||
| } | ||
| } | ||
| }; | ||
|
|
||
| int main() { | ||
| Bank bank; | ||
| int choice; | ||
|
|
||
| while (true) { | ||
| cout << "\nWelcome to Indian Bank" << endl; | ||
| cout << "Your Trusted Partner in Financial Services." << endl; | ||
| cout << "\n1. Create Account\n2. Delete Account\n3. Transaction Management\n4. Account Balance Inquiry\n5. Calculate Interest\n6. Display All Accounts\n7. Exit\nChoose an option: "; | ||
| cin >> choice; | ||
|
|
||
| switch (choice) { | ||
| case 1: | ||
| bank.createAcc(); | ||
| break; | ||
| case 2: { | ||
| int accNum; | ||
| cout << "Enter Account Number to delete: "; | ||
| cin >> accNum; | ||
| bank.deleteAccount(accNum); | ||
| break; | ||
| } | ||
| case 3: | ||
| bank.transactionManagement(); | ||
| break; | ||
| case 4: | ||
| bank.AccBalanceInquiry(); | ||
| break; | ||
| case 5: | ||
| bank.interestCal(); | ||
| break; | ||
| case 6: | ||
| bank.displayAllAcc(); | ||
| break; | ||
| case 7: | ||
| cout << "Exiting..." << endl; | ||
| return 0; | ||
| default: | ||
| cout << "Invalid option! Please try again." << endl; | ||
| } | ||
| } | ||
| } | ||
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Fix withdraw boundary checks for fees and overdraft.
The current guard enforces the Rs100 minimum against
balance - amountand ignores the Rs5 fee. A Rs105 savings balance can withdraw Rs5 and end up at Rs95, violating the minimum. The same check also blocks current accounts from ever dipping into their overdraft, becausebalance - amount < minBalancetrips even when the overdraft limit would cover the shortfall. Please account for the fee when evaluating the post-transaction balance and relax the min-balance requirement when an overdraft limit is provided.Apply this diff to correct the logic:
void withdraw(double amount, double overdraftLimit) { const double withdrawalFee = 5.00; const double minBalance = 100.00; - if (amount > 0 && (balance + overdraftLimit) >= (amount + withdrawalFee)) { - if ((balance - amount) < minBalance) { - cout << "Withdrawal denied! Minimum balance of Rs100.00 is required." << endl; - return; - } - balance -= (amount + withdrawalFee); - cout << "Successfully withdrew: Rs" << amount << " (with a fee of Rs" << withdrawalFee << ")" << endl; - } else { - cout << "Withdrawal amount exceeds balance and overdraft limit!" << endl; - } + if (amount <= 0) { + cout << "Invalid withdrawal amount!" << endl; + return; + } + + if ((amount + withdrawalFee) > (balance + overdraftLimit)) { + cout << "Withdrawal amount exceeds balance and overdraft limit!" << endl; + return; + } + + double newBalance = balance - amount - withdrawalFee; + + if (overdraftLimit <= 0) { + if (newBalance < minBalance) { + cout << "Withdrawal denied! Minimum balance of Rs100.00 is required." << endl; + return; + } + } else if (newBalance < -overdraftLimit) { + cout << "Withdrawal amount exceeds overdraft allowance!" << endl; + return; + } + + balance = newBalance; + cout << "Successfully withdrew: Rs" << amount << " (with a fee of Rs" << withdrawalFee << ")" << endl; }📝 Committable suggestion
🤖 Prompt for AI Agents