Professional Documents
Culture Documents
employeepayrollJDBC;
enum Exception_Type{
CONNECTION_FAILED,
QUERY_FAILED,
ADD_FAILED,
READ_FAILED,
UPDATE_FAILED,
READ_IN_DATERANGE_FAILED
super(message);
this.type = type;
package com.capgemini.employeepayrollJDBC;
import java.util.*;
import java.util.stream.Collectors;
import java.util.stream.Stream;
import java.io.*;
import java.nio.file.*;
public class EmployeeFile {
EmployeeFile() {
data.append(e.toString() + "\n");
try {
Files.write(filePath, data.toString().getBytes());
e1.printStackTrace();
int count = 0;
try {
} catch (IOException e) {
e.printStackTrace();
}
return count;
try {
} catch (IOException e) {
e.printStackTrace();
int i = 0;
try {
for (String s : e) {
data[i] = e[i].split("=")[1];
i += 1;
i = 0;
empPayRoll.add(new EmployeePayRoll(Integer.parseInt(data[0]), data[1],
Double.parseDouble(data[2])));
return empPayRoll;
} catch (IOException e) {
e.printStackTrace();
return null;
package com.capgemini.employeepayrollJDBC;
import java.time.LocalDate;
import java.util.List;
this.id = id;
this.name = name;
this.salary = salary;
public EmployeePayRoll(int id, String name, String gender, double salary, int companyId,
this.gender = gender;
this.companyId = companyId;
this.departmentName = departmentName;
this.startDate = startDate;
return id;
this.id = id;
return name;
this.name = name;
this.salary = salary;
return companyId;
this.companyId = companyId;
return departmentName;
this.departmentName = departmentName;
return startDate;
}
this.startDate = startDate;
return gender;
this.gender = gender;
@Override
int result = 1;
long temp;
temp = Double.doubleToLongBits(salary);
return result;
}
@Override
if (this == obj)
return true;
if (obj == null)
return false;
if (getClass() != obj.getClass())
return false;
if (companyId != other.companyId)
return false;
if (departmentName == null) {
if (other.departmentName != null)
return false;
} else if (!departmentName.equals(other.departmentName))
return false;
if (gender == null) {
if (other.gender != null)
return false;
} else if (!gender.equals(other.gender))
return false;
if (id != other.id)
return false;
if (name == null) {
if (other.name != null)
return false;
} else if (!name.equals(other.name))
return false;
if (Double.doubleToLongBits(salary) != Double.doubleToLongBits(other.salary))
return false;
if (startDate == null) {
if (other.startDate != null)
return false;
} else if (!startDate.equals(other.startDate))
return false;
return true;
package com.capgemini.employeepayrollJDBC;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.time.LocalDate;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
private EmployeePayrollDBService() {
if (employeePayRollDBService == null)
return employeePayRollDBService;
Statement statement;
statement = connection.createStatement();
result = statement.executeQuery(query);
employeePayRollList = getDatafromResultset(result);
} catch (SQLException e) {
return employeePayRollList;
if (n == 1)
return this.updateSalaryUsingStatement(name, salary);
else
preparedStatement = connection
preparedStatement.setDouble(1, salary);
preparedStatement.setString(2, name);
return result;
} catch (SQLException e) {
"update employee_payroll set salary = %.2f where name = '%s' and is_active = true ;", salary,
name);
Statement statement;
int result = 0;
statement = connection.createStatement();
result = statement.executeUpdate(query);
return result;
} catch (SQLException e) {
preparedStatement = connection
preparedStatement.setString(1, name);
employeePayRollList = getDatafromResultset(result);
return employeePayRollList.get(0);
} catch (SQLException e) {
preparedStatement = connection.prepareStatement(
"select * from employee_payroll where is_active = true and id in (select emp_id from
employee_department where start_date between cast(? as date) and cast(? as date))");
preparedStatement.setString(1, startDate);
preparedStatement.setString(2, endDate);
employeePayRollList = getDatafromResultset(result);
return employeePayRollList;
} catch (SQLException e) {
functionMap.put("minMale", min.get(0));
functionMap.put("minFemale", min.get(1));
functionMap.put("maxMale", max.get(0));
functionMap.put("maxFemale", max.get(1));
functionMap.put("sumMale", sum.get(0));
functionMap.put("sumFemale", sum.get(1));
functionMap.put("avgMale", avg.get(0));
functionMap.put("avgFemale", avg.get(1));
functionMap.put("countMale", count.get(0));
functionMap.put("countFemale", count.get(1));
return functionMap;
Statement statement;
ResultSet result;
statement = connection.createStatement();
result = statement.executeQuery(query);
int i = 0;
while (result.next()) {
i = i + 1;
functionList.add(result.getDouble(1));
return functionList;
} catch (SQLException e) {
public EmployeePayRoll addEmployee(String name, String gender, double salary, LocalDate startDate)
throws CustomSQLException {
int employeeId = 0;
if (rowAffected == 1) {
if (result.next())
employeeId = result.getInt(1);
return employee;
} catch (SQLException e) {
int employeeId = 0;
try {
connection = this.getConnection();
connection.setAutoCommit(false);
e2.printStackTrace();
if (rowAffected == 1) {
if (result.next())
employeeId = result.getInt(1);
} catch (SQLException e) {
try {
connection.rollback();
return employee;
e1.printStackTrace();
int departmentId = 0;
try {
while (result3.next()) {
departmentId = (result3.getInt("id"));
try {
connection.rollback();
e1.printStackTrace();
} catch (SQLException e) {
try {
connection.rollback();
e1.printStackTrace();
if (rowAffected == 1) {
departmentNameList.add(departmentName);
startDates.add(startDate);
startDates);
} catch (SQLException e) {
try {
connection.rollback();
e1.printStackTrace();
try {
connection.commit();
} finally {
if (connection != null)
try {
connection.close();
} catch (SQLException e) {
e.printStackTrace();
return employee;
String query = String.format("update employee_payroll set is_active = false where name = '%s'",
name);
Statement statement;
int result = 0;
statement = connection.createStatement();
result = statement.executeUpdate(query);
employeePayRollList = readData();
if (employee.getName().equals(name)) {
employeePayRollList.remove(employee);
return employeePayRollList;
} catch (SQLException e) {
try {
} catch (SQLException e) {
return connection;
employeePayRollList = this.readData();
while (result.next()) {
preparedStatement1.setInt(1, result.getInt("id"));
while (result1.next()) {
startDates.add(result1.getDate("start_date").toLocalDate());
department_id.add(result1.getInt("dept_id"));
preparedStatement2.setInt(1, i);
while (result2.next()) {
department_name.add(result2.getString("name"));
employeePayRollList.add(
} catch (SQLException e) {
e.printStackTrace();
return employeePayRollList;
package com.capgemini.employeepayrollJDBC;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.Enumeration;
import com.mysql.cj.jdbc.Driver;
try {
Class.forName("com.mysql.cj.jdbc.Driver");
con = DriverManager.getConnection(url, userName, password);
System.out.println("Connection Successful");
} catch (ClassNotFoundException e) {
} catch (SQLException e) {
e.printStackTrace();
listAllDrivers();
try {
} catch (Exception e) {
e.printStackTrace();
return con;
while (driverList.hasMoreElements()) {
package com.capgemini.employeepayrollJDBC;
import java.time.LocalDate;
import java.util.*;
this();
this.empPayRollList = empPayRollList;
public EmployeePayService() {
EmployeePayrollDBService = EmployeePayrollDBService.getInstance();
empPayRollService.readData();
empPayRollService.writeData("Console");
int id = sc.nextInt();
sc.nextLine();
empPayRollList.add(empPayRollObject);
if (source.equals("File")) {
return empPayRollList.size();
} else if (source.equals("DB")) {
empPayRollList = EmployeePayrollDBService.readData();
return empPayRollList.size();
return 0;
if (success == 1) {
if (e.getName().equals(name)) {
e.setSalary(salary);
}
if (destination.equals("Console"))
else if (destination.equals("File"))
new EmployeeFile().writeData(empPayRollList);
if (destination.equals("Console"))
return empPayRollList.size();
else if (destination.equals("File"))
return 0;
if (destination.equals("Console"))
System.out.println(e.toString() + "\n");
else if (destination.equals("File"))
new EmployeeFile().printData();
}
public boolean checkDBInSyncWithList(String name) throws CustomSQLException {
return employeeDB.equals(employeeList);
return EmployeePayrollDBService.preparedStatementReadData(name);
return EmployeePayrollDBService.getMinMaxSumAvgCount();
public void addEmployee(String name, String gender, double salary, LocalDate startDate) throws
CustomSQLException {
if (employee != null)
empPayRollList.add(employee);
}
public void addEmployeeAndPayRoll(String name, String gender, double salary, int companyId,
departmentName, startDate);
if (employee != null)
empPayRollList.add(employee);
empPayRollList = EmployeePayrollDBService.deleteEmployee(name);
if (e.getName().equals(name)) {
System.out.println(e.getName());
result = true;
return result;
employeeList.forEach(e -> {
System.out.println("Employee adding : " + e.getName());
try {
e1.printStackTrace();
});
System.out.println(empPayRollList.size());
return empPayRollList.size();
employeeList.forEach(e -> {
additionStatus.put(e.hashCode(), false);
try {
e1.printStackTrace();
additionStatus.put(e.hashCode(), true);
};
Thread thread = new Thread(task, e.name);
thread.start();
});
while (additionStatus.containsValue(false)) {
try {
Thread.sleep(10);
} catch (InterruptedException e) {
e.printStackTrace();
return empPayRollList.size();
package com.capgemini.employeepayrollJDBC;
import org.junit.*;
import java.time.Duration;
import java.time.Instant;
import java.time.LocalDate;
import java.util.*;
@Test
empPayRollService.writeData("File");
empPayRollService.printData("File");
Assert.assertTrue(result);
@Test
int entries;
try {
entries = empPayRollService.readData("File");
Assert.assertTrue(result);
} catch (CustomSQLException e) {
e.printStackTrace();
@Test
int entries;
try {
entries = empPayRollService.readData("DB");
boolean result = entries == 7 ? true : false;
Assert.assertTrue(result);
} catch (CustomSQLException e) {
e.printStackTrace();
@Test
try {
Assert.assertTrue(result);
} catch (CustomSQLException e) {
e.printStackTrace();
@Test
try {
Assert.assertTrue(result);
} catch (CustomSQLException e) {
e.printStackTrace();
@Test
EmployeePayRoll employee;
try {
employee = empPayRollService.preparedStatementReadData("Mark");
Assert.assertTrue(result);
} catch (CustomSQLException e) {
e.printStackTrace();
@Test
try {
Assert.assertTrue(result);
} catch (CustomSQLException e) {
e.printStackTrace();
}
@Test
try {
Assert.assertTrue(result);
} catch (CustomSQLException e) {
e.printStackTrace();
@Test
try {
int companyId = 3;
Assert.assertTrue(result);
} catch (CustomSQLException e) {
e.printStackTrace();
@Test
try {
int companyId = 3;
Arrays.asList(startDate));
Assert.assertTrue(result);
} catch (CustomSQLException e) {
e.printStackTrace();
@Test
int companyId = 4;
Arrays.asList(startDate));
Assert.assertTrue(result);
} catch (CustomSQLException e) {
e.printStackTrace();
@Test
try {
empPayRollService.deleteEmployee("Rita");
Assert.assertFalse(result);
} catch (CustomSQLException e) {
e.printStackTrace();
}
@Test
try {
EmployeePayRoll[] employeeArray = {
Arrays.asList(LocalDate.parse("2018-05-21"))),
Arrays.asList(LocalDate.parse("2016-07-29"))),
Arrays.asList(LocalDate.parse("2015-08-07"))),
Arrays.asList(LocalDate.parse("2017S-04-29"))) };
Assert.assertTrue(result);
} catch (CustomSQLException e) {
e.printStackTrace();
@Test
public void givenMultipleEmployeeWithThreads_WhenAddedToDB_ShouldMatchCountOfEntries() {
try {
EmployeePayRoll[] employeeArray = {
Arrays.asList(LocalDate.parse("2008-05-01"))),
Arrays.asList(LocalDate.parse("2011-07-29"))),
Arrays.asList(LocalDate.parse("2017-07-07"))),
Arrays.asList(LocalDate.parse("2020-04-29"))) };
empPayRollService.addEmployeeAndPayRoll(Arrays.asList(employeeArray));
int countOfEntries =
empPayRollService.addEmployeeAndPayRollWithThread(Arrays.asList(employeeArray));
Assert.assertTrue(result);
} catch (CustomSQLException e) {
e.printStackTrace();
}
}