• Decoded World

    The Mysteries of Humanity: Leonardo da Vinci

  • Decoded World

    The Unsung Hero: Nikola Tesla

  • Decoded World

    The Wizard of Light: Thomas Edison

  • Decoded World

    Adolf Hitler: The Master Manipulator

Showing posts with label ADF2. Show all posts
Showing posts with label ADF2. Show all posts

22 April 2016

TEST ADF2 Aptech FPT 2016

[TEST ADF2 Aptech FPT ]

Example >>  http://giai-ma.blogspot.com/2016/04/scanner-arraylist-sort-save-file-read.html
Option.Java
Java Advanced 2017
package com.fsoft;

import java.util.ArrayList;

public interface Option {

    public void addContact(ArrayList list);
    public void findTell(ArrayList list);
    public void display(ArrayList list);
}
Main.Java
Java Advanced 2017
package com.fsoft;

import java.util.ArrayList;
import java.util.Scanner;

import com.fsoft.model.Contact;

public class Main implements Option {

    Scanner s = new Scanner(System.in);

    // ==========NEW ARRAYLIST
    ArrayList<Contact> listContact = new ArrayList();
    LogicOption logicOption = new LogicOption();

    public static void main(String[] args) {
        Main m = new Main();
        m.start();
    }

    public void start() {
        while (true) {
            System.out.println("1. Add new contact");
            System.out.println("2. Find a contact by name");
            System.out.println("3. Display contacts");
            System.out.println("4. Exit");
            try {

                int num = Integer.parseInt(s.nextLine());
                if (num == 0 || num == 1 || num == 2 || num == 3 || num == 4) {

                    switch (num) {

                        case 1:
                            System.out.println("#Chose 1");
                            addContact(listContact);
                            break;
                        case 2:
                            System.out.println("#Chose 2");
                            findTell(listContact);
                            break;
                        case 3:
                            System.out.println("#Chose 3");
                            display(listContact);
                            break;
                        case 4:
                            System.out.println("Good Bye! Stop Application");
                            System.exit(0);
                            break;
                        default:
                            break;

                    }
                } else {
                    System.out.println("Ban da chon sai vui long chon lai");
                }
            } catch (Exception e) {
                System.out.println("Vui long chon lai");
            }

        }
    }

    @Override
    public void addContact(ArrayList list) {
        // TODO Auto-generated method stub
        logicOption.addContact(list);

    }

    @Override
    public void findTell(ArrayList list) {
        // TODO Auto-generated method stub
        System.out.print("Finding by name: ");
        String phone = s.nextLine();
        logicOption.findContact(list, phone);

    }

    @Override
    public void display(ArrayList list) {
        // TODO Auto-generated method stub
        logicOption.displayContact(list);
    }

}
Contact.Java
Java Advanced 2017
package com.fsoft.model;

public class Contact {

    private String name;
    private String Company;
    private String Email;
    private String Phone;

    public String getName() {
        return name;
    }

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

    public String getCompany() {
        return Company;
    }

    public void setCompany(String company) {
        Company = company;
    }

    public String getEmail() {
        return Email;
    }

    public void setEmail(String email) {
        Email = email;
    }

    public String getPhone() {
        return Phone;
    }

    public void setPhone(String phone) {
        Phone = phone;
    }

    public Contact(String name, String company, String email, String phone) {
        super();
        this.name = name;
        Company = company;
        Email = email;
        Phone = phone;
    }

    public Contact() {
        super();
        // TODO Auto-generated constructor stub
    }

}
LogicOption.Java
Java Advanced 2017
package com.fsoft;

import java.util.ArrayList;
import java.util.Scanner;

import com.fsoft.model.Contact;

public class LogicOption {

    Scanner s = new Scanner(System.in);

    public ArrayList addContact(ArrayList<Contact> list) {

        String answer = "";
        do {
            System.out.print("Enter your name: ");
            String name = s.nextLine();
            System.out.print("Enter your company: ");
            String company = s.nextLine();
            System.out.print("Enter your email: ");
            String email = s.nextLine();
            System.out.print("Enter your phone: ");
            String phone = s.nextLine();
            System.out.print("Chose Yes/No back home: ");
            answer = s.nextLine();
            list.add(new Contact(name, company, email, phone));
        } while (answer.equalsIgnoreCase("No"));
        return list;
    }

    public void findContact(ArrayList<Contact> list, String name) {
        System.out.println("Name || Company || Email  ||  Phone");
        for (int i = 0; i < list.size(); i++) {
            if (name.equalsIgnoreCase(list.get(i).getName())) {
                System.out.println(list.get(i).getName() + "  " + list.get(i).getCompany() + "  " + list.get(i).getEmail() + "  " + list.get(i).getPhone() + "\n");
                break;
            }
        }
        System.out.println();
    }

    public void displayContact(ArrayList<Contact> list) {
        System.out.println("Name || Company || Email  ||  Phone");
        for (Contact c : list) {
            System.out.println(c.getName() + "  " + c.getCompany() + "  " + c.getEmail() + "  " + c.getPhone());
        }
        System.out.println();
    }

}

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
    }

}

04 April 2016

ADF2 ArrayList Serializable java...2

Example: ArrayList , Scanner, Serializable, Exception , Loop java



Student Management
-Comparator
-ArrayList
-Scanner
-Serializable
-File

MainPage.xaml
Windows Store 2017

package javaapplication2;

import java.io.FileInputStream;
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.Scanner;
import java.util.logging.Level;
import java.util.logging.Logger;

public class Main {

    /**
     * @param args the command line arguments
     */
    public static void main(String[] args) {
        // TODO code application logic here
        Scanner sc = new Scanner(System.in);
        StudentConsole cs = new StudentConsole();
        int choice = 0;
        do {
            cs.Menu();
            System.out.println("Enter Your Choice :");
            choice = sc.nextInt();
            sc.nextLine();
            switch (choice) {
                case 1:
                    cs.AddStudent();
                    break;
                case 2:
                    cs.Modify();
                    break;
                case 3:
                    cs.Display();
                    break;
                case 4:
                    System.exit(0);
                default:
                    System.out.println("Choice from 1 - 4");
            }
        } while (choice != 5);
    }

}

class Student implements Serializable {

    private String id;
    private String name;
    private String address;
    private String phone;

    public Student() {
    }

    public Student(String id, String name, String address, String phone) {
        this.id = id;
        this.name = name;
        this.address = address;
        this.phone = phone;
    }

    public String getAddress() {
        return address;
    }

    public void setAddress(String address) {
        this.address = address;
    }

    public String getId() {
        return id;
    }

    public void setId(String id) {
        this.id = id;
    }

    public String getName() {
        return name;
    }

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

    public String getPhone() {
        return phone;
    }

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

    @Override
    public String toString() {
        return id + "" + name + "" + address + "" + phone;
    }

}

class StudentConsole {

    Student std = null;
    StudentList test = null;
    Scanner sc = null;

    public StudentConsole() {
        std = new Student();
        test = new StudentList();
        sc = new Scanner(System.in);
    }
    //add student

    public void AddStudent() {
        String answer = "";
        do {
            System.out.println("Enter Student ID :");
            String id = sc.nextLine();
            std = test.FindByID(id);
            if (std != null) {
                System.out.println("Student ID Exist !");
            } else {
                System.out.println("Enter Student Name :");
                String name = sc.nextLine();
                System.out.println("Enter Student Address :");
                String address = sc.nextLine();
                System.out.println("Enter Student Phone :");
                String phone = sc.nextLine();
                std = new Student(id, name, address, phone);
                test.Add(std);
                test.Save();
                System.out.println("Continue [Y/N] ?");
                answer = sc.nextLine();
            }
        } while (answer.equalsIgnoreCase("y"));
    }

    //Modify Student
    public void Modify() {
        System.out.println("Modification is not allowed ");
    }

    //Display
    public void Display() {
        test.Load();
        Student[] arr = test.showAll();
        System.out.printf("%-15s\t%-15s\t%-15s\t%-15s\n", "ID", "Name", "Address", "Phone");
        for (int i = 0; i < arr.length; i++) {
            System.out.printf("%-15s\t%-15s\t%-15s\t%-15s\n", arr[i].getId(), arr[i].getName(), arr[i].getAddress(), arr[i].getPhone());
        }
    }

    //menu
    public void Menu() {
        System.out.println("------------------");
        System.out.println("1. Add Student ");
        System.out.println("2. Modify Student ");
        System.out.println("3. Display Student Record ");
        System.out.println("4.Exit");
        System.out.println("------------------");
    }
}

class StudentList {

    ArrayList<Student> list = null;
    Student std = null;

    public StudentList() {
        list = new ArrayList<Student>();
        std = new Student();
    }
    //add Student

    public void Add(Student st) {
        if (st != null) {
            list.add(st);
        }
    }
    //find by ID

    public Student FindByID(String id) {
        if (id != null) {
            for (int i = 0; i < list.size(); i++) {
                if (id.equalsIgnoreCase(list.get(i).getId())) {
                    return list.get(i);
                }
            }
        }
        return null;
    }
    //show all

    public Student[] showAll() {
        return list.toArray(new Student[list.size()]);
    }
    //save

    public void Save() {
        String filename = "student.dat";
        try {
            ObjectOutputStream out = new ObjectOutputStream(new FileOutputStream(filename));
            int count = list.size();
            out.writeInt(count);
            out.writeObject(list);
            out.flush();
            out.close();
        } catch (IOException ex) {
            Logger.getLogger(StudentList.class.getName()).log(Level.SEVERE, null, ex);
        }

    }
    //load

    public void Load() {
        ObjectInputStream ois = null;
        try {
            String filename = "student.dat";
            ois = new ObjectInputStream(new FileInputStream(filename));
            int count = list.size();
            count = ois.readInt();
            try {
                list = (ArrayList<Student>) ois.readObject();
            } catch (ClassNotFoundException ex) {
                Logger.getLogger(StudentList.class.getName()).log(Level.SEVERE, null, ex);
            }
        } catch (IOException ex) {
            Logger.getLogger(StudentList.class.getName()).log(Level.SEVERE, null, ex);
        } finally {
            try {
                ois.close();
            } catch (IOException ex) {
                Logger.getLogger(StudentList.class.getName()).log(Level.SEVERE, null, ex);
            }
        }
    }
}

 

BACK TO TOP

Xuống cuối trang