Advertisement
Brandan

Untitled

Mar 13th, 2014
348
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 12.46 KB | None | 0 0
  1. class root {
  2.  
  3. private:
  4.     const std::string currentDateTime() {
  5.         time_t     now = time(0);
  6.         struct tm  tstruct;
  7.         char       buf[80];
  8.         tstruct = *localtime(&now);
  9.  
  10.         strftime(buf, sizeof(buf), "%Y-%m-%d.%X", &tstruct);
  11.  
  12.         return buf;
  13.     }
  14.     Account* accounts;
  15. protected:
  16.     int ARRAY_SIZE;
  17. public:
  18.     root(int SIZE);
  19.  
  20.     void initializeAccounts() {
  21.         accounts = new Account[ARRAY_SIZE];
  22.     }
  23.  
  24.     void unloadAccounts() {
  25.         delete this;
  26.     }
  27.  
  28.     // ====== BOSS ======
  29.     // Everything in the bank teller and customer
  30.     // List total number of employees in bank
  31.     // List total amount of money in the bank
  32.     // Total deposits in a day
  33.     // total withdrawls in a day
  34.     // Abillity to see detailed log of all transactions
  35.     // Sort databade (optional)
  36.     // Create money (optional)
  37.     // Reload DB (optional)
  38.     // Save DB (optional)
  39.  
  40.     void listEmployees() {
  41.         for (int i = 0; i <= ARRAY_SIZE - 1; i++) {
  42.             if (accounts[i].Permissions == Permissions::Employee) {
  43.                 if (accounts[i].account != -1) {
  44.                     cout
  45.                         << "=======================================" << endl
  46.                         << "Account Infomation: " << endl
  47.                         << "Account #: " << accounts[i].account << endl
  48.                         << "Username: " << accounts[i].username << endl;
  49.                 }
  50.             }
  51.         }
  52.     }
  53.  
  54.     // Not added to the options/command list!!!! <================================== <- Fixed
  55.     void listAdmin() {
  56.         for (int i = 0; i <= ARRAY_SIZE - 1; i++) {
  57.                 if (accounts[i].Permissions == Permissions::Root) {
  58.                 if (accounts[i].account != -1) {
  59.                     cout
  60.                         << "=======================================" << endl
  61.                         << "Account Infomation: " << endl
  62.                         << "Account #: " << accounts[i].account << endl
  63.                         << "Username: " << accounts[i].username << endl;
  64.                 }
  65.             }
  66.         }
  67.     }
  68.  
  69.     // Sorry about the CTRL + C CTRL + V code, I normally dont do this, just for time sake. Need to debug other things then worry about
  70.     // Fixing this code so its more robust.
  71.     void totalDeposits() { // <--- Needs work
  72.         string date;
  73.         string td = currentDateTime();
  74.         std::string delimiter = ".";
  75.  
  76.         size_t pos = 0;
  77.         std::string token;
  78.         while ((pos = td.find(delimiter)) != std::string::npos) {
  79.             token = td.substr(0, pos);
  80.             date = token;
  81.             break;
  82.             td.erase(0, pos + delimiter.length());
  83.         }
  84.  
  85.         string filename;
  86.         ifstream data;
  87.  
  88.         data.open("log_file.txt");
  89.  
  90.         // Exit on I/O error
  91.         if (!data.is_open()) {
  92.             cout << "Failed to open file, press enter to exit";
  93.             getchar();
  94.             getchar();
  95.         }
  96.  
  97.         double amount = 0;
  98.         while (!data.eof()) {
  99.             string date2 = "";
  100.             string timestamp = "";
  101.             string type;
  102.             double in = 0;
  103.             string dummy = "";
  104.  
  105.             data >> timestamp >> type >> dummy >> in;
  106.             td = timestamp;
  107.             data.ignore();
  108.  
  109.             while ((pos = td.find(delimiter)) != std::string::npos) {
  110.                 token = td.substr(0, pos);
  111.                 date2 = token;
  112.                 td.erase(0, pos + delimiter.length());
  113.             }
  114.             if (type == "Deposit") {
  115.                 if (date == date2) {
  116.                     amount += in;
  117.                 }
  118.             }
  119.         }
  120.  
  121.         cout << "Todays Total Deposits: " << amount << endl;
  122.     }
  123.  
  124.     void totalWithdrawl() { // <--- Needs Work
  125.         string date;
  126.         string td = currentDateTime();
  127.         std::string delimiter = ".";
  128.  
  129.         size_t pos = 0;
  130.         std::string token;
  131.         while ((pos = td.find(delimiter)) != std::string::npos) {
  132.             token = td.substr(0, pos);
  133.             date = token;
  134.             break;
  135.             td.erase(0, pos + delimiter.length());
  136.         }
  137.  
  138.         string filename;
  139.         ifstream data;
  140.  
  141.         data.open("log_file.txt");
  142.  
  143.         // Exit on I/O error
  144.         if (!data.is_open()) {
  145.             cout << "Failed to open file, press enter to exit";
  146.             getchar();
  147.             getchar();
  148.         }
  149.  
  150.         double amount = 0;
  151.         while (!data.eof()) {
  152.             string date2 = "";
  153.             string timestamp = "";
  154.             string type;
  155.             double in = 0;
  156.             string dummy = "";
  157.  
  158.  
  159.             while ((pos = td.find(delimiter)) != std::string::npos) {
  160.                 token = td.substr(0, pos);
  161.                 date2 = token;
  162.                 td.erase(0, pos + delimiter.length());
  163.             }
  164.  
  165.             data >> timestamp >> type >> dummy >> in;
  166.            
  167.             data.ignore();
  168.             td = timestamp;
  169.  
  170.            
  171.             while ((pos = td.find(delimiter)) != std::string::npos) {
  172.                 token = td.substr(0, pos);
  173.                 date2 = token;
  174.                 td.erase(0, pos + delimiter.length());
  175.             }
  176.             if (type == "Withdraw") {
  177.                 if (date == date2) {
  178.                     amount += in;
  179.                 }
  180.             }
  181.         }
  182.  
  183.         cout << "Todays Total Withdrawl: " << amount << endl;
  184.  
  185.     }
  186.  
  187.     void viewLog() {
  188.         ifstream myReadFile;
  189.         myReadFile.open("log_file.txt");
  190.         char output[100];
  191.         if (myReadFile.is_open()) {
  192.             while (!myReadFile.eof()) {
  193.                 myReadFile >> output;
  194.                 cout << output;
  195.             }
  196.         }
  197.         cout << endl;
  198.         myReadFile.close();
  199.     }
  200.  
  201.     void reloadDatabase() {
  202.        
  203.         // Clean DB
  204.         for (int i = 0; i <= ARRAY_SIZE - 1; i++) {
  205.                 // Clean up and remove user!
  206.                 accounts[i].account = -1;
  207.                 accounts[i].username = "";
  208.                 accounts[i].password = "";
  209.                 accounts[i].cash = 0;
  210.                 accounts[i].money = 0;
  211.                 accounts[i].type = AccountType::Checking;
  212.                 accounts[i].Permissions = Permissions::Customer;
  213.         }
  214.  
  215.  
  216.         string filename;
  217.         ifstream data;
  218.  
  219.         int account;
  220.         string username;
  221.         string password;
  222.         string firstname;
  223.         string lastname;
  224.         double cash;
  225.         double money;
  226.  
  227.         int type;
  228.         int permissions;
  229.  
  230.         data.open("members.db");
  231.  
  232.         // Exit on I/O error
  233.         if (!data.is_open()) {
  234.             cout << "Failed to open file...";
  235.             getchar();
  236.             getchar();
  237.         }
  238.  
  239.         int i = 0;
  240.  
  241.         while (!data.eof()){
  242.             data >> account >> username >> password >> firstname >> lastname >> cash >> money >> type >> permissions;
  243.             accounts[i].account = account;
  244.             accounts[i].username = username;
  245.             accounts[i].password = password;
  246.             accounts[i].firstName = firstname;
  247.             accounts[i].lastName = lastname;
  248.             accounts[i].cash = cash;
  249.             accounts[i].money = money;
  250.             accounts[i].type = static_cast<AccountType>(type);
  251.             accounts[i].Permissions = static_cast<Permissions>(permissions);
  252.             i++;
  253.         }
  254.  
  255.     }
  256.  
  257.     void saveDatabase() {
  258.         if (remove("members.db") != 0){
  259.             cout << "Error deleting file";
  260.         }
  261.         ofstream membersdb("members.db");
  262.         if (membersdb.is_open())
  263.         {
  264.             for (int i = 0; i <= ARRAY_SIZE - 1; i++) {
  265.                 if (accounts[i].account != -1) {
  266.                     if (i != 0) {
  267.                         membersdb << endl; // Duct tape over a real issue.
  268.                     }
  269.                    
  270.                     membersdb << accounts[i].account << " " << accounts[i].username << " " << accounts[i].password
  271.                         << " " << accounts[i].firstName << " " << accounts[i].lastName
  272.                         << " " << accounts[i].cash << " " << accounts[i].money << " " << accounts[i].type
  273.                         << " " << accounts[i].Permissions;
  274.                 }
  275.             }
  276.         }
  277.         membersdb.close();
  278.     }
  279.  
  280.     bool createMoney(double ammount, int customerid) {
  281.         if (customerid != -1) {
  282.             for (int i = 0; i <= ARRAY_SIZE - 1; i++) {
  283.                 if (accounts[i].account == customerid) {
  284.                     accounts[i].money = accounts[i].money + ammount;
  285.                     break;
  286.                 }
  287.             }
  288.         }
  289.         else {
  290.             return false;
  291.         }
  292.     }
  293.  
  294.     bool addCustomer(string username, string password, string firstname, string lastname, Permissions permissions, AccountType accounttype) { //AC
  295.         bool found = false;
  296.         int AccountID = -1;
  297.         /*
  298.         // Account # Algorithm (will break after 2000, unless the array size is changed)
  299.         for (int i = 0; i <= size - 1; i++) {
  300.         for (int b = 0; b <= size - 1; b++) {
  301.         if (accounts[i].account == b) {
  302.         found = true;
  303.         }
  304.         if (found == true) {
  305.         AccountID = b;
  306.         break;
  307.         }
  308.         }
  309.         }
  310.         */
  311.  
  312.         for (int i = 0; i <= ARRAY_SIZE - 1; i++) {
  313.             if (accounts[i].account == -1) {
  314.                 accounts[i].account = i + 1;
  315.                 accounts[i].username = username;
  316.                 accounts[i].password = password;
  317.                 accounts[i].firstName = firstname;
  318.                 accounts[i].lastName = lastname;
  319.                 accounts[i].type = accounttype;
  320.                 accounts[i].Permissions = permissions;
  321.                 cout << "Success! Account #: " << i + 1;
  322.                 return true;
  323.                 break;
  324.             }
  325.         }
  326.         return false;
  327.     }
  328.  
  329.     bool delCustomer(int customerid) { // DC
  330.         if (customerid != -1) {
  331.             for (int i = 0; i <= ARRAY_SIZE - 1; i++) {
  332.                 if (accounts[i].account == customerid) {
  333.  
  334.                     // Clean up and remove user!
  335.                     accounts[i].account = -1;
  336.                     accounts[i].username = "";
  337.                     accounts[i].password = "";
  338.                     accounts[i].cash = 0;
  339.                     accounts[i].money = 0;
  340.                     accounts[i].type = AccountType::Checking;
  341.                     accounts[i].Permissions = Permissions::Customer;
  342.  
  343.                     return true;
  344.                     break;
  345.                 }
  346.             }
  347.         }
  348.         return false;
  349.     }
  350.  
  351.     // Case Sensitive!
  352.     void search(string customername) { // search
  353.         for (int i = 0; i <= ARRAY_SIZE - 1; i++) {
  354.             if (accounts[i].firstName == customername) {
  355.                 cout << "Account Infomation: " << endl
  356.                     << "Account #: " << accounts[i].account << endl
  357.                     << "Username: " << accounts[i].username << endl
  358.                     << "Money (in checking): " << accounts[i].money << endl
  359.                     << "Account Type: " << accounts[i].type << endl
  360.                     << "Permissions: " << accounts[i].Permissions << endl;
  361.             }
  362.         }
  363.     }
  364.  
  365.     void balance(int customerid) { // <B>
  366.         if (customerid != -1) {
  367.             for (int i = 0; i <= ARRAY_SIZE - 1; i++) {
  368.                 if (customerid == accounts[i].account) {
  369.                     cout << "Current balance: " << accounts[i].money << endl;
  370.                 }
  371.             }
  372.         }
  373.     }
  374.  
  375.     bool depost(int customerid, double amount) { // <D>
  376.         if (customerid != -1) {
  377.             for (int i = 0; i <= ARRAY_SIZE - 1; i++) {
  378.                 if (customerid == accounts[i].account) {
  379.                     if (amount > accounts[i].cash) {
  380.                         cout << "You do not have enough cash to make this deposit!" << endl;
  381.                         return false;
  382.                     }
  383.                     else {
  384.                         accounts[i].money = accounts[i].money + amount;
  385.                         accounts[i].cash = accounts[i].cash - amount;
  386.                         cout << "Deposit Successful! Amount: " << amount << endl;
  387.                         return true;
  388.                     }
  389.                 }
  390.             }
  391.         }
  392.         cout << "Could not find account!" << endl;
  393.         return false;
  394.     }
  395.  
  396.     bool withdraw(int customerid, double amount) { // <W>
  397.         if (customerid != -1) {
  398.             for (int i = 0; i <= ARRAY_SIZE - 1; i++) {
  399.                 if (customerid == accounts[i].account) {
  400.                     if (amount > accounts[i].money) {
  401.                         cout << "You do not have enough money to make this withdrawl!" << endl;
  402.                         return false;
  403.                     }
  404.                     else {
  405.                         accounts[i].money = accounts[i].money - amount;
  406.                         accounts[i].cash = accounts[i].cash + amount;
  407.                         cout << "Withdraw Successful! Amount: " << amount << endl;
  408.                         return true;
  409.                     }
  410.                 }
  411.             }
  412.         }
  413.         cout << "Could not find account!" << endl;
  414.         return false;
  415.     }
  416.  
  417.     bool transfer(int customerid, double amount, int accountid) { // <W>
  418.         if (customerid != -1) {
  419.             int myaccount = -1;
  420.             // Find my account number (index).
  421.             for (int i = 0; i <= ARRAY_SIZE - 1; i++) {
  422.                 if (customerid == accounts[i].account) {
  423.                     myaccount = i;
  424.                 }
  425.             }
  426.  
  427.             if (myaccount == -1) {
  428.                 return false;
  429.             }
  430.  
  431.             // Find the account number of the transfer person.
  432.             for (int i = 0; i <= ARRAY_SIZE - 1; i++) {
  433.                 if (accountid == accounts[i].account) {
  434.                     if (accounts[myaccount].money < amount) { // < means less than, abit dyslexic when it comes to <> so I get it wrong, its the other way.
  435.                         return false;
  436.                     }
  437.                     else {
  438.                         accounts[myaccount].money = accounts[myaccount].money - amount;
  439.                         accounts[i].money = accounts[i].money + amount;
  440.                         cout << "Transfer Successful! Amount: " << amount << endl;
  441.                         return true;
  442.                     }
  443.                 }
  444.             }
  445.         }
  446.         cout << "Could not find account!" << endl;
  447.         return true;
  448.     }
  449.  
  450.     void customercash(int customerid) { // <CC>
  451.         if (customerid != -1) {
  452.             for (int i = 0; i <= ARRAY_SIZE - 1; i++) {
  453.                 if (accounts[i].account == customerid) {
  454.                     cout << "Your current cash is: " << accounts[i].cash << endl;
  455.                     break;
  456.                 }
  457.             }
  458.         }
  459.     }
  460.  
  461.     bool login(string username, string password) {
  462.  
  463.         for (int i = 0; i <= ARRAY_SIZE - 1; i++) {
  464.             // Case Sensitive ALL FEILDS!
  465.             if ((username == accounts[i].username) && (password == accounts[i].password)) {
  466.                 return true;
  467.             }
  468.  
  469.  
  470.         }
  471.         return false;
  472.     }
  473.  
  474.     int getAccount(string username) {
  475.         for (int i = 0; i <= ARRAY_SIZE - 1; i++) {
  476.             if ((username == accounts[i].username)) {
  477.                 return accounts[i].account;
  478.             }
  479.         }
  480.         return -1;
  481.     }
  482.  
  483.     int permission(int customerid) {
  484.         for (int i = 0; i <= ARRAY_SIZE - 1; i++) {
  485.             if ((customerid == accounts[i].account)) {
  486.                 return accounts[i].Permissions;
  487.             }
  488.         }
  489.         return -1;
  490.     }
  491.  
  492.     string permissionname(int permissionlvl) {
  493.         if (permissionlvl == Permissions::Customer) {
  494.             return "Customer";
  495.         }
  496.         else if (permissionlvl == Permissions::Employee) {
  497.             return "Employee";
  498.         }
  499.         else if (permissionlvl == Permissions::Root) {
  500.             return "Root/Administrator";
  501.         }
  502.         else {
  503.             return "Malicious User";
  504.         }
  505.     }
  506.    
  507.     ~root(void);
  508. };
  509.  
  510. root::root(int SIZE)     // constuctor
  511. {
  512.     ARRAY_SIZE = SIZE;
  513. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement