Step 1>
package com.kartik.file;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Scanner;
public class ReadCVS {
public static void main(String[] args)
throws FileNotFoundException,
IOException {
ReadCVS obj = new ReadCVS();
obj.run();
}
public void run() throws
FileNotFoundException, IOException {
Scanner scanner = new Scanner(new
File("D:\\Kartik\\employee.csv"));
Scanner dataScanner = null;
int index = 0;
List<Employee> empList = new
ArrayList<Employee>();
while (scanner.hasNextLine()) {
dataScanner = new
Scanner(scanner.nextLine());
dataScanner.useDelimiter(",");
Employee emp = new Employee();
while (dataScanner.hasNext()) {
String data = dataScanner.next();
if (index == 0)
emp.setId(Integer.parseInt(data));
else if (index == 1)
emp.setName(data);
else if (index == 2)
emp.setRole(data);
else if (index == 3)
emp.setSalary(data);
else if (index == 4)
emp.setDate(stringDateToDateConvert(data));
else
System.out.println("invalid
data::" + data);
index++;
}
index = 0;
empList.add(emp);
}
DateFormat df = new
SimpleDateFormat("yyyy-MM-dd");
Date fromDate = stringDateToDateConvert("2014-01-20");
Date toDate =
stringDateToDateConvert("2015-12-20");
List<Employee> empObjList = new
ArrayList<Employee>();
for (Employee employee : empList) {
if (compareDatesByCompareTo(df,
fromDate, toDate,
employee.getDate())) {
Employee empObj = new Employee();
empObj.setDate(employee.getDate());
empObj.setId(employee.getId());
empObj.setName(employee.getName());
empObj.setSalary(employee.getSalary());
empObjList.add(empObj);
}
System.out.println(employee.getName());
}
int totalSal = 0;
for (Employee
employee : empObjList) {
String salary =
employee.getSalary().split(" ")[0]; // Extracting only the numeric
part
totalSal += Integer.parseInt(salary);
System.out.println("Employee Name
--> " + employee.getName());
}
System.out.println("Total
Salary: " + totalSal);
System.out.println("Employee
Name-->" + employee.getName());
}
System.out.println("Total Sal
" + totalSal);
}
/**
*
* @param df
* @param oldDate
* @param newDate
* @param currentDate
* @return
*/
public static boolean
compareDatesByCompareTo(DateFormat df, Date oldDate,
Date newDate, Date currentDate) {
return
(currentDate.compareTo(fromDate) >= 0 &&
currentDate.compareTo(toDate) <= 0);
}
/**
*
* @param date
* @return
*/
public Date
stringDateToDateConvert(String date) {
Date convertedDate = null;
DateFormat formatter = null;
// Creating SimpleDateFormat with
yyyy-MM-dd format e.g."2011-09-14"
// String yyyyMMdd =
"20110914";
formatter = new
SimpleDateFormat("yyyy-MM-dd");
try {
convertedDate = (Date)
formatter.parse(date);
} catch (ParseException e) {
e.printStackTrace();
}
return convertedDate;
}
}
Step 2>
package com.kartik.file;
import java.util.Date;
public class Employee {
private int id;
private String name;
private String role;
private String salary;
private Date date;
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getRole() {
return role;
}
public void setRole(String role) {
this.role = role;
}
public String getSalary() {
return salary;
}
public void setSalary(String salary) {
this.salary = salary;
}
/**
* @return the date
*/
public Date getDate() {
return date;
}
/**
* @param date the date to set
*/
public void setDate(Date date) {
this.date = date;
}
@Override
public String toString() {
return "\nID=" + getId() +
"::Name" + getName() + "::Role=" + getRole()
+ "::Salary=" +
getSalary() + "::Date=" + getDate();
}
}
|
The code you've provided is a Java program that reads a CSV
file (specifically employee.csv) containing employee information such as id, name,
role, salary, and date. It processes this information, converts the date
strings into Date objects, filters the employees based on the date range,
calculates the total salary, and then prints out the employees and the total
salary.
Step-by-Step Overview of the Code:
1. Reading the CSV File:
- The ReadCVS
class reads the file employee.csv using a Scanner object and parses each
line using a dataScanner to tokenize based on the comma delimiter.
- It
stores each employee's details in an Employee object, and then adds the
object to a list of employees (empList).
2. Date Conversion:
- The stringDateToDateConvert
method is used to convert a date string (in yyyy-MM-dd format) to a Date
object.
3. Filtering Employees by Date:
- The
program compares the employee's date of joining with a given range (fromDate
to toDate). It filters employees whose joining date falls within this
range and stores the filtered employees in empObjList.
4. Salary Calculation:
- The
program splits the salary string (which includes both the amount and the
currency, e.g., "5000 USD") to extract the numeric part of the
salary and adds it to a total salary sum.
5. Employee Class:
- The Employee
class contains fields like id, name, role, salary, and date along with
their getters, setters, and a toString method for formatting the output.
Example Employee CSV Data (employee.csv):
Plain text
1,John
Doe,Manager,5000 USD,2014-01-15
2,Jane
Smith,Developer,4500 USD,2015-02-10
3,Bob
Johnson,Designer,4000 USD,2014-03-25
|
Conclusion:
The provided code works for reading a CSV file, parsing
employee data, filtering by date, and calculating salaries. By fixing the
mentioned issues, adding better error handling, and using proper resource
management, you can ensure the code is more robust and maintainable.
 |
Read CSV file and Display salary between from date and to date |
0 Comments