Home  • Programming • Java

Java CRUD CSV

ddd Employee.java
import java.io.BufferedReader;
import java.io.FileReader;
import java.io.IOException;
import java.io.FileWriter;
import java.io.PrintWriter;
import java.util.List;
import java.util.ArrayList;

class Employee {
	public static String EMPLOYEE_FILE = "data\employee.txt";

	public String id;
	public String name;
	public String mobile;
	public String email;
	public String position;
	public String dept;
	public String doj;
	public String dob;
	public String address;
	public String salary;

	public Employee() {
	}

	public void save() {

		String csv = "" + this.id + "," + this.name + "," + this.mobile + "," + this.email + "," + this.position + ","
				+ this.dept + "," + this.doj + "," + this.dob + "," + this.salary + "," + this.address + "
";

		try (PrintWriter out = new PrintWriter(new FileWriter(EMPLOYEE_FILE, true))) {
			out.write(csv.toString());
		} catch (IOException e) {
			e.printStackTrace();
		}

	}

	public static List<Employee> delete(String id) {

		List<Employee> employees = new ArrayList<Employee>();
		String line = "";
		try {

			try (BufferedReader br = new BufferedReader(new FileReader(EMPLOYEE_FILE))) {
				while ((line = br.readLine()) != null) {
					String[] employee = line.split(",");

					if (!employee[0].equals(id)) {
						Employee emp = new Employee();
						emp.id = employee[0];
						emp.name = employee[1];
						emp.mobile = employee[2];
						emp.email = employee[3];
						emp.position = employee[4];
						emp.dept = employee[5];
						emp.doj = employee[6];
						emp.dob = employee[7];
						emp.salary = employee[8];
						emp.address = employee[9];
						employees.add(emp);
					}

				}
			}
			// Save Change
			try (PrintWriter out = new PrintWriter(new FileWriter(EMPLOYEE_FILE))) {
				for (Employee emp : employees) {
					String csv = "" + emp.id + "," + emp.name + "," + emp.mobile + "," + emp.email + "," + emp.position
							+ "," + emp.dept + "," + emp.doj + "," + emp.dob + "," + emp.salary + "," + emp.address
							+ "
";
					out.write(csv.toString());
				}

			} catch (IOException e) {
				e.printStackTrace();
			}

		} catch (IOException e) {
			e.printStackTrace();
		}

		return employees;

	}

	public List<Employee> edit() {
		List<Employee> employees = new ArrayList<Employee>();
		String line = "";
		try {

			try (BufferedReader br = new BufferedReader(new FileReader(EMPLOYEE_FILE))) {
				while ((line = br.readLine()) != null) {
					String[] employee = line.split(",");
					Employee em = new Employee();

					if (employee[0].equals(this.id)) {
						em.id = this.id;
						em.name = this.name;
						em.mobile = this.mobile;
						em.email = this.email;
						em.position = this.position;
						em.dept = this.dept;
						em.doj = this.doj;
						em.dob = this.dob;
						em.salary = this.salary;
						em.address = this.address;
						employees.add(em);
					} else {

						em.id = employee[0];
						em.name = employee[1];
						em.mobile = employee[2];
						em.email = employee[3];
						em.position = employee[4];
						em.dept = employee[5];
						em.doj = employee[6];
						em.dob = employee[7];
						em.salary = employee[8];
						em.address = employee[9];
						employees.add(em);

					}

				}
			}
			// Save Change
			try (PrintWriter out = new PrintWriter(new FileWriter(EMPLOYEE_FILE))) {
				for (Employee emp : employees) {
					String csv = "" + emp.id + "," + emp.name + "," + emp.mobile + "," + emp.email + "," + emp.position
							+ "," + emp.dept + "," + emp.doj + "," + emp.dob + "," + emp.salary + "," + emp.address
							+ "
";
					out.write(csv.toString());
				}

			} catch (IOException e) {
				e.printStackTrace();
			}

		} catch (IOException e) {
			e.printStackTrace();
		}

		return employees;

	}

	public static ArrayList<Employee> all() {

		ArrayList<Employee> employees = new ArrayList<Employee>();

		String line = "";

		try {

			try (BufferedReader br = new BufferedReader(new FileReader(EMPLOYEE_FILE))) {
				while ((line = br.readLine()) != null) {
					String[] employee = line.split(",");
					Employee emp = new Employee();
					emp.id = employee[0];
					emp.name = employee[1];
					emp.mobile = employee[2];
					emp.email = employee[3];
					emp.position = employee[4];
					emp.dept = employee[5];
					emp.doj = employee[6];
					emp.dob = employee[7];
					emp.salary = employee[8];
					emp.address = employee[9];
					employees.add(emp);
					// use comma as separator
					// System.out.println("Emp[Id=" + employee[0] + ", Name=" + employee[1] + ",
					// Contact=" + employee[2] + ", Email= " + employee[3] + "]");
				}
			}
		} catch (IOException e) {
			e.printStackTrace();
		}

		return employees;

	}

	public static void console(Employee emp) {
		System.out.println("Emp[Id=" + emp.id + ", Name=" + emp.name + ", Contact=" + emp.mobile + ", Email="
				+ emp.email + ", Position= " + emp.position + ", Dept= " + emp.dept + "]");
	}

	public static Employee find(String id) {

		Employee emp = new Employee();
		String line = "";

		try {

			try (BufferedReader br = new BufferedReader(new FileReader(EMPLOYEE_FILE))) {
				while ((line = br.readLine()) != null) {
					String[] employee = line.split(",");

					if (employee[0].equals(id)) {
						emp.id = employee[0];
						emp.name = employee[1];
						emp.mobile = employee[2];
						emp.email = employee[3];
						emp.position = employee[4];
						emp.dept = employee[5];
						emp.doj = employee[6];
						emp.dob = employee[7];
						emp.salary = employee[8];
						emp.address = employee[9];
						return emp;
					}

				}
			}

		} catch (IOException e) {
			e.printStackTrace();
		}

		return emp;
	}

	public static boolean isExist(String id) {

		String line = "";

		try {

			try (BufferedReader br = new BufferedReader(new FileReader(EMPLOYEE_FILE))) {
				while ((line = br.readLine()) != null) {
					String[] employee = line.split(",");

					if (employee[0].equals(id)) {

						return true;
					}

				}
			}

		} catch (IOException e) {
			e.printStackTrace();
		}

		return false;
	}

}

Comments 0


Copyright © 2024. Powered by Intellect Software Ltd