Projet_VictorAvecUnK/app/src/Tortue.cpp
2020-05-30 15:18:21 +02:00

148 lines
3.2 KiB
C++

//
// Created by Victor Le Roch on 20/05/2020.
//
#include "../include/Tortue.h"
#include "../../core_simulation.h"
//#include <string>
Tortue::Tortue() {
++nbTortue;
}
Tortue::Tortue(const string &nom, const std::string &naissance, const string &sexe) : nom(nom), sexe(sexe), naissance(naissance) {
nbTortue ++;
}
std::string Tortue::getAge() const {
time_t rawtime;
struct tm * timeinfo ;
std::string date = naissance;
std::string message ="";
int annee = std::stoi(date.substr(6,9));
int mois = std::stoi(date.substr(3,4));
int jour = std::stoi(date.substr(0,2));
int h = 0;
int m = 0;
int s = 0;
time(&rawtime);
timeinfo = localtime(&rawtime);
timeinfo->tm_year = annee - 1900;
timeinfo->tm_mon= mois - 1;
timeinfo->tm_mday = jour;
timeinfo->tm_hour = h;
timeinfo->tm_min = m;
timeinfo->tm_sec = s;
long double naiss =std::mktime(timeinfo);
//std::cout << naiss << std::endl;
std::time_t now = std::time(nullptr);
long double time = now - naiss;
//std::cout << time <<std::endl;
int agean = time / 31557600;
time = time - (agean*31557600);
int agemois = time / 2592000;
message = to_string(agean) + " ans et " + to_string(agemois) + " mois";
return message;
}
int Tortue::nbTortue = 0;
const std::string &Tortue::getNom() const {
return nom;
}
string Tortue::getNaissance() const {
return naissance;
}
const std::string &Tortue::getSexe() const {
return sexe;
}
int Tortue::getNbTortue() {
return nbTortue;
}
std::string Tortue::getInfo() {
std::string message;
message = "\nMy name is "+this->nom;
message += "\n" + this->getAge();
message += "\n"+this->sexe;
return message;
}
void Tortue::setNom(const string &nom) {
Tortue::nom = nom;
}
void Tortue::setNaissance(std::string naissance) {
time_t rawtime;
struct tm * timeinfo ;
std::string date = naissance;
if (date.size() != 10 || date[2] != '/'
|| date[5] != '/'){
throw ExceptionDate(2);
}
int annee = std::stoi(date.substr(6,9));
int mois = std::stoi(date.substr(3,4));
int jour = std::stoi(date.substr(0,2));
if (annee < 1900) throw ExceptionDate(3);
else if (mois > 12) throw ExceptionDate(4);
else if (jour > 31) throw ExceptionDate(5);
int h = 0;
int m = 0;
int s = 0;
time(&rawtime);
timeinfo = localtime(&rawtime);
timeinfo->tm_year = annee - 1900;
timeinfo->tm_mon= mois - 1;
timeinfo->tm_mday = jour;
timeinfo->tm_hour = h;
timeinfo->tm_min = m;
timeinfo->tm_sec = s;
long double naiss =std::mktime(timeinfo);
//std::cout << naiss << std::endl;
std::time_t now = std::time(nullptr);
long double time = now - naiss;
if (time < 0){
throw ExceptionDate(1);
}
this->naissance = naissance;
}
void Tortue::setSexe(const string &sexe) {
Tortue::sexe = sexe;
}
void Tortue::delete1() {
--nbTortue;
}
ostream &operator<<(ostream &os, const Tortue &tortue) {
os << "nom: " << tortue.nom << " naissance: " << tortue.naissance << " sexe: " << tortue.sexe;
return os;
}
Tortue::ExceptionDate::ExceptionDate(int id) : id(id) {}