Advertisement
Guest User

Untitled

a guest
Jan 19th, 2017
137
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.30 KB | None | 0 0
  1. #include <string>
  2. #include <iostream>
  3. #include <vector>
  4. #include <Windows.h>
  5. #include <sstream>
  6. using namespace std;
  7.  
  8. static const double INITIAL_BALANCE = 500;
  9.  
  10. class User
  11. {
  12. public:
  13. int ID;
  14. string Password;
  15. double Balance;
  16.  
  17. User(int id, string pass, double balance)
  18. {
  19. this->ID = id;
  20. this->Password = pass;
  21. this->Balance = balance;
  22. }
  23.  
  24. void Withdraw(double x)
  25. {
  26. if (x < 0)
  27. throw exception("Withdrawal amount must be a positive number");
  28. if (x > this->Balance)
  29. throw exception("The account does not have enough balance");
  30. Balance -= x;
  31. }
  32.  
  33. void Deposit(double x)
  34. {
  35. if (x > 500)
  36. throw exception("Too much money?");
  37. if (x < 0)
  38. throw exception("Deposit amount must be a positive number");
  39. Balance += x;
  40. }
  41.  
  42. void TransferTo(User* target, double amount)
  43. {
  44. this->Withdraw(amount);
  45. target->Deposit(amount);
  46. }
  47.  
  48. string GetName()
  49. {
  50. stringstream s;
  51. s << "User" << (this->ID + 1);
  52. return s.str();
  53. }
  54. };
  55.  
  56. vector<User*> users;
  57.  
  58. void FillByPass(string passwordList[], int num)
  59. {
  60. for (int i = 0; i < users.size(); i++)
  61. {
  62. delete users[i];
  63. }
  64. users.clear();
  65. for (int i = 0; i < num; i++)
  66. {
  67. users.push_back(new User(i, passwordList[i], INITIAL_BALANCE));
  68. }
  69. }
  70.  
  71.  
  72. int main()
  73. {
  74. string pass[] = {"password1", "password2", "password3"};
  75. FillByPass(pass, 3);
  76. cout << "=======================================" << endl;
  77. cout << "Welcome in our great banking system!" << endl;
  78. cout << "=======================================" << endl;
  79. ::Sleep(2000);
  80. while (true)
  81. {
  82. cout << "Choose account you want to manage!" << endl;
  83. for (int i = 0; i < users.size(); i++)
  84. {
  85. cout << "#" << users[i]->GetName() << " ";
  86. }
  87. cout << endl;
  88. bool authed = false;
  89. User* currentUser = NULL;
  90. do
  91. {
  92. cout << "Username: ";
  93. string s;
  94. cin >> s;
  95. int user = -1;
  96. for (int i = 0; i < users.size(); i++)
  97. {
  98. if (s == users[i]->GetName())
  99. {
  100. user = i;
  101. break;
  102. }
  103. }
  104. cout << "Password: ";
  105. cin >> s;
  106. if (user >= 0 && s == users[user]->Password)
  107. {
  108. authed = true;
  109. currentUser = users[user];
  110. cout << "You have been logged in as " << currentUser->GetName() << endl;
  111. }
  112. else
  113. {
  114. cout << "Invalid credentials" << endl;
  115. }
  116. }
  117. while (!authed);
  118.  
  119. Sleep(2000);
  120. system("cls");
  121. bool stop = false;
  122. while (!stop)
  123. {
  124. double op = 0;
  125. int input;
  126. //double x; //operation amount
  127. cout << "These are options you are able to do on your account.\nWrite provided number to choose which one you want " << endl;
  128. cout << "1. Balance" << endl;
  129. cout << "2. Withdraw" << endl;
  130. cout << "3. Deposit" << endl;
  131. cout << "4. Transfer" << endl;
  132. cout << "5. Log out" << endl;
  133. cout << "6. Exit" << endl;
  134. cout << "Input: ";
  135. cin >> input;
  136. switch (input)
  137. {
  138. case 1:
  139. cout << "Your balance: " << currentUser->Balance << "$" << endl;
  140. getchar();
  141. break;
  142. case 2:
  143. cout << "How much money do you want to withdraw? ";
  144. cin >> op;
  145. try
  146. {
  147. currentUser->Withdraw(op);
  148. cout << "Money has been successfully withdrawn from your account" << endl;
  149. }
  150. catch (exception e)
  151. {
  152. cout << "Error: " << e.what() << endl;
  153. }
  154.  
  155. getchar();
  156. break;
  157. case 3:
  158. cout << "How much money do you want to deposit? ";
  159. cin >> op;
  160. try
  161. {
  162. currentUser->Deposit(op);
  163. cout << "You have successfully deposited your money" << endl;
  164. }
  165. catch (exception e)
  166. {
  167. cout << "Error: " << e.what();
  168. }
  169. getchar();
  170. break;
  171. case 4:
  172. int id;
  173. cout << "Who do you want to trasfer money to? Use only the ID of the user. ";
  174. cin >> id;
  175. if (id <= 0 || id > users.size())
  176. {
  177. cout << "No such user" << endl;
  178. break;
  179. }
  180.  
  181. cout << "How much money do you want to trasfer? ";
  182. cin >> op;
  183. try
  184. {
  185. currentUser->TransferTo(users[id - 1], op);
  186. cout << "You have successfully transferred the money" << endl;
  187. }
  188. catch (exception e)
  189. {
  190. cout << "Error: " << e.what();
  191. }
  192. getchar();
  193. break;
  194. case 5:
  195. stop = true;
  196. break;
  197. case 6:
  198. return 1;
  199. default:
  200. cout << "Error";
  201. getchar();
  202. break;
  203. }
  204. getchar();
  205. system("cls");
  206. }
  207. }
  208.  
  209. return 0;
  210. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement