Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <string>
- #include <vector>
- #include <fstream>
- #include <cstdlib>
- #include <ctime>
- using namespace std;
- /* ------- header part ------- */
- class Osoba // nazwa wymuszona przez zadanie, alternatywna wersja: Person
- {
- public:
- Osoba();
- void EnterData();
- unsigned int ObliczWiek() const; // nazwa wymuszona przez zadania, alternatywna wersja: GetAge()
- string StringizeBirthdate() const;
- string StringizeGender() const;
- public: // getters
- inline const string &Name() const { return m_name; }
- inline const string &Surname() const { return m_surname; }
- inline const string &EyeColor() const { return m_eyeColor; }
- inline const unsigned int &Height() const { return m_height; }
- private:
- string m_name;
- string m_surname;
- struct
- {
- unsigned long m_day; // 1 ...31
- unsigned long m_month; // 1 ...12
- unsigned long m_year; // 1900 ...GetCurrentYear()
- } m_birthdate;
- string m_eyeColor;
- bool m_male; // gender: true = male, false = female
- unsigned int m_height; // unit: cm
- };
- class Application
- {
- public:
- Application() : m_running(false) {}
- int Run(const int argc, char *const argv[]);
- static void ClearConsole();
- static unsigned int GetCurrentYear();
- private:
- void ShowHeader();
- void Welcome();
- void Menu();
- void AddPerson();
- void RemovePerson();
- void FindPerson();
- void DumpData();
- void Quit();
- private:
- bool m_running;
- vector<Osoba> m_data;
- };
- /* ------- source part ------- */
- /* ------- Osoba ------- */
- Osoba::Osoba()
- : m_birthdate{ 0 }
- , m_male(false)
- , m_height(0)
- {
- }
- void Osoba::EnterData()
- {
- cout << "Imie > ";
- cin >> m_name;
- cout << "Nazwisko > ";
- cin >> m_surname;
- string birthdate;
- cout << "Data urodzenia (format: DD.MM.YYYY) > ";
- cin >> birthdate;
- // TODO Check user input
- m_birthdate.m_day = stoul(birthdate.substr(0, 2));
- m_birthdate.m_month = stoul(birthdate.substr(3, 2));
- m_birthdate.m_year = stoul(birthdate.substr(6, 4));
- cout << "Kolor oczu > ";
- cin >> m_eyeColor;
- for (;;)
- {
- char gender = '\0';
- cout << "Plec (m/k) > ";
- cin >> gender;
- if (gender == 'm' || gender == 'k')
- {
- m_male = (gender == 'm');
- break;
- } else cout << "Niepoprawna plec, wprowadz m(ezczyzna) lub k(obieta)" << endl;
- }
- cout << "Wzrost > ";
- cin >> m_height;
- }
- unsigned int Osoba::ObliczWiek() const
- {
- return Application::GetCurrentYear() - m_birthdate.m_year;
- }
- string Osoba::StringizeBirthdate() const
- {
- char result[256];
- sprintf(result, "%02d.%02d.%04d", m_birthdate.m_day, m_birthdate.m_month, m_birthdate.m_year);
- return string(result);
- }
- string Osoba::StringizeGender() const
- {
- return m_male ? "M" : "K";
- }
- /* ------- Application ------- */
- int Application::Run(const int argc, char *const argv[])
- {
- Welcome();
- for (m_running = true; m_running; Menu());
- return 0;
- }
- void Application::ShowHeader()
- {
- cout << "***************************" << endl;
- cout << "*** Program ewidencyjny ***" << endl;
- cout << "***************************" << endl;
- cout << " Liczba zaewidencjonowanych osob: " << m_data.size() << endl;
- }
- void Application::Welcome()
- {
- ShowHeader();
- char choice = 'n';
- cout << endl << "Czy chcesz dodac informacje o nowej osobie? [y/n] > ";
- cin >> choice;
- if(choice == 'y')
- {
- AddPerson();
- }
- }
- void Application::Menu()
- {
- ShowHeader();
- struct Option { void(Application::*callback)(); string name; }
- static const options[] = {
- { nullptr, "" },
- { &Application::AddPerson, "Dodaj osobe" },
- { &Application::RemovePerson, "Usun osobe" },
- { &Application::FindPerson, "Wyszukaj osobe" },
- { &Application::DumpData, "Archiwizacja danych do pliku tekstowego" },
- { &Application::Quit, "Wyjscie" }
- };
- static const size_t optionsCount = sizeof(options) / sizeof(options[0]);
- cout << endl << " Menu:" << endl;
- for (size_t i = 0; i < optionsCount; ++i)
- {
- if(!options[i].callback) continue;
- cout << " [" << i << "] " << options[i].name << endl;
- }
- for (;;)
- {
- cout << endl << "Wybor > ";
- unsigned int choice = 0;
- cin >> choice;
- if(choice >= optionsCount || !options[choice].callback)
- {
- cout << "Nie ma takiej opcji do wyboru!" << endl;
- continue;
- }
- (this->*(options[choice].callback))();
- break;
- }
- }
- void Application::AddPerson()
- {
- cout << " < Dodawanie osoby >" << endl << endl;
- Osoba person;
- person.EnterData();
- m_data.push_back(person);
- cout << " Osoba zostala dodana" << endl;
- }
- void Application::RemovePerson()
- {
- if(m_data.empty())
- {
- cout << "Zadna osoba nie jest zaewidencjonowana!" << endl;
- return;
- }
- cout << " < Usuwanie osoby >" << endl << endl;
- cout << "Zaewidencjonowane osoby:" << endl;
- for (size_t i = 0; i < m_data.size(); ++i)
- {
- const Osoba &person = m_data[i];
- cout << " [" << i << "] " << person.Name() << " " << person.Surname() << endl;
- }
- cout << endl;
- for (;;)
- {
- size_t personToRemove = 0;
- cout << "Wprowadz index ktory chcesz usunac [ 0, ..." << m_data.size() - 1 << " ] > ";
- cin >> personToRemove;
- if(personToRemove >= m_data.size())
- {
- cout << "Niepoprawna wartosc!" << endl;
- continue;
- }
- m_data.erase(m_data.begin() + personToRemove);
- cout << "Index zostal usuniety" << endl;
- break;
- }
- }
- void Application::FindPerson()
- {
- // TODO Implementacja tej metody
- }
- void Application::DumpData()
- {
- ofstream file("data.txt", ios::binary);
- if(!file)
- {
- cout << "Nie mozna otworzyc pliku do zapisu!" << endl;
- return;
- }
- for(size_t i = 0; i < m_data.size(); ++i)
- {
- const Osoba &person = m_data[i];
- file << "[" << i << "] " << person.Name() << " " << person.Surname()
- << " " << person.StringizeBirthdate() << " " << person.StringizeGender()
- << " " << person.Height() << "\r\n" /* windows-like new line */;
- }
- }
- void Application::Quit()
- {
- m_running = false;
- }
- void Application::ClearConsole()
- {
- #ifdef _WIN32
- system("cls");
- #else
- // TODO Write linux/osx implementation of clear console function
- #endif
- }
- unsigned int Application::GetCurrentYear()
- {
- const time_t t = time(nullptr);
- const struct tm *const info = localtime(&t);
- return info->tm_year + 1900;
- }
- /* ------- entry point ------- */
- int main(int argc, char *argv[])
- {
- Application app;
- return app.Run(argc, argv);
- }
- /* eof */
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement