Advertisement
Guest User

Untitled

a guest
Nov 18th, 2017
416
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 22.43 KB | None | 0 0
  1. #include <iostream>
  2. #include <cstring>
  3. #include <fstream>
  4. #include <ctime>
  5. #include <ctype.h>
  6. #include <cstdlib>
  7. #include <conio.h>
  8. #include <time.h>
  9. #include <windows.h>
  10. #include <locale.h>
  11.  
  12. using namespace std;
  13.  
  14. enum
  15. {
  16.     CONST_NAME_CHECK,
  17.     CONST_PASSWORD_CHECK,
  18.     CONST_NUMBER_CHECK
  19. };
  20.  
  21. ifstream file;
  22. ofstream out_file;
  23.  
  24. struct Register
  25. {
  26.     char name[100], password[10];
  27.    
  28.     void writeIntoFile(bool isClient);
  29. };
  30.  
  31. struct FlightInfo
  32. {
  33.     int ID, ticket_price, total_tickets;
  34.     char company[100], plane[50], time[6], date[11], destiny[100];
  35.    
  36.     void bookFlight();
  37. };
  38.  
  39. void SetColor(int ForgC);
  40. void writeInColor(const char* phrase, int color);
  41. void clientMenu();
  42. void companyMenu();
  43. void Login(char name[], char password[], bool registered, bool client);
  44. void signUp();
  45. bool isStringValid(char str[], int check_type);
  46. void adjustName(char name[]);
  47. int getIndexesSize(char* line, char split_character);
  48. void getIndexesBySplittingSentence(char* line, char split_character, int j[]);
  49. int getNameOccurrences(char name[], bool isClient);
  50. void fillSentence(char line[], int begin, int end, char target[]);
  51. void toLower(char w[]);
  52. int getYesOrNo();
  53. bool checkCombination(char login[], char password[], bool isClient);
  54. void clientLogin(char login[], char password[]);
  55. void companyLogin(char login[], char password[]);
  56. int getFileLineCount(const char* file_name);
  57. void bookFlight(char name[]);
  58.  
  59. int main()
  60. {
  61.     bool _time = true, program_running = true;
  62.     setlocale(LC_ALL, "Portuguese");
  63.     system("color F0");
  64.    
  65.     cout << "-----------------------------------------------------------------------" << endl;
  66.     cout << "************** BEM VINDO AO SISTEMA DE CONTROLE DE AEROPORTOS **************" << endl;
  67.     cout << "-----------------------------------------------------------------------" << endl << endl;
  68.    
  69.     while(program_running)
  70.     {
  71.         if(!_time) system("CLS");
  72.        
  73.         char action[10];
  74.         cout << "-----------------------------------------------------------------------" << endl;
  75.         cout << "          ************* VOCÊ DESEJA *************" << endl << endl;
  76.        
  77.         cout << "       -> ";
  78.         writeInColor("entrar", 9);
  79.         cout << endl << "       -> ";
  80.         writeInColor("registrar", 9);
  81.         cout << endl << "       -> ";
  82.         writeInColor("sair", 4);
  83.         cout << endl << endl << "-----------------------------------------------------------------------" << endl << endl;
  84.        
  85.         _time = false;
  86.        
  87.         cin.getline(action, 10);
  88.        
  89.         toLower(action);
  90.        
  91.         system("CLS");
  92.        
  93.         char login[100], password[100];
  94.        
  95.         if(strcmp(action, "entrar") == 0)
  96.             Login(login, password, false, false);
  97.         else if(strcmp(action, "registrar") == 0)
  98.             signUp();
  99.         else if(strcmp(action, "sair") == 0) program_running = false;
  100.     }
  101. }
  102.  
  103. void SetColor(int ForgC)
  104. {
  105.     WORD wColor;
  106.    
  107.     HANDLE hStdOut = GetStdHandle(STD_OUTPUT_HANDLE);
  108.     CONSOLE_SCREEN_BUFFER_INFO csbi;
  109.    
  110.     if(GetConsoleScreenBufferInfo(hStdOut, &csbi))
  111.     {
  112.         wColor = (csbi.wAttributes & 0xF0) + (ForgC & 0x0F);
  113.         SetConsoleTextAttribute(hStdOut, wColor);
  114.     }
  115.    
  116.     return;
  117. }
  118.  
  119. void writeInColor(const char* phrase, int color)
  120. {
  121.     SetColor(color);
  122.     cout << phrase;
  123.     SetColor(0);
  124. }
  125.  
  126. bool isStringValid(char str[], int check_type) 
  127. {
  128.     if(check_type == CONST_NAME_CHECK)
  129.     {
  130.         bool check_name_logic = false;
  131.        
  132.         if(strlen(str) < 3)
  133.         {
  134.             cout << "-> Nome muito curto!" << endl;
  135.             return false;
  136.         }
  137.        
  138.         for(unsigned int i = 0; i < strlen(str); i++)
  139.         {
  140.             if(isalpha(str[i]))
  141.                 check_name_logic = true;
  142.             else if(!isspace(str[i])) return false;
  143.         }
  144.        
  145.         if(!check_name_logic) return false;
  146.     }
  147.     else if(check_type == CONST_NUMBER_CHECK)
  148.     {
  149.         for(unsigned int i = 0; i < strlen(str); i++)
  150.             if(!isdigit(str[i]))
  151.                 return false;
  152.     }  
  153.     else
  154.     {
  155.         unsigned int max = 10;
  156.    
  157.         if(strlen(str) < 4 || strlen(str) > max) return false;
  158.        
  159.         for(unsigned int i = 0; i < strlen(str); i++)
  160.             if(!isalpha(str[i]) && !isdigit(str[i]))
  161.                 return false;
  162.     }
  163.        
  164.     return true;
  165. }
  166.  
  167. void signUp()
  168. {
  169.     bool continue_loop = true;
  170.    
  171.     while(continue_loop)
  172.     {
  173.         bool isClient, wrongAnswer = false;
  174.        
  175.         if(wrongAnswer)
  176.         {
  177.             writeInColor("          OPERAÇÃO INVÁLIDA", 4);
  178.             cout << endl << endl;
  179.         }
  180.            
  181.         char action[10];
  182.         cout << "-----------------------------------------------------------------------" << endl;
  183.         cout << "          ************* VOCÊ É *************" << endl << endl;
  184.        
  185.         cout << "       -> ";
  186.         writeInColor("cliente", 9);
  187.         cout << endl << "       -> ";
  188.         writeInColor("companhia", 9);
  189.         cout << endl << "       -> ";
  190.         writeInColor("voltar", 4);
  191.         cout << endl << endl << "-----------------------------------------------------------------------" << endl << endl;
  192.        
  193.         cin.getline(action, 10);
  194.            
  195.         toLower(action);
  196.        
  197.         system("CLS");
  198.        
  199.         if(strcmp(action, "cliente") == 0)
  200.         {
  201.             isClient = true;
  202.             continue_loop = false;
  203.         }
  204.         else if(strcmp(action, "companhia") == 0)
  205.         {
  206.             isClient = false;
  207.             continue_loop = false;
  208.         }
  209.         else if(strcmp(action, "voltar") == 0) return;
  210.         else
  211.             wrongAnswer = true;
  212.        
  213.         system("CLS");
  214.        
  215.         Register info;
  216.        
  217.         if(!wrongAnswer)
  218.         {
  219.             bool check_password = true, repeated_name = false;
  220.  
  221.             do
  222.             {
  223.                 bool check_name = true;
  224.                
  225.                 if(repeated_name)
  226.                     cout << " Nome repetido." << endl << endl;
  227.                
  228.                 do
  229.                 {
  230.                     if(!check_name)
  231.                         cout << "Nome inválido." << endl << endl;
  232.                    
  233.                     if(isClient)
  234.                         writeInColor(" Nome", 9);
  235.                     else
  236.                         writeInColor(" Nome da companhia", 9);
  237.                    
  238.                     cout << ": ";
  239.                    
  240.                     cin.getline(info.name, 100);
  241.                     check_name = false;
  242.                     system("CLS");
  243.                 } while(!isStringValid(info.name, CONST_NAME_CHECK));
  244.                
  245.                 adjustName(info.name);
  246.                 repeated_name = true;
  247.             } while(getNameOccurrences(info.name, isClient) > 0);
  248.                
  249.    
  250.             do
  251.             {
  252.                 if(!check_password)
  253.                 {
  254.                     cout << "Senha inválida." << endl;
  255.                     cout << "MIN. 4 MÁX. 10 caracteres. APENAS LETRAS E NÚMEROS!" << endl << endl;
  256.                 }
  257.                
  258.                 int count = 0;
  259.                
  260.                 writeInColor(" Senha", 9);
  261.                 cout << ": ";
  262.        
  263.                 while(count <= 10)
  264.                 {      
  265.                     if(count == 10) {info.password[10] = '\0'; break;}
  266.                    
  267.                     info.password[count] = _getch();
  268.            
  269.                     if(info.password[count] == '\n' || info.password[count] == '\r')
  270.                     {
  271.                         info.password[count] = '\0';
  272.                         break;
  273.                     }
  274.                    
  275.                     cout << "*";
  276.                     count++;
  277.                 }
  278.                
  279.                 check_password = false;
  280.                 system("CLS");
  281.             } while(!isStringValid(info.password, CONST_PASSWORD_CHECK));
  282.            
  283.             cout << "  -> Nome: " << info.name << endl;
  284.             cout << "  -> Senha: " << info.password << endl << endl;
  285.            
  286.             cout << "-> Pressione qualquer tecla para finalizar o registro." << endl;
  287.            
  288.             getch();
  289.            
  290.             info.writeIntoFile(isClient);
  291.            
  292.             Login(info.name, info.password, true, isClient);
  293.         }
  294.     }
  295. }
  296.  
  297. int getYesOrNo()
  298. {
  299.     int ret;
  300.    
  301.     cout << "  (";
  302.     writeInColor("0", 9);
  303.     cout << ") - sim" << endl;
  304.     cout << "  (";
  305.     writeInColor("1", 9);
  306.     cout << ") - nao" << endl;
  307.    
  308.     do
  309.     {
  310.         cin >> ret;
  311.         system("CLS");
  312.     } while(ret != 0 && ret != 1);
  313.    
  314.     return ret;
  315. }
  316.  
  317. void Login(char name[], char password[], bool registered, bool client)
  318. {
  319.     bool isClient;
  320.    
  321.     if(registered)
  322.         isClient = client;
  323.     else
  324.     {
  325.         char action[10];
  326.         cout << "-----------------------------------------------------------------------" << endl;
  327.         cout << "          ************* VOCÊ É *************" << endl << endl;
  328.        
  329.         cout << "       -> ";
  330.         writeInColor("cliente", 9);
  331.         cout << endl << "       -> ";
  332.         writeInColor("companhia", 9);
  333.         cout << endl << endl << "-----------------------------------------------------------------------" << endl << endl;
  334.        
  335.         cin.getline(action, 10);
  336.            
  337.         toLower(action);
  338.        
  339.         system("CLS");
  340.        
  341.         if(strcmp(action, "cliente") == 0)
  342.             isClient = true;
  343.         else if(strcmp(action, "companhia") == 0)
  344.             isClient = false;
  345.         else return;   
  346.     }
  347.    
  348.     if(!registered)
  349.     {
  350.         bool check_login = true;
  351.         char login[100], _password[11];
  352.        
  353.         do
  354.         {  
  355.             if(!check_login)
  356.             {
  357.                 cout << " Combinação inválida. (login inexistente ou senha incorreta)" << endl << endl;
  358.                 cout << "-> Pressione qualquer tecla para continuar." << endl;
  359.                 getch();
  360.                
  361.                 return;
  362.             }
  363.            
  364.             cout << "-----------------------------" << endl;
  365.             cout << "********** LOGANDO **********" << endl;
  366.             cout << "-----------------------------" << endl << endl;
  367.            
  368.             writeInColor(" Nome", 9);
  369.             cout << ": ";
  370.        
  371.             cin.getline(login, 100);
  372.            
  373.             writeInColor(" Senha", 9);
  374.             cout << ": ";
  375.    
  376.             int count = 0;
  377.             while(count <= 10)
  378.             {      
  379.                 if(count == 10) {_password[10] = '\0'; break;}
  380.                
  381.                 _password[count] = _getch();
  382.        
  383.                 if(_password[count] == '\n' || _password[count] == '\r')
  384.                 {
  385.                     _password[count] = '\0';
  386.                     break;
  387.                 }
  388.                
  389.                 cout << "*";
  390.                 count++;
  391.             }
  392.            
  393.             system("CLS");
  394.             check_login = false;
  395.         } while(!checkCombination(login, _password, isClient));
  396.        
  397.         strcpy(name, login);
  398.         strcpy(password, _password);
  399.        
  400.     }
  401.    
  402.     if(isClient)
  403.         clientLogin(name, password);
  404.     else
  405.         companyLogin(name, password);
  406. }
  407.  
  408. void clientLogin(char login[], char password[])
  409. {
  410.     //compra passagem
  411.     //busca de horarios
  412.     //avisos previos*
  413.     //checagem de voos (q ele comprou)
  414.     //desistencia
  415.    
  416.     system("CLS");
  417.    
  418.     bool _time = true, loop_running = true;
  419.     cout << "-----------------------------------------------------------------------" << endl;
  420.     cout << "************** CLIENTE **************" << endl;
  421.     cout << "-----------------------------------------------------------------------" << endl << endl;
  422.    
  423.     while(loop_running)
  424.     {
  425.         if(!_time) system("CLS");
  426.        
  427.         char action[9];
  428.         cout << "-----------------------------------------------------------------------" << endl;
  429.         cout << "          ************* VOCÊ DESEJA *************" << endl << endl;
  430.        
  431.         cout << "       -> ";
  432.         writeInColor("comprar ", 9);
  433.         cout << "passagems" << endl;
  434.         cout << "       -> ";
  435.        
  436.         writeInColor("buscar ", 9);
  437.         cout << "horários de voos" << endl;
  438.        
  439.         cout << "       -> ";
  440.         writeInColor("checar ", 9);
  441.         cout << "seus voos" << endl;
  442.        
  443.         cout << "       -> ";
  444.         writeInColor("desistir ", 9);
  445.         cout << "de algum voo" << endl;
  446.        
  447.         cout << "       -> ";
  448.         writeInColor("sair", 4);
  449.         cout << endl << endl << "-----------------------------------------------------------------------" << endl << endl;
  450.        
  451.         _time = false;
  452.        
  453.         cin.getline(action, 9);
  454.        
  455.         toLower(action);
  456.        
  457.         system("CLS");
  458.    
  459.         /*if(strcmp(action, "comprar") == 0)
  460.             //buyTickets(login);
  461.         else if(strcmp(action, "buscar") == 0)
  462.             //searchFlights();
  463.         else if(strcmp(action, "checar") == 0)
  464.             //checkFlights(login, true);
  465.         else if(strcmp(action, "desistir") == 0)
  466.             //giveUpOnFlight(login);*/
  467.         /*else*/ if(strcmp(action, "sair") == 0) loop_running = false;
  468.     }
  469. }
  470.  
  471. void companyLogin(char login[], char password[])
  472. {
  473.     //criação de voos
  474.     //voos da agencia
  475.     //cancelamento de voos
  476.    
  477.     system("CLS");
  478.    
  479.     bool _time = true, loop_running = true;
  480.     cout << "-----------------------------------------------------------------------" << endl;
  481.     cout << "************** COMPANHIA **************" << endl;
  482.     cout << "-----------------------------------------------------------------------" << endl << endl;
  483.    
  484.     while(loop_running)
  485.     {
  486.         if(!_time) system("CLS");
  487.        
  488.         char action[12];
  489.         cout << "-----------------------------------------------------------------------" << endl;
  490.         cout << "          ************* VOCÊ DESEJA *************" << endl << endl;
  491.        
  492.         cout << "       -> ";
  493.         writeInColor("agendar ", 9);
  494.         cout << "um voo" << endl;
  495.         cout << "       -> ";
  496.        
  497.         writeInColor("voos ", 9);
  498.         cout << "agendados da companhia" << endl;
  499.        
  500.         cout << "       -> ";
  501.         writeInColor("cancelamento ", 9);
  502.         cout << "de voos" << endl;
  503.        
  504.         cout << "       -> ";
  505.         writeInColor("sair", 4);
  506.         cout << endl << endl << "-----------------------------------------------------------------------" << endl << endl;
  507.        
  508.         _time = false;
  509.        
  510.         cin.getline(action, 12);
  511.        
  512.         toLower(action);
  513.        
  514.         system("CLS");
  515.    
  516.         if(strcmp(action, "agendar") == 0)
  517.             bookFlight(login);
  518.         /*else if(strcmp(action, "voos") == 0)
  519.             //checkFlights(login, false);
  520.         else if(strcmp(action, "cancelamento") == 0)
  521.             //cancelFlight(login);*/
  522.         /*else*/ if(strcmp(action, "sair") == 0) loop_running = false;
  523.     }  
  524. }
  525.  
  526. void bookFlight(char name[])
  527. {
  528.     FlightInfo flight;
  529.     int ticket_price, count = 0;
  530.     char plane[50], date[11], hour[6], destiny[100];
  531.     bool wrong_answer = false;
  532.    
  533.     time_t t = time(NULL);
  534.     tm* timePtr = localtime(&t);
  535.    
  536.     cout << "* Aviões disponíveis *" << endl;
  537.     cout << " -> ";
  538.     writeInColor("Airbus A318 ", 9);
  539.     cout << "(5.7mil km) (120 passageiros)" << endl;
  540.    
  541.     cout << " -> ";
  542.     writeInColor("Airbus A319 ", 9);
  543.     cout << "(7.2mil km) (132 passageiros)" << endl;
  544.    
  545.     cout << " -> ";
  546.     writeInColor("Airbus A320 ", 9);
  547.     cout << "(5.7mil km) (162 passageiros)" << endl;
  548.    
  549.     cout << " -> ";
  550.     writeInColor("ATR 42-500 ", 9);
  551.     cout << "(1.5mil km) (48 passageiros)" << endl << endl;
  552.    
  553.     cout << " Seu avião de escolha: ";
  554.    
  555.     cin.getline(plane, 50);
  556.    
  557.     adjustName(plane);
  558.    
  559.     system("CLS");
  560.    
  561.     if(strcmp(plane, "Airbus A318") == 0)
  562.         flight.total_tickets = 120;
  563.     else if(strcmp(plane, "Airbus A319") == 0)
  564.         flight.total_tickets = 132;
  565.     else if(strcmp(plane, "Airbus A320") == 0)
  566.         flight.total_tickets = 162;
  567.     else if(strcmp(plane, "ATR 42-500") == 0)
  568.         flight.total_tickets = 48;
  569.     else
  570.     {
  571.         cout << " Modelo de avião inválido." << endl << endl;
  572.         cout << "-> Pressione qualquer tecla para continuar." << endl;
  573.         getch();
  574.        
  575.         return;
  576.     }
  577.    
  578.     strcpy(flight.company, name);
  579.    
  580.     strcpy(flight.plane, plane);
  581.     flight.ID = getFileLineCount("flight.txt") + 1;
  582.    
  583.     do
  584.     {
  585.         if(wrong_answer)
  586.             cout << " -> Valor inválido." << endl;
  587.        
  588.         cout << " Preço da passagem: ";
  589.        
  590.         cin >> ticket_price;
  591.         wrong_answer = true;
  592.        
  593.         system("CLS");
  594.     } while(ticket_price < 0);
  595.    
  596.     flight.ticket_price = ticket_price;
  597.    
  598.     cout << " Data do voo: ";
  599.    
  600.     while(count < 10)
  601.     {
  602.         date[count] = _getch();
  603.        
  604.         cout << date[count];
  605.        
  606.         if(date[count] == '\n' || date[count] == '\r')
  607.         {
  608.             date[count] = '\0';
  609.             break;
  610.         }
  611.        
  612.         if(count == 1 || count == 4)
  613.         {
  614.             date[++count] = '/';
  615.             cout << "/";
  616.         }
  617.        
  618.         count++;
  619.        
  620.         if(count == 10) date[10] = '\0';
  621.     }
  622.  
  623.     system("CLS");
  624.  
  625.     if(strlen(date) != 10)
  626.     {
  627.         cout << " Data inválida." << endl << endl;
  628.         cout << "-> Pressione qualquer tecla para continuar." << endl;
  629.         getch();
  630.        
  631.         return;
  632.     }
  633.  
  634.     int g[getIndexesSize(date, '/')], day, month, year;
  635.     int day_now = timePtr->tm_mday, month_now = timePtr->tm_mon + 1, year_now = timePtr->tm_year + 1900;
  636.     getIndexesBySplittingSentence(date, '/', g);
  637.            
  638.     char day_str[3], mon_str[3], year_str[5];
  639.    
  640.     fillSentence(date, -1, g[0], day_str);
  641.     fillSentence(date, g[0], g[1], mon_str);
  642.     fillSentence(date, g[1], strlen(date), year_str);
  643.    
  644.     if(!isStringValid(day_str, CONST_NUMBER_CHECK) || !isStringValid(mon_str, CONST_NUMBER_CHECK) || !isStringValid(year_str, CONST_NUMBER_CHECK))
  645.     {
  646.         cout << " Data inválida." << endl << endl;
  647.         cout << "-> Pressione qualquer tecla para continuar." << endl;
  648.         getch();
  649.        
  650.         return;
  651.     }
  652.    
  653.     day = atoi(day_str);
  654.     month = atoi(mon_str);
  655.     year = atoi(year_str);
  656.    
  657.     if(year < year_now || year <= 0)
  658.     {
  659.         cout << " Data inválida." << endl << endl;
  660.         cout << "-> Pressione qualquer tecla para continuar." << endl;
  661.         getch();
  662.        
  663.         return;
  664.     }
  665.     else if(year == year_now)
  666.     {
  667.         if(month < month_now || month <= 0 || month > 12)
  668.         {
  669.             cout << " Data inválida." << endl << endl;
  670.             cout << "-> Pressione qualquer tecla para continuar." << endl;
  671.             getch();
  672.            
  673.             return;
  674.         }
  675.         else if(month == month_now)
  676.             if(day < day_now || day <= 0 || day > 31 || (month == 2 && day > 27))
  677.             {
  678.                 cout << " Data inválida." << endl << endl;
  679.                 cout << "-> Pressione qualquer tecla para continuar." << endl;
  680.                 getch();
  681.                
  682.                 return;
  683.             }
  684.     }
  685.    
  686.     strcpy(flight.date, date);
  687.    
  688.     count = 0;
  689.    
  690.     cout << " Horário do voo: ";
  691.    
  692.     while(count < 5)
  693.     {
  694.         hour[count] = _getch();
  695.        
  696.         cout << hour[count];
  697.        
  698.         if(hour[count] == '\n' || hour[count] == '\r')
  699.         {
  700.             hour[count] = '\0';
  701.             break;
  702.         }
  703.        
  704.         if(count == 1)
  705.         {
  706.             hour[++count] = ':';
  707.             cout << ":";
  708.         }
  709.        
  710.         count++;
  711.        
  712.         if(count == 5) hour[5] = '\0';
  713.     }
  714.  
  715.     system("CLS");
  716.    
  717.     int i[getIndexesSize(hour, ':')], hr, min;
  718.    
  719.     getIndexesBySplittingSentence(hour, ':', i);
  720.            
  721.     char hr_str[3], min_str[3];
  722.    
  723.     fillSentence(hour, -1, i[0], hr_str);
  724.     fillSentence(hour, i[0], strlen(hour), min_str);
  725.    
  726.     if(!isStringValid(hr_str, CONST_NUMBER_CHECK) || !isStringValid(min_str, CONST_NUMBER_CHECK))
  727.     {
  728.         cout << "aqui" << endl;
  729.         cout << " Hora inválida." << endl << endl;
  730.         cout << "-> Pressione qualquer tecla para continuar." << endl;
  731.         getch();
  732.        
  733.         return;
  734.     }
  735.    
  736.     hr = atoi(hr_str);
  737.     min = atoi(min_str);
  738.    
  739.     if(hr > 23 || hr < 0)
  740.     {
  741.         cout << " Hora inválida." << endl << endl;
  742.         cout << "-> Pressione qualquer tecla para continuar." << endl;
  743.         getch();
  744.        
  745.         return;
  746.     }
  747.     else if(min > 59 || min < 0)
  748.     {
  749.         cout << " Hora inválida." << endl << endl;
  750.         cout << "-> Pressione qualquer tecla para continuar." << endl;
  751.         getch();
  752.        
  753.         return;
  754.     }
  755.    
  756.     strcpy(flight.time, hour);
  757.    
  758.     cin.ignore();
  759.    
  760.     cout << " Destino: ";
  761.    
  762.     cin.getline(destiny, 100);
  763.    
  764.     if(!isStringValid(destiny, CONST_NAME_CHECK))
  765.     {
  766.         cout << " Destino inválido." << endl << endl;
  767.         cout << "-> Pressione qualquer tecla para continuar." << endl;
  768.         getch();
  769.        
  770.         return;
  771.     }
  772.    
  773.     strcpy(flight.destiny, destiny);
  774.    
  775.     system("CLS");
  776.    
  777.     flight.bookFlight();
  778.     cout << "  -> Voo agendado!" << endl << endl;
  779.     cout << "Pressione qualquer tecla para continuar." << endl;
  780.     getch();
  781.    
  782. }
  783.  
  784. void FlightInfo::bookFlight()
  785. {  
  786.     out_file.open("flight.txt", ios_base::app | ios_base::out);
  787.    
  788.     out_file << "ID:|" << this->ID << "|";
  789.     out_file << "Empresa:|" << this->company << "|";
  790.     out_file << "Avião:|" << this->plane << "|";
  791.     out_file << "Preço:|" << this->ticket_price << "|";
  792.     out_file << "Total:|0/" << this->total_tickets << "|";
  793.     out_file << "Hora:|" << this->time << "|";
  794.     out_file << "Data:|" << this->date << "|";
  795.     out_file << "Destino:|" << this->destiny << "|" << endl;
  796.    
  797.     out_file.close();
  798. }
  799.  
  800.  
  801. int getFileLineCount(const char* file_name)
  802. {
  803.     file.open(file_name);
  804.     int count = 0;
  805.     char line[2000];
  806.    
  807.     while(file.getline(line, 2000)) count++;
  808.     file.close();
  809.    
  810.     return count;
  811. }
  812.  
  813. bool checkCombination(char login[], char password[], bool isClient)
  814. {
  815.     bool ret = false;
  816.    
  817.     ifstream file_now;
  818.    
  819.     if(isClient)
  820.         file_now.open("client.txt");
  821.     else
  822.         file_now.open("company.txt");
  823.    
  824.     char line[2000];
  825.    
  826.     while(file_now.getline(line, 2000))
  827.     {  
  828.         int i[getIndexesSize(line, '/')];
  829.         getIndexesBySplittingSentence(line, '/', i);
  830.        
  831.         int login_size = i[1] - i[0] - 1, password_size = i[3] - i[2] - 1;
  832.         char log[login_size], pass[password_size];
  833.        
  834.         fillSentence(line, i[0], i[1], log);
  835.         fillSentence(line, i[2], i[3], pass);
  836.        
  837.         if(strcmp(login, log) == 0 && strcmp(password, pass) == 0)
  838.         {
  839.             ret = true;
  840.             break;
  841.         }
  842.     }
  843.    
  844.     file_now.close();
  845.    
  846.     return ret;
  847. }
  848.  
  849. void adjustName(char name[])
  850. {
  851.     int size = getIndexesSize(name, ' ');
  852.     cout << "";
  853.    
  854.     if(size == 0)
  855.         name[0] = toupper(name[0]);
  856.     else
  857.     {
  858.         int index[size];
  859.         getIndexesBySplittingSentence(name, ' ', index);
  860.        
  861.         for(int i = 0; i < size - 1; i++)
  862.         {
  863.             int size = i[index + 1] - i[index] - 1, count = 0;
  864.             char part[size];
  865.            
  866.             for(int j = index[i]; j < index[i + 1]; j++)
  867.                 part[count++] = name[j];
  868.            
  869.             part[sizeof(part)] = '\0';
  870.            
  871.             if(strcmp(part, "de") != 0 && strcmp(part, "do") != 0 && strcmp(part, "a") != 0 && strcmp(part, "da") != 0 && strcmp(part, "di") != 0 && strcmp(part, "du") != 0)
  872.                 name[index[i]] = toupper(name[index[i]]);
  873.            
  874.         }
  875.     }
  876. }
  877.  
  878. int getIndexesSize(char* line, char split_character)
  879. {
  880.     int size = strlen(line) + 1, count = 0;
  881.     bool continue_func = false;
  882.    
  883.     for(unsigned int i = 0; i < strlen(line); i++)
  884.     {
  885.         if(line[i] == split_character)
  886.         {
  887.             continue_func = true;
  888.             break;
  889.         }
  890.     }
  891.        
  892.     if(!continue_func) return count;
  893.    
  894.     if(line[0] != split_character && line[strlen(line)] != split_character)
  895.         size += 2;
  896.    
  897.     char working_on[size];
  898.    
  899.     if((unsigned int) size == strlen(line) + 1)
  900.         strcpy(working_on, line);
  901.     else
  902.     {
  903.         working_on[0] = ' ';
  904.         working_on[size - 2] = ' ';
  905.        
  906.         for(int k = 1; k < size - 2; k++)
  907.             working_on[k] = line[k - 1];
  908.        
  909.         working_on[size - 1] = '\0';
  910.        
  911.     }
  912.    
  913.     for(unsigned int i = 0; i <= strlen(working_on); i++)
  914.         if(working_on[i] == split_character)
  915.             count++;
  916.        
  917.     return count;
  918. }
  919.  
  920. void getIndexesBySplittingSentence(char* line, char split_character, int j[])
  921. {  
  922.     int size = strlen(line) + 1, count = 0;
  923.    
  924.     if(line[0] != split_character && line[strlen(line)] != split_character && split_character == ' ')
  925.         size += 2;
  926.    
  927.     char working_on[size];
  928.    
  929.     if((unsigned int) size == strlen(line) + 1)
  930.         strcpy(working_on, line);
  931.     else
  932.     {
  933.         working_on[0] = ' ';
  934.         working_on[size - 1] = ' ';
  935.        
  936.         for(int k = 1; k < size - 2; k++)
  937.             working_on[k] = line[k - 1];
  938.        
  939.         working_on[size] = '\0';
  940.     }
  941.    
  942.     for(unsigned int i = 0; i <= strlen(working_on); i++)
  943.         if(working_on[i] == split_character)
  944.             j[count++] = i;
  945.  
  946. }
  947.  
  948. int getNameOccurrences(char name[], bool isClient)
  949. {
  950.     if(isClient)
  951.         file.open("client.txt");
  952.     else
  953.         file.open("company.txt");
  954.    
  955.     char line[2000];
  956.     int count_name = 0;
  957.  
  958.     while(file.getline(line, 2000))
  959.     {
  960.         int i[getIndexesSize(line, '/')];
  961.         getIndexesBySplittingSentence(line, '/', i);
  962.        
  963.         int name_size = i[1] - i[0] - 1;
  964.         char name_now[name_size];
  965.  
  966.         fillSentence(line, i[0], i[1], name_now);
  967.        
  968.         if(strcmp(name_now, name) == 0) count_name++;
  969.        
  970.     }
  971.        
  972.     file.close();
  973.     return count_name;
  974. }
  975.  
  976. void fillSentence(char line[], int begin, int end, char target[])
  977. {
  978.     int count = 0;
  979.    
  980.     for(int i = begin + 1; i < end; i++)
  981.         target[count++] = line[i];
  982.    
  983.     target[end - begin - 1] = '\0';
  984. }
  985.  
  986. void Register::writeIntoFile(bool isClient)
  987. {
  988.     if(isClient)
  989.         out_file.open("client.txt", ios_base::app | ios_base::out);
  990.     else
  991.         out_file.open("company.txt", ios_base::app | ios_base::out);
  992.    
  993.     out_file << "Nome:/" << this->name << "/";
  994.     out_file << "Senha:/" << this->password << "/";
  995.    
  996.     if(isClient)
  997.         out_file << "IDs:/sem/";
  998.    
  999.     cout << endl;
  1000.    
  1001.     out_file.close();
  1002. }
  1003.  
  1004. void toLower(char w[])
  1005. {
  1006.     for(unsigned int i = 0; i < strlen(w); i++)
  1007.         w[i] = tolower(w[i]);
  1008. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement