Ahi

Télécharger au format txt, pdf ou txt
Télécharger au format txt, pdf ou txt
Vous êtes sur la page 1sur 5

Hijab::Hijab(string marque, string couleur, int prix){

ostringstream flux;
flux << "HIJ" << setfill('0') << setw(3) << ++Hijab::compteur;
this->id = flux.str();
this->marque = marque;
this->couleur = couleur;
this->prix = prix;
}

void Hijab::save(){
ofstream flux (Hijab::file, ios::out | ios::app);
if(flux){
flux << this->id << ";" << this->marque << ";" << this->couleur << ";" <<
this->prix << endl;
cout << "Enregistrement reussi" << endl;
}
else
cout << "Impossible d'ouvrir le fichier" << endl;

flux.close();
}

void Hijab::afficher()
{
cout << "=============================" << endl;
cout << "Id : " << this->id << endl;
cout << "Marque : " << this->marque << endl;
cout << "Couleur : " << this->couleur << endl;
cout << "Prix : " << this->prix << endl;
cout << "=============================" << endl;
}

void Hijab::liste()
{
Hijab h;
string ligne;
bool trouve = false;
ifstream flux (Hijab::file, ios::in);
if(flux){
while(getline(flux,ligne))
{
h.split(ligne);
h.afficher();
trouve = true;
}
flux.close();
if(!trouve)
{
cout << "Liste vide" << endl;
}
}
else{
cout << "Ce fichier n'a pas pû être ouvert "<< endl;
}
}

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

void Hijab::update(){
bool trouve = false;
string ligne, data = "", marque, couleur;
int prix;
ifstream flux (Hijab::file, ios::in);
if (flux){
while(getline(flux,ligne)){
if (ligne.substr(0,6) != id){
data += ligne + "\n";
} else{
cout << "Veuillez saisir les nouvelles informations" << endl;
cout << "Marque >> "; cin >> marque;
cout << "Couleur >> "; cin >> couleur;
cout << "Prix >> "; cin >> prix;
data += ligne.substr(0,6) + ";";
data += marque + ";";
data += couleur + ";";
data += to_string(prix) + "\n";
trouve = true;
}
}
flux.close();
ofstream fluxSortie;
if (trouve){
fluxSortie.open(Hijab::file, ios::out | ios::trunc);
fluxSortie << data;
fluxSortie.close();
cout << "Modification réussi" << endl;
}
else{
cout << "Hijab Introuvable" << endl;
}
}
else{
cout << "Ce fichier n'a pas pû être ouvert" << endl;
}
}

void Hijab::del()
{
bool trouve = false;
string ligne, data = "";
ifstream flux (Hijab::file, ios::in);
if (flux){
while(getline(flux,ligne)){
if(ligne.substr(0,6) != id){
data += ligne + "\n";
} else{
trouve = true;
}
}
flux.close();
ofstream fluxSortie;
if(trouve){
fluxSortie.open(Hijab::file, ios::out | ios::trunc);
fluxSortie << data;
fluxSortie.close();
cout << "Suppression réussi" << endl;
} else{
cout << "Ce hijab est introuvable" << endl;
}
} else{
cout << "Ce fichier n'a pas pû être ouvert" << endl;
}
}

void Hijab::split(string ligne)


{
int pos = 6, nexPos;
id = ligne.substr(0,pos);
nexPos = ligne.find(";",pos+1);
marque = ligne.substr(pos+1,nexPos-pos-1);
pos = nexPos;
nexPos = ligne.find(";",pos+1);
couleur = ligne.substr(pos+1,nexPos-pos-1);
pos = nexPos;
nexPos = ligne.find(";",pos+1);
prix = stoi(ligne.substr(pos+1,nexPos-pos-1));
}

int Hijab::getId(){
ifstream flux (Hijab::file, ios::in);
string ligne, ligneCopie = "HIJ000";
while (getline(flux, ligne))
ligneCopie = ligne;
int idLigne = stoi(ligneCopie.substr(3,3));

return idLigne;
}

class App
{
private:
static void save();
static void readAll();
static void search();
static void update();
static void del();
static void pause();
public:
static void run();
};

void App::run()
{
int choix;
do{
system("clear");
cout << "==================================" << endl;
cout << "-- 1. Ajouter un hijab" << endl;
cout << "-- 2. Afficher un hijab" << endl;
cout << "-- 3. Rechercher un hijab" << endl;
cout << "-- 4. Modifier un hijab" << endl;
cout << "-- 5. Supprimer un hijab" << 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::del(); App::pause(); break;
case 6 : break;
default:
cout << " Choix Incorrect " << endl;
break;
}
}while(choix != 6);
}

void App::save()
{
string marque, couleur;
int prix;
cout << "==== ENREGISTREMENT D'UN HIJAB ====" << endl;
cout << "Marque >> "; cin >> marque;
cout << "Couleur >> "; cin >> couleur;
cout << "Prix >> "; cin >> prix;
Hijab(marque,couleur,prix).save();
}

void App::readAll()
{
cout << "==== LISTES DES HIJABS ====" << endl;
Hijab::liste();
}

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

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

void App::del()
{
string id;
cout << "=== SUPPRESSION D'UN HIJAB ===" << endl;
cout << "Veuillez saisir son identifiant >> "; cin >> id;
Hijab(id).del();
}

void App::pause()
{
char systempause;
cin.ignore(255,'\n');
cout << "Appuyez sur une touche pour continuer...";
cin.get(systempause);
}

int main(){
App::run();
return 0;
}

Vous aimerez peut-être aussi