diff --git a/Banking-System/Bank.data b/Banking-System/Bank.data new file mode 100644 index 0000000..eb0d3ce --- /dev/null +++ b/Banking-System/Bank.data @@ -0,0 +1,16 @@ +1 +Yusuf +Salaudeen +25000 +2 +Zainab +Akande +3000 +3 +Jim +Rohn +10000 +4 +Kylian +Mbappe +12000 diff --git a/Banking-System/a.exe b/Banking-System/a.exe new file mode 100644 index 0000000..8117511 Binary files /dev/null and b/Banking-System/a.exe differ diff --git a/Banking-System/banking.cpp b/Banking-System/banking.cpp new file mode 100644 index 0000000..22e54d8 --- /dev/null +++ b/Banking-System/banking.cpp @@ -0,0 +1,128 @@ +#include "banking.h" + +// Account class implementation + +long Account::nextActNum_ = 0; +Account::Account(string firstName, string lastName, double bal){ + firstName_ = firstName; + lastName_ = lastName; + bal_ = bal; + nextActNum_++; + actNum_ = nextActNum_; +} + +// accesors +double Account::bal() { return bal_; } +long Account::actNum() { return actNum_; } +string Account::firstName() { return firstName_; } +string Account::lastName() { return lastName_; } + +// manipulators +void Account::deposit(double amount){ bal_ += amount; } +void Account::withdraw(double amount){ + if (bal_ - amount < minimum_bal) { + throw InsufficientFunds(); + } + bal_-=amount; +} +void Account::lastActNum(long actNum){ nextActNum_ = actNum; } +long Account::lastActNum(){ return nextActNum_; } + +ofstream &operator << (ofstream &ofs, Account &account){ + ofs <> (ifstream &ifs, Account &account){ + ifs >> account.actNum_; + ifs >> account.firstName_; + ifs >> account.lastName_; + ifs >> account.bal_; + return ifs; +} +ostream &operator << (ostream &os, Account &account){ + os << "First Name: " << account.firstName() << endl; + os << "Last Name: " << account.lastName() << endl; + os << "Account Number: " << account.actNum() << endl; + os << "Balance: " << account.bal() << endl; + return os; +} + +// Bank class implementation + +Bank::Bank(){ + Account account; + ifstream infile; + infile.open("Bank.data"); + if(!infile){ + //cout << "Error in opening, file not found!\n"; + return; + } + while(!infile.eof()){ + infile >> account; + pair entry; + entry = make_pair(account.actNum(), account); + accounts.insert(entry); + + // == accounts.insert(pair(account.actNum(), account)); + } + Account::lastActNum(account.actNum()); + infile.close(); +} + +Account Bank::openAct(string firstName, string lastName, double bal){ + ofstream outfile; + Account account(firstName, lastName, bal); + accounts.insert(pair(account.actNum(), account)); + + outfile.open("Bank.data", ios::trunc); + + map::iterator itr; + for(itr = accounts.begin(); itr != accounts.end(); itr++){ + outfile << itr->second; + } + outfile.close(); + return account; +} + +Account Bank::balanceEnquiry(long actNum){ + map::iterator itr; + itr = accounts.find(actNum); + return itr->second; +} +Account Bank::deposit(long actNum, double amount){ + map::iterator itr; + itr = accounts.find(actNum); + itr->second.deposit(amount); + return itr->second; +} +Account Bank::withdraw(long actNum, double amount){ + map::iterator itr; + itr = accounts.find(actNum); + itr->second.withdraw(amount); + return itr->second; +} +void Bank::closeAccount(long actNum){ + map::iterator itr; + itr = accounts.find(actNum); + cout << "Account deleted.\n"; + accounts.erase(actNum); +} +void Bank::showAllAccounts(){ + map::iterator itr; + for (itr = accounts.begin(); itr != accounts.end(); itr++){ + cout << "Account " << itr->first << endl << itr->second << endl; + } +} +Bank::~Bank(){ + ofstream outfile; + outfile.open("Bank.data", ios::trunc); + + map::iterator itr; + for (itr = accounts.begin(); itr != accounts.end(); itr++){ + outfile << itr->second; + } + outfile.close(); +} \ No newline at end of file diff --git a/Banking-System/banking.h b/Banking-System/banking.h new file mode 100644 index 0000000..6939c6b --- /dev/null +++ b/Banking-System/banking.h @@ -0,0 +1,55 @@ +#include +#include +#include +#include +#include + +using namespace std; + +double const minimum_bal = 500.0; +class InsufficientFunds{}; + +class Account { + private: + double bal_; + long actNum_; + string firstName_; + string lastName_; + static long nextActNum_; + public: + // Constructors + Account() {}; + Account(string firstName, string lastName, double bal); + + // Accesors + double bal(); + long actNum(); + string firstName(); + string lastName(); + + void deposit(double amount); + void withdraw(double amount); + static void lastActNum(long actNum); + static long lastActNum(); + + // Operator overloading + friend ofstream &operator << (ofstream &ofs, Account &acccount); + friend ifstream &operator >> (ifstream &ifs, Account &account); + friend ostream &operator << (ostream &os, Account &account); +}; + +class Bank{ + private: + map accounts; + public: + //Constructors + Bank(); + // Methods + Account openAct(string firstName, string lastName, double bal); + Account balanceEnquiry(long actNum); + Account deposit(long actNum, double amount); + Account withdraw(long actNum, double amount); + void closeAccount(long actNum); + void showAllAccounts(); + ~Bank(); +}; diff --git a/Banking-System/main.cpp b/Banking-System/main.cpp new file mode 100644 index 0000000..23f5fc3 --- /dev/null +++ b/Banking-System/main.cpp @@ -0,0 +1,72 @@ +#include "banking.cpp" + +int main(){ + + Bank bank; + Account account; + + int choice; + string fname, lname; + long actNum; + double balance; + double amount; + + cout << "\n\t******** Welcome to Our Banking System ***********" << endl; + do { + cout << "\n\t Select one option below"; + cout << "\n\t 1: Open an Account"; + cout << "\n\t 2: Balance Enquiry"; + cout << "\n\t 3: Deposit Money"; + cout << "\n\t 4: Withdraw Money"; + cout << "\n\t 5: Close an Account"; + cout << "\n\t 6: Show all Accounts"; + cout << "\n\t 7: Quit"; + cout << "\nEnter your choice: "; + cin >> choice; + switch (choice) + { + case 1: // Open an account + cout << "Enter first name: "; cin >> fname; + cout << "Enter last name: "; cin >> lname; + cout << "Enter initial balance: "; cin >> balance; + account = bank.openAct(fname, lname, balance); + cout << "\nCongratulations! Account opened successfully!\n"; + cout << account; + break; + case 2: // Balance enquiry + cout << "Enter the account number: "; cin >> actNum; + account = bank.balanceEnquiry(actNum); + cout << "Your balance enquiry is: " << account; + break; + case 3: // Deposit money + cout << "Enter the account number: "; cin >> actNum; + cout << "Enter amount to deposit: "; cin >> amount; + account = bank.deposit(actNum, amount); + cout << "The sum of " << amount + << " has been deposited to the account associated with the account number " << actNum; + break; + case 4: // Withdraw money + cout << "Enter the account number: "; cin >> actNum; + cout << "Enter amount to withdraw: "; cin >> amount; + account = bank.withdraw(actNum, amount); + cout << "The sum of " << amount + << " has been withdrawn from the account associated with the account number " << actNum; + break; + case 5: // Close an account + cout << "Enter the account number: "; cin >> actNum; + bank.closeAccount(actNum); + cout << "The account having the account number " << actNum << " was closed successfully"; + break; + case 6: // Show all accounts + bank.showAllAccounts(); + break; + case 7: // Quit + break; + default: + cout << "Enter a valid choice"; + exit(0); + } + } while (choice != 7); + + return 0; +}