Advertisement
Guest User

Untitled

a guest
Oct 22nd, 2018
141
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 6.33 KB | None | 0 0
  1. #include <iostream>
  2. #include <string>
  3. #include <vector>
  4. #include <fstream>
  5.  
  6. #include <cstdlib>
  7. #include <ctime>
  8.  
  9. using namespace std;
  10.  
  11. /* ------- header part ------- */
  12.  
  13. class Osoba // nazwa wymuszona przez zadanie, alternatywna wersja: Person
  14. {
  15. public:
  16.     Osoba();
  17.  
  18.     void EnterData();
  19.    
  20.     unsigned int ObliczWiek() const; // nazwa wymuszona przez zadania, alternatywna wersja: GetAge()
  21.  
  22.     string StringizeBirthdate() const;
  23.     string StringizeGender() const;
  24.  
  25. public: // getters
  26.     inline const string &Name() const { return m_name; }
  27.     inline const string &Surname() const { return m_surname; }
  28.     inline const string &EyeColor() const { return m_eyeColor; }
  29.     inline const unsigned int &Height() const { return m_height; }
  30.  
  31. private:
  32.     string m_name;
  33.     string m_surname;
  34.     struct
  35.     {
  36.         unsigned long m_day;    // 1 ...31
  37.         unsigned long m_month;  // 1 ...12
  38.         unsigned long m_year;   // 1900 ...GetCurrentYear()
  39.     } m_birthdate;
  40.     string m_eyeColor;
  41.     bool m_male;                // gender: true = male, false = female
  42.     unsigned int m_height;      // unit: cm
  43. };
  44.  
  45. class Application
  46. {
  47. public:
  48.     Application() : m_running(false) {}
  49.  
  50.     int Run(const int argc, char *const argv[]);
  51.  
  52.     static void ClearConsole();
  53.     static unsigned int GetCurrentYear();
  54.  
  55. private:
  56.     void ShowHeader();
  57.     void Welcome();
  58.     void Menu();
  59.  
  60.     void AddPerson();
  61.     void RemovePerson();
  62.     void FindPerson();
  63.     void DumpData();
  64.     void Quit();
  65.  
  66. private:
  67.     bool m_running;
  68.     vector<Osoba> m_data;
  69. };
  70.  
  71. /* ------- source part ------- */
  72.  
  73. /* ------- Osoba ------- */
  74.  
  75. Osoba::Osoba()
  76.     : m_birthdate{ 0 }
  77.     , m_male(false)
  78.     , m_height(0)
  79. {
  80. }
  81.  
  82. void Osoba::EnterData()
  83. {
  84.     cout << "Imie > ";
  85.     cin >> m_name;
  86.  
  87.     cout << "Nazwisko > ";
  88.     cin >> m_surname;
  89.  
  90.     string birthdate;
  91.     cout << "Data urodzenia (format: DD.MM.YYYY) > ";
  92.     cin >> birthdate;
  93.     // TODO Check user input
  94.     m_birthdate.m_day = stoul(birthdate.substr(0, 2));
  95.     m_birthdate.m_month = stoul(birthdate.substr(3, 2));
  96.     m_birthdate.m_year = stoul(birthdate.substr(6, 4));
  97.  
  98.     cout << "Kolor oczu > ";
  99.     cin >> m_eyeColor;
  100.  
  101.     for (;;)
  102.     {
  103.         char gender = '\0';
  104.         cout << "Plec (m/k) > ";
  105.         cin >> gender;
  106.         if (gender == 'm' || gender == 'k')
  107.         {
  108.             m_male = (gender == 'm');
  109.             break;
  110.         } else cout << "Niepoprawna plec, wprowadz m(ezczyzna) lub k(obieta)" << endl;
  111.     }
  112.  
  113.     cout << "Wzrost > ";
  114.     cin >> m_height;
  115. }
  116.  
  117. unsigned int Osoba::ObliczWiek() const
  118. {
  119.     return Application::GetCurrentYear() - m_birthdate.m_year;
  120. }
  121.  
  122. string Osoba::StringizeBirthdate() const
  123. {
  124.     char result[256];
  125.     sprintf(result, "%02d.%02d.%04d", m_birthdate.m_day, m_birthdate.m_month, m_birthdate.m_year);
  126.     return string(result);
  127. }
  128.  
  129. string Osoba::StringizeGender() const
  130. {
  131.     return m_male ? "M" : "K";
  132. }
  133.  
  134. /* ------- Application ------- */
  135.  
  136. int Application::Run(const int argc, char *const argv[])
  137. {
  138.     Welcome();
  139.     for (m_running = true; m_running; Menu());
  140.     return 0;
  141. }
  142.  
  143. void Application::ShowHeader()
  144. {
  145.     cout << "***************************" << endl;
  146.     cout << "*** Program ewidencyjny ***" << endl;
  147.     cout << "***************************" << endl;
  148.     cout << " Liczba zaewidencjonowanych osob: " << m_data.size() << endl;
  149.  
  150.  
  151. }
  152.  
  153. void Application::Welcome()
  154. {
  155.     ShowHeader();
  156.  
  157.     char choice = 'n';
  158.     cout << endl << "Czy chcesz dodac informacje o nowej osobie? [y/n] > ";
  159.     cin >> choice;
  160.  
  161.     if(choice == 'y')
  162.     {
  163.         AddPerson();
  164.     }
  165. }
  166.  
  167. void Application::Menu()
  168. {
  169.     ShowHeader();
  170.  
  171.     struct Option { void(Application::*callback)(); string name; }
  172.     static const options[] = {
  173.         { nullptr,                      "" },
  174.         { &Application::AddPerson,      "Dodaj osobe" },
  175.         { &Application::RemovePerson,   "Usun osobe" },
  176.         { &Application::FindPerson,     "Wyszukaj osobe" },
  177.         { &Application::DumpData,       "Archiwizacja danych do pliku tekstowego" },
  178.         { &Application::Quit,           "Wyjscie" }
  179.     };
  180.     static const size_t optionsCount = sizeof(options) / sizeof(options[0]);
  181.  
  182.     cout << endl << " Menu:" << endl;
  183.     for (size_t i = 0; i < optionsCount; ++i)
  184.     {
  185.         if(!options[i].callback) continue;
  186.         cout << " [" << i << "] " << options[i].name << endl;
  187.     }
  188.  
  189.     for (;;)
  190.     {
  191.         cout << endl << "Wybor > ";
  192.  
  193.         unsigned int choice = 0;
  194.         cin >> choice;
  195.  
  196.         if(choice >= optionsCount || !options[choice].callback)
  197.         {
  198.             cout << "Nie ma takiej opcji do wyboru!" << endl;
  199.             continue;
  200.         }
  201.  
  202.         (this->*(options[choice].callback))();
  203.         break;
  204.     }
  205. }
  206.  
  207. void Application::AddPerson()
  208. {
  209.     cout << " < Dodawanie osoby >" << endl << endl;
  210.     Osoba person;
  211.     person.EnterData();
  212.     m_data.push_back(person);
  213.     cout << " Osoba zostala dodana" << endl;
  214. }
  215.  
  216. void Application::RemovePerson()
  217. {
  218.     if(m_data.empty())
  219.     {
  220.         cout << "Zadna osoba nie jest zaewidencjonowana!" << endl;
  221.         return;
  222.     }
  223.  
  224.     cout << " < Usuwanie osoby >" << endl << endl;
  225.  
  226.     cout << "Zaewidencjonowane osoby:" << endl;
  227.     for (size_t i = 0; i < m_data.size(); ++i)
  228.     {
  229.         const Osoba &person = m_data[i];
  230.         cout << " [" << i << "] " << person.Name() << " " << person.Surname() << endl;
  231.     }
  232.     cout << endl;
  233.  
  234.     for (;;)
  235.     {
  236.         size_t personToRemove = 0;
  237.         cout << "Wprowadz index ktory chcesz usunac [ 0, ..." << m_data.size() - 1 << " ] > ";
  238.         cin >> personToRemove;
  239.  
  240.         if(personToRemove >= m_data.size())
  241.         {
  242.             cout << "Niepoprawna wartosc!" << endl;
  243.             continue;
  244.         }
  245.  
  246.         m_data.erase(m_data.begin() + personToRemove);
  247.         cout << "Index zostal usuniety" << endl;
  248.         break;
  249.     }
  250. }
  251.  
  252. void Application::FindPerson()
  253. {
  254.     // TODO Implementacja tej metody
  255. }
  256.  
  257. void Application::DumpData()
  258. {
  259.     ofstream file("data.txt", ios::binary);
  260.     if(!file)
  261.     {
  262.         cout << "Nie mozna otworzyc pliku do zapisu!" << endl;
  263.         return;
  264.     }
  265.  
  266.     for(size_t i = 0; i < m_data.size(); ++i)
  267.     {
  268.         const Osoba &person = m_data[i];
  269.         file << "[" << i << "] " << person.Name() << " " << person.Surname()
  270.              << " " << person.StringizeBirthdate() << " " << person.StringizeGender()
  271.              << " " << person.Height() << "\r\n" /* windows-like new line */;
  272.     }
  273. }
  274.  
  275. void Application::Quit()
  276. {
  277.     m_running = false;
  278. }
  279.  
  280. void Application::ClearConsole()
  281. {
  282. #ifdef _WIN32
  283.     system("cls");
  284. #else
  285.     // TODO Write linux/osx implementation of clear console function
  286. #endif
  287. }
  288.  
  289. unsigned int Application::GetCurrentYear()
  290. {
  291.     const time_t t = time(nullptr);
  292.     const struct tm *const info = localtime(&t);
  293.     return info->tm_year + 1900;
  294. }
  295.  
  296. /* ------- entry point ------- */
  297.  
  298. int main(int argc, char *argv[])
  299. {
  300.     Application app;
  301.     return app.Run(argc, argv);
  302. }
  303.  
  304. /* eof */
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement