Advertisement
Bassel_11

Untitled

Mar 26th, 2023
562
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 11.26 KB | None | 0 0
  1. #include <iostream>
  2. #include <fstream>>
  3. #include <string.h>
  4.  
  5. using namespace std;
  6.  
  7. class Passengers
  8. {
  9. public:
  10.     char name[20];
  11.     int id, age;
  12.     char destination[30];
  13.  
  14.     void WriteText()
  15.     {
  16.         ofstream out;
  17.         out.open("test.txt", ios::out | ios::app);
  18.         if (out.is_open())
  19.         {
  20.             out << "hello guys"
  21.                 << "\n";
  22.             out.close();
  23.         }
  24.         else
  25.         {
  26.             cout << "file can't be found"
  27.                  << "\n";
  28.         }
  29.     }
  30.  
  31.     void ReadText()
  32.     {
  33.         char c, str[20];
  34.         fstream in;
  35.         in.open("test.txt", ios::in);
  36.         if (in.is_open())
  37.         {
  38.             in >> str;
  39.             cout << str;
  40.             in.get(c);
  41.             cout << c;
  42.             in.get(str, 20);
  43.             cout << str;
  44.             in.close();
  45.         }
  46.         else
  47.         {
  48.             cout << "file is not found"
  49.                  << "\n";
  50.         }
  51.     }
  52.  
  53.     void writePassenger()
  54.     {
  55.         char ch;
  56.         fstream outfile;
  57.         outfile.open("Passengers.txt", ios::out | ios::app);
  58.         if (outfile.is_open())
  59.         {
  60.             Passengers p1;
  61.             do
  62.             {
  63.                 cout << "enter passenger's id : ";
  64.                 cin >> p1.id;
  65.                 cout << "enter passenger's name : ";
  66.                 cin >> p1.name;
  67.                 cout << "enter passenger's age : ";
  68.                 cin >> p1.age;
  69.                 cout << "enter passenger's destination : ";
  70.                 cin >> p1.destination;
  71.                 cout << "passenger's Information have been saved successfully"
  72.                      << "\n";
  73.                 cout << "do you want to enter new passengers (y/n) : ";
  74.                 cin >> ch;
  75.                 outfile.write((char *)&p1, sizeof(p1));
  76.             }
  77.             while (ch == 'y');
  78.             outfile.close();
  79.         }
  80.         else
  81.         {
  82.             cout << "file is not found"
  83.                  << "\n";
  84.         }
  85.     }
  86.  
  87.     void Readpassengers()
  88.     {
  89.  
  90.         Passengers p1;
  91.         fstream in;
  92.         int id = 0;
  93.         in.open("Passengers.txt", ios::in);
  94.         if (in.is_open())
  95.         {
  96.             cout << "ID"
  97.                  << "\t"
  98.                  << "name"
  99.                  << "\t"
  100.                  << "age"
  101.                  << "\t"
  102.                  << "Destination"
  103.                  << "\n";
  104.             in.read((char *)&p1, sizeof(p1));
  105.             while (!in.eof())
  106.             {
  107.                 if (p1.id != id)
  108.                 {
  109.                     cout << p1.id << "\t" << p1.name << "\t" << p1.age << "\t" << p1.destination << "\n";
  110.                     in.read((char *)&p1, sizeof(p1));
  111.                 }
  112.             }
  113.             in.close();
  114.         }
  115.         else
  116.         {
  117.             cout << "file is not found"
  118.                  << "\n";
  119.         }
  120.     }
  121.  
  122.     void SearchforName()
  123.     {
  124.         bool found = false;
  125.         char str[20];
  126.         cout << "write a name to search for : ";
  127.         cin >> str;
  128.         Passengers p1;
  129.         fstream in;
  130.         int id = 0;
  131.         in.open("Passengers.txt", ios::in);
  132.         if (in.is_open())
  133.         {
  134.  
  135.             in.read((char *)&p1, sizeof(p1));
  136.             while (!in.eof())
  137.             {
  138.                 if (strcmp(str, p1.name) == 0)
  139.                 {
  140.                     cout << "ID"
  141.                          << "\t"
  142.                          << "name"
  143.                          << "\t"
  144.                          << "age"
  145.                          << "\t"
  146.                          << "Destination"
  147.                          << "\n";
  148.                     cout << p1.id << "\t" << p1.name << "\t" << p1.age << "\t" << p1.destination << "\n";
  149.                     found = true;
  150.                 }
  151.                 in.read((char *)&p1, sizeof(p1));
  152.             }
  153.             if (!found)
  154.             {
  155.                 cout << "Name is not found"
  156.                      << "\n";
  157.             }
  158.             in.close();
  159.         }
  160.         else
  161.         {
  162.             cout << "file is not found"
  163.                  << "\n";
  164.         }
  165.     }
  166.  
  167.     void CalculateLength()
  168.     {
  169.         fstream in;
  170.         in.open("test.txt", ios::in);
  171.         if (in.is_open())
  172.         {
  173.             in.seekg(0, ios::end);
  174.             cout << "the length of the file is : " << in.tellg();
  175.             in.close();
  176.         }
  177.     }
  178.  
  179.     void CopyContent()
  180.     {
  181.         char ch;
  182.         fstream in;
  183.         fstream out;
  184.         in.open("test.txt", ios::in);
  185.         out.open("copy test.txt", ios::out);
  186.         if (in.is_open())
  187.         {
  188.             while (in.get(ch))
  189.                 out.put(ch);
  190.             in.close();
  191.             out.close();
  192.         }
  193.     };
  194.  
  195.     void updatepassenger()
  196.     {
  197.         bool found = false;
  198.         char name[20];
  199.         cout << "Enter the name of the passenger to update: ";
  200.         cin >> name;
  201.  
  202.         Passengers p;
  203.         fstream file;
  204.         file.open("Passengers.txt", ios::in | ios::out | ios::binary);
  205.  
  206.         if (file.is_open())
  207.         {
  208.             while (!file.eof())
  209.             {
  210.                 streampos pos = file.tellg();
  211.                 file.read((char*)&p, sizeof(p));
  212.  
  213.                 if (strcmp(p.name, name) == 0)
  214.                 {
  215.                     cout << "What do you want to update? (name/age/id/destination): ";
  216.                     char field[20];
  217.                     cin >> field;
  218.  
  219.                     if (strcmp(field, "name") == 0)
  220.                     {
  221.                         char new_name[20];
  222.                         cout << "Enter the new name: ";
  223.                         cin >> new_name;
  224.                         strcpy(p.name, new_name);
  225.                     }
  226.                     else if (strcmp(field, "age") == 0)
  227.                     {
  228.                         int new_age;
  229.                         cout << "Enter the new age: ";
  230.                         cin >> new_age;
  231.                         p.age = new_age;
  232.                     }
  233.                     else if (strcmp(field, "id") == 0)
  234.                     {
  235.                         int new_id;
  236.                         cout << "Enter the new id: ";
  237.                         cin >> new_id;
  238.                         p.id = new_id;
  239.                     }
  240.                     else if (strcmp(field, "destination") == 0)
  241.                     {
  242.                         char new_dest[30];
  243.                         cout << "Enter the new destination: ";
  244.                         cin >> new_dest;
  245.                         strcpy(p.destination, new_dest);
  246.                     }
  247.                     else
  248.                     {
  249.                         cout << "Invalid field name. Please try again." << endl;
  250.                         return;
  251.                     }
  252.  
  253.                     file.seekp(pos);
  254.                     file.write((char*)&p, sizeof(p));
  255.                     found = true;
  256.                     cout << "Passenger updated successfully." << endl;
  257.                     break;
  258.                 }
  259.             }
  260.             if (!found)
  261.             {
  262.                 cout << "Passenger not found." << endl;
  263.             }
  264.             file.close();
  265.         }
  266.         else
  267.         {
  268.             cout << "File not found." << endl;
  269.         }
  270.     }
  271.  
  272.     void deletepassenger()
  273.     {
  274.         Passengers p1;
  275.         p1.Readpassengers();
  276.  
  277.         bool found = false;
  278.         char str[20];
  279.         cout << "write a name to delete : ";
  280.         cin >> str;
  281.         fstream in;
  282.         fstream out;
  283.         out.open("temp.txt", ios::out);
  284.         int id = 0;
  285.         in.open("Passengers.txt", ios::in);
  286.         if (in.is_open())
  287.         {
  288.  
  289.             in.read((char *)&p1, sizeof(p1));
  290.             while (!in.eof())
  291.             {
  292.                 if (strcmp(str, p1.name) != 0)
  293.                 {
  294.                     out.write((char *)&p1, sizeof(p1));
  295.                     found = true;
  296.                 }
  297.                 in.read((char *)&p1, sizeof(p1));
  298.             }
  299.             if (!found)
  300.                 cout << "Name is not found"
  301.                      << "\n";
  302.  
  303.             in.close();
  304.             out.close();
  305.             remove("Passengers.txt");
  306.             rename("temp.txt", "Passengers.txt");
  307.         }
  308.         else
  309.         {
  310.             cout << "file is not found"
  311.                  << "\n";
  312.         }
  313.         p1.Readpassengers();
  314.     }
  315. };
  316. int main()
  317. {
  318.     Passengers p1;
  319.     int ch;
  320.     do
  321.     {
  322.         cout << "------------BUS RESERVATION SYSTEM---------------"
  323.              << "\n";
  324.         cout << "(Choose one of these to do)"
  325.              << "\n";
  326.         cout << "1 - Enter new passenger"
  327.              << "\n";
  328.         cout << "2 - View all passengers"
  329.              << "\n";
  330.         cout << "3 - Search for a passenger"
  331.              << "\n";
  332.         cout << "4 - Change passenger information"
  333.              << "\n";
  334.         cout << "5 - Delete a passenger"
  335.              << "\n";
  336.         cout << "6 - Exit"
  337.              << "\n";
  338.         cout << "Your choice : ";
  339.         cin >> ch;
  340.  
  341.         switch (ch)
  342.         {
  343.         case 1:
  344.             p1.writePassenger();
  345.             break;
  346.         case 2:
  347.             p1.Readpassengers();
  348.             break;
  349.         case 3:
  350.             p1.SearchforName();
  351.             break;
  352.         case 4:
  353.             p1.updatepassenger();
  354.             break;
  355.         case 5:
  356.             p1.deletepassenger();
  357.             break;
  358.         case 6:
  359.             cout << "\n Thanks for using our System , we hope you like it :)"
  360.                  << "\n";
  361.             return 0;
  362.         }
  363.     }
  364.     while (ch >= 1 && ch <= 6);
  365.  
  366.     if (ch < 1 || ch > 6)
  367.     {
  368.         cout << "\n please choose one of the shown choices"
  369.              << "\n"
  370.              << "\n";
  371.         do
  372.         {
  373.             cout << "------------BUS RESERVATION SYSTEM---------------"
  374.                  << "\n";
  375.             cout << "((Choose one of these to do))"
  376.                  << "\n";
  377.             cout << "1 - Enter new passenger"
  378.                  << "\n";
  379.             cout << "2 - View all passengers"
  380.                  << "\n";
  381.             cout << "3 - Search for a passenger"
  382.                  << "\n";
  383.             cout << "4 - Change passenger information"
  384.                  << "\n";
  385.             cout << "5 - Delete a passenger"
  386.                  << "\n";
  387.             cout << "6 - Exit"
  388.                  << "\n";
  389.             cout << "Your choice : ";
  390.             cin >> ch;
  391.  
  392.             switch (ch)
  393.             {
  394.             case 1:
  395.                 p1.writePassenger();
  396.                 cout << "\n";
  397.                 break;
  398.             case 2:
  399.                 p1.Readpassengers();
  400.                 cout << "\n";
  401.                 break;
  402.             case 3:
  403.                 p1.SearchforName();
  404.                 cout << "\n";
  405.                 break;
  406.             case 4:
  407.                 p1.updatepassenger();
  408.                 cout << "\n";
  409.                 break;
  410.             case 5:
  411.                 p1.deletepassenger();
  412.                 cout << "\n";
  413.                 break;
  414.             case 6:
  415.                 cout << "\n Thanks for using our System , we hope you like it :)"
  416.                      << "\n";
  417.                 return 0;
  418.             }
  419.         }
  420.         while (ch > 6 || ch < 1);
  421.     }
  422.  
  423.     return 0;
  424. }
  425.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement