Advertisement
Guest User

Untitled

a guest
Nov 17th, 2019
145
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 10.32 KB | None | 0 0
  1. //"I pledge my honor that I have abided by the Stevens Honor System" - ajmauceri
  2.  
  3. //declaring libraries
  4. #include <iostream>
  5. #include <string>
  6. #include <cmath>
  7. #include <math.h>
  8. #include <map>
  9. #include <algorithm>
  10. #include <chrono>
  11. #include <ctime>
  12. #include <time.h>
  13. #include <stdio.h>
  14. #include <iomanip>
  15. #include <string.h>
  16. #include <string>
  17. #include <fstream>
  18. #include <sstream>
  19. #include <list>
  20. #include <conio.h>
  21.  
  22. //declaring namespace
  23. using namespace std;
  24.  
  25. enum DepositType { cash, check };
  26.  
  27. //creating a list of strings for saving data
  28. list <string> splitString(string str, string delimiter) {
  29. size_t position = 0;
  30. string token;
  31. list <string> result;
  32. while ((position = str.find(delimiter)) != string::npos) {
  33. token = str.substr(0, position);
  34. result.push_back(token);
  35. str.erase(0, position + delimiter.length());
  36. }
  37. return result;
  38. }
  39.  
  40. //returns lowercase values of typed functions
  41. string stringToLower(string input)
  42. {
  43. transform(input.begin(), input.end(), input.begin(), tolower);
  44. return input;
  45. }
  46.  
  47. bool SaveData(string FilePath, string contents) {
  48. ofstream writer;
  49.  
  50. //1. open a real file on your hard drive
  51. writer.open(FilePath); //Creates a file titled 'FilePath' and ios::app makes it so it only adds info without deleting previous data
  52.  
  53. if (writer.is_open()) {
  54. //2. getting data and saving data
  55. writer << contents;
  56.  
  57. //3. Save and close that file.
  58. writer.close();
  59.  
  60. return true; //returning that the file was saved sucessfully
  61. }
  62.  
  63. else {
  64. writer.close();
  65. return false; //returning tht the file failed
  66. }
  67. }
  68.  
  69. string LoadData(string FilePath) {
  70. ifstream reader;
  71. stringstream strStream;
  72.  
  73. //1. Open a file so you can read from it
  74. reader.open(FilePath);
  75.  
  76. //2. go ahead and read everything in it
  77. strStream << reader.rdbuf();
  78.  
  79. //3. close the file
  80. reader.close();
  81.  
  82. return strStream.str();
  83. }
  84.  
  85. //setting the color of a sentence
  86. void PrintMessageInColor(string message, int color = 36) {
  87. cout << "\033[1;" << color << "m" << message << "\033[0m" << endl;
  88. }
  89.  
  90. //standard color for Error Messages
  91. void PrintError(string message)
  92. {
  93. PrintMessageInColor(message , 31);
  94. }
  95.  
  96. //setting command prompt to * when typing passwords
  97. string TypePassword() {
  98. string password = "";
  99. char character;
  100. character = _getch();
  101. while (character != 13) {//character 13 is enter
  102.  
  103. if (character == 8) {
  104. password = password.substr(0, max((unsigned int)0, password.size() - 1));
  105. cout << "\33[2K\r";
  106. if (password.size() > 0) {
  107. for (unsigned int i = 0; i < password.size(); i++) {
  108. cout << "*";
  109. }
  110. }
  111. }
  112.  
  113. else
  114. {
  115. password.push_back(character);
  116. cout << '*';
  117. }
  118.  
  119. character = _getch();
  120. }
  121. cout << endl;
  122. return password;
  123. }
  124.  
  125. //teaching the compiler what a bank account is
  126. class BankAccount
  127. {
  128. public:
  129. string username;
  130.  
  131. private:
  132. string password;
  133. float currentBalance;
  134.  
  135. public:
  136. string toString() {
  137. return username + "\t" + password + "\t" + to_string(currentBalance) + "\n";
  138. }
  139.  
  140. BankAccount(string username, string password, float balance = 1000) {
  141. this->username = username;
  142. this->password = password;
  143. currentBalance = balance;
  144. }
  145.  
  146. bool CheckPassword(string password) {
  147. return (this->password == password);
  148. }
  149.  
  150. void CheckBal() {
  151. PrintMessageInColor("Your balance: $" + to_string(currentBalance), 32);
  152. }
  153.  
  154. void ResetPassword() {
  155. PrintMessageInColor("Please enter your new password");
  156. password = TypePassword();
  157. }
  158.  
  159. bool Withdraw(float amount) {
  160. if (amount > 0 && amount <= 200) { //check if it's a valid amount
  161. if (currentBalance - amount >= 0) {
  162. if (fmod(amount, 20) == 0) { //check if it's divisable to 20
  163. currentBalance -= amount; //removing from balance
  164. PrintMessageInColor("You have successfully withdrawn: $" + to_string(amount) + "\nHere is your cash.", 32); //printing message
  165. return true; //returning success
  166. }
  167. else {
  168. PrintError("You can only withdraw amounts divisable by $20!");
  169. return false;
  170. }
  171. }
  172. else {
  173. PrintError("You are too poor to make this transaction, please make more money and deposit it!");
  174. return false;
  175. }
  176. }
  177. else {
  178. PrintError("Sorry you can only withdraw a positive number up to $200.");
  179. return false;
  180. }
  181. }
  182.  
  183. bool Deposit(float amount, DepositType depositType) {
  184. if (depositType == cash) { //depositing cash
  185. if (amount < 100 && amount > 0) { //checking valid amount for cash
  186. currentBalance += amount; //adding to balance
  187. PrintMessageInColor("You have successfully deposited: $" + to_string(amount), 32); //printing message
  188. return true; //returning success
  189. }
  190. else { //invalid amount printing error, returning false
  191. PrintError("Invalid amount deposited!");
  192. return false;
  193. }
  194.  
  195. }
  196. else if (depositType == check) { //depositing check
  197. if (amount > 0) { //checking valid amount
  198. currentBalance += amount; //adding amount to balance
  199. PrintMessageInColor("You have successfully deposited: $" + to_string(amount), 32); //printing sucess message
  200. return true; //returning success
  201. }
  202. else { //invalid amount printing error returning false
  203. PrintError("Invalid amount deposited!");
  204. return false;
  205. }
  206.  
  207. }
  208. return false; //if all else failed, return false
  209. }
  210.  
  211. void AttemptDeposit() {
  212. string op;
  213. float amount;
  214. DepositType depositType = check;
  215. PrintMessageInColor("Would you like to Deposit 'Check' or 'Cash'?");
  216. cin >> op;
  217. if (stringToLower(op) == "cash") {
  218. depositType = cash;
  219. }
  220. if (stringToLower(op) == "cash" || stringToLower(op) == "check") {
  221. PrintMessageInColor("How much would you like to Deposit?");
  222. cin >> amount;
  223. Deposit(amount, depositType);
  224. }
  225. else {
  226. PrintError("Invalid Choice.");
  227. AttemptDeposit();
  228. }
  229. }
  230. };
  231.  
  232. //teach the compiler about banks
  233. class OneClickBank {
  234. list<BankAccount> accounts;
  235.  
  236. private:
  237. BankAccount CurrentActiveAccount = BankAccount("example", "password");
  238.  
  239. public:
  240. void SaveBank() {
  241. SaveData("Bank.tsv", toString());
  242. }
  243.  
  244. void LoadBank() {
  245. string loadedData = LoadData("Bank.tsv");
  246. list <string> loadedAccounts = splitString(loadedData, "\n");
  247. for (string account : loadedAccounts) {
  248. list <string> accountData = splitString(account, "\t");
  249. list <string>::iterator it = accountData.begin();
  250. string accountName = *it;
  251. it++;
  252. string password = *it;
  253. it++;
  254. float balance = stof(*it);
  255. BankAccount loadedAccount = BankAccount(accountName, password, balance);
  256. accounts.push_back(loadedAccount);
  257. }
  258. }
  259.  
  260. string toString() {
  261. string result = "";
  262. for (BankAccount Account : accounts) {
  263. result += Account.toString();
  264. }
  265. }
  266.  
  267. void MainScreen() {
  268. string op;
  269. PrintMessageInColor("Type 'Deposit' to deposit money.\nType 'Withdraw' to withdraw money from your account.\nTo see your balance type 'Bal'.\nTo Change password type 'reset'.\nType 'Logout' to logout", 37);
  270. cin >> op;
  271. if (stringToLower(op) == "deposit") {
  272. CurrentActiveAccount.AttemptDeposit();
  273. }
  274. if (stringToLower(op) == "withdraw") {
  275. float withdraw;
  276. PrintMessageInColor("How much would you like to withdraw?");
  277. cin >> withdraw;
  278. CurrentActiveAccount.Withdraw(withdraw);
  279. }
  280. if (stringToLower(op) == "bal" || stringToLower(op) == "balance") {
  281. CurrentActiveAccount.CheckBal();
  282. }
  283. if (stringToLower(op) == "reset") {
  284. AttemptToLogin();
  285. CurrentActiveAccount.ResetPassword();
  286. }
  287. if (stringToLower(op) == "logout") {
  288. system("CLS");
  289. Welcome();
  290. }
  291. else {
  292. MainScreen();
  293. }
  294. }
  295.  
  296. void LoginScreen() {
  297. string op;
  298. PrintMessageInColor("Type 'Login' to login, 'Create' to create an account.", 37);
  299. cin >> op;
  300. if (stringToLower(op) == "login") {
  301. AttemptToLogin();
  302. MainScreen();
  303. }
  304. if (stringToLower(op) == "create") {
  305. AttemptToCreateBankAccount();
  306. MainScreen();
  307. }
  308. else {
  309. LoginScreen();
  310. }
  311. }
  312.  
  313. void AttemptToLogin() {
  314. string username, password;
  315. bool SuccessfullyLoggedIn = false;
  316. while (!SuccessfullyLoggedIn)
  317. {
  318. cout << "Please enter your username" << endl;
  319. cin >> username;
  320. cout << "Please enter your password" << endl;
  321. SuccessfullyLoggedIn = Login(username, TypePassword());
  322. }
  323. }
  324.  
  325. void AttemptToCreateBankAccount() {
  326. string username, password;
  327. bool SuccessfullyCreatedAccount = false;
  328. while (!SuccessfullyCreatedAccount)
  329. {
  330. cout << "Please enter a username" << endl;
  331. cin >> username;
  332. cout << "Please enter a password" << endl;
  333. password = TypePassword();
  334. SuccessfullyCreatedAccount = CreateBankAccount(username, password);
  335. }
  336. Login(username, password);
  337. }
  338.  
  339. void Welcome() {
  340. PrintMessageInColor("Welcome to 'One Click Bank' the one and only C++ bank where idiots store their cash!", 36);
  341. LoginScreen();
  342. }
  343.  
  344. bool CreateBankAccount(string username, string password) {
  345. if (password.size() < 6) {
  346. PrintError("Sorry! Your password must be at least 6 characters.");
  347. return false;
  348. }
  349. for (BankAccount bankAccount : accounts) { //this code scans over each BankAccount in the list checking if it's the last one, if not it adds one to check the next account
  350. if (bankAccount.username == username) { //checks the username from the current iterator (it) and sees if the username already exits
  351. PrintError("Sorry, the username you entered already exits.");
  352. return false;
  353. }
  354. }
  355. //We have successfully checked and our username doesn't already exist!
  356. BankAccount NewAccount(username, password);
  357. accounts.push_back(NewAccount);
  358. PrintMessageInColor("You have successfully created an account! As a thankyou we have deposited $1000 to " + username + ".", 36);
  359. return true;
  360. }
  361.  
  362. bool Login(string username, string password) {
  363. for (BankAccount bankAccount : accounts) { //this code scans over each BankAccount in the list checking if it's the last one, if not it adds one to check the next account
  364. if (bankAccount.username == username && bankAccount.CheckPassword(password)) { //checks the username from the current iterator (it) and sees if the username already exits
  365. PrintMessageInColor("Welcome back " + username + "!");
  366. CurrentActiveAccount = bankAccount;
  367. return true;
  368. }
  369. }
  370. PrintError("Invalid login, please try again.");
  371. return false;
  372. }
  373. };
  374.  
  375. int main()
  376. {
  377. OneClickBank oneClickBank;
  378. oneClickBank.LoadBank();
  379. oneClickBank.Welcome();
  380. return 1;
  381. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement