Vous êtes sur la page 1sur 7

Prisonnier();

Prisonnier(string);
Prisonnier(string, string, string, int, float, short, short, short, string,
int, int);
void save();
void afficher();
static void liste();
void search();
void update();
void relacher();
static int getId();
void format(string);
void autreformat();
};

string Prisonnier::file = "data.csv";


int Prisonnier::compteur = Prisonnier::getId();

Prisonnier::Prisonnier(){}

Prisonnier::Prisonnier(string id){
this->id = id;
}

Prisonnier::Prisonnier(string nom, string prenom, string genre, int age, float


taille, short jour, short mois, short annee, string crime, int peine, int numcel)
{
ostringstream flux;
flux << "P23" << setfill('0') << setw(3) << ++Prisonnier::compteur;
this->id = flux.str();
this->nom = nom;
this->prenom = prenom;
this->genre = genre;
this->age = age;
this->taille = taille;
this->jour = jour;
this->mois = mois;
this->annee = annee;
this->crime = crime;
this->peine = peine;
this->numcel = numcel;
}

void Prisonnier::save()
{
ofstream flux (Prisonnier::file, ios::out | ios::app);
if(flux)
{
flux << this->id << ";" << this->nom << ";" << this->prenom << ";" << this-
>genre << ";" << this->age << ";" << this->taille << ";" << this->jour << ";" <<
this->mois << ";" << this->annee << ";" << this->crime << ";" << this->peine << ";"
<< this->numcel << endl;
cout << "Enregistrement réussi" << endl;
}
else{
cout << "Erreur, le fichier n'a pas pû être ouvert" << endl;
}
}
int Prisonnier::getId()
{
string ligne, ligneCopie = "P23000";
ifstream flux (Prisonnier::file, ios::in);
while(getline(flux,ligne))
{
ligneCopie = ligne;
} int idLigne = stoi(ligneCopie.substr(3,3));

return idLigne;
}

void Prisonnier::afficher()
{
cout << "=====================================" << endl;
cout << "ID : " << this->id << endl;
cout << "Nom : " << this->nom << endl;
cout << "Prenom : " << this->prenom << endl;
cout << "Genre : " << this->genre << endl;
cout << "Age : " << this->age << endl;
cout << "Taille : " << this->taille << endl;
cout << "Jour : " << this->jour << endl;
cout << "Mois : " << this->mois << endl;
cout << "Année : " << this->annee << endl;
cout << "Crime : " << this->crime << endl;
cout << "Peine : " << this->peine << endl;
cout << "Numéro de cellule : " << this->numcel << endl;
cout << "=====================================" << endl;
}

void Prisonnier::liste()
{
Prisonnier p;
ifstream flux (Prisonnier::file, ios::in);
string ligne;
bool trouve = false;
if(flux){
while(getline(flux,ligne)){
//cout << ligne << endl;
p.format(ligne);
p.afficher();
trouve = true;
}
flux.close();
if(!trouve){
cout << "Liste vide" << endl;
}
} else cout << "Erreur d'ouverture du fichier" << endl;
}

void Prisonnier::search()
{
Prisonnier p;
ifstream flux (Prisonnier::file, ios::in);
string ligne;
bool trouve = false;
if(flux){
while(getline(flux,ligne))
{
if(id == ligne.substr(0,6))
{
trouve = true;
break;
}
}
flux.close();
if(trouve){
//cout << ligne << endl;
p.format(ligne);
p.afficher();
}
else
{
cout << "Prisonnier Introuvable" << endl;
}
}
else
{
cout << "Ce fichier n'a pas pû être ouvert" << endl;
}
}

void Prisonnier::update()
{
bool trouve = false;
string nom;
string prenom;
string genre;
int age;
float taille;
short jour;
short mois;
short annee;
string crime;
int peine;
int numcel;
string ligne, data = "";
ifstream flux (Prisonnier::file, ios::in);
if(flux){
while(getline(flux,ligne))
{
if(id != ligne.substr(0,6))
{
data += ligne + "\n";
} else{
cout << "Veuillez saisir les nouvelles infos " << endl;
cout << "Nom >> "; cin >> nom;
cout << "Prenom >>"; cin >> prenom;
cout << "Genre >> "; cin >> genre;
cout << "Age >>"; cin >> age;
cout << "Taille >> "; cin >> taille;
cout << "Jour >> "; cin >> jour;
cout << "Mois >> "; cin >> mois;
cout << "Année >> "; cin >> annee;
cout << "Crime >> "; cin >> crime;
cout << "Peine >> "; cin >> peine;
cout << "Numéro Cellule >> "; cin >> numcel;
data += ligne.substr(0,6) + ";";
data += nom + ";";
data += prenom + ";";
data += genre + ";";
data += to_string(age) + ";";
data += to_string(taille) + ";";
data += to_string(jour) + ";";
data += to_string(mois) + ";";
data += to_string(annee) + ";";
data += crime + ";";
data += to_string(peine) + ";";
data += to_string(numcel) + "\n";
trouve = true;
}
}
flux.close();
ofstream fluxSortie;
if(trouve){
fluxSortie.open(Prisonnier::file, ios::out | ios::trunc);
fluxSortie << data;
fluxSortie.close();
cout << "Modification réussie" << endl;
} else{
cout << "Prisonnier Introuvable" << endl;
}
} else cout << "Ce fichier n'a pas pu être ouvert" << endl;
}

void Prisonnier::relacher()
{
bool trouve = false;
string ligne, data = "";
ifstream flux (Prisonnier::file, ios::in);
if (flux){
while (getline(flux,ligne))
{
if(id != ligne.substr(0,6))
{
data += ligne + "\n";
} else {
trouve = true;
}
}
flux.close();
ofstream fluxSortie;
if(trouve){
fluxSortie.open(Prisonnier::file, ios::out | ios::trunc);
fluxSortie << data;
fluxSortie.close();
cout << "Prisonnier relacher avec succès" << endl;
} else{
cout << "Prisonnier Introuvable" << endl;
}
} else{
cout << "Ce fichier n'a pas pû être ouvert !!!" << endl;
}
}
void Prisonnier::format(string ligne)
{
//P23001;DIALLO;KARIM;M;
int pos = 6, nextPos;
id = ligne.substr(0,pos);
nextPos = ligne.find(";", pos+1);
nom = ligne.substr(pos+1,nextPos-pos-1);
pos = nextPos;
nextPos = ligne.find(";",pos+1);
prenom = ligne.substr(pos+1,nextPos-pos-1);
pos = nextPos;
nextPos = ligne.find(";",pos+1);
genre = ligne.substr(pos+1,nextPos-pos-1);
pos = nextPos;
nextPos = ligne.find(";",pos+1);
age = stoi(ligne.substr(pos+1,nextPos-pos-1));
pos = nextPos;
nextPos = ligne.find(";",pos+1);
taille = stoi(ligne.substr(pos+1,nextPos-pos-1));
pos = nextPos;
nextPos = ligne.find(";",pos+1);
jour = stoi(ligne.substr(pos+1,nextPos-pos-1));
pos = nextPos;
nextPos = ligne.find(";",pos+1);
mois = stoi(ligne.substr(pos+1,nextPos-pos-1));
pos = nextPos;
nextPos = ligne.find(";",pos+1);
annee = stoi(ligne.substr(pos+1,nextPos-pos-1));
pos = nextPos;
nextPos = ligne.find(";",pos+1);
crime = ligne.substr(pos+1,nextPos-pos-1);
pos = nextPos;
nextPos = ligne.find(";",pos+1);
peine = stoi(ligne.substr(pos+1,nextPos-pos-1));
pos = nextPos;
nextPos = ligne.find(";",pos+1);
numcel = stoi(ligne.substr(pos+1,nextPos-pos-1));
}

void Prisonnier::autreformat()
{
cout << "lkqjsdf | qmslkdjf |qfsd " << endl;
cout << "=====================================" << endl;
cout << "ID : " << this->id << endl;
cout << "Nom : " << this->nom << endl;
cout << "Prenom : " << this->prenom << endl;
cout << "Genre : " << this->genre << endl;
cout << "Age : " << this->age << endl;
cout << "Taille : " << this->taille << endl;
cout << "Jour : " << this->jour << endl;
cout << "Mois : " << this->mois << endl;
cout << "Année : " << this->annee << endl;
cout << "Crime : " << this->crime << endl;
cout << "Peine : " << this->peine << endl;
cout << "Numéro de cellule : " << this->numcel << endl;
cout << "=====================================" << endl;
}
class App{
private:
static void save();
static void readAll();
static void search();
static void update();
static void relacher();
static void pause();
public:
static void run();
};

void App::run()
{
int choix;
do{
system("clear");
cout << "==================================" << endl;
cout << "-- 1. Ajouter un prisonnier" << endl;
cout << "-- 2. Afficher un prisonnier" << endl;
cout << "-- 3. Rechercher un prisonnier" << endl;
cout << "-- 4. Modifier un prisonnier" << endl;
cout << "-- 5. Supprimer un prisonnier" << endl;
cout << "-- 6. Quitter " << endl;
cout << "===================================" << endl;
cout << "Votre choix >> "; cin >> choix;
switch (choix)
{
case 1 : App::save(); App::pause(); break;
case 2 : App::readAll(); App::pause(); break;
case 3 : App::search(); App::pause(); break;
case 4 : App::update(); App::pause(); break;
case 5 : App::relacher(); App::pause(); break;
case 6 : break;
default:
cout << " Choix Incorrect " << endl;
break;
}
}while(choix != 6);
}

void App::save()
{
string nom;
string prenom;
string genre;
int age;
float taille;
short jour;
short mois;
short annee;
string crime;
int peine;
int numcel;
cout << "ENREGISTREMENT D'UN PRISONNIER" << endl;
cout << "Veuillez saisir les informations basiques du prisonnier" << endl;
cout << "Nom >> "; cin >> nom;
cout << "Prenom >>"; cin >> prenom;
cout << "Genre >> "; cin >> genre;
cout << "Age >>"; cin >> age;
cout << "Taille >> "; cin >> taille;
cout << "Jour >> "; cin >> jour;
cout << "Mois >> "; cin >> mois;
cout << "Année >> "; cin >> annee;
cout << "Crime >> "; cin >> crime;
cout << "Peine >> "; cin >> peine;
cout << "Numéro Cellule >> "; cin >> numcel;

Prisonnier(nom,prenom,genre,age,taille,jour,mois,annee,crime,peine,numcel).save();
}

void App::readAll()
{
cout << "LISTE DES PRISONNIERS" << endl;
Prisonnier::liste();
}

void App::search()
{
string id;
cout << "=== RECHERCHE D'UN PRISONNIER ===" << endl;
cout << "Veuillez saisir son identifiant >> "; cin >> id;
Prisonnier(id).search();
}

void App::update()
{
string id;
cout << "=== MODIFICATION D'UN PRISONNIER ===" << endl;
cout << "Veuillez saisir son identifiant >> "; cin >> id;
Prisonnier(id).update();
}

void App::relacher()
{
string id;
cout << "=== SUPPRESSION D'UN PRISONNIER ===" << endl;
cout << "Veuillez saisir son identifiant >> "; cin >> id;
Prisonnier(id).relacher();
}
void App::pause()
{
char systempause;
cin.ignore(255,'\n');
cout << "Appuyez sur une touche pour continuer...";
cin.get(systempause);
}

int main(){

/*Prisonnier p1("Diallo","Fatoumata Djoumo","Feminin",21,1.71,7,13,2002,"Se


fache tout le temps", 200, 1001),
p2("Bah","Amadou","Masculin",21,1.71,7,13,2003,"Attaque", 200, 1002);
p1.save();
p2.save();
Prisonnier("P23016").relacher();*/
App::run();
return 0;
}

Vous aimerez peut-être aussi