Facebook
From mhabrar, 2 Weeks ago, written in C++.
Embed
Download Paste or View Raw
Hits: 79
  1. #include <bits/stdc++.h>
  2. using namespace std;
  3.  
  4. vector<string> menu_items= {
  5.  "New", "Withdrawal", "Deposit", "Check Balance", "Display All", "Fund Transfer", "Exit"
  6. };
  7. const int EXIT_CODE = menu_items.size();
  8.  
  9. typedef struct {
  10.  int id;
  11.  string name;
  12.  float balance;
  13. } MyAccount;
  14.  
  15. map<int, MyAccount> accounts;
  16.  
  17.  
  18. void add();
  19. void withdraw(){};
  20. void deposite(){};
  21. void checkBalance();
  22. void displayAll();
  23. void fundTransfer();
  24. void loadDataTable();
  25.  
  26. int ran = 0;
  27.  
  28. int menu() {
  29.  
  30.  if (ran == 0) {
  31.   loadDataTable();
  32.   ++ran;
  33.  }
  34.  
  35.  int op = 0;
  36.  int items = menu_items.size();
  37.  
  38.  cout << "<----- MENU -----&gt;\n";
  39.  for (int i = 0; i < items; i++) {
  40.   cout << '\t' << i+1 << ". " << menu_items[i] << '\n';
  41.  }
  42.  
  43.  printf("Enter your choice (1-%d): ", items);
  44.  cin >> op;
  45.  
  46.  if ( !(op >= 1 && op <= items) ) {
  47.   cout << "Invalid Choice! Please select again!\n";
  48.   menu();
  49.  }
  50.  
  51.  return op;
  52. }
  53.  
  54. int main() {
  55.  
  56.  int op = menu();
  57.  
  58.  if (op == EXIT_CODE) {
  59.   cout << "<----- Program Exit -----&gt;\n";
  60.   return 0;
  61.  }
  62.  
  63.  cout << "\n\n\t<----- " << menu_items[op-1] << " -----&gt;\n";
  64.  switch (op) {
  65.   case 1: add(); break;
  66.   case 2: withdraw(); break;
  67.   case 3: deposite(); break;
  68.   case 4: checkBalance(); break;
  69.   case 5: displayAll(); break;
  70.  }
  71.  
  72.  main();
  73.  return 0;
  74. }
  75.  
  76.  
  77. void add() {
  78.  
  79.  MyAccount ac;
  80.  
  81.  while(true) {
  82.   cout << "Enter your account ID: ";
  83.   cin >> ac.id;
  84.  
  85.   if (accounts.find(ac.id) == accounts.end()) break;
  86.  
  87.   cout << "This account already exists! Enter a different ID.\n";
  88.  }
  89.  
  90.  cout << "Enter your name: ";
  91.  cin >> ac.name;
  92.  
  93.  cout << "Enter your deposite amount: ";
  94.  cin >> ac.balance;
  95.  
  96.  accounts[ac.id] = ac;
  97.  
  98.  cout << "\n\t\tAccount creation successful!\n";
  99.  return;
  100.  
  101. }
  102.  
  103. void checkBalance() {
  104.  cout << "Enter your account ID: ";
  105.  
  106.  int id;
  107.  cin >> id;
  108.  
  109.  if (accounts.find(id) == accounts.end()) {
  110.   cout << "Account not found!\n\n";
  111.   return;
  112.  }
  113.  
  114.  cout << "\n\n\tOwner name: " << accounts[id].name << '\n';
  115.  cout << "\tBalance: " << accounts[id].balance << '\n';
  116.  cout << "\n\n";
  117.  
  118. }
  119.  
  120. void displayAll() {
  121.  for (auto ac: accounts) {
  122.   cout << "\n\n\tAccount ID: " << ac.second.id << '\n';
  123.   cout << "\tAccount Owner Name: " << ac.second.name << '\n';
  124.   cout << "\tBalance: " << ac.second.balance << '\n';
  125.  }
  126.  
  127.  cout << "\n\n";
  128. }
  129.  
  130. void fundTransfer() {
  131.  // validify sender and receiver accounts.
  132. }
  133.  
  134. void loadDataTable() {
  135.  
  136.  ifstream file&#40;"accounts.txt"&#41;;
  137.  
  138.  while(!file.eof()) {
  139.   MyAccount ac;
  140.   file >> ac.id >> ac.name >> ac.balance;
  141.   accounts[ac.id] = ac;
  142.  }
  143. }
  144.