Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- //"I pledge my honor that I have abided by the Stevens Honor System" - ajmauceri
- //declaring libraries
- #include <iostream>
- #include <string>
- #include <cmath>
- #include <math.h>
- #include <map>
- #include <algorithm>
- #include <chrono>
- #include <ctime>
- #include <time.h>
- #include <stdio.h>
- #include <iomanip>
- #include <string.h>
- #include <string>
- #include <fstream>
- #include <sstream>
- #include <list>
- #include <conio.h>
- //declaring namespace
- using namespace std;
- enum DepositType { cash, check };
- //creating a list of strings for saving data
- list <string> splitString(string str, string delimiter) {
- size_t position = 0;
- string token;
- list <string> result;
- while ((position = str.find(delimiter)) != string::npos) {
- token = str.substr(0, position);
- result.push_back(token);
- str.erase(0, position + delimiter.length());
- }
- return result;
- }
- //returns lowercase values of typed functions
- string stringToLower(string input)
- {
- transform(input.begin(), input.end(), input.begin(), tolower);
- return input;
- }
- bool SaveData(string FilePath, string contents) {
- ofstream writer;
- //1. open a real file on your hard drive
- writer.open(FilePath); //Creates a file titled 'FilePath' and ios::app makes it so it only adds info without deleting previous data
- if (writer.is_open()) {
- //2. getting data and saving data
- writer << contents;
- //3. Save and close that file.
- writer.close();
- return true; //returning that the file was saved sucessfully
- }
- else {
- writer.close();
- return false; //returning tht the file failed
- }
- }
- string LoadData(string FilePath) {
- ifstream reader;
- stringstream strStream;
- //1. Open a file so you can read from it
- reader.open(FilePath);
- //2. go ahead and read everything in it
- strStream << reader.rdbuf();
- //3. close the file
- reader.close();
- return strStream.str();
- }
- //setting the color of a sentence
- void PrintMessageInColor(string message, int color = 36) {
- cout << "\033[1;" << color << "m" << message << "\033[0m" << endl;
- }
- //standard color for Error Messages
- void PrintError(string message)
- {
- PrintMessageInColor(message , 31);
- }
- //setting command prompt to * when typing passwords
- string TypePassword() {
- string password = "";
- char character;
- character = _getch();
- while (character != 13) {//character 13 is enter
- if (character == 8) {
- password = password.substr(0, max((unsigned int)0, password.size() - 1));
- cout << "\33[2K\r";
- if (password.size() > 0) {
- for (unsigned int i = 0; i < password.size(); i++) {
- cout << "*";
- }
- }
- }
- else
- {
- password.push_back(character);
- cout << '*';
- }
- character = _getch();
- }
- cout << endl;
- return password;
- }
- //teaching the compiler what a bank account is
- class BankAccount
- {
- public:
- string username;
- private:
- string password;
- float currentBalance;
- public:
- string toString() {
- return username + "\t" + password + "\t" + to_string(currentBalance) + "\n";
- }
- BankAccount(string username, string password, float balance = 1000) {
- this->username = username;
- this->password = password;
- currentBalance = balance;
- }
- bool CheckPassword(string password) {
- return (this->password == password);
- }
- void CheckBal() {
- PrintMessageInColor("Your balance: $" + to_string(currentBalance), 32);
- }
- void ResetPassword() {
- PrintMessageInColor("Please enter your new password");
- password = TypePassword();
- }
- bool Withdraw(float amount) {
- if (amount > 0 && amount <= 200) { //check if it's a valid amount
- if (currentBalance - amount >= 0) {
- if (fmod(amount, 20) == 0) { //check if it's divisable to 20
- currentBalance -= amount; //removing from balance
- PrintMessageInColor("You have successfully withdrawn: $" + to_string(amount) + "\nHere is your cash.", 32); //printing message
- return true; //returning success
- }
- else {
- PrintError("You can only withdraw amounts divisable by $20!");
- return false;
- }
- }
- else {
- PrintError("You are too poor to make this transaction, please make more money and deposit it!");
- return false;
- }
- }
- else {
- PrintError("Sorry you can only withdraw a positive number up to $200.");
- return false;
- }
- }
- bool Deposit(float amount, DepositType depositType) {
- if (depositType == cash) { //depositing cash
- if (amount < 100 && amount > 0) { //checking valid amount for cash
- currentBalance += amount; //adding to balance
- PrintMessageInColor("You have successfully deposited: $" + to_string(amount), 32); //printing message
- return true; //returning success
- }
- else { //invalid amount printing error, returning false
- PrintError("Invalid amount deposited!");
- return false;
- }
- }
- else if (depositType == check) { //depositing check
- if (amount > 0) { //checking valid amount
- currentBalance += amount; //adding amount to balance
- PrintMessageInColor("You have successfully deposited: $" + to_string(amount), 32); //printing sucess message
- return true; //returning success
- }
- else { //invalid amount printing error returning false
- PrintError("Invalid amount deposited!");
- return false;
- }
- }
- return false; //if all else failed, return false
- }
- void AttemptDeposit() {
- string op;
- float amount;
- DepositType depositType = check;
- PrintMessageInColor("Would you like to Deposit 'Check' or 'Cash'?");
- cin >> op;
- if (stringToLower(op) == "cash") {
- depositType = cash;
- }
- if (stringToLower(op) == "cash" || stringToLower(op) == "check") {
- PrintMessageInColor("How much would you like to Deposit?");
- cin >> amount;
- Deposit(amount, depositType);
- }
- else {
- PrintError("Invalid Choice.");
- AttemptDeposit();
- }
- }
- };
- //teach the compiler about banks
- class OneClickBank {
- list<BankAccount> accounts;
- private:
- BankAccount CurrentActiveAccount = BankAccount("example", "password");
- public:
- void SaveBank() {
- SaveData("Bank.tsv", toString());
- }
- void LoadBank() {
- string loadedData = LoadData("Bank.tsv");
- list <string> loadedAccounts = splitString(loadedData, "\n");
- for (string account : loadedAccounts) {
- list <string> accountData = splitString(account, "\t");
- list <string>::iterator it = accountData.begin();
- string accountName = *it;
- it++;
- string password = *it;
- it++;
- float balance = stof(*it);
- BankAccount loadedAccount = BankAccount(accountName, password, balance);
- accounts.push_back(loadedAccount);
- }
- }
- string toString() {
- string result = "";
- for (BankAccount Account : accounts) {
- result += Account.toString();
- }
- }
- void MainScreen() {
- string op;
- 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);
- cin >> op;
- if (stringToLower(op) == "deposit") {
- CurrentActiveAccount.AttemptDeposit();
- }
- if (stringToLower(op) == "withdraw") {
- float withdraw;
- PrintMessageInColor("How much would you like to withdraw?");
- cin >> withdraw;
- CurrentActiveAccount.Withdraw(withdraw);
- }
- if (stringToLower(op) == "bal" || stringToLower(op) == "balance") {
- CurrentActiveAccount.CheckBal();
- }
- if (stringToLower(op) == "reset") {
- AttemptToLogin();
- CurrentActiveAccount.ResetPassword();
- }
- if (stringToLower(op) == "logout") {
- system("CLS");
- Welcome();
- }
- else {
- MainScreen();
- }
- }
- void LoginScreen() {
- string op;
- PrintMessageInColor("Type 'Login' to login, 'Create' to create an account.", 37);
- cin >> op;
- if (stringToLower(op) == "login") {
- AttemptToLogin();
- MainScreen();
- }
- if (stringToLower(op) == "create") {
- AttemptToCreateBankAccount();
- MainScreen();
- }
- else {
- LoginScreen();
- }
- }
- void AttemptToLogin() {
- string username, password;
- bool SuccessfullyLoggedIn = false;
- while (!SuccessfullyLoggedIn)
- {
- cout << "Please enter your username" << endl;
- cin >> username;
- cout << "Please enter your password" << endl;
- SuccessfullyLoggedIn = Login(username, TypePassword());
- }
- }
- void AttemptToCreateBankAccount() {
- string username, password;
- bool SuccessfullyCreatedAccount = false;
- while (!SuccessfullyCreatedAccount)
- {
- cout << "Please enter a username" << endl;
- cin >> username;
- cout << "Please enter a password" << endl;
- password = TypePassword();
- SuccessfullyCreatedAccount = CreateBankAccount(username, password);
- }
- Login(username, password);
- }
- void Welcome() {
- PrintMessageInColor("Welcome to 'One Click Bank' the one and only C++ bank where idiots store their cash!", 36);
- LoginScreen();
- }
- bool CreateBankAccount(string username, string password) {
- if (password.size() < 6) {
- PrintError("Sorry! Your password must be at least 6 characters.");
- return false;
- }
- 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
- if (bankAccount.username == username) { //checks the username from the current iterator (it) and sees if the username already exits
- PrintError("Sorry, the username you entered already exits.");
- return false;
- }
- }
- //We have successfully checked and our username doesn't already exist!
- BankAccount NewAccount(username, password);
- accounts.push_back(NewAccount);
- PrintMessageInColor("You have successfully created an account! As a thankyou we have deposited $1000 to " + username + ".", 36);
- return true;
- }
- bool Login(string username, string password) {
- 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
- if (bankAccount.username == username && bankAccount.CheckPassword(password)) { //checks the username from the current iterator (it) and sees if the username already exits
- PrintMessageInColor("Welcome back " + username + "!");
- CurrentActiveAccount = bankAccount;
- return true;
- }
- }
- PrintError("Invalid login, please try again.");
- return false;
- }
- };
- int main()
- {
- OneClickBank oneClickBank;
- oneClickBank.LoadBank();
- oneClickBank.Welcome();
- return 1;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement