Guest User

Untitled

a guest
Jan 26th, 2019
315
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 7.47 KB | None | 0 0
  1. #pragma once
  2.  
  3. #include <iostream>
  4. #include <string>
  5. #include <vector>
  6. #include <algorithm>
  7.  
  8.  
  9. enum type_acc {customer, staff};
  10.  
  11. class Account {
  12. public:
  13. std::string username;
  14. std::string password;
  15. double balance;
  16. type_acc account_type;
  17. bool operator==(const Account& rhs);
  18.  
  19. };
  20.  
  21. class BankInterface {
  22. public:
  23. static Account loggedinaccount;
  24. static std::vector<Account> registered_accounts;
  25. static void LoginMenu();
  26. static void RegisterAccount();
  27. static void Login();
  28. static void MainMenu();
  29. static void DisplayBalance();
  30. static void MakeWithdrawal();
  31. static void MakeDeposit();
  32. static type_acc AccountType(std::string uc);
  33.  
  34.  
  35. };
  36.  
  37. class StaffInterface {
  38. public:
  39. static void StaffMenu();
  40. static void CustomerBalance();
  41. static void AccountShutdown();
  42. };
  43.  
  44. Account BankInterface::loggedinaccount;
  45. std::vector<Account> BankInterface::registered_accounts;
  46.  
  47. // AccountSystem.cpp : This file contains the 'main' function. Program execution begins and ends there.
  48. //
  49.  
  50. #include "pch.h"
  51. #include "AccountSystem.h"
  52.  
  53. void BankInterface::LoginMenu() {
  54. std::cout << "Please select an option: n 1. Register a new account. n 2. Login to account.n";
  55.  
  56. std::string user_choice;
  57. std::cin >> user_choice;
  58.  
  59. if (user_choice == "1") {
  60. RegisterAccount();
  61. }
  62. else if (user_choice == "2") {
  63. Login();
  64. }
  65.  
  66. else {
  67. std::cout << "We did not recognise your input, please try again...n";
  68. LoginMenu();
  69. }
  70. }
  71.  
  72. type_acc BankInterface::AccountType(std::string uc) {
  73. if (uc == "y") {
  74. return staff;
  75. }
  76. else if (uc == "n") {
  77. return customer;
  78. }
  79. else {
  80. std::cout << "We did not recognise your input, please try again.n";
  81. std::string user_choice;
  82. std::cin >> user_choice;
  83. return AccountType(user_choice);
  84. }
  85. }
  86.  
  87. void BankInterface::RegisterAccount() {
  88. Account new_account;
  89. std::cout << "Please enter the desired username of your account: n";
  90. std::cin >> new_account.username;
  91. std::cout << "Please enter your desired password for your account.n";
  92. std::cin >> new_account.password;
  93. std::cout << "Please enter your initial balance for your account.n";
  94. std::cin >> new_account.balance;
  95. std::cout << "Are you a member of staff? y/n?n";
  96. std::string user_choice;
  97. std::cin >> user_choice;
  98. new_account.account_type = BankInterface::AccountType(user_choice);
  99. registered_accounts.push_back(new_account);
  100. std::cout << "Account created.n";
  101.  
  102. for (auto &account : registered_accounts) {
  103. std::cout << account.username << "n";
  104. }
  105.  
  106. LoginMenu();
  107.  
  108. }
  109.  
  110. void BankInterface::Login() {
  111. std::cout << "Please enter your username.n";
  112. std::string username;
  113. std::cin >> username;
  114.  
  115. for (auto &account : registered_accounts) {
  116. if (account.username == username) {
  117. std::cout << "Please enter your password.n";
  118. std::string password;
  119. std::cin >> password;
  120. if (account.password == password) {
  121. loggedinaccount = account;
  122.  
  123. if (loggedinaccount.account_type == customer) {
  124. MainMenu();
  125. }
  126. else {
  127. StaffInterface::StaffMenu();
  128. }
  129.  
  130. }
  131. }
  132.  
  133. }
  134. std::cout << "That username does not exist in our system.n";
  135. Login();
  136. }
  137.  
  138. void BankInterface::MainMenu() {
  139. std::cout << " 1. Check account balance.n 2. Make a withdrawal.n 3. Make a deposit. n";
  140.  
  141. int user_choice = 0;
  142. std::cin >> user_choice;
  143.  
  144. switch (user_choice) {
  145. case 1:
  146. DisplayBalance();
  147. break;
  148. case 2:
  149. MakeWithdrawal();
  150. break;
  151. case 3:
  152. MakeDeposit();
  153. break;
  154. default:
  155. std::cout << "We did not understand your input, please try again.";
  156. MainMenu();
  157. break;
  158. }
  159.  
  160. }
  161.  
  162. void BankInterface::DisplayBalance() {
  163. std::cout << "Your balance is: " << loggedinaccount.balance << "n";
  164. MainMenu();
  165. }
  166.  
  167. void BankInterface::MakeWithdrawal() {
  168. std::cout << "Please enter the amount you wish to withdraw: n";
  169. double amt;
  170. std::cin >> amt;
  171.  
  172. if (amt > loggedinaccount.balance) {
  173. std::cout << "You do not have this much money in your account, please input a feasible sum to withdraw. n";
  174. MakeWithdrawal();
  175. }
  176. else if (amt <= 0) {
  177. std::cout << "You cannot withdraw a negative amount, or zero, dollars, please try again.n";
  178. MakeWithdrawal();
  179. }
  180. else {
  181. loggedinaccount.balance -= amt;
  182. std::cout << "Money withdrawn, new balance: " << loggedinaccount.balance << "n";
  183. MainMenu();
  184. }
  185.  
  186. }
  187.  
  188. void BankInterface::MakeDeposit() {
  189. std::cout << "How much money do you wish to deposit? n";
  190. double amt;
  191. std::cin >> amt;
  192.  
  193. if (amt <= 0) {
  194. std::cout << "You cannot deposit a negative amount of, or zero, dollars. Please try again.n";
  195. MakeDeposit();
  196. }
  197. else {
  198. loggedinaccount.balance += amt;
  199. std::cout << "Your new balance is: " << loggedinaccount.balance << "n";
  200. MainMenu();
  201. }
  202.  
  203. }
  204.  
  205. void StaffInterface::StaffMenu() {
  206. std::cout << "1. View customer's balance.n2. Shut down customer's account.n";
  207.  
  208. int staff_choice = 0;
  209. std::cin >> staff_choice;
  210.  
  211. switch (staff_choice) {
  212. case 1:
  213. CustomerBalance();
  214. break;
  215. case 2:
  216. AccountShutdown();
  217. break;
  218. default:
  219. std::cout << "We did not understand your input, please try again.";
  220. StaffMenu();
  221. break;
  222. }
  223.  
  224. }
  225.  
  226. void StaffInterface::CustomerBalance() {
  227. std::cout << "Enter the customer's username to view their balance: n";
  228. std::string cust_name;
  229. std::cin >> cust_name;
  230.  
  231. for (auto &account : BankInterface::registered_accounts) {
  232. if (cust_name == account.username) {
  233. std::cout << account.balance << "n";
  234. StaffMenu();
  235. }
  236. else {
  237. std::cout << "We could not detect an account with that username, please try again.n";
  238. CustomerBalance();
  239. }
  240. }
  241. }
  242.  
  243. bool Account::operator==(const Account& rhs) {
  244. if (username == rhs.username) {
  245. return true;
  246. }
  247. else {
  248. return false;
  249. }
  250. }
  251.  
  252. void StaffInterface::AccountShutdown() {
  253.  
  254. std::cout << "Enter the customer's username of the account you want to delete.n";
  255. std::string username_del;
  256. std::cin >> username_del;
  257. for (auto &account : BankInterface::registered_accounts) {
  258. if (username_del == account.username) {
  259. std::cout << "Account detected, are you sure you wish to delete this account? y/n n";
  260. std::string confirmation;
  261. bool ans = false;
  262.  
  263. while (ans != true) {
  264. std::cin >> confirmation;
  265.  
  266. if (confirmation == "y") {
  267. auto pos = find(BankInterface::registered_accounts.begin(), BankInterface::registered_accounts.end(), account);
  268. BankInterface::registered_accounts.erase(pos);
  269. std::cout << "Account deleted, returning to main staff menu. n";
  270. ans = true;
  271. StaffMenu();
  272. }
  273. else if (confirmation == "n") {
  274. ans = true;
  275. StaffMenu();
  276. }
  277. else {
  278. std::cout << "We did not recognise that option, please try again.n";
  279. }
  280.  
  281. }
  282.  
  283. }
  284. }
  285. }
  286.  
  287. int main()
  288. {
  289. BankInterface::LoginMenu();
  290. }
Add Comment
Please, Sign In to add comment