Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- //
- //
- //Functions.cpp
- //
- //
- //
- #include "Functions.h"
- void ClearScreen()
- {
- HANDLE hStdOut;
- CONSOLE_SCREEN_BUFFER_INFO csbi;
- DWORD count;
- DWORD cellCount;
- COORD homeCoords = { 0, 0 };
- hStdOut = GetStdHandle( STD_OUTPUT_HANDLE );
- if (hStdOut == INVALID_HANDLE_VALUE) return;
- /* Get the number of cells in the current buffer */
- if (!GetConsoleScreenBufferInfo( hStdOut, &csbi )) return;
- cellCount = csbi.dwSize.X *csbi.dwSize.Y;
- /* Fill the entire buffer with spaces */
- if (!FillConsoleOutputCharacter(
- hStdOut,
- (TCHAR) ' ',
- cellCount,
- homeCoords,
- &count
- )) return;
- /* Fill the entire buffer with the current colors and attributes */
- if (!FillConsoleOutputAttribute(
- hStdOut,
- csbi.wAttributes,
- cellCount,
- homeCoords,
- &count
- )) return;
- /* Move the cursor home */
- SetConsoleCursorPosition( hStdOut, homeCoords );
- }
- void Swap(int &a, int&b)
- {
- a = a+b;
- b = a-b;
- a = a-b;
- }
- void Swap(char &a, char &b)
- {
- a ^= b ^= a ^= b;
- }
- int PressAnyKey( const char *prompt )
- {
- DWORD mode;
- HANDLE hstdin;
- INPUT_RECORD inrec;
- DWORD count;
- char default_prompt[] = "Press a key to continue...";
- /* Set the console mode to no-echo, raw input, */
- /* and no window or mouse events. */
- hstdin = GetStdHandle( STD_INPUT_HANDLE );
- if (hstdin == INVALID_HANDLE_VALUE
- || !GetConsoleMode( hstdin, &mode )
- || !SetConsoleMode( hstdin, 0 ))
- return 0;
- if (!prompt) prompt = default_prompt;
- /* Instruct the user */
- WriteConsole(
- GetStdHandle( STD_OUTPUT_HANDLE ),
- prompt,
- lstrlen( prompt ),
- &count,
- NULL
- );
- FlushConsoleInputBuffer( hstdin );
- /* Get a single key RELEASE */
- do ReadConsoleInput( hstdin, &inrec, 1, &count );
- while ((inrec.EventType != KEY_EVENT) || inrec.Event.KeyEvent.bKeyDown);
- /* Restore the original console mode */
- SetConsoleMode( hstdin, mode );
- return inrec.Event.KeyEvent.wVirtualKeyCode;
- }
- void DrawLine(string String, int Len)
- {
- int x = 0;
- cout << endl;
- for (int i = 0; i <Len; i++)
- {
- cout << String[x];
- if (x == (String.size()-1))
- x = -1;
- x++;
- }
- if (Len != 80)
- cout << endl;
- }
- int readkey()
- {
- HANDLE hstdin = GetStdHandle( STD_INPUT_HANDLE );
- DWORD mode;
- GetConsoleMode( hstdin, &mode );
- SetConsoleMode( hstdin, 0 );
- INPUT_RECORD inrec;
- DWORD count;
- do ReadConsoleInput( hstdin, &inrec, 1, &count );
- while ((inrec.EventType != KEY_EVENT) || inrec.Event.KeyEvent.bKeyDown);
- SetConsoleMode( hstdin, mode );
- return inrec.Event.KeyEvent.wVirtualKeyCode;
- }
- HANDLE Global::hStdin = GetStdHandle(STD_INPUT_HANDLE);
- HANDLE Global::hStdout = GetStdHandle(STD_OUTPUT_HANDLE);
- void SetColor(int a)
- {
- switch(a)
- {
- //
- //White & Gray
- case 0:
- SetConsoleTextAttribute (
- Global::hStdout,
- FOREGROUND_RED|FOREGROUND_BLUE|FOREGROUND_GREEN
- );
- break;
- case 1:
- SetConsoleTextAttribute (
- Global::hStdout,
- FOREGROUND_RED|FOREGROUND_BLUE|FOREGROUND_GREEN|FOREGROUND_INTENSITY
- );
- break;
- //
- //Red
- case 2:
- SetConsoleTextAttribute (
- Global::hStdout,
- FOREGROUND_RED
- );
- break;
- case 3:
- SetConsoleTextAttribute (
- Global::hStdout,
- FOREGROUND_RED|FOREGROUND_INTENSITY
- );
- break;
- //
- //Blue
- case 4:
- SetConsoleTextAttribute (
- Global::hStdout,
- FOREGROUND_BLUE
- );
- break;
- case 5:
- SetConsoleTextAttribute (
- Global::hStdout,
- FOREGROUND_BLUE|FOREGROUND_INTENSITY
- );
- break;
- //
- //Green
- case 6:
- SetConsoleTextAttribute (
- Global::hStdout,
- FOREGROUND_GREEN
- );
- break;
- case 7:
- SetConsoleTextAttribute (
- Global::hStdout,
- FOREGROUND_GREEN|FOREGROUND_INTENSITY
- );
- break;
- //
- //Cyan
- case 8:
- SetConsoleTextAttribute (
- Global::hStdout,
- FOREGROUND_BLUE|FOREGROUND_GREEN
- );
- break;
- case 9:
- SetConsoleTextAttribute (
- Global::hStdout,
- FOREGROUND_BLUE|FOREGROUND_GREEN|FOREGROUND_INTENSITY
- );
- break;
- //
- //Magenta
- case 10:
- SetConsoleTextAttribute (
- Global::hStdout,
- FOREGROUND_BLUE|FOREGROUND_RED
- );
- break;
- case 11:
- SetConsoleTextAttribute (
- Global::hStdout,
- FOREGROUND_BLUE|FOREGROUND_RED|FOREGROUND_INTENSITY
- );
- break;
- //
- //YELLOW
- case 12:
- SetConsoleTextAttribute (
- Global::hStdout,
- FOREGROUND_RED|FOREGROUND_GREEN
- );
- break;
- case 13:
- SetConsoleTextAttribute (
- Global::hStdout,
- FOREGROUND_RED|FOREGROUND_GREEN|FOREGROUND_INTENSITY
- );
- break;
- }
- }
- //
- //
- //Functions.h
- //
- //
- //
- #ifndef _FUNCTIONS_H_INCLUDED_
- #define _FUNCTIONS_H_INCLUDED_
- #include "default.h"
- static HANDLE hStdout, hStdin;
- void ClearScreen();
- void Swap(int&, int&);
- void Swap (char&, char&);
- int PressAnyKey(const char *prompt = "\n\nPress any key to continue . . .");
- void DrawLine(string String, int Len = 80);
- int readkey();
- void SetColor(int a = 1);
- struct Global
- {
- static HANDLE hStdin;
- static HANDLE hStdout;
- };
- #endif
- //
- //
- //BANK.cpp
- //
- //
- //
- #include "Functions.h"
- #include <Windows.h>
- double Round(double a)
- {
- while (int(a)%10 != 0)
- {
- a -= 1;
- }
- return a;
- }
- class Account_t
- {
- private:
- string AccNumber;
- long double iniBalance;
- long double Balance;
- long double initLoan;
- long double Loan;
- static int Interest;
- public:
- Account_t();
- Account_t Set(string accNumber, long int balance);
- string rAccNumber() {return AccNumber;}
- long double rBalance () {return Balance;}
- Account_t Deposit(long int);
- Account_t Withdraw(long int);
- Account_t TakeLoan();
- long double rIniLon() {return initLoan;}
- long double rLoan() {return Loan;}
- long double rInitialBalance() {return iniBalance;}
- Account_t CalcLoan();
- Account_t Repay();
- };
- int Account_t::Interest = 5;
- Account_t::Account_t()
- {
- AccNumber = "N/A";
- Balance = 0;
- Loan = 0;
- }
- Account_t Account_t::Set(string accNumber, long int balance)
- {
- AccNumber = accNumber;
- iniBalance = Balance = balance;
- return *this;
- }
- Account_t Account_t::Deposit(long int a)
- {
- Balance += a;
- return *this;
- }
- Account_t Account_t::Withdraw(long int a)
- {
- Balance -= a;
- return *this;
- }
- Account_t Account_t::TakeLoan()
- {
- if (int(Loan) == 0)
- {
- int a,b,c,d,s;
- a = rand()%7 * 1000;
- b = rand()%10 * 100;
- c = rand()%10 * 10;
- d = rand()%10;
- s = a+b+c+d;
- if (s > Balance)
- s = Balance*0.1;
- Loan = initLoan = s;
- Balance += initLoan;
- }
- return *this;
- }
- Account_t Account_t::CalcLoan()
- {
- Loan += ((Loan*Interest)/100);
- long double ratio;
- ratio = Loan/Balance;
- ratio /= 10;
- Loan -= ratio*Balance;
- Balance -= ratio*Balance;
- return *this;
- }
- Account_t Account_t::Repay()
- {
- int a,b,c,d,s = 0;
- int x = 0;
- double l = Loan;
- if (Loan > 0)
- {
- if ( Loan > 1000)
- {
- x = Loan/1000;
- a = rand()%x * 1000;
- s+=a;
- }
- if ( Loan > 100)
- {
- x = (Loan/100)- Round(l*0.01);
- if ( x != 0)
- {
- b = rand()%x * 100;
- s+= b;
- }
- }
- if ( Loan > 10)
- {
- x = Loan/10 - Round(l/10);
- if ( x != 0)
- {
- c = rand()%x * 10;
- s+=c;
- }
- }
- d = rand()%10;
- s+= d;
- if (s > Loan)
- s = Loan;
- }
- Loan -= s;
- Balance -= s;
- return *this;
- }
- class Customer
- {
- public:
- Account_t Account;
- Customer();
- Customer Set();
- };
- Customer customer[10];
- vector<int> Pre;
- bool Used(long a)
- {
- bool X = true;
- int x;
- for (int i = 0; i < Pre.size(); i++)
- {
- if (a != Pre[i])
- X = true;
- else if (a == Pre[i])
- {
- X = false;
- break;
- }
- }
- if (X)
- Pre.push_back(a);
- return X;
- }
- Customer::Customer()
- {
- long int a, b, c, d, s;
- a = (rand()%10) * 1000;
- b = (rand()%10) * 100;
- c = (rand()%10) * 10;
- d = (rand()%10);
- s = a+b+c+d;
- long w,x,y,z, h;
- w = (rand()%10) * 1000;
- x = (rand()%10) * 100;
- y = (rand()%10) * 10;
- z = (rand()%10);
- h = w+x+y+z;
- stringstream buffer;
- buffer << h;
- Used(h);
- Account.Set(buffer.str() , s);
- }
- Customer Customer::Set()
- {
- long int a, b, c, d, s;
- a = (rand()%10) * 1000;
- b = (rand()%10) * 100;
- c = (rand()%10) * 10;
- d = (rand()%10);
- s = a+b+c+d;
- long w,x,y,z, h;
- while (true)
- {
- w = (rand()%10) * 1000 +1;
- x = (rand()%10) * 100+1;
- y = (rand()%10) * 10;
- z = (rand()%10);
- h = w+x+y+z;
- if (Used(h))
- break;
- }
- string Str;
- stringstream buffer;
- buffer.setf(std::ios_base::hex, std::ios::basefield);
- buffer << h;
- Str = buffer.str();
- for (int i = 0; i < Str.size();i++)
- {
- if (isalpha(Str[i]))
- {
- Str[i] = toupper(Str[i]);
- }
- }
- Account.Set(Str , s);
- return *this;
- }
- long Amount(int i)
- {
- long int a, b, c, d, s;
- while(true)
- {
- a = (rand()%10) * 1000;
- b = (rand()%10) * 100;
- c = (rand()%10) * 10;
- d = (rand()%10);
- s = a+b+c+d;
- if (s < customer[i].Account.rBalance() && (customer[i].Account.rBalance()-s) > 500)
- break;
- else if ((customer[i].Account.rBalance()-s) > 500)
- {
- s = 0;
- break;
- }
- }
- return s;
- }
- int main ()
- {
- SetColor(1);
- Pre.push_back(0);
- srand(time(NULL));
- int chance;
- int Loan;
- long amount;
- int person ;
- vector<double> balances;
- for (int i = 0; i < 10; i++)
- {
- customer[i].Set();
- }
- for ( int i = 0; i < 600; i++)
- {
- for (int j = 0; j < 4; j++)
- {
- Loan = rand()%100;
- person = rand()%10;
- chance = rand()%100;
- amount = Amount(person);
- int repay = rand()%100;
- if (chance <= 35)
- customer[person].Account.Withdraw(amount);
- else if (chance > 65)
- customer[person].Account.Deposit(amount);
- if (Loan >= 25)
- customer[person].Account.TakeLoan();
- if (customer[person].Account.rLoan() > 0 && i%15 == 0)
- customer[person].Account.CalcLoan();
- if (repay > 65)
- customer[person].Account.Repay();
- for (int i = 0; i < 10; i++)
- {
- customer[i].Account.Deposit(customer[i].Account.rBalance()*0.01);
- }
- }
- /*
- for (int i = 0; i < 10; i++)
- {
- SetColor(2);
- cout << endl << "Customer " << i+1;
- DrawLine("-", 11);
- SetColor(2);
- cout << "\tAccount Number: " << customer[i].Account.rAccNumber() << endl;
- cout << "\tInitial Balance: " << customer[i].Account.rInitialBalance() << endl;
- cout << "\tBalance: " << customer[i].Account.rBalance() << endl;
- cout << "\tLoan: " << customer[i].Account.rLoan() << endl;
- SetColor(3);
- DrawLine("_");
- PressAnyKey("");
- }
- */
- cout << i << " "; //To see at what iteration does the loop end.
- }
- ClearScreen();
- long Bank_Balances = 0;
- for (int i = 0; i < 10; i++)
- {
- balances.push_back(customer[i].Account.rBalance());
- }
- for (int i = 0; i <balances.size();i++)
- {
- Bank_Balances += balances[i];
- }
- for (int i = 0; i < 10; i++)
- {
- SetColor(2);
- cout << endl << "Customer " << i+1;
- DrawLine("-", 11);
- SetColor(2);
- cout << "\tAccount Number: " << customer[i].Account.rAccNumber() << endl;
- cout << "\tInitial Balance: " << customer[i].Account.rInitialBalance() << endl;
- cout << "\tBalance: " << customer[i].Account.rBalance() << endl;
- cout << "\tLoan: " << customer[i].Account.rLoan() << endl;
- PressAnyKey("");
- SetColor(3);
- DrawLine("_");
- }
- cout << "\n\nBank Balance: " << Bank_Balances;
- PressAnyKey();
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment