05 April 2016

ADF2 Scanner > arraylist > sort > save file > read JAVA



Key : Comparator, Serializable, ArrayList, Collection sort,  Try - cat
Test.Java
Java 2016
package demo3;

public class Test {

    public static void main(String[] args) {
        // TODO Auto-generated method stub
        ContactConsole ct = new ContactConsole();
        ct.start();
    }

}
ContactConsole.Java
Java 2016
package demo3;

import java.util.Scanner;

public class ContactConsole {

    Manager control = new Manager();
    Scanner s = new Scanner(System.in);

    public void start() {

        while (true) {
            int choice = menu();
            switch (choice) {
                case 1:
                    add();
                    break;
                case 2:
                    read();
                    break;
                case 3:
                    System.exit(0);

            }
        }
    }

    public int menu() {
        System.out.println("|1-[add] |2-[read] |3-[exit] ");
        int choice = readln();
        return choice;
    }

    public int readln() {
        int n = 0;
        try {
            n = Integer.parseInt(s.nextLine());
        } catch (Exception e) {
            // TODO: handle exception
        }
        return n;
    }

    public void add() {
        control.addContact();

    }

    public void read() {
        control.readContact();
    }

}
Manager.Java
Java 2016
package demo3;

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.io.Serializable;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.Scanner;

public class Manager implements Serializable {

    Scanner s = new Scanner(System.in);

    // Scan and save file!
    public void addContact() {

        Name a = new Name("Tuan", "Duong");
        Name b = new Name("Hong", "Quan");
        Name c = new Name("Viena", "Slovakia");

        List<ContactBooks> list = new ArrayList<ContactBooks>();

        list.add(new ContactBooks(01722, "tuanduong@gmail.com", a));
        list.add(new ContactBooks(01243, "hongquan@yahoo.com.vn", b));
        list.add(new ContactBooks(01777, "slovakia@gmail.com", c));

        Collections.sort(list, new ContactComparator());

        try {

            FileOutputStream fos = new FileOutputStream("E:/java.txt");
            ObjectOutputStream oos = new ObjectOutputStream(fos);
            String y;
            do {
                System.out.print("firstName: ");
                String firstName = s.nextLine();
                System.out.print("lastName: ");
                String lastName = s.nextLine();
                System.out.print("Email: ");
                String email = s.nextLine();
                System.out.print("Phone: ");
                int phone = Integer.parseInt(s.nextLine());
                System.out.print("Tiep tuc [y/n]: ");
                y = s.nextLine();
                Name name = new Name(firstName, lastName);
                ContactBooks ctb = new ContactBooks(phone, email, name);
                list.add(ctb);
            } while (y.compareTo("y") == 0);

            oos.writeObject(list);
            oos.close();
            System.out.println("Save to file successful !");
        } catch (FileNotFoundException e1) {
            // TODO Auto-generated catch block
            e1.printStackTrace();
        } catch (IOException e1) {
            // TODO Auto-generated catch block
            e1.printStackTrace();
        }
    }

    //Read
    public void readContact() {
        List<ContactBooks> list = new ArrayList();
        try {
            FileInputStream fi = new FileInputStream("E:/java.txt");
            ObjectInputStream oi = new ObjectInputStream(fi);
            list = (List<ContactBooks>) oi.readObject();
            oi.close();
            fi.close();
            int i = 0;
            for (ContactBooks ctb : list) {
                System.out.println("=====================[" + i++ + "]");
                System.out.println("FullName: " + ctb.getName().getFirstName() + " " + ctb.getName().getLastName());
                System.out.println("Email:" + ctb.getEmail());
                System.out.println("Phone: " + ctb.getPhone());
            }
        } catch (Exception e) {
            // TODO: handle exception
        }

    }

}
ContactBooks.Java
Java 2016
package demo3;

import java.io.Serializable;

public class ContactBooks implements Serializable {

    private int phone;
    private String email;
    private Name name;

    public int getPhone() {
        return phone;
    }

    public void setPhone(int phone) {
        this.phone = phone;
    }

    public String getEmail() {
        return email;
    }

    public void setEmail(String email) {
        this.email = email;
    }

    public void setName(Name name) {
        this.name = name;
    }

    public Name getName() {
        return name;
    }

    public ContactBooks(int phone, String email, Name name) {
        super();
        this.phone = phone;
        this.email = email;
        this.name = name;
    }

    public String toString() {
        return "Name: " + name + " Email: " + email + " Phone: " + phone + "\n";
    }

}
Name.Java
Java 2016
package demo3;

import java.io.Serializable;

public class Name implements Serializable {

    private String firstName;
    private String lastName;

    public String getFirstName() {
        return firstName;
    }

    public void setFirstName(String firstName) {
        this.firstName = firstName;
    }

    public String getLastName() {
        return lastName;
    }

    public void setLastName(String lastName) {
        this.lastName = lastName;
    }

    public Name(String firstName, String lastName) {
        super();
        this.firstName = firstName;
        this.lastName = lastName;
    }

}
ContactComparator.Java
Java 2016
package demo3;

import java.util.Comparator;

public class ContactComparator implements Comparator<ContactBooks> {

    @Override
    public int compare(ContactBooks o1, ContactBooks o2) {
        // TODO Auto-generated method stub
        if (o1 == null || o1.getName() == null) {
            return -1;
        }
        if (o2 == null || o2.getName() == null) {
            return 1;
        }
        if (o1.getName().getLastName().compareTo(o2.getName().getLastName()) == 0) {
            if (o1 == null || o1.getName() == null) {
                return -1;
            }
            if (o2 == null || o2.getName() == null) {
                return 1;
            }
            return o1.getName().getFirstName().compareTo(o2.getName().getFirstName());
        }
        return o1.getName().getLastName().compareTo(o2.getName().getLastName());
    }

}
-- Remove Refer --
public void remove() {
//  List<ContactBooks> list = new ArrayList();
    try {
        FileOutputStream fo = new FileOutputStream("E:/java3.txt");
        ObjectOutputStream oos = new ObjectOutputStream(fo);
        do {

            System.out.print("Remove: ");
            int remove = Integer.parseInt(s.nextLine());
            list.remove(remove);
            System.out.println("Remove succsessful..");
            oos.writeObject(list);

        } while (false);
        oos.close();
        fo.close();
    } catch (Exception e) {
        // TODO: handle exception
    }

}

0 nhận xét:

Post a Comment

 

BACK TO TOP

Xuống cuối trang