/******************************************************************************
Online C++ Compiler.
Code, Compile, Run and Debug C++ program online.
Write your code in this editor and press "Run" button to compile and execute it.
*******************************************************************************/
#include <iostream>
#include <regex>
#include <ctime>
#include <iomanip>
using namespace std;
class Worker
{
std::string _firstName{"Imie_______"};
std::string _middleName{};
std::string _lastName{"Nazwisko____"};
int _age = 0;
int _salary = 0;
std::tm tm{};
std::time_t _startWork;
public:
Worker()
{
// std::cout<<"\nworker fname :";
set(_firstName, "Podaj Imie [Imie]: ", std::regex("[A-Za-z]{2,}"));
set(_middleName, "Podaj drugie Imie [Imie]: ", std::regex("[A-Za-z]*"));
set(_lastName, "Podaj Nazwisko [Nazwisko]: ");
while (1)
{
int age = set(_age, "Podaj wiek [0-100]");
if (_age > 100)
{
std::cout << "Narazie nie przyjmujemy pracownikow powyzej 100lat \n\
jak beda takie przyjecia to zadzwonimy\n ";
}
else
break;
}
while (1)
{
int salary;
if ((salary = set(_salary, "Podaj zarobki [1500-4000]")) < 1500)
{
std::cout << "Miej ze litosc!!!";
}
else if (salary > 4000)
{
std::cout << "Panie tu nie Ameryka.";
}
else
break;
}
set(tm,"Podaj date zatrudnienia dzień, miesiąc, rok [##.##.####]:");
_startWork=mktime(&tm);
}
void print()
{
std::cout << "\n"
<< _firstName << " ";
if (_middleName != "")
std::cout << _middleName << " ";
std::cout << _lastName << " " << _age << " lat. \n";
std::stringstream buffer;
buffer << std::put_time(&tm, "%F");
std::cout<<"Data zatrudnienia: "<< buffer.str()<<std::endl;
std::cout << "Wyplata po "<<_numOfYr<<" latach pracy " << _salary << " PLN\n";
// std::cout << tm.tm_year << months[tm.tm_mon] << tm.tm_mday;
std::cout << std::endl;
}
void set(std::string &val, const std::string &msg, std::regex name = std::regex("[A-Za-z]+[-]{0,1}[A-Za-z]+$"))
{
std::string line;
for (int i = 0; i < 3; i++)
{
std::cout << msg << std::flush;
// std::cin>>val;
getline(cin, line);
// cout<<val<<val.size()<<std::regex_match(val, std::regex("[A-Za-z]+"))<<endl;
if (cin.fail())
cin.clear();
if (!std::regex_match(line, name))
{
std::cout << "\nBledne dane\n"
<< std::flush;
continue;
}
val = line;
break;
}
return;
}
int set(int &val, const std::string &msg)
{
std::string line;
for (int i = 0; i < 3; i++)
{
std::cout << msg << std::flush;
getline(cin, line);
if (cin.fail())
cin.clear();
if (!std::regex_match(line, std::regex("[0-9]+")))
{
std::cout << "\nBledne dane\n"
<< std::flush;
continue;
}
stringstream ss(line);
ss >> val;
break;
}
return val;
}
void set(std::tm &date, const string &msg, std::regex rgxDate = std::regex("([0-9]{1,2})\\.([0-9]{1,2})\\.(?:[0-9]{2}|([1-2]{1}[0-9]{3}))"))
{
std::string line;
for (int i = 0; i < 3; i++)
{
std::cout << msg << std::flush;
getline(cin, line);
if (cin.fail())
cin.clear();
if (!std::regex_match(line, rgxDate))
{
std::cout << "\nBledne dane\n"
<< msg << std::flush;
continue;
}
stringstream ss(line);
char dot;
int year;
ss >> date.tm_mday >> dot >> date.tm_mon >> dot >> year;
date.tm_mon--;
if (year < 100)
year += 100;
else if (year < 1900)
std::cerr << "bledne dane" << flush;
else
date.tm_year = year - 1900;
break;
}
return;
}
void rise() {
_numOfYr=std::difftime(toTestOnly(2022, 11, 11),_startWork)/oneyear();
for (int i=0; i<_numOfYr;i++) {
_salary*=1.15;
}
}
private:
int _numOfYr{};
const std::vector<string> months = {"Styczeń", "luty", "marzec", " kwiecień", "maj", "czerwiec", "lipiec", "sierpien", "wrzesien ", "pazdziernik", "listopad", "grudzien"};
double getTimeSec(){
return std::mktime(&tm);
}
std::time_t toTestOnly(const int &year, const int &month, const int &day)
{
std::tm dateToTime{};
dateToTime.tm_year = year - 1900;
dateToTime.tm_mon = month - 1;
dateToTime.tm_mday = day;
return std::mktime(&dateToTime);
}
const double oneyear(const int &year1 = 2022, const int &year2 = 2021)
{
std::tm tmyear1{};
std::tm tmyear2{};
tmyear1.tm_year = year1 - 1900;
tmyear2.tm_year = year2 - 1900;
return mktime(&tmyear1) - mktime(&tmyear2);
}
};
int main()
{
Worker j;
std::regex rgxDate = std::regex("([0-9]{1,2})\\.([0-9]{1,2})\\.(?:[0-9]{2}|([1-2]{1}[0-9]{3}))");
cout << std::regex_match("10.10.2020", rgxDate) << endl;
cout << std::regex_match("10.10.20", rgxDate) << endl;
cout << std::regex_match("1.1.2020", rgxDate) << endl;
cout << std::regex_match("1.1.3020", rgxDate) << endl;
cout << std::regex_match("1.1.1020", rgxDate) << endl;
cout << std::regex_match("1.1.2020", rgxDate) << endl;
cout << std::regex_match("10.10.20a", rgxDate) << endl;
cout << std::regex_match("10.10.202", rgxDate) << endl;
// j.setname("Rob");
j.rise();
j.print();
return 0;
}