154 lines
4.6 KiB
C++
154 lines
4.6 KiB
C++
#include <iostream>
|
|
#include <random>
|
|
#include <ctime>
|
|
#include <string>
|
|
#include "ticket.h"
|
|
#include <fstream>
|
|
#include <sstream>
|
|
#include <iomanip>
|
|
|
|
|
|
//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<unsigned int>(std::time(0)));
|
|
std::uniform_int_distribution<int> 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<int>(time(nullptr));
|
|
this -> expire_date = static_cast<int>(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 <<" "<<ticket_type << " "<<is_valid << " "<<buy_date<<" "<<expire_date<<" "<<use_date;
|
|
lineChecker(id);
|
|
|
|
}
|
|
|
|
void Ticket::lineChecker(std::string id_input) {
|
|
std::fstream file("tickets.txt");
|
|
std::string line;
|
|
while(std::getline(file,line)){
|
|
std::istringstream iss(line);
|
|
std::string id;
|
|
if (iss >> 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 <<id <<" "<<ticket_type << " "<<is_valid << " "<<buy_date<<" "<<expire_date<<" "<<use_date;
|
|
}
|
|
else {
|
|
temp_file <<line;
|
|
}
|
|
counter++;
|
|
}
|
|
remove("tickets.txt");
|
|
std::rename("tickets_temp.txt","tickets.txt");
|
|
}
|
|
|
|
|
|
|
|
void Ticket::ticketInfo(){
|
|
std::string ticket_type_name;
|
|
if(this-> ticket_type == 0){
|
|
ticket_type_name = "normalny";
|
|
}
|
|
if(this-> ticket_type == 1){
|
|
ticket_type_name = "ulgowy";
|
|
}
|
|
std::cout << "Id biletu: "<<this ->id <<"\n";
|
|
std::cout << "Typ biletu: "<<ticket_type_name << "(" << this ->ticket_type << ")"<<"\n";
|
|
std::cout << "Ważny: "<<this ->is_valid <<"\n";
|
|
std::cout << "Data_zakupu: "<<std::put_time(std::localtime(&this -> buy_date), "%Y-%m-%d %H:%M:%S")<<"\n";
|
|
std::cout << "Data_wygaśnięcia: "<<std::put_time(std::localtime(&this -> expire_date), "%Y-%m-%d %H:%M:%S") <<"\n";
|
|
if (this->use_date != -1) {
|
|
std::cout << "Data_wykorzystania: "<<std::put_time(std::localtime(&this -> 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<int>(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<int>(time(nullptr));
|
|
ticketDatabaseUpdate();
|
|
std::cout <<"Bilet został wykorzystany pomyślnie."<<std::endl;
|
|
}
|
|
else {
|
|
std::cout << "Ten bilet nie może zostać użyty, gdyż jest już wykorzystany lub wygasł."<<std::endl;
|
|
}
|
|
}
|