0% found this document useful (0 votes)
6 views6 pages

Question 4

Uploaded by

swarajya laxmi
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
6 views6 pages

Question 4

Uploaded by

swarajya laxmi
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
You are on page 1/ 6

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:

Use separate threads for the following tasks:


Thread 1: Read data from the file and store it in a collection (e.g., ArrayList).
Thread 2: Insert data from the collection into the database.
Thread 3: Process the employee data to calculate the average salary of all
employees.
Exception Handling:

Handle exceptions that may occur during:


File reading/writing.
Database connection and queries.
Thread synchronization issues.

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;

Employee(int id, String name, String department, double salary) {


this.id = id;
this.name = name;
this.department = department;
this.salary = salary;
}
}

public class EmployeeDataProcessing {

private static final String DB_URL =


"jdbc:mysql://localhost:3306/employees_db";
private static final String DB_USER = "root";
private static final String DB_PASSWORD = "password";
private static final String FILE_NAME = "employees.csv";

private static final List<Employee> employeeList = new ArrayList<>();


private static final Lock lock = new ReentrantLock();

public static void main(String[] args) {


Thread fileReaderThread = new Thread(new FileReaderTask());
Thread databaseInserterThread = new Thread(new DatabaseInserterTask());
Thread salaryProcessorThread = new Thread(new SalaryProcessorTask());

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());
}
}

static class FileReaderTask implements Runnable {


@Override
public void run() {
try (BufferedReader br = new BufferedReader(new FileReader(FILE_NAME)))
{
String line;
while ((line = br.readLine()) != null) {
String[] details = line.split(",");
if (details.length == 4) {
int id = Integer.parseInt(details[0].trim());
String name = details[1].trim();
String department = details[2].trim();
double salary = Double.parseDouble(details[3].trim());

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());
}
}
}

static class DatabaseInserterTask implements Runnable {


@Override
public void run() {
try (Connection connection = DriverManager.getConnection(DB_URL,
DB_USER, DB_PASSWORD);
PreparedStatement statement = connection.prepareStatement("INSERT
INTO employees (id, name, department, salary) VALUES (?, ?, ?, ?)");) {

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());
}
}
}

static class SalaryProcessorTask implements Runnable {


@Override
public void run() {
lock.lock();
try {
double totalSalary = 0;
for (Employee emp : employeeList) {
totalSalary += emp.salary;
}
double averageSalary = employeeList.isEmpty() ? 0 : totalSalary /
employeeList.size();
System.out.println("Average salary of employees: " +
averageSalary);
} finally {
lock.unlock();
}
}
}
}

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:

Test Case 1: Valid Input Data


Description: Test with a valid CSV file containing employee details.

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

===================================================================================
==

Test Case 2: Empty File


Description: Test with an empty employees.csv file.

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

Notes: Rows with missing fields are skipped.

===============================================================================
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

You might also like