Guest User

Untitled

a guest
Nov 23rd, 2016
28
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 7.52 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.        
  103.         for(Employee x : emp)
  104.         {
  105.             if(x.getEmpName().equalsIgnoreCase(empName))
  106.             {
  107.                 System.out.println("\n" + x.toString() + "\n");
  108.                 break;
  109.             }else
  110.             {
  111.                 System.out.println("\nEmployee Not Found.\n");
  112.             }
  113.         }//End For
  114.         //System.out.println("\nEmployee Not Found.\n");
  115.     }
  116.  
  117.     public void removeUser(long id) {
  118.         for (Iterator<Employee> it = emp.iterator(); it.hasNext();)
  119.         {
  120.             Employee user = it.next();
  121.  
  122.             if (id == currentUser)
  123.             {
  124.                 System.out.println("\nCannot delete a user that is logged in. ID: [" + currentUser + "].");
  125.                 break;
  126.             } else if (id == user.getId())
  127.             {
  128.                 it.remove();
  129.                 System.out.println("Employee Removed Successfully.");
  130.                 break;
  131.             } else if (id > getLargestId() || id < getSmallestId())
  132.             {
  133.                 System.out.println("ID out of bounds.");
  134.                 break;
  135.             }
  136.         }
  137.     }
  138.    
  139.     public boolean isValidName(String name)
  140.     {
  141.         for(Employee x : emp)
  142.         {
  143.             if(x.getEmpName().equalsIgnoreCase(name))
  144.             {
  145.                 return false;
  146.             }
  147.         }
  148.        
  149.         return true;
  150.     }
  151.    
  152.     public boolean isValidId(long id)
  153.     {
  154.         for(Employee x : emp)
  155.         {
  156.             if(x.getId() == id)
  157.             {
  158.                 return true;
  159.             }
  160.         }
  161.         return false;
  162.     }
  163.    
  164.     public String retrunNamefromId(long id)
  165.     {
  166.         for(Employee e : emp)
  167.         {
  168.             if(id == e.getId())
  169.             {
  170.                 return e.getEmpName();
  171.             }
  172.         }
  173.         return "-1";
  174.     }
  175.  
  176.     public void editUser(long searchId ,String empName, int day, int month, int year, String password, String phone)
  177.     {
  178.        
  179.         for(Employee x : emp)
  180.         {
  181.             if(x.getId() == searchId)
  182.             {
  183.                
  184.                 if(!empName.equalsIgnoreCase("-1"))
  185.                 {
  186.                     x.setEmpName(empName);
  187.                 }
  188.  
  189.                 if(day != -1)
  190.                 {
  191.                     x.setDay(day);
  192.                 }
  193.                
  194.                 if(month != -1)
  195.                 {
  196.                     x.setMonth(month);
  197.                 }
  198.                
  199.                 if(year != -1)
  200.                 {
  201.                     x.setYear(year);
  202.                 }
  203.                
  204.                 if(!password.equalsIgnoreCase("-1"))
  205.                 {
  206.                     x.setPassword(password);
  207.                 }
  208.                
  209.                 if(!phone.equalsIgnoreCase("-1"))
  210.                 {
  211.                     x.setPhone(phone);
  212.                 }
  213.                
  214.             }
  215.         }
  216.     }
  217.  
  218.     public boolean isEmptyInput(String empName, String password, String phone)
  219.     {
  220.         return empName.equals("") || password.equals("") || phone.equals("");
  221.     }
  222.  
  223.     public void writeToFile()
  224.     {
  225.         try (
  226.                 Writer writer = new BufferedWriter(new OutputStreamWriter(new FileOutputStream(fileName), "utf-8")))
  227.         {
  228.             for (Employee emp : emp)
  229.             {
  230.                 writer.write(emp.toFile() + "\n");
  231.             }
  232.         } catch (Exception e)
  233.         {
  234.             System.out.println("Error occured : " + e);
  235.         }
  236.     }
  237.  
  238.     public void writeToArray() throws FileNotFoundException
  239.     {
  240.         try
  241.         {
  242.             Scanner inputFile = new Scanner(new File(fileName));
  243.             StringTokenizer tok;
  244.             String nextLine;
  245.             long id = 0;
  246.             String name = "";
  247.             int day = 0, month = 0, year = 0;
  248.             String password = "";
  249.             String email = "";
  250.             String phone = "";
  251.  
  252.             while (inputFile.hasNext())
  253.             {
  254.                 nextLine = inputFile.nextLine();
  255.                 tok = new StringTokenizer(nextLine, ";");
  256.  
  257.                 while (tok.hasMoreTokens())
  258.                 {
  259.                     //id = Long.parseLong(tok.nextToken().trim());
  260.                     name = tok.nextToken().trim();
  261.                     String dateOfBirth = tok.nextToken();
  262.                     String str[] = dateOfBirth.split("-");
  263.                     day = Integer.parseInt(str[0]);
  264.                     month = Integer.parseInt(str[1]);
  265.                     year = Integer.parseInt(str[2]);
  266.                     password = tok.nextToken().trim();
  267.                     phone = tok.nextToken().trim();
  268.                 }
  269.  
  270.                 //TODO: Fill ArrayList with Objects
  271.                 emp.add(new Employee(/*long id*/name, day, month, year, password, phone));
  272.             }
  273.         } catch (FileNotFoundException e)
  274.         {
  275.             System.out.println("Error occured : " + e);
  276.         }
  277.     }
  278.  
  279. }
Add Comment
Please, Sign In to add comment