Acc
Beans
package com.demo.beans;
import java.io.Serializable;
public class Employee implements Serializable {
private int empid;
private String ename;
private double sal;
private double bonus;
public Employee() {
super();
}
public Employee(int empid, String ename, double sal, double bonus) {
super();
System.out.println("in parametrised constructor");
this.empid = empid;
this.ename = ename;
this.sal = sal;
this.bonus = bonus;
}
@Override
public boolean equals(Object obj) {
System.out.println("in employee equals method "+this.empid+"-----"+((Employee)obj).empid);
return this.empid==((Employee)obj).empid;
}
public Employee(int empid) {
super();
this.empid = empid;
}
public int getEmpid() {
System.out.println("in getEmpid");
return empid;
}
public void setEmpid(int empid) {
System.out.println("in setEmpid");
this.empid = empid;
}
public String getEname() {
System.out.println("in getEname");
return ename;
}
public void setEname(String ename) {
System.out.println("in setename");
this.ename = ename;
}
public double getSal() {
return sal;
}
public void setSal(double sal) {
System.out.println("in setSal");
this.sal = sal;
}
public double getBonus() {
System.out.println("in getBonus");
return bonus;
}
public void setBonus(double bonus) {
System.out.println("in setBonus");
this.bonus = bonus;
}
@Override
public String toString() {
return "Employee [empid=" + empid + ", ename=" + ename + ", sal=" + sal + ", bonus=" + bonus + "]";
}
}
Dao :
package com.demo.dao;
import java.util.List;
import com.demo.beans.Employee;
import com.demo.exceptions.EmployeeNotFound;
public interface EmployeeDao {
void readDataFromFile(String fname);
void save(Employee e);
List<Employee> findAll();
void writeToFile(String fname);
boolean removeById(int id) throws EmployeeNotFound;
boolean modifyById(int id, double sal) throws EmployeeNotFound;
}
package com.demo.dao;
import java.io.EOFException;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.util.ArrayList;
import java.util.List;
import com.demo.beans.Employee;
import com.demo.exceptions.EmployeeNotFound;
public class EmployeeDaoImpl implements EmployeeDao{
static List<Employee> elst;
static {
elst=new ArrayList<>();
}
@Override
public void readDataFromFile(String fname) {
try(ObjectInputStream oos=new ObjectInputStream(new FileInputStream(fname));){
while(true) {
Employee e=(Employee) oos.readObject();
elst.add(e);
}
} catch(EOFException e) {
System.out.println("reached to end of file...."+elst.size());
} catch(FileNotFoundException e) {
System.out.println("file does not exists");
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (ClassNotFoundException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
}
@Override
public void save(Employee e) {
elst.add(e);
}
@Override
public List<Employee> findAll() {
return elst;
}
@Override
public void writeToFile(String fname) {
try(ObjectOutputStream oos=new ObjectOutputStream(new FileOutputStream(fname));){
for(Employee e:elst) {
oos.writeObject(e);
}
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
@Override
public boolean removeById(int id) throws EmployeeNotFound {
boolean flag= elst.remove(new Employee(id));
if(flag)
return flag;
throw new EmployeeNotFound("Not found "+id);
}
@Override
public boolean modifyById(int id, double sal) throws EmployeeNotFound {
int pos=elst.indexOf(new Employee(id));
if(pos!=-1) {
Employee e=elst.get(pos);
e.setSal(sal);
return true;
}
throw new EmployeeNotFound("updation fail, not found "+id);
}
}
Exceptions
package com.demo.exceptions;
public class EmployeeNotFound extends Exception{
public EmployeeNotFound(String msg) {
super(msg);
}
}
Test :
package com.demo.test;
import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
public class TestBufferedInputStream {
public static void main(String[] args) {
File f=new File("test1copy.txt");
BufferedOutputStream bos=null;
try {
if(f.exists()) {
//effectively final
//open the file in append mode
bos=new BufferedOutputStream(new FileOutputStream(f,true));
}else {
//open the file in write mode
bos=new BufferedOutputStream(new FileOutputStream(f));
}
}catch(IOException e) {
e.printStackTrace();
}
try(BufferedInputStream bis=new BufferedInputStream(new FileInputStream("test1.txt"));
BufferedOutputStream bos1=bos;){
//read one byte and write one byte till end of the file
int c=bis.read();
while(c!=-1) {
bos.write(c);
c=bis.read();
}
}catch(FileNotFoundException e) {
System.out.println("file not found");
}catch(IOException e) {
System.out.println("input output error occured");
}
}
}
Test 2
package com.demo.test;
import java.util.List;
import java.util.Scanner;
import com.demo.beans.Employee;
import com.demo.exceptions.EmployeeNotFound;
import com.demo.service.EmployeeService;
import com.demo.service.EmployeeServiceImpl;
public class TestEmployeemgnt {
public static void main(String[] args) {
Scanner sc=new Scanner(System.in);
int choice=0;
EmployeeService eservice=new EmployeeServiceImpl();
eservice.readFile("empdata.dat");
do {
System.out.println("1. Add new Employee\n2. delete Employee\n3. update employee\n");
System.out.println("4. display all\n5.exit\nchoice:");
choice=sc.nextInt();
switch(choice) {
case 1->{
eservice.addnewEmployee();
}
case 2->{
try {
System.out.println("enter id");
int id=sc.nextInt();
boolean status=eservice.deleteById(id);
if(status) {
System.out.println("deleted successfully");
}
}catch(EmployeeNotFound e) {
System.out.println(e.getMessage());
}
}
case 3->{
try {
System.out.println("enter id");
int id=sc.nextInt();
System.out.println("enter new salary");
double sal=sc.nextDouble();
boolean status=eservice.updateById(id,sal);
if(status) {
System.out.println("updated successfully");
}
}catch(EmployeeNotFound e) {
System.out.println(e.getMessage());
}
}
case 4->{
List<Employee> elst=eservice.getAll();
elst.stream().forEach(System.out::println);
}
case 5->{
eservice.writeFile("empdata.dat");
System.out.println("thank you for visiting....");
sc.close();
}
default->{}
}
}while(choice!=5);
}
}
Test 3
package com.demo.test;
import java.io.BufferedReader;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
public class TestBufferedReader {
public static void main(String[] args) {
try(BufferedReader br=new BufferedReader(new FileReader("test1.txt"));){
String s=br.readLine();
while(s!=null) {
System.out.println(s);
s=br.readLine();
}
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
ServiceImpl :
package com.demo.service;
import java.util.List;
import java.util.Scanner;
import com.demo.beans.Employee;
import com.demo.dao.EmployeeDao;
import com.demo.dao.EmployeeDaoImpl;
import com.demo.exceptions.EmployeeNotFound;
public class EmployeeServiceImpl implements EmployeeService{
private EmployeeDao edao;
public EmployeeServiceImpl() {
edao=new EmployeeDaoImpl();
}
@Override
public void readFile(String fname) {
edao.readDataFromFile(fname);
}
@Override
public void addnewEmployee() {
Scanner sc=new Scanner(System.in);
System.out.println("enter id");
int id=sc.nextInt();
System.out.println("enter name");
String nm=sc.next();
System.out.println("enter sal");
double sal=sc.nextDouble();
Employee e=new Employee(id,nm,sal,sal*0.10);
edao.save(e);
}
@Override
public List<Employee> getAll() {
return edao.findAll();
}
@Override
public void writeFile(String fname) {
edao.writeToFile(fname);
}
@Override
public boolean deleteById(int id) throws EmployeeNotFound {
return edao.removeById(id);
}
@Override
public boolean updateById(int id, double sal) throws EmployeeNotFound {
return edao.modifyById(id,sal);
}
}
Comments
Post a Comment