Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package lol;
- import java.io.FileNotFoundException;
- import java.util.Scanner;
- import java.util.ArrayList;
- public class Main
- {
- public static void main(String[] args) throws FileNotFoundException
- {
- Scanner in = new Scanner(System.in);
- EmployeeStore store = new EmployeeStore();
- ArrayList<Employee> emp;
- store.writeToArray();
- String loggedUser = "";
- boolean authState = false;
- boolean loopState = true; //Boolean for Switch Statement While Loop
- //Pre-Created Users
- //store.addUser("admin", 23, 04, 1997, "1234", "08617569");
- //Update *emp* array
- emp = store.getAllUsers();
- System.out.println("Warning: Terminating Application during Runtime will not save any changes made.\n");
- System.out.println("Amending Employees in 'employees.txt'. Format: name;day-month-year;password;phone\n");
- System.out.println("List Of All ID's and Names:");
- for(Employee e : emp)
- {
- System.out.println(e.getId() + " : " + e.getEmpName());
- }
- System.out.println("\nEnter Employee ID: ");
- long empId = in.nextLong();
- in.nextLine();
- System.out.println("Enter Password: ");
- String password = in.nextLine();
- //User Login
- for(Employee x : emp)
- {
- if(x.isAuthorised(empId, password))
- {
- loggedUser = x.getEmpName();
- x.setLogInState();
- authState = x.getLogInState();
- store.getCurrentUser(x.getId());
- }
- }
- //User Logged In
- while(authState == true)
- {
- System.out.println("\nWelcome : " + store.capFirst(loggedUser));
- boolean isAdmin = false;
- if(loggedUser.equalsIgnoreCase("admin"))
- {
- isAdmin = true;
- }
- while(loopState == true)
- {
- System.out.println("\nSelect a Menu: "
- + "\n[1]: Add an Employee "
- + "\n[2]: Edit an Employee "
- + "\n[3]: Delete an Employee "
- + "\n[4]: Show All Employees "
- + "\n[5]: Search Employee Name "
- + "\n[6]: Log out. "
- + "\nChoice: ");
- int choice = in.nextInt();
- switch(choice)
- {
- case 1://Add New Employee
- in.nextLine();
- System.out.println("Enter Employee Name: ");
- String newUsername = in.nextLine();
- System.out.println("Enter Birth [Day]: ");
- int newDay = in.nextInt();
- while(newDay > 31 || newDay < 1)
- {
- System.out.println("\nDay out of Bounds. Try again:");
- newDay = in.nextInt();
- }
- System.out.println("Enter Birth [Month]: ");
- int newMonth = in.nextInt();
- while(newMonth > 12 || newMonth < 1)
- {
- System.out.println("\nMonth out of Bounds. Try again:");
- newMonth = in.nextInt();
- }
- System.out.println("Enter Birth [Year]: ");
- int newYear = in.nextInt();
- while(newYear < 1950 || newYear > 1998)
- {
- System.out.println("\nYear out of Bounds. Try again:");
- newYear = in.nextInt();
- }
- in.nextLine();
- System.out.println("Enter Password: ");
- String newPassword = in.nextLine();
- System.out.println("Enter Phone Number [8-Digits Long]: ");
- String newPhone = in.nextLine();
- while(newPhone.length() != 8)
- {
- System.out.println("\nIncorrect.\nEnter Phone Number [8-Digits Long]: ");
- newPhone = in.nextLine();
- }
- if(!store.isEmptyInput(newUsername, newPassword, newPhone))
- {
- store.addNewUser(newUsername, newDay, newMonth, newYear, newPassword, newPhone);
- emp = store.getAllUsers();
- }else
- {
- System.out.println("User not added. \nOne or more fields were left empty.");
- }
- break;
- case 2:
- for(Employee user : emp)
- {
- System.out.println(user.toString());
- }
- System.out.println("\nEnter ID to Edit: ");
- long searchId = in.nextLong();
- in.nextLine();
- if(store.retrunNamefromId(searchId).equalsIgnoreCase("admin"))
- {
- System.out.println("Cannot Edit Admin, Please Refer Back to 'employees.txt' to edit.");
- break;
- }
- if(store.isValidId(searchId))
- {
- System.out.println("\nEnter New Employee Name or Type [-1] to Keep Old Name:");
- String nameNew = in.nextLine();
- while(store.isValidName(nameNew) == false)
- {
- System.out.println("\nName already exists. Try again or Type [-1] to Keep Old Name:");
- nameNew = in.nextLine();
- }
- System.out.println("\nEnter New [Day] of Birth or Type [-1] to Keep Old Day:");
- int dayNew = in.nextInt();
- while(dayNew > 31 || dayNew < 1)
- {
- if(dayNew == -1)
- {
- break;
- }
- System.out.println("\nDay out of Bounds. Try again:");
- dayNew = in.nextInt();
- }
- System.out.println("\nEnter New [Month] of Birth or Type [-1] to Keep Old Month:");
- int monthNew = in.nextInt();
- while(monthNew > 12 || monthNew < 1)
- {
- if(dayNew == -1)
- {
- break;
- }
- System.out.println("\nMonth out of Bounds. Try again:");
- monthNew = in.nextInt();
- }
- System.out.println("\nEnter New [Year] of Birth or Type [-1] to Kee Old Year:");
- int yearNew = in.nextInt();
- while(yearNew < 1950 || yearNew > 1998)
- {
- if(dayNew == -1)
- {
- break;
- }
- System.out.println("\nDay out of Bounds. Try again:");
- yearNew = in.nextInt();
- }
- in.nextLine();
- System.out.println("\nEnter New Password or Type [-1] to Keep Old Password:");
- String passwordNew = in.nextLine();
- System.out.println("\nEnter New Phone Number or Type [-1] to Keep Old Phone Number:");
- String phoneNew = in.nextLine();
- while(phoneNew.length() != 8)
- {
- if(dayNew == -1)
- {
- break;
- }
- System.out.println("\nIncorrect.\nEnter Phone Number [8-Digits Long]:");
- phoneNew = in.nextLine();
- }
- store.editUser(searchId, nameNew , dayNew, monthNew, yearNew, passwordNew, phoneNew);
- }
- else
- {
- System.out.println("User with ID [" + searchId + "] does not exist");
- }
- break;
- case 3: //Remove Employee
- if(isAdmin == true)
- {
- for(Employee user : emp)
- {
- System.out.println(user.toString());
- }
- System.out.println("\nEnter ID to Delete: ");
- long id = in.nextLong();
- store.removeUser(id);
- }else
- {
- System.out.println("You are not an admin.");
- }
- break;
- case 4: //Display All Users
- for(Employee user : emp)
- {
- System.out.println(user.toString());
- }
- break;
- case 5: //Seach Specific Employee
- in.nextLine(); //Fix Java Bug with nextInt()
- System.out.println("Enter A Name To Search For: ");
- String searchName = in.nextLine();
- store.displaySpecificName(searchName);
- break;
- case 6: //Logout
- authState = false;
- loopState = false;
- store.writeToFile();
- System.out.println("Saving changes to File\nLogging Out...");
- break;
- default:
- System.out.println("Incorrect Option.");
- break;
- }
- }//End While(true)
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement