Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- //using std with cout and cin instead of namespace
- #include <iostream>
- #include <fstream>
- #include <string>
- #include <ctime>
- #include <conio.h>
- #include <stdio.h>
- #include <sstream>
- using namespace std;
- struct User {
- int acctNumber1; //User account number 1
- int acctNumber2;
- double acctBalance1; //user account balance 1
- double acctBalance2;
- int acctInterest1; //user account interest rate1
- int acctInterest2;
- char acctName[256]; //User account Name
- char acctPass[256]; //user account password
- int logInNumber;
- bool isAdmin;
- } account[8999]; // 9999 - 1000 because we virtually start at record '1000'
- void saveAccounts()
- {/*
- std::stringstream stream;
- char buffer[100];
- FILE *accountsFile = fopen("C:\\Users\\James\\Desktop\\accounts.txt", "w+");
- if (accountsFile!=NULL)
- {
- for(int i = 0; i < 8999; i++) {
- if(account[i].acctNumber1!=NULL) {
- stream << account[i].acctNumber1;
- string s = stream.str();
- fputs(s.c_str(), accountsFile);
- accountsFile << account[i].acctNumber1 << '\n';
- }
- }
- } else { cout << "Unable to open file!"; }
- fclose(accountsFile);
- */
- ofstream accountsFile;
- accountsFile.open ("accounts.dat");
- for(int i = 0; i < 8999; i++) {
- if(account[i].acctNumber1 != NULL) {
- accountsFile << account[i].acctNumber1 << "\n";
- }
- }
- accountsFile.close();
- _getch();
- }
- void printSummary(int user) {
- printf("\n\n"
- "+-----------------+\n"
- "| Account Summary |\n"
- "+=================+\n\n"
- "+-----------------------------------------------------+\n"
- "| Account | Account number | Balance | Interest Rate |\n"
- "+---------+----------------+--------------------------+\n"
- "| 1 | %d | %.2lf \n" //add interest rate 1 and 2
- "| 2 | %d | %.2lf \n\n\n",
- account[user].acctNumber1, account[user].acctBalance1, account[user].acctNumber2, account[user].acctBalance2);
- }
- void createAccount(int *n) {
- *n = *n+1;
- int *inputInt = new int[1];
- std::cout << "Please Fill out the following information to create a new account" << std::endl;
- std::cout << "Full name: ";
- std::cin >> account[*n].acctName;
- std::cout << "Password: ";
- std::cin >> account[*n].acctPass;
- // get first account number
- std::cout << "First account number: ";
- std::cin >> *inputInt;
- while(*inputInt < 1000 || *inputInt > 9999) { // check none of the users already have this account number, if they do keep prompting
- std::cout << "Account number invalid. Must be between 1000 and 9999.\n"
- << "------------------------------------------------------\n"
- << "First account number: ";
- std::cin >> *inputInt;
- }
- account[*n].acctNumber1 = *inputInt; // valid (new) account number, add it to the user
- // get first account balance
- std::cout << "First account Balance: ";
- std::cin >> account[*n].acctBalance1;
- //check its between 0 and 100,000 else re ask
- //get first interest rate, check its between .01% and 15% else re ask
- // get second account number
- std::cout << "Second account number: ";
- std::cin >> *inputInt;
- while(*inputInt < 1000 || *inputInt > 8999) { // check none of the users already have this account number, if they do keep prompting
- std::cout << "Account number invalid. Must be between 1000 and 9999.\n"
- << "------------------------------------------------------\n"
- << "Second account number: ";
- std::cin >> *inputInt;
- }
- account[*n].acctNumber2 = *inputInt; // valid (new) account number, add it to the user
- //get second account balance
- std::cout << "Second account balance: ";
- std::cin >> account[*n].acctBalance2;
- //check its between 0 and 100,000 else re ask
- //get first interest rate, check its between .01% and 15% else re ask
- }
- void login(int *userId, int *loggedIn) {
- char *inputText = new char[256];
- std::cout << "Please enter username: ";
- std::cin >> inputText;
- for(int i = 0; i < 8999; i++) {
- if ( strcmp (account[i].acctName, inputText) == 0 ) {
- std::cout << "Welcome " << account[i].acctName << ", Please enter your password: " << std::endl; //move to logged in menu
- std::cin >> inputText;
- if ( strcmp (account[i].acctPass, inputText) == 0 ) {
- printSummary(i);
- std::cout << "Press [Enter] to continue" << std::endl;
- _getch();
- *loggedIn = 1;
- account[i].logInNumber++;
- *userId = i;
- }
- else {
- std::cout << "Incorrect password." << std::endl;
- break;
- }
- }
- }
- saveAccounts();
- }
- void transferMoney(int *userId) {
- double amount = 0.00;
- bool enoughFunds = false;
- std::cout << "Please enter amount to transer: ";
- std::cin >> amount;
- if(account[*userId].acctNumber1 == account[*userId].acctNumber2) {
- printf( " ^ +-----------+\n"
- " / ! \\ | Declined: | Account numbers are the same.\n"
- "/____ \\ +-----------+\n\n");
- }
- if(amount > account[*userId].acctBalance1) {
- printf( " ^ +-----------+\n"
- " / ! \\ | Declined: | Not enough founds in account number %d\n"
- "/____ \\ +-----------+\n\n",
- account[*userId].acctNumber1);
- } else { enoughFunds = true; }
- if(enoughFunds && (account[*userId].acctBalance1 - amount) < 10.00) {
- printf( "+----------+\n"
- "| Warning: | Account number %d now has a balance less than $10.00.\n"
- "+----------+\n\n",
- account[*userId].acctNumber1);
- account[*userId].acctBalance2 += amount;
- account[*userId].acctBalance1 -= amount;
- std::cout << "Press [Enter] to continue" << std::endl;
- enoughFunds = false; //prevent a second transfer happening
- }
- if(enoughFunds) {
- if((amount + account[*userId].acctBalance2) > 100000.00) {
- printf( "+----------+\n"
- "| Warning: | Account number %d will have a balance greater\n"
- "+----------+ than the federally insurable amount of\n"
- " $100,000.00 if you proceed.\n\n",
- account[*userId].acctNumber2);
- char yn = 'x';
- // loop until the input is a y or n
- while(yn != 'y' && yn != 'n') {
- std::cout << "Continue with transfer? Select [Y]es or [N]o" << std::endl;
- std::cin >> yn;
- }
- if(yn = 'n') {
- exit(0);
- }
- } else {
- // make the transfer
- account[*userId].acctBalance2 += amount;
- account[*userId].acctBalance1 -= amount;
- printf( "\n\n\n+--------------------+"
- "| Transfer complete. |"
- "+--------------------+\n\n\n");
- printSummary(*userId);
- }
- }
- std::cout << "Press [Enter] to continue" << std::endl;
- _getch();
- }
- void saveData() {//save all user account data to a csv file
- //ACCTNAME, ACCTNUMBER1, ACCTBALANCE1, ACCTINTEREST1, ACCTNUMBER2, ACCTBALANCE2, ACCT INTEREST2, PASSWORD,
- }
- void accountDeposit(int *userId) {
- int acctDepNum = 0;
- int acctDepAmt = 0;
- std::cout << "Please enter the account number you wish to deposit to" << std::endl
- << "1." << std::endl //user ACCTNUMBER 1
- << "2." << std::endl //user acctnumber 2
- << std::endl;
- std::cin >> acctDepNum;
- std::cout << "Please enter deposit amount" << std::endl;
- std::cin >> acctDepNum;
- if (acctDepNum == 1) {
- account[*userId].acctBalance1 += acctDepAmt;
- }
- if (acctDepNum == 2) {
- account[*userId].acctBalance2 += acctDepAmt;
- }
- saveAccounts();
- return; //return to logged in menu
- }
- void accountWithdraw(int *userId) {
- int acctWitNum = 0;
- int acctWitAmt = 0;
- std::cout<<"Please enter the account number you wish to deposit to"<<std::endl
- <<"1."<<std::endl //user ACCTNUMBER 1
- <<"2."<<std::endl //user acctnumber 2
- <<std::endl;
- std::cin >> acctWitNum;
- std::cout << "Please enter deposit amount"<< std::endl;
- std::cin >> acctWitNum;
- if(acctWitNum == 1) {
- account[*userId].acctBalance1 += acctWitAmt;
- }
- if(acctWitNum == 2) {
- account[*userId].acctBalance2 += acctWitAmt;
- }
- saveAccounts(); //run save data
- return;
- }
- void loggedInMenu(int *userId) {
- int loggedinmenuselect = 0;
- while(loggedinmenuselect == 0) { //show menu when logged in is complete.
- std::cout <<"Hello, acctname! Welcome to your account screen, Please select from the following menu: " <<std::endl;
- std::cout <<"1. Print account summary" <<std::endl;
- std::cout <<"2. Initiate Balance Transfer"<<std::endl;
- std::cout <<"3. Deposit to account"<<std::endl;
- std::cout <<"4. Withdraw from account"<<std::endl;
- std::cout <<"5. Calculate Interest/ Balance Forecast"<<std::endl;
- std::cout <<"6. Log out."<<std::endl;
- std::cin >> loggedinmenuselect;
- //1. account summary
- if(loggedinmenuselect ==1) {
- printSummary(*userId);
- }
- //2. initiate balance transfer
- if(loggedinmenuselect ==2) {
- transferMoney(userId);
- }
- //3. deposit to account
- if(loggedinmenuselect ==3) {
- printSummary(*userId);
- }
- //4. withdraw from account
- if(loggedinmenuselect ==4) {
- printSummary(*userId);
- }
- //5. balance forecast
- if(loggedinmenuselect ==5) {
- printSummary(*userId);
- }
- //6. exit
- if(loggedinmenuselect ==6) {
- //savedata
- //break; Break; Break;
- }
- }
- }
- int main()
- {
- time_t _time;
- struct tm timeinfo;
- time ( &_time );
- localtime_s (&timeinfo,&_time);
- // loops and menu initialisation
- int n = 0;
- int menuInput = 0;
- //top level menu
- int subMenuInput = 2; //logged in account level menu
- int loggedIn = 0;
- // user account details
- int inputInt = 0;
- int userId = 10000;
- std::cout << "Welcome to Interbanking Proprietary limited Super Squad" << std::endl; //Welcome Message
- //createAccount(&n);
- while(loggedIn == 0) // present the menu screen until the user logs in
- {
- std::cout << "Please select from the following menu" <<std::endl; //Welcome Menu
- std::cout << "1. Log in with existing bank account." <<std::endl;
- std::cout << "2. Create new bank account." <<std::endl;
- std::cin >> menuInput;
- if(menuInput == 1)
- {
- login(&n, &loggedIn); // end option 1
- }
- if(menuInput == 2)
- {
- createAccount(&n);
- menuInput = 0;
- }
- login(&userId, &loggedIn);
- } // end loggedIn loop
- } // end main
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement