Advertisement
Guest User

Untitled

a guest
Nov 23rd, 2016
93
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 11.70 KB | None | 0 0
  1. package lol;
  2. import java.io.FileNotFoundException;
  3. import java.util.Scanner;
  4. import java.util.ArrayList;
  5.  
  6. public class Main
  7. {
  8.     public static void main(String[] args) throws FileNotFoundException
  9.     {
  10.         Scanner in = new Scanner(System.in);
  11.        
  12.         EmployeeStore store = new EmployeeStore();
  13.         ArrayList<Employee> emp;
  14.         store.writeToArray();
  15.        
  16.         String loggedUser = "";
  17.         boolean authState = false;
  18.         boolean loopState = true; //Boolean for Switch Statement While Loop
  19.  
  20.        
  21.         //Pre-Created Users
  22.         //store.addUser("admin", 23, 04, 1997, "1234", "08617569");
  23.        
  24.         //Update *emp* array
  25.         emp = store.getAllUsers();
  26.        
  27.         System.out.println("Warning: Terminating Application during Runtime will not save any changes made.\n");
  28.         System.out.println("Amending Employees in 'employees.txt'. Format: name;day-month-year;password;phone\n");
  29.        
  30.         System.out.println("List Of All ID's and Names:");
  31.         for(Employee e : emp)
  32.         {
  33.             System.out.println(e.getId() + " : " + e.getEmpName());
  34.         }
  35.        
  36.         System.out.println("\nEnter Employee ID: ");
  37.         long empId = in.nextLong();
  38.        
  39.         in.nextLine();
  40.        
  41.         System.out.println("Enter Password: ");
  42.         String password = in.nextLine();
  43.        
  44.         //User Login
  45.         for(Employee x : emp)
  46.         {
  47.             if(x.isAuthorised(empId, password))
  48.             {
  49.                 loggedUser = x.getEmpName();
  50.                 x.setLogInState();
  51.                 authState = x.getLogInState();
  52.                 store.getCurrentUser(x.getId());
  53.             }
  54.            
  55.         }
  56.        
  57.         //User Logged In
  58.         while(authState == true)
  59.         {
  60.             System.out.println("\nWelcome : " + store.capFirst(loggedUser));
  61.            
  62.             boolean isAdmin = false;
  63.            
  64.             if(loggedUser.equalsIgnoreCase("admin"))
  65.             {
  66.                 isAdmin = true;
  67.             }
  68.            
  69.             while(loopState == true)
  70.             {
  71.            
  72.             System.out.println("\nSelect a Menu: "
  73.                     + "\n[1]: Add an Employee "
  74.                     + "\n[2]: Edit an Employee "
  75.                     + "\n[3]: Delete an Employee "
  76.                     + "\n[4]: Show All Employees "
  77.                     + "\n[5]: Search Employee Name "
  78.                     + "\n[6]: Log out. "
  79.                     + "\nChoice: ");
  80.  
  81.                 int choice = in.nextInt();
  82.                
  83.                 switch(choice)
  84.                 {
  85.                     case 1://Add New Employee
  86.                        
  87.                         in.nextLine();
  88.                        
  89.                         System.out.println("Enter Employee Name: ");
  90.                         String newUsername = in.nextLine();
  91.                        
  92.                         System.out.println("Enter Birth [Day]: ");
  93.                         int newDay = in.nextInt();
  94.                        
  95.                         while(newDay > 31 || newDay < 1)
  96.                         {
  97.                             System.out.println("\nDay out of Bounds. Try again:");
  98.                             newDay = in.nextInt();
  99.                         }
  100.                        
  101.                         System.out.println("Enter Birth [Month]: ");
  102.                         int newMonth = in.nextInt();
  103.                        
  104.                         while(newMonth > 12 || newMonth < 1)
  105.                         {
  106.                             System.out.println("\nMonth out of Bounds. Try again:");
  107.                             newMonth = in.nextInt();
  108.                         }
  109.                        
  110.                         System.out.println("Enter Birth [Year]: ");
  111.                         int newYear = in.nextInt();
  112.                        
  113.                         while(newYear < 1950 || newYear > 1998)
  114.                         {
  115.                             System.out.println("\nYear out of Bounds. Try again:");
  116.                             newYear = in.nextInt();
  117.                         }
  118.                        
  119.                         in.nextLine();
  120.  
  121.                         System.out.println("Enter Password: ");
  122.                         String newPassword = in.nextLine();
  123.                        
  124.                         System.out.println("Enter Phone Number [8-Digits Long]: ");
  125.                         String newPhone = in.nextLine();
  126.                        
  127.                         while(newPhone.length() != 8)
  128.                         {
  129.                             System.out.println("\nIncorrect.\nEnter Phone Number [8-Digits Long]: ");
  130.                             newPhone = in.nextLine();
  131.                         }
  132.                        
  133.                         if(!store.isEmptyInput(newUsername, newPassword, newPhone))
  134.                         {
  135.                             store.addNewUser(newUsername, newDay, newMonth, newYear, newPassword, newPhone);
  136.                             emp = store.getAllUsers();
  137.                         }else
  138.                         {
  139.                             System.out.println("User not added. \nOne or more fields were left empty.");
  140.                         }
  141.                        
  142.                         break;
  143.                        
  144.                     case 2:
  145.                        
  146.                         for(Employee user : emp)
  147.                         {
  148.                             System.out.println(user.toString());
  149.                         }
  150.                        
  151.                         System.out.println("\nEnter ID to Edit: ");
  152.                         long searchId = in.nextLong();
  153.                        
  154.                         in.nextLine();
  155.                        
  156.                         if(store.retrunNamefromId(searchId).equalsIgnoreCase("admin"))
  157.                         {
  158.                             System.out.println("Cannot Edit Admin, Please Refer Back to 'employees.txt' to edit.");
  159.                             break;
  160.                         }
  161.                        
  162.                         if(store.isValidId(searchId))
  163.                         {
  164.  
  165.                             System.out.println("\nEnter New Employee Name or Type [-1] to Keep Old Name:");
  166.                             String nameNew = in.nextLine();
  167.  
  168.                             while(store.isValidName(nameNew) == false)
  169.                             {
  170.                                 System.out.println("\nName already exists. Try again or Type [-1] to Keep Old Name:");
  171.                                 nameNew = in.nextLine();
  172.                             }
  173.  
  174.                             System.out.println("\nEnter New [Day] of Birth or Type [-1] to Keep Old Day:");
  175.                             int dayNew = in.nextInt();
  176.  
  177.                             while(dayNew > 31 || dayNew < 1)
  178.                             {
  179.                                 if(dayNew == -1)
  180.                                 {
  181.                                     break;
  182.                                 }
  183.                                 System.out.println("\nDay out of Bounds. Try again:");
  184.                                 dayNew = in.nextInt();
  185.                             }
  186.  
  187.                             System.out.println("\nEnter New [Month] of Birth or Type [-1] to Keep Old Month:");
  188.                             int monthNew = in.nextInt();
  189.  
  190.                             while(monthNew > 12 || monthNew < 1)
  191.                             {
  192.                                 if(dayNew == -1)
  193.                                 {
  194.                                     break;
  195.                                 }
  196.                                 System.out.println("\nMonth out of Bounds. Try again:");
  197.                                 monthNew = in.nextInt();
  198.                             }
  199.  
  200.                             System.out.println("\nEnter New [Year] of Birth or Type [-1] to Kee Old Year:");
  201.                             int yearNew = in.nextInt();
  202.  
  203.                             while(yearNew < 1950 || yearNew > 1998)
  204.                             {
  205.                                 if(dayNew == -1)
  206.                                 {
  207.                                     break;
  208.                                 }
  209.                                 System.out.println("\nDay out of Bounds. Try again:");
  210.                                 yearNew = in.nextInt();
  211.                             }
  212.  
  213.                             in.nextLine();
  214.  
  215.                             System.out.println("\nEnter New Password or Type [-1] to Keep Old Password:");
  216.                             String passwordNew = in.nextLine();
  217.  
  218.                             System.out.println("\nEnter New Phone Number or Type [-1] to Keep Old Phone Number:");
  219.                             String phoneNew = in.nextLine();
  220.  
  221.                             while(phoneNew.length() != 8)
  222.                             {
  223.                                 if(dayNew == -1)
  224.                                 {
  225.                                     break;
  226.                                 }
  227.                                 System.out.println("\nIncorrect.\nEnter Phone Number [8-Digits Long]:");
  228.                                 phoneNew = in.nextLine();
  229.                             }
  230.                            
  231.                             store.editUser(searchId, nameNew , dayNew, monthNew, yearNew, passwordNew, phoneNew);
  232.                         }
  233.                         else
  234.                         {
  235.                             System.out.println("User with ID [" + searchId + "] does not exist");
  236.                         }
  237.                        
  238.                         break;
  239.  
  240.                     case 3: //Remove Employee
  241.                        
  242.                         if(isAdmin == true)
  243.                         {
  244.                                 for(Employee user : emp)
  245.                                 {
  246.                                     System.out.println(user.toString());
  247.                                 }
  248.  
  249.                             System.out.println("\nEnter ID to Delete: ");
  250.                             long id = in.nextLong();
  251.  
  252.                             store.removeUser(id);
  253.                         }else
  254.                         {
  255.                             System.out.println("You are not an admin.");
  256.                         }
  257.  
  258.                         break;
  259.                        
  260.                     case 4: //Display All Users
  261.  
  262.                         for(Employee user : emp)
  263.                         {
  264.                             System.out.println(user.toString());
  265.                         }
  266.  
  267.                         break;
  268.                        
  269.                     case 5: //Seach Specific Employee
  270.                        
  271.                         in.nextLine(); //Fix Java Bug with nextInt()
  272.                        
  273.                         System.out.println("Enter A Name To Search For: ");
  274.                         String searchName = in.nextLine();
  275.                        
  276.                         store.displaySpecificName(searchName);
  277.                        
  278.                         break;
  279.  
  280.                     case 6: //Logout
  281.  
  282.                         authState = false;
  283.                         loopState = false;
  284.                         store.writeToFile();
  285.                         System.out.println("Saving changes to File\nLogging Out...");
  286.  
  287.                         break;
  288.                        
  289.                     default:
  290.                        
  291.                         System.out.println("Incorrect Option.");
  292.                         break;
  293.                 }
  294.                
  295.             }//End While(true)
  296.         }
  297.     }
  298. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement