Advertisement
Guest User

Untitled

a guest
Aug 18th, 2016
91
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 5.50 KB | None | 0 0
  1. int userRegistration()
  2. {
  3.         int RegistrationMenuChoice;
  4.         int newUserRole;
  5.         string newUserRoleChoice;
  6.         //string user;
  7.         ofstream UserRegistration;
  8.         try
  9.         {
  10.             do{
  11.                 cout << "-----------------------------------" << endl;
  12.                 cout << "User Registration Menu" << endl;
  13.                 cout << "-----------------------------------" << endl;
  14.                 cout << "1. Add User" << endl;
  15.                 cout << "2. Edit User" << endl;
  16.                 cout << "3. Delete User" << endl;
  17.                 cout << "4. Display All Users" << endl;
  18.                 cin >> RegistrationMenuChoice;
  19.  
  20.                 if (RegistrationMenuChoice == 1)
  21.                 {
  22.                     string newUserName;
  23.                     cout << "Enter New Username: " << endl;
  24.                     cin >> newUserName;
  25.  
  26.                     for (int i = 0; i <= arrayIndex; i++)
  27.                     {
  28.                         while (newUserName == user[i].getUsername())
  29.                         {
  30.                             cout << "The username you entered is currently not available." << endl;
  31.                             cout << "Please enter your new username: " << endl;
  32.                             cin >> newUserName;
  33.                         }
  34.                     }
  35.  
  36.                     cout << "Enter New User Password: " << endl;
  37.                     cin >> newUserPassword;
  38.  
  39.                     cout << "Enter User Role" << endl;
  40.                     cout << "1. Purchase Manager" << endl;
  41.                     cout << "2. Sales Manager" << endl;
  42.                     cout << "3. Administrator" << endl;
  43.                     cin >> newUserRole;
  44.  
  45.                     if (newUserRole == 1)
  46.                     {
  47.                         newUserRoleChoice = "PM";
  48.                     }
  49.                     else if (newUserRole == 2)
  50.                     {
  51.                         newUserRoleChoice = "SM";
  52.                     }
  53.                     else if (newUserRole == 3)
  54.                     {
  55.                         newUserRoleChoice = "Admin";
  56.                     }
  57.  
  58.                     //Load array and add new info from new variables
  59.                     User userTemp(NextUserID, newUserName, newUserPassword, newUserRoleChoice);
  60.                     user[arrayIndex] = userTemp;
  61.                     arrayIndex++;
  62.  
  63.                     UserRegistration.open("UserRegistration.dat");
  64.  
  65.                     for (int i = 0; i <= arrayIndex; i++)
  66.                     {
  67.                         UserRegistration << user[i].getUserID() << "\t";
  68.                         UserRegistration << user[i].getUsername() << "\t";
  69.                         UserRegistration << user[i].getPassword() << "\t";
  70.                         UserRegistration << user[i].getRole() << endl;
  71.                     }
  72.                    
  73.                     NextUserID++;
  74.                 }
  75.  
  76.                 //-------------------------------------------------------
  77.                 else if (RegistrationMenuChoice == 2)
  78.                 {
  79.  
  80.                     string userEditUserName;
  81.                     string userEditPassword;
  82.                     int userEditRole;
  83.                     int userEditID;
  84.                     string userEditRoleChoice;
  85.  
  86.                     cout << "Enter User ID to edit: ";
  87.                     cin >> userEditID;
  88.  
  89.                     for (int i = 0; i <= arrayIndex; i++)
  90.                     {
  91.                         if (userEditID == user[i].getUserID())
  92.                         {
  93.                             userRegistrationIndex = i;
  94.                         }
  95.                     }
  96.  
  97.                     if (userRegistrationIndex == -1)
  98.                     {
  99.                         cout << "There is no such ID" << endl;
  100.                     }
  101.                     else
  102.                     {
  103.                         cout << endl << "Enter New Username for that user: " << endl;
  104.                         cin >> userEditUserName;
  105.                         cout << endl << "Enter New Password for the user: " << endl;
  106.                         cin >> userEditPassword;
  107.                         cout << endl << "Enter New Role for the user: " << endl;
  108.                         cout << "1. Sales Manager" << endl;
  109.                         cout << "2. Purchase Manager" << endl;
  110.                         cout << "3. Administrator" << endl;
  111.                         cin >> userEditRole;
  112.  
  113.                         if (userEditRole == 1)
  114.                         {
  115.                             userEditRoleChoice = "PM";
  116.                         }
  117.                         else if (userEditRole == 2)
  118.                         {
  119.                             userEditRoleChoice = "SM";
  120.                         }
  121.                         else if (userEditRole == 3)
  122.                         {
  123.                             userEditRoleChoice = "Admin";
  124.                         }
  125.  
  126.                         User userTemp(userEditID, userEditUserName, userEditPassword, userEditRoleChoice);
  127.                         user[userRegistrationIndex] = userTemp;
  128.  
  129.                         UserRegistration.open("UserRegistration.dat");
  130.  
  131.  
  132.                         for (int i = 0; i <= arrayIndex; i++)
  133.                         {
  134.                             UserRegistration << user[i].getUserID() << "\t";
  135.                             UserRegistration << user[i].getUsername() << "\t";
  136.                             UserRegistration << user[i].getPassword() << "\t";
  137.                             UserRegistration << user[i].getRole() << endl;
  138.                         }
  139.  
  140.                        
  141.                     }
  142.  
  143.  
  144.                    
  145.  
  146.                 }
  147.  
  148.                 //-------------------------------------------------------
  149.                 else if (RegistrationMenuChoice == 3)
  150.                 {
  151.                     int removeUserID;
  152.  
  153.                     cout << "Enter User ID to remove: " << endl;
  154.                     cin >> removeUserID;
  155.  
  156.                     //Checking if information is in the array
  157.                     for (int i = 0; i <= arrayIndex; i++)
  158.                     {
  159.                         if (removeUserID == user[i].getUserID())
  160.                         {
  161.                             userDeleteIndex = i;
  162.                         }
  163.                     }
  164.  
  165.  
  166.  
  167.                     if (userDeleteIndex == -1)
  168.                     {
  169.                         cout << "There is no such ID" << endl;
  170.                     }
  171.                     else
  172.                     {
  173.                         cout << "" << userDeleteIndex;
  174.                         while (userDeleteIndex < arrayIndex){
  175.                             user[userDeleteIndex] = user[userDeleteIndex + 1];
  176.                             userDeleteIndex++;
  177.                         }
  178.  
  179.                         arrayIndex--;
  180.  
  181.  
  182.                     }
  183.  
  184.                     ofstream userRegistration;
  185.                     userRegistration.open("UserRegistration.dat");
  186.  
  187.                     for (int i = 0; i <= arrayIndex; i++)
  188.                     {
  189.                         userRegistration << user[i].getUserID() << "\t";
  190.                         userRegistration << user[i].getUsername() << "\t";
  191.                         userRegistration << user[i].getPassword() << "\t";
  192.                         userRegistration << user[i].getRole() << endl;
  193.                     }
  194.                 }
  195.  
  196.                 //-------------------------------------------------------
  197.                 else if (RegistrationMenuChoice == 4)
  198.                 {
  199.                     for (int i = 0; i <= arrayIndex-1; i++)
  200.                     {
  201.                         cout << "ID: " << user[i].getUserID() << endl;
  202.                         cout << "Username: " << user[i].getUsername() << endl;
  203.                         cout << "Password: " << user[i].getPassword() << endl;
  204.                         cout << "Role: " << user[i].getRole() << endl << endl;
  205.                     }
  206.                 }
  207.  
  208.                 UserRegistration.close();
  209.                 return 0;
  210.             } while (RegistrationMenuChoice > 4);
  211.  
  212.  
  213.         }
  214.         catch (int e)
  215.         {
  216.             cout << "Exception Error";
  217.         }
  218. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement