Vous êtes sur la page 1sur 6

#include <iostream>

#include <fstream>
#include <sstream>
#include <iomanip>

using namespace std;

class Ordinateur
{
private:
static string file;
static int compteur;
string id;
string se;
int capacite;
public:
Ordinateur();
Ordinateur(string);
Ordinateur(string, int);
void afficher();
void save();
void del();
void update();
void search();
void split(string);
static int getId();
static void getAll();
};

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


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

// Définition des fonctions


Ordinateur::Ordinateur(){}

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

Ordinateur::Ordinateur (string se, int capacite)


{
ostringstream flux;
flux << "G6PC" << setfill('0') << setw(3) << ++Ordinateur::compteur;
this->id = flux.str();
this->se = se;
this->capacite = capacite;
}

void Ordinateur::save()
{
ofstream flux (Ordinateur::file, ios::out | ios::app);
if(flux)
{
flux << this->id << ";" << this->se << ";" << this->capacite << endl;
cout << "\tEnregistrement effectuée avec succès " << endl;
}
else
cout << "Impossible d'ouvrir le fichier" << endl;
flux.close();
}

void Ordinateur::getAll()
{
bool found = false;
string ligne;
Ordinateur o;
ifstream flux (Ordinateur::file, ios::in);
if(flux)
{
while(getline(flux, ligne))
{
//cout << ligne << endl;
o.split(ligne);
o.afficher();
found = true;
}
flux.close();
if(!found)
{
cout << "### Liste Vide ###" << endl;
}

}
else
{
cout << "### Données Innaccessibles ###" << endl;
}
}

void Ordinateur::afficher()
{
cout << "====================================" << endl;
cout << "Identifiant : " << id << endl;
cout << "Système d'exploitation : " << se << endl;
cout << "Capacité : " << capacite << endl;
cout << "====================================" << endl;
}

void Ordinateur::del()
{
bool found = false;
string data = "", ligne;
ifstream flux (Ordinateur::file, ios::in);
if(flux)
{
while(getline(flux, ligne))
{
if(ligne.substr(0,7) != id)
{
data += ligne + "\n";
}
else
{
found = true;
}
}
flux.close();
ofstream fluxSortie;
if(found)
{
fluxSortie.open(Ordinateur::file, ios::out | ios::trunc);
fluxSortie << data;
fluxSortie.close();
cout << "### Suppression reussie ###" << endl;
}
else
{
cout << "### Ordinateur Introuvable ###" << endl;
}
}
else
{
cout << "### Données Innaccessibles ###" << endl;
}
}

void Ordinateur::update()
{
bool found = false;
string data = "", ligne, se;
int capacite;
ifstream flux (Ordinateur::file, ios::in);
if(flux)
{
while(getline(flux, ligne))
{
if(ligne.substr(0,7) != id)
{
data += ligne + "\n";
}
else
{
cout << "Veuillez saisir les nouvelles valeurs " << endl;
cout << "Système d'exploitation >> ";
cin >> se;
cout << "Capacité >> ";
cin >> capacite;
data += ligne.substr(0,7) + ";";
data += se + ";";
data += to_string(capacite) + "\n";
found = true;
}
}
flux.close();
ofstream fluxSortie;
if(found)
{
fluxSortie.open(Ordinateur::file, ios::out | ios::trunc);
fluxSortie << data;
fluxSortie.close();
cout << "### Modification effectuée avec succès ###" << endl;
}
else
{
cout << "### Ordinateur Introuvable ###" << endl;
}
}
else
{
cout << "### Données Innaccessibles ###" << endl;
}
}

void Ordinateur::search()
{
Ordinateur o;
bool found = false;
string data = "", ligne;
ifstream flux (Ordinateur::file, ios::in);
if(flux)
{
while(getline(flux, ligne))
{
if(ligne.substr(0,7) == id)
{
found = true;
break;
}
}
flux.close();
if(!found)
{
cout << "### Ordinateur Introuvable ###" << endl;
}
else
{
//cout << ligne << endl;
o.split(ligne);
o.afficher();
}
}
else
{
cout << "### Données Innaccessibles ###" << endl;
}
}

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

return idLigne;
}

void Ordinateur::split(string ligne)


{
int pos = 7, nextPos;
id = ligne.substr(0,pos);
nextPos = ligne.find(";",pos+1);
se = ligne.substr(pos+1,nextPos-pos-1);
capacite = stoi(ligne.substr(nextPos+1));
//, ligne.size()-1-nextPos;
}

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 ordinateur" << endl;
cout << "-- 2. Afficher un ordinateur" << endl;
cout << "-- 3. Rechercher un ordinateur" << endl;
cout << "-- 4. Modifier un ordinateur" << endl;
cout << "-- 5. Supprimer un ordinateur" << 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()
{
int capacite;
string se;
cout << "==== ENREGISTREMENT D'UN ORDINATEUR ====" << endl;
cout << "Système d'exploitation >> "; cin >> se;
cout << "Capacité >> "; cin >> capacite;
Ordinateur(se,capacite).save();
}

void App::readAll()
{
cout << "==== LISTES DES ORDINATEUR ====" << endl;
Ordinateur::getAll();
}

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

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

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

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

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

Vous aimerez peut-être aussi