Question 4
Question 4
-----------------------------------------------
Employee Data Processing System
Problem Statement:
Design and implement a Java application that performs the following operations:
File Handling:
Read employee data from a file (employees.csv).
Each line in the file contains details of one employee in the following format:
id, name, department, salary
Example:
1, Alice, HR, 50000
2, Bob, IT, 60000
3, Charlie, Sales, 55000
Database Operations:
Insert the employee data into a database using JDBC.
The database schema should have a table named employees with columns:
id: INT (Primary Key)
name: VARCHAR
department: VARCHAR
salary: DOUBLE.
Multithreading:
Collections:
Store the employee data in a collection (ArrayList or HashMap) for further
processing.
Use this collection to calculate the average salary of employees.
===================================================================================
===================================================================================
=
import java.io.*;
import java.sql.*;
import java.util.*;
import java.util.concurrent.locks.*;
class Employee {
int id;
String name;
String department;
double salary;
fileReaderThread.start();
try {
fileReaderThread.join();
} catch (InterruptedException e) {
System.out.println("Error waiting for FileReaderThread: " +
e.getMessage());
}
databaseInserterThread.start();
salaryProcessorThread.start();
try {
databaseInserterThread.join();
salaryProcessorThread.join();
} catch (InterruptedException e) {
System.out.println("Error waiting for threads: " + e.getMessage());
}
}
lock.lock();
try {
employeeList.add(new Employee(id, name, department,
salary));
} finally {
lock.unlock();
}
}
}
System.out.println("File reading completed. Data loaded into the
collection.");
} catch (IOException e) {
System.out.println("Error reading file: " + e.getMessage());
}
}
}
lock.lock();
try {
for (Employee emp : employeeList) {
statement.setInt(1, emp.id);
statement.setString(2, emp.name);
statement.setString(3, emp.department);
statement.setDouble(4, emp.salary);
statement.addBatch();
}
statement.executeBatch();
System.out.println("Data inserted into the database
successfully.");
} finally {
lock.unlock();
}
} catch (SQLException e) {
System.out.println("Error inserting data into database: " +
e.getMessage());
}
}
}
Database Creation:-
CREATE DATABASE employees_db;
USE employees_db;
CREATE TABLE employees (
id INT PRIMARY KEY,
name VARCHAR(100),
department VARCHAR(50),
salary DOUBLE
);
Sample Output:-
File reading completed. Data loaded into the collection.
Data inserted into the database successfully.
Average salary of employees: 55000.0
==============================================================================
Test Cases:
Input:
employees.csv content:
1, Alice, HR, 50000
2, Bob, IT, 60000
3, Charlie, Sales, 55000
Expected Output:
Console Output:
File reading completed. Data loaded into the collection.
Data inserted into the database successfully.
Average salary of employees: 55000.0
DataBase output:
SELECT * FROM employees;
Result:
id name department salary
1 Alice HR 50000.0
2 Bob IT 60000.0
3 Charlie Sales 55000.0
===================================================================================
==
Input:
employees.csv content: (Empty)
Expected Output:
Console Output:
File reading completed. Data loaded into the collection.
Data inserted into the database successfully.
Average salary of employees: 0.0
Database output:
No records inserted.
==============================================================================
Test Case 3: Missing or Malformed Data
Description: Test with a CSV file containing missing or incorrect fields.
Input:
employees.csv content:
1, Alice, HR, 50000
2, , IT, 60000
3, Charlie, Sales
Expected Output:
Console Output:
File reading completed. Data loaded into the collection.
Data inserted into the database successfully.
Average salary of employees: 50000.0
Database output:
SELECT * FROM employees;
Result:
id name department salary
1 Alice HR 50000.0
===============================================================================
Test Case 4: Duplicate Employee IDs
Description: Test with duplicate IDs in the CSV file.
Input:
employees.csv content:
1, Alice, HR, 50000
1, Bob, IT, 60000
Expected Output:
Console Output:
File reading completed. Data loaded into the collection.
Error inserting data into database: Duplicate entry '1' for key 'PRIMARY'
Average salary of employees: 50000.0
Database output:
SELECT * FROM employees;
Result:
id name department salary
1 Alice HR 50000.0
===================================================================================
=
Test Case 5: Non-Numeric Salary
Description: Test with invalid (non-numeric) salary values.
Input:
employees.csv content:
1, Alice, HR, 50000
2, Bob, IT, sixty_thousand
3, Charlie, Sales, 55000
Expected Output:
Console Output:
Error reading file: For input string: \"sixty_thousand\"
File reading completed. Data loaded into the collection.
Data inserted into the database successfully.
Average salary of employees: 52500.0
Database output:
SELECT * FROM employees;
Result:
id name department salary
1 Alice HR 50000.0
3 Charlie Sales 55000.0
===================================================================================
===
Test Case 6: Large Data Set
Description: Test with a large CSV file containing 10,000+ employee records.
Input:
employees.csv content:
1, Alice, HR, 50000
2, Bob, IT, 60000
...
10000, Zed, Marketing, 75000
Expected Output:
Console Output:
File reading completed. Data loaded into the collection.
Data inserted into the database successfully.
Average salary of employees: <Calculated Average>
Database output:
SELECT COUNT(*) FROM employees;
10000 rows inserted