#include #include #include #include #include "ticket.h" #include #include #include //Funkcja generująca losowe id dla biletu std::string generateRandomString(unsigned int length) { const std::string characters = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789"; std::string randomString; // Inicjalizacja generatora liczb losowych z wykorzystaniem czasu std::mt19937 generator(static_cast(std::time(0))); std::uniform_int_distribution distribution(0, characters.size() - 1); for (size_t i = 0; i < length; i++) { randomString += characters[distribution(generator)]; } return randomString; } Ticket::Ticket(int ticket_type){ this -> id = generateRandomString(10); this-> ticket_type = ticket_type; this -> is_valid = true; this -> buy_date = static_cast(time(nullptr)); this -> expire_date = static_cast(time(nullptr)) + 2678400; this -> use_date = -1; ticketDatabaseAdd(); }; Ticket::Ticket(std::string id_input){ std::string line; std::fstream file("tickets.txt"); while(std::getline(file,line)){ std::istringstream iss(line); std::string id; if (iss >> id){ if (id == id_input){ int ticket_type; bool is_valid; time_t expire_date,buy_date,use_date; if(iss >> ticket_type >> is_valid >> buy_date >> expire_date >> use_date) { this -> id = id; this-> ticket_type = ticket_type; this -> is_valid = is_valid; this -> buy_date = buy_date; this -> expire_date = expire_date; this -> use_date = use_date; } break; } } this->line_in_file ++; } } void Ticket::ticketDatabaseAdd() { std::fstream file("tickets.txt", std::ios::app); if (file.tellg() !=0) { file<<"\n"; } file <> id){ if (id == id_input){ break; } } this->line_in_file ++; } } void Ticket::ticketDatabaseUpdate() { int counter = 0; std::string line; std::fstream file("tickets.txt"); std::fstream temp_file("tickets_temp.txt", std::ios::out |std::ios::app); while (std::getline(file,line)) { if (temp_file.tellg() !=0) { temp_file<<"\n"; } if (counter == line_in_file) { temp_file < ticket_type == 0){ ticket_type_name = "normalny"; } if(this-> ticket_type == 1){ ticket_type_name = "ulgowy"; } std::cout << "Id biletu: "<id <<"\n"; std::cout << "Typ biletu: "<ticket_type << ")"<<"\n"; std::cout << "Ważny: "<is_valid <<"\n"; std::cout << "Data_zakupu: "< buy_date), "%Y-%m-%d %H:%M:%S")<<"\n"; std::cout << "Data_wygaśnięcia: "< expire_date), "%Y-%m-%d %H:%M:%S") <<"\n"; if (this->use_date != -1) { std::cout << "Data_wykorzystania: "< use_date), "%Y-%m-%d %H:%M:%S") <<"\n"; } } //Sprawdzenie ważności biletu bool Ticket::ticketValidityCheck(){ if (!is_valid or expire_date < static_cast(time(nullptr))) { is_valid = false; ticketDatabaseUpdate(); return false; } return true; } //Metoda służąca do "kasowania biletu" i aktualizacji danych w pliku z biletami void Ticket::ticketUse() { if (ticketValidityCheck()) { is_valid = false; use_date = static_cast(time(nullptr)); ticketDatabaseUpdate(); std::cout <<"Bilet został wykorzystany pomyślnie."<