Advertisement
Zeinab_Hamdy

Final

Mar 26th, 2023
221
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 17.44 KB | None | 0 0
  1. #include <bits/stdc++.h>
  2. using namespace std;
  3.  
  4. class Customer
  5. {
  6.  
  7.     int id ;
  8.     char name[20], password[20], username[20];
  9.  
  10. public :
  11.  
  12.  
  13.     int getId()
  14.     {
  15.         return id;
  16.     }
  17.  
  18.     char* getName()
  19.     {
  20.         return name;
  21.     }
  22.  
  23.     char* getPassword()
  24.     {
  25.         return password;
  26.     }
  27.  
  28.     char* getUsername()
  29.     {
  30.         return username;
  31.     }
  32.  
  33.  
  34.     bool searchId(int val)
  35.     {
  36.  
  37.         ifstream inFile("Customer.txt", ios::in);
  38.  
  39.         if(inFile.is_open())
  40.         {
  41.             Customer c1 ;
  42.             while(!inFile.eof())
  43.             {
  44.                 inFile.read( (char *)&c1, sizeof(c1) );
  45.  
  46.                 if(c1.getId() == val) return true;
  47.             }
  48.  
  49.         }
  50.         //    else
  51.         //      cout <<"Cann't be opened the file \n" ;
  52.         inFile.close();
  53.  
  54.         return false;
  55.     }
  56.  
  57.  
  58.     bool validId(double val)
  59.     {
  60.         return val > 0 and int(val)==val and !searchId(val) ;
  61.     }
  62.  
  63.     void setID() // new user only
  64.     {
  65.         double x ;
  66.         cout << "Enter ID : ";
  67.         cin >> x;
  68.  
  69.  
  70.         while(!validId(x))
  71.         {
  72.             cout <<"Invalid Id , please try again \n";
  73.             cout << "Enter ID : ";
  74.             cin >> x;
  75.  
  76.         }
  77.         id = int(x);
  78.     }
  79.  
  80.  
  81.     void setName()
  82.     {
  83.         cout <<"Enter Name : ";
  84.         cin >> name;
  85.     }
  86.  
  87.     bool validUsername(char val[20])    // search if found same username in the file
  88.     {
  89.  
  90.         ifstream inFile("Customer.txt", ios::in);
  91.  
  92.         if(inFile.is_open())
  93.         {
  94.             Customer c1 ;
  95.             while(!inFile.eof())
  96.             {
  97.                 inFile.read( (char *)&c1, sizeof(c1) );
  98.  
  99.                 if(strcmp(c1.getUsername(), val)==0 ) return false;
  100.             }
  101.         }
  102.         //   else
  103.         //      cout <<"Cann't be opened the file \n" ;
  104.  
  105.         inFile.close();
  106.  
  107.         return true;
  108.     }
  109.  
  110.     void setUsername( int option )
  111.     {
  112.         if(option == 1) // new user -> check if valid username
  113.         {
  114.             char temp[20];
  115.             cout <<"Enter username : " ;
  116.             cin >> temp;
  117.  
  118.             while(!validUsername(temp))
  119.             {
  120.                 cout <<"Invalid username , please try again \n";
  121.                 cout << "Enter username : ";
  122.                 cin >> temp;
  123.             }
  124.  
  125.             for(int i=0; i< 20; i++) username[i]=temp[i];
  126.  
  127.         }
  128.  
  129.         else  // old user
  130.         {
  131.             cout << "Enter username : ";
  132.             cin >> username;
  133.         }
  134.     }
  135.  
  136.     void setPassword()
  137.     {
  138.         cout <<"Enter Password : ";
  139.         cin >> password;
  140.     }
  141.  
  142.  
  143.     bool validLogin( char user[20], char pass [20] )
  144.     {
  145.         ifstream inFile("Customer.txt", ios::in);
  146.  
  147.         if(inFile.is_open())
  148.         {
  149.             Customer c1 ;
  150.             while(!inFile.eof())
  151.             {
  152.                 inFile.read( (char *)&c1, sizeof(c1) );
  153.  
  154.                 if(strcmp(c1.getUsername(), user)==0
  155.                 and strcmp(c1.getPassword(), pass)==0)
  156.                 {
  157.                     cout << "\n\tLogged in Successfully \n\t\tWelcome\n\n";
  158.                     cout << "Id\tname\tusername\tpassword\n";
  159.                     cout << c1.getId() << '\t' << c1.getName() << '\t' <<
  160.                          c1.getUsername() << '\t' << c1.getPassword() << "\n\n";
  161.                     return true;
  162.                 }
  163.             }
  164.         }
  165.  
  166.         //  else
  167.         //     cout <<"Cann't be opened the file \n" ;
  168.  
  169.          inFile.close();
  170.  
  171.         return false;
  172.     }
  173.  
  174.     string decrypted(string s)
  175.     {
  176.         for(int i=0 ; i < 20 ; i++ )
  177.             s[i]+='&';
  178.         return s;
  179.     }
  180.  
  181.     string incrypted(string s)
  182.     {
  183.         for(int i=0 ; i < 20 ; i++ )
  184.             s[i]-='&';
  185.         return s;
  186.     }
  187.     void printCustomers()
  188.     {
  189.         ifstream inFile("Customer.txt", ios::in);
  190.         if(inFile.is_open())
  191.         {
  192.             Customer c1 ;
  193.             cout <<"ID\tName\tUsername\tpassword\n\n";
  194.             while(!inFile.eof())
  195.             {
  196.                 inFile.read((char *)&c1, sizeof(c1));
  197.  
  198.  
  199.                 string temp  = c1.getPassword();
  200.  
  201.                 if(!inFile.eof())
  202.                     cout <<c1.getId() <<'\t' << c1.getName() <<'\t' <<
  203.                          c1.getUsername() << '\t' << decrypted(temp) << "\n";
  204.             }
  205.         }
  206.         //     else cout <<"Cann't opened this file\n";
  207.  
  208.         inFile.close();
  209.  
  210.     }
  211.  
  212. };
  213.  
  214.  
  215. class Employee
  216. {
  217.  
  218.     int id ;
  219.     char name[20], password[20], username[20];
  220.  
  221. public :
  222.  
  223.  
  224.     int getId()
  225.     {
  226.         return id;
  227.     }
  228.  
  229.     char* getName()
  230.     {
  231.         return name;
  232.     }
  233.  
  234.     char* getPassword()
  235.     {
  236.         return password;
  237.     }
  238.  
  239.     char* getUsername()
  240.     {
  241.         return username;
  242.     }
  243.  
  244.  
  245.  
  246.     // search about id in file of customers
  247.     bool searchId(int val)
  248.     {
  249.  
  250.         ifstream inFile("Employee.txt", ios::in);
  251.  
  252.         if(inFile.is_open())
  253.         {
  254.             Employee e1 ;
  255.             while(!inFile.eof())
  256.             {
  257.                 inFile.read( (char *)&e1, sizeof(e1) );
  258.  
  259.                 if(e1.getId() == val) return true;
  260.             }
  261.  
  262.         }
  263.         //  else
  264.         //    cout <<"Cann't be opened the file \n" ;
  265.         inFile.close();
  266.  
  267.         return false;
  268.     }
  269.  
  270.  
  271.     bool validId(double val)
  272.     {
  273.         return val > 0 and int(val)==val and !searchId(val) ;
  274.     }
  275.  
  276.     void setID() // new user only
  277.     {
  278.         double x ;
  279.         cout << "Enter ID : ";
  280.         cin >> x;
  281.  
  282.  
  283.         while(!validId(x))
  284.         {
  285.             cout <<"Invalid Id , please try again \n";
  286.             cout << "Enter ID : ";
  287.             cin >> x;
  288.  
  289.         }
  290.         id = int(x);
  291.     }
  292.  
  293.  
  294.     void setName()
  295.     {
  296.         cout <<"Enter Name : ";
  297.         cin >> name;
  298.     }
  299.  
  300.     bool validUsername(char val[20])    // search if found same username in the file
  301.     {
  302.  
  303.         ifstream inFile("Employee.txt", ios::in);
  304.  
  305.         if(inFile.is_open())
  306.         {
  307.             Employee e1 ;
  308.             while(!inFile.eof())
  309.             {
  310.                 inFile.read( (char *)&e1, sizeof(e1) );
  311.  
  312.                 if(strcmp(e1.getUsername(), val)==0 ) return false;
  313.             }
  314.         }
  315.         // else
  316.         //   cout <<"Cann't be opened the file \n" ;
  317.  
  318.         inFile.close();
  319.  
  320.         return true;
  321.     }
  322.  
  323.     void setUsername( int option )
  324.     {
  325.         if(option == 1) // new user -> check if valid username
  326.         {
  327.             char temp[20];
  328.             cout <<"Enter username : " ;
  329.             cin >> temp;
  330.  
  331.             while(!validUsername(temp))
  332.             {
  333.                 cout <<"Invalid username , please try again \n";
  334.                 cout << "Enter username : ";
  335.                 cin >> temp;
  336.             }
  337.  
  338.             for(int i=0; i< 20; i++) username[i]=temp[i];
  339.  
  340.         }
  341.  
  342.         else  // old user
  343.         {
  344.             cout << "Enter username : ";
  345.             cin >> username;
  346.         }
  347.     }
  348.  
  349.     void setPassword()
  350.     {
  351.         cout <<"Enter Password : ";
  352.         cin >> password;
  353.     }
  354.  
  355.  
  356.     bool validLogin( char user[20], char pass [20] )
  357.     {
  358.         ifstream inFile("Employee.txt", ios::in);
  359.  
  360.         if(inFile.is_open())
  361.         {
  362.             Employee e1 ;
  363.             while(!inFile.eof())
  364.             {
  365.                 inFile.read( (char *)&e1, sizeof(e1) );
  366.  
  367.                 if(strcmp(e1.getUsername(), user)==0
  368.                         and strcmp(e1.getPassword(), pass)==0)
  369.                 {
  370.                     cout << "\tLogged in Successfully \n\t\t\tWelcome\n";
  371.                     cout << "Id\tname\tusername\tpassword\n";
  372.                     cout << e1.getId() << '\t' << e1.getName() << '\t' <<
  373.                          e1.getUsername() << '\t' << e1.getPassword() << "\n";
  374.                     return true;
  375.                 }
  376.             }
  377.         }
  378.         //  else
  379.         //    cout <<"Cann't be opened the file \n" ;
  380.  
  381.         inFile.close();
  382.  
  383.         return false;
  384.     }
  385.  
  386.     string decrypted(string s)
  387.     {
  388.         for(int i=0 ; i < 20 ; i++ )
  389.             s[i]+='&';
  390.         return s;
  391.     }
  392.  
  393.     string incrypted(string s)
  394.     {
  395.         for(int i=0 ; i < 20 ; i++ )
  396.             s[i]-='&';
  397.         return s;
  398.     }
  399.     void printEmployee()
  400.     {
  401.         ifstream inFile("Employee.txt", ios::in);
  402.         if(inFile.is_open())
  403.         {
  404.             Employee e1 ;
  405.             cout <<"ID\tName\tUsername\tpassword\n\n";
  406.             while(!inFile.eof())
  407.             {
  408.                 inFile.read((char *)&e1, sizeof(e1));
  409.  
  410.  
  411.                 string temp  = e1.getPassword();
  412.  
  413.                 if(!inFile.eof())
  414.                     cout <<e1.getId() <<'\t' << e1.getName() <<'\t' <<
  415.                          e1.getUsername() << '\t' << decrypted(temp) << "\n";
  416.             }
  417.         }
  418.         //    else cout <<"Cann't opened this file\n";
  419.  
  420.         inFile.close();
  421.  
  422.     }
  423.  
  424. };
  425.  
  426.  
  427.  
  428.  
  429.  
  430.  
  431. class Product
  432. {
  433.     int id, price;
  434.     char name[20];
  435.  
  436. public:
  437.  
  438.     void setId()
  439.     {
  440.         cout << "Enter ID : ";
  441.         cin >> id;
  442.     }
  443.  
  444.     int getId()
  445.     {
  446.         return id;
  447.     }
  448.  
  449.  
  450.  
  451.     void setPrice()
  452.     {
  453.         cout << "Enter price : ";
  454.         cin >> price;
  455.     }
  456.  
  457.     int getPrice()
  458.     {
  459.         return price;
  460.     }
  461.  
  462.     void setName()
  463.     {
  464.         cout << "Enter name : ";
  465.         cin >> name;
  466.     }
  467.  
  468.     char *getName()
  469.     {
  470.         return name;
  471.     }
  472.  
  473.  
  474. };
  475.  
  476. void writeProduct()
  477. {
  478.     fstream out("Product.txt", ios::out | ios::app);
  479.     Product p1;
  480.     char ch;
  481.     do
  482.     {
  483.         p1.setId();
  484.         p1.setName();
  485.         p1.setPrice();
  486.  
  487.         out.write((char *)&p1, sizeof(p1));
  488.         cout << "if you want to enter another data\tpress Y/N \n";
  489.         cin >> ch;
  490.     }
  491.     while (ch == 'Y' || ch == 'y');
  492.     out.close();
  493. }
  494.  
  495. void readProduct()
  496. {
  497.     ifstream in("Product.txt", ios::in);
  498.  
  499.     if (in.is_open())
  500.     {
  501.         Product p2;
  502.         cout << "Id\tName\tPrice\n";
  503.         while (!in.eof())
  504.         {
  505.             in.read((char *)&p2, sizeof(p2));
  506.             if (!in.eof())
  507.                 cout << p2.getId() << '\t' << p2.getName() << '\t' << p2.getPrice() << "\n";
  508.         }
  509.     }
  510.     //else
  511.     //   cout << "Can't open the file \n";
  512.  
  513.     in.close();
  514. }
  515.  
  516. void searchProduct()
  517. {
  518.     ifstream in2("Product.txt", ios::in);
  519.  
  520.     if (in2.is_open())
  521.     {
  522.         Product p3;
  523.         bool found = false;
  524.         char str[20];
  525.  
  526.         cout << "Enter name to search for : ";
  527.         cin >> str;
  528.  
  529.         in2.read((char *)&p3, sizeof(p3));
  530.         while (!in2.eof())
  531.         {
  532.             if (strcmp(str, p3.getName()) == 0)
  533.             {
  534.                 cout << p3.getId() << '\t' << p3.getName() << '\t' << p3.getPrice() << "\n";
  535.                 found = true;
  536.                 break;
  537.             }
  538.  
  539.             in2.read((char *)&p3, sizeof(p3));
  540.         }
  541.  
  542.         if (!found)
  543.             cout << "Name not found\n";
  544.  
  545.         in2.close();
  546.     }
  547.     // else
  548.     //     cout << "Cannot open this file\n";
  549. }
  550.  
  551. void updateProduct()
  552. {
  553.     Product p4;
  554.     fstream f("Product.txt", ios::in | ios::out);
  555.     if (f.is_open())
  556.     {
  557.         char str[20];
  558.         cout << "Enter the product name to update his record : ";
  559.         cin >> str;
  560.         bool found = false;
  561.         while (!f.eof())
  562.         {
  563.             f.read((char *)&p4, sizeof(p4));
  564.  
  565.             if (strcmp(str, p4.getName()) == 0)
  566.             {
  567.                 p4.setPrice();
  568.                  int curpos = f.tellg(), stusize = sizeof(p4);
  569.  
  570.                 f.seekp(curpos - stusize, ios::beg);
  571.                 f.write((char *)&p4, sizeof(p4));
  572.                 found = true;
  573.                 break;
  574.             }
  575.         }
  576.         f.close();
  577.         if (!found)
  578.             cout << "Product name does not exist.\n";
  579.     }
  580. }
  581.  
  582. void deleteRecord()
  583. {
  584.     fstream in("Product.txt", ios::in);
  585.     ofstream out("Product Temp.txt", ios::out);
  586.     if (in.is_open())
  587.     {
  588.         Product p5;
  589.  
  590.         int num;
  591.         cout << "Enter the id of product to delete his record : ";
  592.         cin >> num;
  593.  
  594.         while (in.read((char *)&p5, sizeof(p5)))
  595.             if (num!=p5.getId())
  596.                 out.write((char *)&p5, sizeof(p5));
  597.  
  598.         in.close();
  599.         out.close();
  600.         remove("Product.txt");
  601.         rename("Product Temp.txt", "Product.txt");
  602.     }
  603. }
  604.  
  605.  
  606. int main()
  607. {
  608.  
  609.     int ch;
  610.     cout << "1.customer\t2.employee\nEnter Your choise : " ;
  611.     cin >> ch;
  612.     cout <<"\n";
  613.  
  614.     if(ch==1)  // customer
  615.     {
  616.         int totalPrice =0;
  617.         int op;
  618.         cout <<"1.log in\t2.sign up\nEnter Your choise : ";
  619.         cin >> op;
  620.  
  621.         cout <<"\n";
  622.         Customer c1;
  623.  
  624.         if(op==1)  // old user
  625.         {
  626.             c1.setUsername(2);
  627.             c1.setPassword();
  628.  
  629.             while(!c1.validLogin(c1.getUsername(), c1.getPassword()))
  630.             {
  631.                 cout << "Invalid log in , please try again :\n";
  632.                 c1.setUsername(2);
  633.                 c1.setPassword();
  634.             }
  635.         }
  636.  
  637.         else   // new user
  638.         {
  639.             c1.setID();
  640.             c1.setName();
  641.             c1.setUsername(1);
  642.             c1.setPassword();
  643.  
  644.             ofstream i1("Customer.txt", ios::out | ios::app);
  645.             i1.write( (char*)&c1, sizeof(c1));
  646.             i1.close();
  647.  
  648.         }
  649.  
  650.  
  651.         cout << "\nMenu for our products :\n\n";
  652.  
  653.         char con='Y';
  654.         ofstream out("Sales.txt", ios::out ); // products that user buys it
  655.  
  656.         do
  657.         {
  658.  
  659.             readProduct();
  660.  
  661.             int ID;
  662.             cout <<"\nEnter id of the product that you want to buy it : " ;
  663.             cin >> ID;
  664.  
  665.  
  666.             ifstream in2("Product.txt", ios::in);
  667.             if(in2.is_open())
  668.             {
  669.                 Product p2 ;
  670.                 while(!in2.eof())
  671.                 {
  672.  
  673.                     in2.read((char *)&p2, sizeof(p2));
  674.  
  675.                     if(in2.eof())  break;
  676.  
  677.                     if(ID==p2.getId())
  678.                     {
  679.                         totalPrice+=p2.getPrice();
  680.                         out.write( (char *)&p2, sizeof(p2));
  681.                         break;
  682.                     }
  683.  
  684.                 }
  685.             }
  686.             //    else cout <<"Cann't open the file \n";
  687.  
  688.  
  689.             in2.close();
  690.  
  691.             cout << "\nDo you want to buy other products  [ Y/N ] : ";
  692.             cin >> con;
  693.  
  694.         }
  695.         while(con=='Y');
  696.         out.close();
  697.  
  698.         cout <<"\nYour sales :\n";
  699.  
  700.         ifstream In("Sales.txt", ios::in);
  701.         if(In.is_open())
  702.         {
  703.             Product p;
  704.             cout <<"ID\tName\tPrice : \n\n" ;
  705.  
  706.             while(!In.eof())
  707.             {
  708.                 In.read( (char *)&p, sizeof(p) );
  709.                 if(!In.eof())
  710.                     cout << p.getId() << "\t" << p.getName() << '\t' << p.getPrice() << "\n";
  711.             }
  712.             cout <<"\nTotal Price : " << totalPrice << "\n";
  713.         }
  714.  
  715.         else cout <<"the file is empty \n";
  716.  
  717.         In.close();
  718.  
  719.  
  720.         int want = 0;
  721.         cout <<"\nif you want to know all users : press 1 ";
  722.         cin >> want ;
  723.         if(want)
  724.         {
  725.             Customer C;
  726.             C.printCustomers();
  727.         }
  728.     }
  729.  
  730.  
  731.     else  // employee
  732.     {
  733.  
  734.  
  735.         int op;
  736.         cout <<"1.log in\t2.sign up\nEnter Your choise : ";
  737.         cin >> op;
  738.  
  739.         cout <<"\n";
  740.         Employee e1;
  741.  
  742.         if(op==1)  // old user
  743.         {
  744.             e1.setUsername(2);
  745.             e1.setPassword();
  746.  
  747.             while(!e1.validLogin(e1.getUsername(), e1.getPassword()))
  748.             {
  749.                 cout << "Invalid log in , please try again :\n";
  750.                 e1.setUsername(2);
  751.                 e1.setPassword();
  752.             }
  753.         }
  754.  
  755.         else   // new user
  756.         {
  757.             e1.setID();
  758.             e1.setName();
  759.             e1.setUsername(1);
  760.             e1.setPassword();
  761.  
  762.             ofstream i1("Employee.txt", ios::out | ios::app);
  763.             i1.write( (char*)&e1, sizeof(e1));
  764.             i1.close();
  765.  
  766.         }
  767.  
  768.         char cn='Y';
  769.  
  770.         do
  771.         {
  772.             int opt;
  773.  
  774.             cout << "\n1.add new products \n";
  775.             cout << "2.search about product by name  \n";
  776.             cout << "3.know all products \n";
  777.             cout << "4.update price of any product by name \n";
  778.             cout << "5.delete any product by id \n";
  779.             cout << "6.exit\n";
  780.             cout << "7.know all Enployees \n\n";
  781.             cout << "Enter Your choise : \n";
  782.             cin >> opt;
  783.  
  784.  
  785.  
  786.             if(opt==1)  // writeProduct
  787.             {
  788.                 writeProduct();
  789.                 cout << "\n";
  790.             }
  791.             else if(opt==2)
  792.             {
  793.                 searchProduct();
  794.                 cout << "\n";
  795.             }
  796.             else if(opt==3)
  797.             {
  798.                 readProduct();
  799.                 cout << "\n";
  800.             }
  801.             else if(opt==4)
  802.             {
  803.                 updateProduct();
  804.                 cout << "\n";
  805.             }
  806.             else if(opt==5)
  807.             {
  808.                 deleteRecord();
  809.             }
  810.             else if(opt==7)
  811.             {
  812.                 Employee e;
  813.                 e.printEmployee();
  814.             }
  815.             else
  816.              return 0;
  817.  
  818.             cout <<"Do you want to do anything more : [ Y/N ] \n";
  819.             cin >> cn;
  820.  
  821.  
  822.         }
  823.         while( cn=='Y');
  824.  
  825.     }
  826.  
  827.     return 0;
  828. }
  829.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement