Monday, 7 December 2015

Mini Project CUI BAse

//Developed By Amol Sitaram Orape
import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.io.Serializable;
import java.util.*;

public class Emp implements Serializable, Comparable {
    int id;
    String name;
    double sal;
    String Deg;

    int getId() {
        return id;
    }

    void setId(int id) {
        this.id = id;
    }

    String getName() {
        return name;
    }

    void setName(String name) {
        this.name = name;
    }

    double getSal() {
        return sal;
    }

    void setSal(double sal) {
        this.sal = sal;
    }

    String getDeg() {
        return Deg;
    }

    void setDeg(String deg) {
        Deg = deg;
    }// end here setter and getter

    public Emp() {
        super();
        this.id = 100;
        this.name = "Amol";
        this.sal = 50000;
        Deg = "Manger";
    }

    public Emp(int id, String name, double sal, String deg) {
        super();
        this.id = id;
        this.name = name;
        this.sal = sal;
        Deg = deg;
    }// end here constr....

    @Override
    public String toString() {
        // TODO Auto-generated method stub
        return "Information is " + id + "\n" + name + "\n" + sal + "\n" + Deg;
    }

    @Override
    public int compareTo(Object o) {
        // TODO Auto-generated method stub
        Emp w = (Emp) o;
        return this.id = w.id;
    }

    @Override
    public boolean equals(Object obj) {
        // TODO Auto-generated method stub
        Emp w = (Emp) obj;
        if (this.id == w.id)
            return true;
        else
            return false;

    }
    public boolean equal(Object obj) {
        // TODO Auto-generated method stub
        Emp w = (Emp) obj;
        if (this.id == w.id)
        {
            return true;
        }
        else
            return false;

    }
}

class Test {
    public static void main(String[] args) throws FileNotFoundException {

        InputStreamReader isr = new InputStreamReader(System.in);
        BufferedReader br = new BufferedReader(isr);
        int ch;
        System.out.println("**********Menu**************");
        System.out.println("1) Create Five Employee Details");
        System.out.println("2) Dispaly all Employee Details");
        System.out.println("3) Add Single Employee Details");
        System.out.println("4) Search By id");
        System.out.println("5) Upadte By id ");
        System.out.println("6) Sort  By id ");
        ArrayList a = new ArrayList();
        do {
            Scanner s = new Scanner(System.in);
            ch = s.nextInt();
            switch (ch) {

            case 1: {
                Emp e1 = new Emp(1, "Amol", 50000, "Manger");
                Emp e2 = new Emp(2, "Akshay", 50000, "Manger");
                Emp e3 = new Emp(3, "Rushikesh", 50000, "Manger");
                Emp e4 = new Emp(4, "Parasad", 50000, "Manger");
                Emp e5 = new Emp(5, "Aaish", 50000, "Manger");

                a.add(e1);
                a.add(e2);
                a.add(e3);
                a.add(e4);
                a.add(e5);
                try {

                    FileOutputStream fio = new FileOutputStream(
                            "D:\\amol\\khushi\\emp.txt");
                    try {
                        ObjectOutputStream os = new ObjectOutputStream(fio);
                        os.writeObject(a);
                        System.out.println("Write Sucessfully");
                        os.close();
                    } catch (IOException e6) {
                        // TODO Auto-generated catch block
                        e6.printStackTrace();
                    }
                } catch (FileNotFoundException e6) {
                    // TODO Auto-generated catch block
                    e6.printStackTrace();
                }
                break;
            }

            case 2: {
                try {
                    FileInputStream fi = new FileInputStream(
                            "D:\\amol\\khushi\\emp.txt");
                    try {
                        ObjectInputStream oa = new ObjectInputStream(fi);
                        try {
                            ArrayList a1 = ((ArrayList) oa.readObject());
                            // System.out.println(a1);
                            Iterator it = a.iterator();
                            while (it.hasNext()) {
                                System.out.println(it.next());
                            }

                            oa.close();

                        } catch (ClassNotFoundException e6) {
                            // TODO Auto-generated catch block
                            e6.printStackTrace();
                        }
                    } catch (IOException e6) {
                        // TODO Auto-generated catch block
                        e6.printStackTrace();
                    }
                } catch (FileNotFoundException e6) {
                    // TODO Auto-generated catch block
                    e6.printStackTrace();
                }
                break;
            }

            case 3: {

                try {
                    System.out.println("Enter the id :");
                    int nm = Integer.parseInt(br.readLine());
                    System.out.println("Enter the Name");
                    String a1 = br.readLine();
                    System.out.println("Enter the salary");
                    double aa = Double.parseDouble(br.readLine());
                    System.out.println("Enter the Degisation ");
                    String as = br.readLine();

                    Emp e11 = new Emp(nm, a1, aa, as);
                    System.out.println(e11);
                    a.add(e11);

                } catch (IOException e12) {
                    // TODO Auto-generated catch block
                    e12.printStackTrace();
                }
                break;
            }
            case 4: {
                FileInputStream fi;
                try {
                    fi = new FileInputStream("D:\\amol\\khushi\\emp.txt");

                    ObjectInputStream oa;

                    oa = new ObjectInputStream(fi);

                    ArrayList a1;

                    a1 = ((ArrayList) oa.readObject());
                    //System.out.println(a1);

                    System.out.println("Enter the id :");
                    int nm = s.nextInt();
                    System.out.println("Enter the Name");
                    String a11 = s.next();
                    System.out.println("Enter the salary");
                    double aa = s.nextDouble();
                    System.out.println("Enter the Degisation ");
                    String as = s.next();

                    Emp e11 = new Emp(nm, a11, aa, as);

                    if (a1.contains(e11)) {
                        System.out.println("Found");
                    } else {
                        System.out.println("Not Found ");
                    }
                } catch (ClassNotFoundException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                } catch (IOException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }

                break;
            }
            case 5: {
                FileInputStream fi;
                try {
                    fi = new FileInputStream("D:\\amol\\khushi\\emp.txt");

                    ObjectInputStream oa;

                    oa = new ObjectInputStream(fi);

                    ArrayList a1;

                    a1 = ((ArrayList) oa.readObject());
                    //System.out.println(a1);
                    System.out.println("Enter the id :");
                    int nm = s.nextInt();
                    System.out.println("Enter the Name");
                    String a11 = s.next();
                    System.out.println("Enter the salary");
                    double aa = s.nextDouble();
                    System.out.println("Enter the Degisation ");
                    String as = s.next();

                    Emp e11 = new Emp(nm, a11, aa, as);
                    if(a1.contains(e11))
                    {
                        System.out.println("Id is Found so enter the updated information");
                       
                        System.out.println(a1);
                        System.out.println("Enter the id :");
                        int nm1 = s.nextInt();
                        System.out.println("Enter the Name");
                        String a111 = s.next();
                        System.out.println("Enter the salary");
                        double aa1 = s.nextDouble();
                        System.out.println("Enter the Degisation ");
                        String as1 = s.next();

                        Emp e111 = new Emp(nm1, a111, aa1, as1);
                        a.add(e111);
                        FileOutputStream fio = new FileOutputStream(
                                "D:\\amol\\khushi\\emp.txt");
                        ObjectOutputStream o=new ObjectOutputStream(fio);
                        o.writeObject(a);
                        o.close();
                        System.out.println("Written Sucessfully");
                        System.out.println("Devloped By AMOL SITARAM ORAPE");
                       
                       
                    }
                    else
                        System.out.println("Updated Id is Not Found..!! Please Try Agains..!! ");
                }
                    catch (IOException | ClassNotFoundException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
               
                }

    break;

            }
           
            case 6: {

                System.out.println("Code in Progress.............!!amol..!");

            }

                break;
            }

        } while (ch != 10);
    }
}

No comments:

Post a Comment