Advertisement
Guest User

Untitled

a guest
Jan 21st, 2018
58
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 6.89 KB | None | 0 0
  1. #include <iostream>
  2. #include <fstream>
  3. #include <ctime>
  4. #include <string>
  5. #include <time.h>
  6. #include <cmath>
  7. #include <cstdlib>
  8.  
  9. using namespace std;
  10.  
  11. typedef struct {
  12.     string number = "";
  13.     string surname = "";
  14.     string firstname = "";
  15.     double maxDebit = 0;
  16.     int maxTransAmount = 0;
  17.     double balance = 0;
  18.     string dateUpdated = "";
  19.     string timeUpdated = "";
  20. } Account;
  21.  
  22. typedef struct {
  23.     string number;
  24.     double amount;
  25.     string date;
  26.     string time;
  27.     int ATMnumber;
  28. }Transaction;
  29.  
  30.  
  31. void create_account_file(Account* accounts, int count) {
  32.  
  33.     ofstream file("account.txt", ios::app);
  34.  
  35.     for (int i = 0; i < count; i++) {
  36.         file << accounts[i].number.c_str() << '\t' << accounts[i].surname.c_str() << '\t' << accounts[i].firstname.c_str() << '\t' << accounts[i].maxDebit << '\t'
  37.             << accounts[i].maxTransAmount << '\t' << accounts[i].balance << '\t' << accounts[i].dateUpdated.c_str() << '\t' << accounts[i].timeUpdated.c_str() << '\n';
  38.     }
  39.  
  40.     file.close();
  41.  
  42. }
  43.  
  44. void create_transaction_file(Transaction* transactions, int count) {
  45.  
  46.     ofstream file("transaction.txt", ios::app);
  47.  
  48.     for (int i = 0; i < count; i++) {
  49.         file << transactions[i].number << '\t' << transactions[i].amount << '\t' << transactions[i].date << '\t' << transactions[i].time << '\t' << transactions[i].ATMnumber << '\n';
  50.     }
  51.  
  52.     file.close();
  53. }
  54.  
  55. string get_date()
  56. {
  57.     time_t rawtime;
  58.     struct tm * timeinfo;
  59.     char buffer[80];
  60.  
  61.     time(&rawtime);
  62.     timeinfo = localtime(&rawtime);
  63.  
  64.     strftime(buffer, sizeof(buffer), "%d-%m-%Y", timeinfo);
  65.  
  66.     string str(buffer);
  67.  
  68.     return str;
  69. }
  70.  
  71. string get_time()
  72. {
  73.     time_t rawtime;
  74.     struct tm * timeinfo;
  75.     char buffer[80];
  76.  
  77.     time(&rawtime);
  78.     timeinfo = localtime(&rawtime);
  79.  
  80.     strftime(buffer, sizeof(buffer), "%I:%M:%S", timeinfo);
  81.  
  82.     string str(buffer);
  83.  
  84.     return str;
  85. }
  86.  
  87. void update_account(Account* account, Transaction* transaction){
  88.  
  89.     fstream transactions_file("transaction.txt",ios::in);
  90.     fstream accounts_file("account.txt",ios::in);
  91.     fstream temp_file("temp.txt",ios::out);
  92.     fstream debit("debety.txt",ios::out);
  93.     fstream limit("limity.txt",ios::out);
  94.  
  95.     while(
  96.         accounts_file>>account->number
  97.         >>account->surname
  98.         >>account->firstname
  99.         >>account->maxDebit
  100.         >>account->maxTransAmount
  101.         >>account->balance
  102.         >>account->dateUpdated
  103.         >>account->timeUpdated
  104.             )
  105.     {
  106.                 if (account->number == transaction->number){
  107.                     if (account->maxTransAmount < transaction->amount){
  108.             limit<<transaction->number<<"\t"
  109.             <<transaction->amount<<"\t"
  110.             <<transaction->date<<"\t"
  111.             <<transaction->time<<"\t"
  112.             <<transaction->ATMnumber<<"\n";
  113.         }      
  114.         else if (account->balance + account->maxDebit < transaction->amount){
  115.             debit<<transaction->number<<"\t"
  116.             <<transaction->amount<<"\t"
  117.             <<transaction->date<<"\t"
  118.             <<transaction->time<<"\t"
  119.             <<transaction->ATMnumber<<"\n";
  120.         }
  121.         else{
  122.             temp_file<<account->number<<"\t"
  123.             <<account->surname<<"\t"
  124.             <<account->firstname<<"\t"
  125.             <<account->maxDebit<<"\t"
  126.             <<account->maxTransAmount<<"\t"
  127.             <<(account->balance = account->balance - transaction->amount)<<"\t"
  128.             <<(account->dateUpdated = transaction->date)<<"\t"
  129.             <<(account->timeUpdated = transaction->time)<<"\n";
  130.         }
  131.         }
  132.         else{
  133.             temp_file<<account->number<<"\t"
  134.             <<account->surname<<"\t"
  135.             <<account->firstname<<"\t"
  136.             <<account->maxDebit<<"\t"
  137.             <<account->maxTransAmount<<"\t"
  138.             <<account->balance<<"\t"
  139.             <<account->dateUpdated<<"\t"
  140.             <<account->timeUpdated<<"\n";
  141.         }
  142.     }  
  143.             temp_file.close();
  144.             accounts_file.close();
  145.            
  146.             temp_file.open("temp.txt",ios::in);
  147.             accounts_file.open("account.txt",ios::out);
  148.            
  149.             while(
  150.             temp_file>>account->number
  151.             >>account->surname
  152.             >>account->firstname
  153.             >>account->maxDebit
  154.             >>account->maxTransAmount
  155.             >>account->balance
  156.             >>account->dateUpdated
  157.             >>account->timeUpdated)
  158.             {
  159.                 accounts_file<<account->number<<"\t";
  160.                 accounts_file<<account->surname<<"\t";
  161.                 accounts_file<<account->firstname<<"\t";
  162.                 accounts_file<<account->maxDebit<<"\t";
  163.                 accounts_file<<account->maxTransAmount<<"\t";
  164.                 accounts_file<<account->balance<<"\t";
  165.                 accounts_file<<account->dateUpdated<<"\t";
  166.                 accounts_file<<account->timeUpdated<<"\n";
  167.                
  168.             }
  169.            
  170.             transactions_file.close();
  171.             accounts_file.close();
  172.             temp_file.close();
  173.             debit.close();
  174.             limit.close();
  175. }
  176.  
  177.        
  178.  
  179. void create_an_account() {
  180.  
  181.     system("cls");
  182.  
  183.     Account* account = new Account;
  184.  
  185.     cout << "Account number: ";
  186.     cin >> account->number;
  187.     cout << "Surname: ";
  188.     cin >> account->surname;
  189.     cout << "First name: ";
  190.     cin >> account->firstname;
  191.     cout << "Maximal debit: ";
  192.     cin >> account->maxDebit;
  193.     cout << "Maximal transaction amount: ";
  194.     cin >> account->maxTransAmount;
  195.     cout << "Account balance: ";
  196.     cin >> account->balance;
  197.     account->dateUpdated = get_date();
  198.     account->timeUpdated = get_time();
  199.  
  200.     create_account_file(account, 1);
  201.  
  202.     delete account;
  203. }
  204.  
  205. void make_a_transaction() {
  206.  
  207.     system("cls");
  208.  
  209.     Transaction* transaction = new Transaction;
  210.     Account* account = new Account;
  211.  
  212.     cout << "Account number: ";
  213.     cin >> transaction->number;
  214.     cout << "Transaction amount: ";
  215.     cin >> transaction->amount;
  216.     transaction->date = get_date();
  217.     transaction->time = get_time();
  218.     cout << "ATM number: ";
  219.     cin >> transaction->ATMnumber;
  220.     system("Pause");
  221.  
  222.     create_transaction_file(transaction, 1);
  223.  
  224.     update_account(account, transaction);
  225.  
  226.     delete transaction;
  227. }
  228.  
  229.  
  230.  
  231. bool menu() {
  232.    
  233.     system("cls");
  234.  
  235.     cout << "MENU" << endl << "1. Create an account" << endl << "2. Make a transaction" << endl << "3. Exit" << endl;
  236.  
  237.     int option;
  238.  
  239.     cin >> option;
  240.  
  241.     switch (option) {
  242.     case 1: {
  243.         create_an_account();
  244.         break;
  245.     }
  246.     case 2: {
  247.         make_a_transaction();
  248.         break;
  249.     }
  250.     case 3: {
  251.         return true;
  252.         break;
  253.     }
  254.     }
  255.     return false;
  256.  
  257. }
  258.  
  259. int main() {
  260.  
  261.     while (menu() == false){  
  262.    
  263.     menu();
  264.     }
  265.  
  266.     return 0;
  267. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement