Guest User

Untitled

a guest
Nov 23rd, 2016
37
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 7.68 KB | None | 0 0
  1. package lol;
  2.  
  3. import java.io.BufferedWriter;
  4. import java.io.FileOutputStream;
  5. import java.io.OutputStreamWriter;
  6. import java.io.Writer;
  7. import java.util.ArrayList;
  8. import java.util.Iterator;
  9. import java.util.StringTokenizer;
  10. import java.io.File;
  11. import java.io.FileNotFoundException;
  12. import java.util.Scanner;
  13.  
  14. public class EmployeeStore
  15. {
  16.  
  17.     private ArrayList<Employee> emp;
  18.     private final String fileName = "employees.txt";
  19.     private Employee x = new Employee();
  20.     private long currentUser = 0;
  21.  
  22.     public EmployeeStore() {
  23.         emp = new ArrayList();
  24.     }
  25.  
  26.     public ArrayList<Employee> getAllUsers()
  27.     {
  28.         return emp;
  29.     }
  30.    
  31.     public String capFirst(String str)
  32.     {
  33.     String[] words = str.split(" ");
  34.     StringBuilder ret = new StringBuilder();
  35.     for(int i = 0; i < words.length; i++)
  36.     {
  37.         ret.append(Character.toUpperCase(words[i].charAt(0)));
  38.         ret.append(words[i].substring(1));
  39.         if(i < words.length - 1)
  40.         {
  41.             ret.append(' ');
  42.         }
  43.     }
  44.     return ret.toString();
  45.     }
  46.  
  47.     public void addUser(String empName, int day, int month, int year, String password, String phone) {
  48.         emp.add(new Employee(empName, day, month, year, password, phone));
  49.     }
  50.  
  51.     public void addUserFromArray(ArrayList<Employee> list)
  52.     {
  53.         emp.addAll(list);
  54.     }
  55.  
  56.     public void getCurrentUser(long id)
  57.     {
  58.         currentUser = id;
  59.     }
  60.  
  61.     public void addNewUser(String empName, int day, int month, int year, String password, String phone)
  62.     {
  63.         ArrayList<Employee> userToBeAdded = new ArrayList();
  64.         for (Iterator<Employee> it = emp.iterator(); it.hasNext();)
  65.         {
  66.             Employee user = it.next();
  67.  
  68.             if (user.isValid(empName, day, month, year, password, phone))
  69.             {
  70.                 System.out.println("A user with similar data already exists.");
  71.                 break;
  72.             } else {
  73.                 userToBeAdded.add(new Employee(empName, day, month, year, password, phone));
  74.                 System.out.println("User Successfully Created.");
  75.                 break;
  76.             }
  77.         }
  78.  
  79.         addUserFromArray(userToBeAdded);
  80.     }
  81.  
  82.     public long getLargestId()
  83.     {
  84.         long largestId;
  85.  
  86.         largestId = emp.get(emp.size() - 1).getId();
  87.  
  88.         return largestId;
  89.     }
  90.    
  91.     public long getSmallestId()
  92.     {
  93.         long smallestId;
  94.  
  95.         smallestId = emp.get(0).getId();
  96.  
  97.         return smallestId;
  98.     }
  99.    
  100.     public void displaySpecificName(String empName)
  101.     {
  102.         for(Employee x : emp)
  103.         {
  104.             if(x.getEmpName().equalsIgnoreCase(empName))
  105.             {
  106.                 System.out.println(x.toString() + "\n");
  107.             }                
  108.         }//End For
  109.     }
  110.  
  111.     public void removeUser(long id) {
  112.         for (Iterator<Employee> it = emp.iterator(); it.hasNext();)
  113.         {
  114.             Employee user = it.next();
  115.  
  116.             if (id == currentUser)
  117.             {
  118.                 System.out.println("\nCannot delete a user that is logged in. ID: [" + currentUser + "].");
  119.                 break;
  120.             } else if (id == user.getId())
  121.             {
  122.                 it.remove();
  123.                 System.out.println("Employee Removed Successfully.");
  124.                 break;
  125.             } else if (id > getLargestId() || id < getSmallestId())
  126.             {
  127.                 System.out.println("ID out of bounds.");
  128.                 break;
  129.             }
  130.         }
  131.     }
  132.    
  133.     public boolean isValidName(String name)
  134.     {
  135.         for(Employee x : emp)
  136.         {
  137.             if(x.getEmpName().equalsIgnoreCase(name))
  138.             {
  139.                 return false;
  140.             }
  141.         }
  142.        
  143.         return true;
  144.     }
  145.    
  146.     public boolean isValidId(long id)
  147.     {
  148.         for(Employee x : emp)
  149.         {
  150.             if(x.getId() == id)
  151.             {
  152.                 return true;
  153.             }
  154.         }
  155.         return false;
  156.     }
  157.    
  158.     public String retrunNamefromId(long id)
  159.     {
  160.         for(Employee e : emp)
  161.         {
  162.             if(id == e.getId())
  163.             {
  164.                 return e.getEmpName();
  165.             }
  166.         }
  167.         return "-1";
  168.     }
  169.  
  170.     public void editUser(long searchId ,String empName, int day, int month, int year, String password, String phone)
  171.     {
  172.        
  173.         for(Employee x : emp)
  174.         {
  175.             if(x.getId() == searchId)
  176.             {
  177.                
  178.                 if(!empName.equalsIgnoreCase("-1"))
  179.                 {
  180.                     x.setEmpName(empName);
  181.                 }
  182.  
  183.                 if(day != -1)
  184.                 {
  185.                     x.setDay(day);
  186.                 }
  187.                
  188.                 if(month != -1)
  189.                 {
  190.                     x.setMonth(month);
  191.                 }
  192.                
  193.                 if(year != -1)
  194.                 {
  195.                     x.setYear(year);
  196.                 }
  197.                
  198.                 if(!password.equalsIgnoreCase("-1"))
  199.                 {
  200.                     x.setPassword(password);
  201.                 }
  202.                
  203.                 if(!phone.equalsIgnoreCase("-1"))
  204.                 {
  205.                     x.setPhone(phone);
  206.                 }
  207.                
  208.                 x.setEmail();
  209.                
  210.             }
  211.         }
  212.        
  213.         if(empName.equals("-1") && day == -1 && month == -1 & year == -1 && password.equals("-1") && phone.equals("-1"))
  214.         {
  215.             System.out.println("\nNo changes made.");
  216.         }else
  217.         {
  218.             System.out.println("\nEmployee Successfully Amended.");
  219.         }
  220.     }
  221.  
  222.     public boolean isEmptyInput(String empName, String password, String phone)
  223.     {
  224.         return empName.equals("") || password.equals("") || phone.equals("");
  225.     }
  226.  
  227.     public void writeToFile()
  228.     {
  229.         try (
  230.                 Writer writer = new BufferedWriter(new OutputStreamWriter(new FileOutputStream(fileName), "utf-8")))
  231.         {
  232.             for (Employee emp : emp)
  233.             {
  234.                 writer.write(emp.toFile() + "\n");
  235.             }
  236.         } catch (Exception e)
  237.         {
  238.             System.out.println("Error occured : " + e);
  239.         }
  240.     }
  241.  
  242.     public void writeToArray() throws FileNotFoundException
  243.     {
  244.         try
  245.         {
  246.             Scanner inputFile = new Scanner(new File(fileName));
  247.             StringTokenizer tok;
  248.             String nextLine;
  249.             long id = 0;
  250.             String name = "";
  251.             int day = 0, month = 0, year = 0;
  252.             String password = "";
  253.             String email = "";
  254.             String phone = "";
  255.  
  256.             while (inputFile.hasNext())
  257.             {
  258.                 nextLine = inputFile.nextLine();
  259.                 tok = new StringTokenizer(nextLine, ";");
  260.  
  261.                 while (tok.hasMoreTokens())
  262.                 {
  263.                     //id = Long.parseLong(tok.nextToken().trim());
  264.                     name = tok.nextToken().trim();
  265.                     String dateOfBirth = tok.nextToken();
  266.                     String str[] = dateOfBirth.split("-");
  267.                     day = Integer.parseInt(str[0]);
  268.                     month = Integer.parseInt(str[1]);
  269.                     year = Integer.parseInt(str[2]);
  270.                     password = tok.nextToken().trim();
  271.                     phone = tok.nextToken().trim();
  272.                 }
  273.  
  274.                 //TODO: Fill ArrayList with Objects
  275.                 emp.add(new Employee(/*long id*/name, day, month, year, password, phone));
  276.             }
  277.         } catch (FileNotFoundException e)
  278.         {
  279.             System.out.println("Error occured : " + e);
  280.         }
  281.     }
  282.  
  283. }
Add Comment
Please, Sign In to add comment