Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <string>
- #include <iostream>
- #include <vector>
- #include <Windows.h>
- #include <sstream>
- using namespace std;
- static const double INITIAL_BALANCE = 500;
- class User
- {
- public:
- int ID;
- string Password;
- double Balance;
- User(int id, string pass, double balance)
- {
- this->ID = id;
- this->Password = pass;
- this->Balance = balance;
- }
- void Withdraw(double x)
- {
- if (x < 0)
- throw exception("Withdrawal amount must be a positive number");
- if (x > this->Balance)
- throw exception("The account does not have enough balance");
- Balance -= x;
- }
- void Deposit(double x)
- {
- if (x > 500)
- throw exception("Too much money?");
- if (x < 0)
- throw exception("Deposit amount must be a positive number");
- Balance += x;
- }
- void TransferTo(User* target, double amount)
- {
- this->Withdraw(amount);
- target->Deposit(amount);
- }
- string GetName()
- {
- stringstream s;
- s << "User" << (this->ID + 1);
- return s.str();
- }
- };
- vector<User*> users;
- void FillByPass(string passwordList[], int num)
- {
- for (int i = 0; i < users.size(); i++)
- {
- delete users[i];
- }
- users.clear();
- for (int i = 0; i < num; i++)
- {
- users.push_back(new User(i, passwordList[i], INITIAL_BALANCE));
- }
- }
- int main()
- {
- string pass[] = {"password1", "password2", "password3"};
- FillByPass(pass, 3);
- cout << "=======================================" << endl;
- cout << "Welcome in our great banking system!" << endl;
- cout << "=======================================" << endl;
- ::Sleep(2000);
- while (true)
- {
- cout << "Choose account you want to manage!" << endl;
- for (int i = 0; i < users.size(); i++)
- {
- cout << "#" << users[i]->GetName() << " ";
- }
- cout << endl;
- bool authed = false;
- User* currentUser = NULL;
- do
- {
- cout << "Username: ";
- string s;
- cin >> s;
- int user = -1;
- for (int i = 0; i < users.size(); i++)
- {
- if (s == users[i]->GetName())
- {
- user = i;
- break;
- }
- }
- cout << "Password: ";
- cin >> s;
- if (user >= 0 && s == users[user]->Password)
- {
- authed = true;
- currentUser = users[user];
- cout << "You have been logged in as " << currentUser->GetName() << endl;
- }
- else
- {
- cout << "Invalid credentials" << endl;
- }
- }
- while (!authed);
- Sleep(2000);
- system("cls");
- bool stop = false;
- while (!stop)
- {
- double op = 0;
- int input;
- //double x; //operation amount
- cout << "These are options you are able to do on your account.\nWrite provided number to choose which one you want " << endl;
- cout << "1. Balance" << endl;
- cout << "2. Withdraw" << endl;
- cout << "3. Deposit" << endl;
- cout << "4. Transfer" << endl;
- cout << "5. Log out" << endl;
- cout << "6. Exit" << endl;
- cout << "Input: ";
- cin >> input;
- switch (input)
- {
- case 1:
- cout << "Your balance: " << currentUser->Balance << "$" << endl;
- getchar();
- break;
- case 2:
- cout << "How much money do you want to withdraw? ";
- cin >> op;
- try
- {
- currentUser->Withdraw(op);
- cout << "Money has been successfully withdrawn from your account" << endl;
- }
- catch (exception e)
- {
- cout << "Error: " << e.what() << endl;
- }
- getchar();
- break;
- case 3:
- cout << "How much money do you want to deposit? ";
- cin >> op;
- try
- {
- currentUser->Deposit(op);
- cout << "You have successfully deposited your money" << endl;
- }
- catch (exception e)
- {
- cout << "Error: " << e.what();
- }
- getchar();
- break;
- case 4:
- int id;
- cout << "Who do you want to trasfer money to? Use only the ID of the user. ";
- cin >> id;
- if (id <= 0 || id > users.size())
- {
- cout << "No such user" << endl;
- break;
- }
- cout << "How much money do you want to trasfer? ";
- cin >> op;
- try
- {
- currentUser->TransferTo(users[id - 1], op);
- cout << "You have successfully transferred the money" << endl;
- }
- catch (exception e)
- {
- cout << "Error: " << e.what();
- }
- getchar();
- break;
- case 5:
- stop = true;
- break;
- case 6:
- return 1;
- default:
- cout << "Error";
- getchar();
- break;
- }
- getchar();
- system("cls");
- }
- }
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement