Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package lol;
- import java.io.BufferedWriter;
- import java.io.FileOutputStream;
- import java.io.OutputStreamWriter;
- import java.io.Writer;
- import java.util.ArrayList;
- import java.util.Iterator;
- import java.util.StringTokenizer;
- import java.io.File;
- import java.io.FileNotFoundException;
- import java.util.Scanner;
- public class EmployeeStore
- {
- private ArrayList<Employee> emp;
- private final String fileName = "employees.txt";
- private Employee x = new Employee();
- private long currentUser = 0;
- public EmployeeStore() {
- emp = new ArrayList();
- }
- public ArrayList<Employee> getAllUsers()
- {
- return emp;
- }
- public String capFirst(String str)
- {
- String[] words = str.split(" ");
- StringBuilder ret = new StringBuilder();
- for(int i = 0; i < words.length; i++)
- {
- ret.append(Character.toUpperCase(words[i].charAt(0)));
- ret.append(words[i].substring(1));
- if(i < words.length - 1)
- {
- ret.append(' ');
- }
- }
- return ret.toString();
- }
- public void addUser(String empName, int day, int month, int year, String password, String phone) {
- emp.add(new Employee(empName, day, month, year, password, phone));
- }
- public void addUserFromArray(ArrayList<Employee> list)
- {
- emp.addAll(list);
- }
- public void getCurrentUser(long id)
- {
- currentUser = id;
- }
- public void addNewUser(String empName, int day, int month, int year, String password, String phone)
- {
- ArrayList<Employee> userToBeAdded = new ArrayList();
- for (Iterator<Employee> it = emp.iterator(); it.hasNext();)
- {
- Employee user = it.next();
- if (user.isValid(empName, day, month, year, password, phone))
- {
- System.out.println("A user with similar data already exists.");
- break;
- } else {
- userToBeAdded.add(new Employee(empName, day, month, year, password, phone));
- System.out.println("User Successfully Created.");
- break;
- }
- }
- addUserFromArray(userToBeAdded);
- }
- public long getLargestId()
- {
- long largestId;
- largestId = emp.get(emp.size() - 1).getId();
- return largestId;
- }
- public long getSmallestId()
- {
- long smallestId;
- smallestId = emp.get(0).getId();
- return smallestId;
- }
- public void displaySpecificName(String empName)
- {
- for(Employee x : emp)
- {
- if(x.getEmpName().equalsIgnoreCase(empName))
- {
- System.out.println("\n" + x.toString() + "\n");
- break;
- }else
- {
- System.out.println("\nEmployee Not Found.\n");
- }
- }//End For
- //System.out.println("\nEmployee Not Found.\n");
- }
- public void removeUser(long id) {
- for (Iterator<Employee> it = emp.iterator(); it.hasNext();)
- {
- Employee user = it.next();
- if (id == currentUser)
- {
- System.out.println("\nCannot delete a user that is logged in. ID: [" + currentUser + "].");
- break;
- } else if (id == user.getId())
- {
- it.remove();
- System.out.println("Employee Removed Successfully.");
- break;
- } else if (id > getLargestId() || id < getSmallestId())
- {
- System.out.println("ID out of bounds.");
- break;
- }
- }
- }
- public boolean isValidName(String name)
- {
- for(Employee x : emp)
- {
- if(x.getEmpName().equalsIgnoreCase(name))
- {
- return false;
- }
- }
- return true;
- }
- public boolean isValidId(long id)
- {
- for(Employee x : emp)
- {
- if(x.getId() == id)
- {
- return true;
- }
- }
- return false;
- }
- public String retrunNamefromId(long id)
- {
- for(Employee e : emp)
- {
- if(id == e.getId())
- {
- return e.getEmpName();
- }
- }
- return "-1";
- }
- public void editUser(long searchId ,String empName, int day, int month, int year, String password, String phone)
- {
- for(Employee x : emp)
- {
- if(x.getId() == searchId)
- {
- if(!empName.equalsIgnoreCase("-1"))
- {
- x.setEmpName(empName);
- }
- if(day != -1)
- {
- x.setDay(day);
- }
- if(month != -1)
- {
- x.setMonth(month);
- }
- if(year != -1)
- {
- x.setYear(year);
- }
- if(!password.equalsIgnoreCase("-1"))
- {
- x.setPassword(password);
- }
- if(!phone.equalsIgnoreCase("-1"))
- {
- x.setPhone(phone);
- }
- }
- }
- }
- public boolean isEmptyInput(String empName, String password, String phone)
- {
- return empName.equals("") || password.equals("") || phone.equals("");
- }
- public void writeToFile()
- {
- try (
- Writer writer = new BufferedWriter(new OutputStreamWriter(new FileOutputStream(fileName), "utf-8")))
- {
- for (Employee emp : emp)
- {
- writer.write(emp.toFile() + "\n");
- }
- } catch (Exception e)
- {
- System.out.println("Error occured : " + e);
- }
- }
- public void writeToArray() throws FileNotFoundException
- {
- try
- {
- Scanner inputFile = new Scanner(new File(fileName));
- StringTokenizer tok;
- String nextLine;
- long id = 0;
- String name = "";
- int day = 0, month = 0, year = 0;
- String password = "";
- String email = "";
- String phone = "";
- while (inputFile.hasNext())
- {
- nextLine = inputFile.nextLine();
- tok = new StringTokenizer(nextLine, ";");
- while (tok.hasMoreTokens())
- {
- //id = Long.parseLong(tok.nextToken().trim());
- name = tok.nextToken().trim();
- String dateOfBirth = tok.nextToken();
- String str[] = dateOfBirth.split("-");
- day = Integer.parseInt(str[0]);
- month = Integer.parseInt(str[1]);
- year = Integer.parseInt(str[2]);
- password = tok.nextToken().trim();
- phone = tok.nextToken().trim();
- }
- //TODO: Fill ArrayList with Objects
- emp.add(new Employee(/*long id*/name, day, month, year, password, phone));
- }
- } catch (FileNotFoundException e)
- {
- System.out.println("Error occured : " + e);
- }
- }
- }
Add Comment
Please, Sign In to add comment