Advertisement
Guest User

Untitled

a guest
Nov 17th, 2019
193
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 7.10 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.  
  21. //declaring namespace
  22. using namespace std;
  23.  
  24. enum DepositType { cash, check };
  25.  
  26. //returns lowercase values of typed functions
  27. string stringToLower(string input)
  28. {
  29. transform(input.begin(), input.end(), input.begin(), tolower);
  30. return input;
  31. }
  32.  
  33. //Using "an" or "a" in a sentence
  34. string aOrAn(string nextWord) {
  35. nextWord = stringToLower(nextWord);
  36. char firstChar = nextWord.at(0);
  37. if (firstChar == 'a' ||
  38. firstChar == 'e' ||
  39. firstChar == 'i' ||
  40. firstChar == 'o' ||
  41. firstChar == 'u')
  42. {
  43. return "an";
  44. }
  45. else {
  46. return "a";
  47. }
  48. }
  49.  
  50. bool SaveData(string FilePath, string contents) {
  51. ofstream writer;
  52.  
  53. //1. open a real file on your hard drive
  54. writer.open(FilePath); //Creates a file titled 'FilePath' and ios::app makes it so it only adds info without deleting previous data
  55.  
  56. if (writer.is_open()) {
  57. //2. getting data and saving data
  58. writer << contents;
  59.  
  60. //3. Save and close that file.
  61. writer.close();
  62.  
  63. return true; //returning that the file was saved sucessfully
  64. }
  65.  
  66. else {
  67. writer.close();
  68. return false; //returning tht the file failed
  69. }
  70. }
  71.  
  72. string LoadData(string FilePath) {
  73. ifstream reader;
  74. stringstream strStream;
  75.  
  76. //1. Open a file so you can read from it
  77. reader.open(FilePath);
  78.  
  79. //2. go ahead and read everything in it
  80. strStream << reader.rdbuf();
  81.  
  82. //3. close the file
  83. reader.close();
  84.  
  85. return strStream.str();
  86. }
  87.  
  88.  
  89. //Capitalizing the first letter in a sentence
  90. string CapitalizeFirstLetter(string input) {
  91. input[0] = toupper(input[0]);
  92. return input;
  93. }
  94.  
  95. //setting the color of a sentence
  96. void PrintMessageInColor(string message, int color = 36) {
  97. cout << "\033[1;" << color << "m" << message << "\033[0m" << endl;
  98. }
  99.  
  100. //standard color for Error Messages
  101. void PrintError(string message)
  102. {
  103. PrintMessageInColor(message , 31);
  104. }
  105.  
  106. //setting a class for a transaction
  107. class Transaction
  108. {
  109. public:
  110. string name = "Transaction";
  111. float value = 0;
  112. time_t transactionTime;
  113. string toUser;
  114. string fromUser;
  115.  
  116.  
  117.  
  118. Transaction(string transactionName, float initialValue) {
  119. name = transactionName;
  120. value = initialValue;
  121. }
  122.  
  123. string ToString() {
  124. return name + " " + std::to_string(value) + "\r\n";
  125. }
  126.  
  127. };
  128.  
  129.  
  130. //teaching the compiler what a bank account is
  131. class BankAccount
  132. {
  133. public:
  134. string username;
  135.  
  136. private:
  137. string password;
  138. float currentBalance;
  139. list<Transaction> trasactions;
  140.  
  141. public:
  142. BankAccount(string username, string password) {
  143. this->username = username;
  144. this->password = password;
  145. currentBalance = 1000;
  146. }
  147.  
  148. bool CheckPassword(string password) {
  149. return (this->password == password);
  150. }
  151.  
  152. void CheckBal() {
  153. PrintMessageInColor("Your balance: $" + to_string(currentBalance), 32);
  154. }
  155.  
  156. bool Withdraw(float amount) {
  157. if (amount > 0 && amount <= 200) { //check if it's a valid amount
  158. if (fmod(amount, 20) == 0) { //check if it's divisable to 20
  159. currentBalance -= amount; //removing from balance
  160. PrintMessageInColor("You have successfully withdrawn: $" + to_string(amount) + "\nHere is your cash.", 32); //printing message
  161. return true; //returning success
  162. }
  163. else {
  164. PrintError("You can only withdraw amounts divisable by $20!");
  165. return false;
  166. }
  167. }
  168. else {
  169. PrintError("Sorry you can only withdraw a positive number up to $200.");
  170. return false;
  171. }
  172. }
  173.  
  174. bool Deposit(float amount, DepositType depositType) {
  175. if (depositType == cash) { //depositing cash
  176. if (amount < 100 && amount > 0) { //checking valid amount for cash
  177. currentBalance += amount; //adding to balance
  178. PrintMessageInColor("You have successfully deposited: $" + to_string(amount), 32); //printing message
  179. return true; //returning success
  180. }
  181. else { //invalid amount printing error, returning false
  182. PrintError("Invalid amount deposited!");
  183. return false;
  184. }
  185.  
  186. }
  187. else if (depositType == check) { //depositing check
  188. if (amount > 0) { //checking valid amount
  189. currentBalance += amount; //adding amount to balance
  190. PrintMessageInColor("You have successfully deposited: $" + to_string(amount), 32); //printing sucess message
  191. return true; //returning success
  192. }
  193. else { //invalid amount printing error returning false
  194. PrintError("Invalid amount deposited!");
  195. return false;
  196. }
  197.  
  198. }
  199. return false; //if all else failed, return false
  200. }
  201. };
  202.  
  203. //teach the compiler about banks
  204. class OneClickBank {
  205. list<BankAccount> accounts;
  206.  
  207. private:
  208. BankAccount CurrentActiveAccount;
  209.  
  210. public:
  211. OneClickBank(string BranchName) {
  212. CurrentActiveAccount = nullptr;
  213. };
  214.  
  215. void AttemptToCreateBankAccount() {
  216. string username, password;
  217. bool SuccessfullyCreatedAccount = false;
  218. while (!SuccessfullyCreatedAccount)
  219. {
  220. cout << "Please enter a username" << endl;
  221. cin >> username;
  222. cout << "Please enter a password" << endl;
  223. cin >> password;
  224. SuccessfullyCreatedAccount = this->CreateBankAccount(username, password);
  225. }
  226. }
  227.  
  228. void Welcome() {
  229. PrintMessageInColor("Welcome to 'One Click Bank' the one and only C++ bank where idiots store their cash!", 36);
  230. }
  231.  
  232. bool CreateBankAccount(string username, string password) {
  233. for (list<BankAccount>::iterator it; it != accounts.end(); it++) { //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
  234. if (it->username == username) { //checks the username from the current iterator (it) and sees if the username already exits
  235. PrintError("Sorry, the username you entered already exits.");
  236. return false;
  237. }
  238. }
  239. //We have successfully checked and our username doesn't already exist!
  240. BankAccount NewAccount(username, password);
  241. accounts.push_back(NewAccount);
  242. PrintMessageInColor("You have successfully created an account! As a thankyou we have deposited $1000 to " + username + ".", 36);
  243. return true;
  244. }
  245.  
  246. bool Login(string username, string password) {
  247. for (list<BankAccount>::iterator it; it != accounts.end(); it++) { //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
  248. if (it->username == username && it->CheckPassword(password)) { //checks the username from the current iterator (it) and sees if the username already exits
  249. PrintMessageInColor("Welcome back" + username + "!");
  250. CurrentActiveAccount = *it;
  251. cout << CurrentActiveAccount.username;
  252. return true;
  253. }
  254. }
  255. PrintError("Invalid login, please try again.");
  256. return false;
  257. }
  258. };
  259.  
  260. int main()
  261. {
  262.  
  263. OneClickBank oneClickBank;
  264. oneClickBank.Welcome();
  265. oneClickBank.AttemptToCreateBankAccount();
  266. oneClickBank.AttemptToCreateBankAccount();
  267. //pretend user wants to create an account
  268.  
  269.  
  270. return 1;
  271. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement