Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // ConsoleApplication2.cpp : Ce fichier contient la fonction 'main'. L'exécution du programme commence et se termine à cet endroit.
- //
- #include <fstream>
- #include <vector>
- #include <iostream>
- #include <string>
- using namespace std;
- class User
- {
- private:
- string name;
- string password;
- bool is_admin;
- public:
- User(string name_a, string password_a, bool admin)
- {
- name = name_a;
- password = password_a;
- is_admin = admin;
- }
- void setname(string new_name)
- {
- name = new_name;
- }
- string getpassword()
- {
- return password;
- }
- void setpassword(string new_password)
- {
- password = new_password;
- }
- string getname()
- {
- return name;
- }
- bool getis_admin()
- {
- return is_admin;
- }
- void setis_admin(bool new_isadmin)
- {
- is_admin = new_isadmin;
- }
- };
- class Part
- {
- private:
- int price;
- string manufacture_date;
- string expiry_date;
- string part_name;
- string company_name;
- public:
- Part(string part_name_, string company_name_, string manufacture_date_, string expiry_date_, int price_)
- {
- part_name = part_name_;
- company_name = company_name_;
- expiry_date = expiry_date_;
- manufacture_date = manufacture_date_;
- price = price_;
- }
- void setcompany_name(string new_company_name)
- {
- company_name = new_company_name;
- }
- string getmanufacture_date()
- {
- return manufacture_date;
- }
- void setmanufacture_date(string new_manufacture_date)
- {
- manufacture_date = new_manufacture_date;
- }
- string getexpiry_date()
- {
- return expiry_date;
- }
- string getpart_name()
- {
- return part_name;
- }
- void setpart_name(string new_part_name)
- {
- part_name = new_part_name;
- }
- string getcompany_name()
- {
- return company_name;
- }
- void setexpiry_date(string new_expiry_date)
- {
- expiry_date = new_expiry_date;
- }
- int getprice()
- {
- return price;
- }
- void setprice(int new_price)
- {
- price = new_price;
- }
- };
- void list_parts(vector<Part>& vector_part)
- {
- cout << endl;
- int i = 0;
- for (; i < vector_part.size(); i++)
- {
- cout << endl << "part number: " << i << endl;
- cout << "part name : " << vector_part[i].getpart_name() << endl;
- cout << "Company name : " << vector_part[i].getcompany_name() << endl;
- cout << "Manufacture date : " << vector_part[i].getmanufacture_date() << endl;
- cout << "Expiry date : " << vector_part[i].getexpiry_date() << endl;
- cout << "Price : " << vector_part[i].getprice() << endl;
- }
- }
- void add_user(vector<User>& vector_user)
- {
- string temp_username, temp_password;
- bool temp_admin;
- cout << endl;
- cout << "username : ";
- cin >> temp_username;
- cout << "password : ";
- cin >> temp_password;
- cout << "Admin (1/0) : ";
- cin >> temp_admin;
- vector_user.push_back(User(temp_username, temp_password, temp_admin));
- cout << endl;
- }
- void modify_part(vector<Part>& vector_part)
- {
- string p_name, nc_name, nm_date, ne_date;
- int p_number, n_price;
- cout << endl;
- cin >> p_number;
- int i = 0;
- for (; i < vector_part.size(); i++)
- {
- if (i == p_number)
- {
- break;
- }
- }
- if (i == vector_part.size())
- {
- cout << endl << "part not found." << endl << endl;
- }
- else
- {
- cout << endl << "new part name : ";
- cin >> p_name;
- cout << "company name : ";
- cin >> nc_name;
- cout << "manufacture date : ";
- cin >> nm_date;
- cout << "expiry date : ";
- cin >> ne_date;
- cout << "price : ";
- cin >> n_price;
- vector_part[i].setpart_name(p_name);
- vector_part[i].setcompany_name(nc_name);
- vector_part[i].setmanufacture_date(nm_date);
- vector_part[i].setexpiry_date(ne_date);
- vector_part[i].setprice(n_price);
- cout << endl;
- }
- }
- void add_to_cab(vector<pair<int, Part> >& vector_cab, vector<Part>& vector_part)
- {
- int prt;
- int qty;
- cout << endl;
- cout << "part number : ";
- cin >> prt;
- int i = 0;
- for (; i < vector_part.size(); i++)
- {
- if (prt == i)
- {
- cout << "quantity : ";
- cin >> qty;
- vector_cab.push_back(make_pair(qty, vector_part[i]));
- break;
- }
- }
- }
- void display_bill(vector<pair<int, Part> >& vector_cab)
- {
- int final_bill = 0;
- cout << endl;
- for (int i = 0; i < vector_cab.size(); i++)
- {
- cout << "unit price : " << vector_cab[i].second.getprice() << endl;
- cout << "quantity : " << vector_cab[i].first << endl;
- cout << endl << "part name : " << vector_cab[i].second.getpart_name() << endl;
- cout << "company name : " << vector_cab[i].second.getcompany_name() << endl;
- final_bill += vector_cab[i].first * vector_cab[i].second.getprice();
- }
- cout << endl << "total price : " << final_bill << endl << endl;
- }
- void delete_part(vector<Part>& vector_part)
- {
- string p_name;
- cout << endl;
- cout << "part name : ";
- cin >> p_name;
- int i = 0;
- for (; i < vector_part.size(); i++)
- {
- if (vector_part[i].getpart_name() == p_name)
- {
- vector_part.erase(vector_part.begin() + i);
- break;
- }
- }
- if (i == vector_part.size())
- {
- cout << endl << "part not found." << endl << endl;
- }
- }
- void delete_user(vector<User> & vector_user)
- {
- string n_name;
- cout << endl;
- cout << "username : ";
- cin >> n_name;
- int i = 0;
- for (; i < vector_user.size(); i++)
- {
- if (vector_user[i].getname() == n_name)
- {
- vector_user.erase(vector_user.begin() + i);
- break;
- }
- }
- if (i == vector_user.size())
- {
- cout << endl << "user not found." << endl << endl;
- }
- }
- void modify_user(vector<User> & vector_user)
- {
- string nu_name, n_name, n_password;
- cout << endl;
- cout << "username : ";
- cin >> n_name;
- int i = 0;
- for (; i < vector_user.size(); i++)
- {
- if (vector_user[i].getname() == n_name)
- {
- break;
- }
- }
- if (i == vector_user.size())
- {
- cout << endl << "user not found." << endl << endl;
- }
- else
- {
- cout << endl << "new username : ";
- cin >> nu_name;
- cout << "new password : ";
- cin >> n_password;
- vector_user[i].setname(nu_name);
- vector_user[i].setpassword(n_password);
- cout << endl;
- }
- }
- void add_part(vector<Part> & vector_part)
- {
- string p_name, c_name, m_date, e_date;
- int price;
- cout << endl;
- cout << "part name : ";
- cin >> p_name;
- cout << "company name : ";
- cin >> c_name;
- cout << "manufacture date : ";
- cin >> m_date;
- cout << "expiry date : ";
- cin >> e_date;
- cout << "price : ";
- cin >> price;
- vector_part.push_back(Part(p_name, c_name, m_date, e_date, price));
- cout << endl;
- }
- void admin_menu(bool& exit, bool& login, vector<User> & vector_user, vector<Part> & vector_part)
- {
- while (!exit && login) {
- cout << endl << "1) logout" << endl;
- cout << "2) exit" << endl;
- cout << "3) list parts" << endl;
- cout << "4) add new part" << endl;
- cout << "5) modify spare part" << endl;
- cout << "6) delete spare part" << endl;
- cout << "7) add new user" << endl;
- cout << "8) modify user" << endl;
- cout << "9) delete user" << endl;
- int choice;
- cout << "your choice (integer) : ";
- cin >> choice;
- cin.clear();
- cin.ignore();
- switch (choice)
- {
- case 1:
- login = false;
- break;
- case 2:
- exit = true;
- break;
- case 3:
- list_parts(vector_part);
- break;
- case 4:
- add_part(vector_part);
- break;
- case 5:
- modify_part(vector_part);
- break;
- case 6:
- delete_part(vector_part);
- break;
- case 7:
- add_user(vector_user);
- break;
- case 8:
- modify_user(vector_user);
- break;
- case 9:
- delete_user(vector_user);
- break;
- default:
- break;
- }
- }
- }
- void customer_menu(bool& exit, bool& login, vector<Part> & vector_part, vector<pair<int, Part> > & vector_cab)
- {
- while (!exit && login) {
- cout << endl << "1) logout" << endl;
- cout << "2) exit" << endl;
- cout << "3) list parts" << endl;
- cout << "4) add a part to the cab" << endl;
- cout << "5) display bill" << endl;
- int choice;
- cout << "your choice (integer) : ";
- cin >> choice;
- cout << choice;
- cin.clear();
- cin.ignore();
- switch (choice)
- {
- case 1:
- login = false;
- vector_cab.clear();
- break;
- case 2:
- exit = true;
- break;
- case 3:
- list_parts(vector_part);
- break;
- case 4:
- add_to_cab(vector_cab, vector_part);
- break;
- case 5:
- display_bill(vector_cab);
- break;
- default:
- break;
- }
- }
- }
- void save_user(vector<User> & vector_user)
- {
- ofstream output("user.txt");
- for (int i = 0; i < vector_user.size(); i++)
- {
- output << vector_user[i].getname() << endl << vector_user[i].getpassword() << endl << boolalpha << vector_user[i].getis_admin() << endl;
- }
- output.close();
- }
- void save_part(vector<Part> & vector_part)
- {
- ofstream output("part.txt");
- for (int i = 0; i < vector_part.size(); i++)
- {
- output << vector_part[i].getpart_name() << endl << vector_part[i].getcompany_name() << endl << vector_part[i].getmanufacture_date() << endl << vector_part[i].getexpiry_date() << endl << vector_part[i].getprice() << endl;
- }
- output.close();
- }
- void load_user(vector<User> & vector_user)
- {
- ifstream input("user.txt");
- string usr, pwd, admin;
- while (input >> usr >> pwd >> admin) {
- bool admin_bool = !admin.compare("true");
- vector_user.push_back(User(usr, pwd, admin_bool));
- }
- input.close();
- }
- void load_part(vector<Part> & vector_part)
- {
- ifstream input("part.txt");
- string p_name, c_name, m_date, e_date;
- int price;
- while (input >> p_name >> c_name >> m_date >> e_date >> price)
- {
- vector_part.push_back(Part(p_name, c_name, m_date, e_date, price));
- }
- input.close();
- }
- int loginf(string username, string password, vector<User> vector_user, bool& login)
- {
- int i = 0;
- for (; i < vector_user.size(); i++)
- {
- if (vector_user[i].getname() == username && vector_user[i].getpassword() == password)
- {
- cout << "welcome " << username << endl;
- login = true;
- break;
- }
- }
- if (i == vector_user.size())
- cout << "incorrect login !" << endl;
- return i;
- }
- int main(int argc, char** argv)
- {
- cout << endl << "Hello, please login :" << endl;
- bool exit = false;
- bool login = false;
- vector<User> vector_user;
- vector<Part> vector_part;
- vector<pair<int, Part> > vector_cab;
- load_user(vector_user);
- load_part(vector_part);
- int index = 0;
- while (!exit) {
- while (!login) {
- string temp_username;
- string temp_password;
- cout << endl << "username : ";
- cin >> temp_username;
- cout << "password : ";
- cin >> temp_password;
- index = loginf(temp_username, temp_password, vector_user, login);
- }
- if (vector_user[index].getis_admin())
- {
- admin_menu(exit, login, vector_user, vector_part);
- }
- else
- {
- customer_menu(exit, login, vector_part, vector_cab);
- }
- }
- save_user(vector_user);
- save_part(vector_part);
- return 0;
- }
- // Exécuter le programme : Ctrl+F5 ou menu Déboguer > Exécuter sans débogage
- // Déboguer le programme : F5 ou menu Déboguer > Démarrer le débogage
- // Conseils pour bien démarrer :
- // 1. Utilisez la fenêtre Explorateur de solutions pour ajouter des fichiers et les gérer.
- // 2. Utilisez la fenêtre Team Explorer pour vous connecter au contrôle de code source.
- // 3. Utilisez la fenêtre Sortie pour voir la sortie de la génération et d'autres messages.
- // 4. Utilisez la fenêtre Liste d'erreurs pour voir les erreurs.
- // 5. Accédez à Projet > Ajouter un nouvel élément pour créer des fichiers de code, ou à Projet > Ajouter un élément existant pour ajouter des fichiers de code existants au projet.
- // 6. Pour rouvrir ce projet plus tard, accédez à Fichier > Ouvrir > Projet et sélectionnez le fichier .sln.
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement