31/10/2019
Project on Collections Framework
Using Collection object as Mini DataBase
================================
In this project we have used complete Core Java concepts.
In this project we have used
1) OOP concepts
2) String Handling
3) Exception Handling with Java 7 new features
4) Collections Framework
5) IO Streams for reading Data from file
6) Serialization and Deserialization
7) Packages as per project standards
In the next version, we will enhance this project using Java 8 Stream API and Java 9 Module System .
In final version we will enhance it to web projects by using
1) HTML, Java Script, CSS, Angular
2) Adv Java (Servlet, JSP, JDBC)
I have attached required images on Project architecture and other details
Here is the code:
//=====DeptMap.java=====
package com.nit.hk.emis.util;
import java.util.HashMap;
public class DeptMap {
private static final HashMap depts;
static {
depts = new HashMap();
depts.put("CRT", 1);
depts.put("C", 2);
depts.put("DS", 3);
depts.put("CJ", 4);
depts.put("OR", 5);
depts.put("HJ", 6);
depts.put("AJ", 7);
depts.put("Spr", 8);
depts.put("Hib", 9);
depts.put("XW", 10);
}
public static int getDeptNum(String dept) {
return depts.get(dept.toUpperCase());
}
}
//=====Employee.java=====
package com.nit.hk.emis.pojo;
import java.io.BufferedReader;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import java.io.Serializable;
import com.nit.hk.emis.util.DeptMap;
public class Employee
implements Comparable,
Serializable{
private static final long serialVersionUID = 1L;
private static String company;
private int eno;
private String ename;
private double sal;
private String dept;
private transient long mobile;
private transient String email;
private transient double totalSal;
static { //initializing company variable
//by reading value from a file
try(
BufferedReader reader =
new BufferedReader(
new FileReader("company.txt"))
){
company = reader.readLine();
}catch(FileNotFoundException e) {
System.out.println(
"The file \"company.txt\" is not found");
System.exit(0);
}catch(IOException e) {
e.printStackTrace();
}
}
private static int count = 0;
{
count++;
}
public Employee() { }
public static String getCompany() {
return company;
}
public static void setCompany(String company) {
Employee.company = company;
}
public int getEno() {
return eno;
}
public void setEno(int eno) {
this.eno = eno;
}
public String getEname() {
return ename;
}
public void setEname(String ename) {
this.ename = ename;
}
public double getSal() {
return sal;
}
public void setSal(double sal) {
this.sal = sal;
}
public String getDept() {
return dept;
}
public void setDept(String dept) {
this.dept = dept;
}
public long getMobile() {
return mobile;
}
public void setMobile(long mobile) {
this.mobile = mobile;
}
public String getEmail() {
return email;
}
public void setEmail(String email) {
this.email = email;
}
public double getTotalSal() {
return totalSal;
}
public void setTotalSal(double totalSal) {
this.totalSal = totalSal;
}
public static int getCount() {
return count;
}
public int hashCode() {
return DeptMap.getDeptNum(dept);
}
public boolean equals(Object obj) {
try {
Employee e =(Employee)obj;
return this.dept.equals(e.dept)
&& this.eno == e.eno;
}catch(ClassCastException | NullPointerException e){
return false;
}
}
public int compareTo(Employee e) {
int deptDiff = this.dept.compareTo(e.dept);
if(deptDiff == 0)
return this.eno - e.eno;
return deptDiff;
}
public String toString() {
return "Employee("
+eno+", "+ename+", "
+dept+", "+sal+", "
+mobile+", "+email+")";
}
}
//==DeptNotFoundException.java==
package com.nit.hk.emis.exceptions;
public class DeptNotFoundException extends Exception {
public DeptNotFoundException() {
}
public DeptNotFoundException(String errMsg) {
super(errMsg);
}
}
//==EmployeeNotFoundException.java==
package com.nit.hk.emis.exceptions;
public class EmployeeNotFoundException extends Exception {
public EmployeeNotFoundException() {
}
public EmployeeNotFoundException(String errMsg) {
super(errMsg);
}
}
//===EmpPersist.java===
package com.nit.hk.emis.dao;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.util.ArrayList;
import java.util.List;
import java.util.Scanner;
import com.nit.hk.emis.exceptions.DeptNotFoundException;
import com.nit.hk.emis.exceptions.EmployeeNotFoundException;
import com.nit.hk.emis.pojo.Employee;
public class EmpPersist {
private List empList;
("unchecked")
public EmpPersist(){
try(
ObjectInputStream ois =
new ObjectInputStream(
new FileInputStream("employees.ser"));
){
empList = (ArrayList)ois.readObject();
}catch(IOException e) {
empList = new ArrayList();
} catch (ClassNotFoundException e) {
e.printStackTrace();
}
}
public void persist() {
try(
ObjectOutputStream oos =
new ObjectOutputStream(
new FileOutputStream("employee.ser"));
){
oos.writeObject(empList);
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
public void store(Employee e) {
empList.add(e);
}
public Employee getEmployee(Employee searchEmp) {
int index = empList.indexOf(searchEmp);
if(index != -1) {
return empList.get(index);
}
return null;
}
public List getEmployeesByDept(String dept) {
List empListByDept =
new ArrayList();
for(Employee emp : empList) {
if(emp.getDept().equalsIgnoreCase(dept)) {
empListByDept.add(emp);
}
}
return empListByDept;
}
public List getEmployees() {
return List.copyOf(empList);
}
public void updateEmployee()
throws EmployeeNotFoundException{
Scanner scn = new Scanner(System.in);
Employee searchEmp = new Employee();
System.out.println(
"Enter employee details for searching");
System.out.print(" Enter eno: ");
searchEmp.setEno(scn.nextInt()); scn.nextLine();
System.out.print(" Enter dept: ");
searchEmp.setDept(scn.nextLine());
Employee emp = this.getEmployee(searchEmp);
if(emp != null) {
System.out.println("\nEnter new values to update");
System.out.println("Do you want to update ename? (Y/N): ");
if(scn.nextLine().equalsIgnoreCase("Y")) {
System.out.println(" Enter new name: ");
emp.setEname(scn.nextLine());
}
System.out.println("Do you want to update sal? (Y/N): ");
if(scn.nextLine().equalsIgnoreCase("Y")) {
System.out.println(" Enter new sal: ");
emp.setSal(scn.nextDouble()); scn.nextLine();
}
System.out.println("Do you want to update dept? (Y/N): ");
if(scn.nextLine().equalsIgnoreCase("Y")) {
System.out.println(" Enter new dept: ");
emp.setDept(scn.nextLine());
}
System.out.println("Do you want to update mobile? (Y/N): ");
if(scn.nextLine().equalsIgnoreCase("Y")) {
System.out.println(" Enter new mobile: ");
emp.setMobile(scn.nextLong()); scn.nextLine();
}
System.out.println("Do you want to update email? (Y/N): ");
if(scn.nextLine().equalsIgnoreCase("Y")) {
System.out.println(" Enter new email: ");
emp.setEmail(scn.nextLine());
}
}else {
throw new EmployeeNotFoundException(
"\nWith the given details "
+ "Employee is not found");
}
}
public void updateSalByDept(String dept, double salIncreaseBy)
throws DeptNotFoundException{
boolean updated = false;
for(Employee emp : empList) {
if(emp.getDept().equalsIgnoreCase(dept)) {
emp.setSal( emp.getSal() + salIncreaseBy );
updated = true;
}
}
if(!updated) {
throw new DeptNotFoundException(
"Department "+dept+" is not found");
}
}
public void updateAllEmpsSal(double salIncreaseBy)
throws EmployeeNotFoundException{
if(!empList.isEmpty()) {
for(Employee emp : empList) {
emp.setSal( emp.getSal() + salIncreaseBy );
}
}else {
throw new EmployeeNotFoundException("No employee found");
}
}
public boolean deleteEmployee(Employee emp) {
return empList.remove(emp);
}
public void deleteAllEmps(){
empList.clear();
}
public void display() {
if(empList.isEmpty()) {
System.out.println("No employee found");
}else {
for(Employee emp : empList) {
System.out.println(emp);
}
}
}
public void deleteEmployeesByDept(String dept)
throws DeptNotFoundException{
boolean deleted = false;
Employee emp;
for(int i=0; i