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();
    }

}

0 nhận xét:

Post a Comment

 

BACK TO TOP

Xuống cuối trang