mannager

 avatar
unknown
plain_text
2 years ago
4.8 kB
6
Indexable
/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package s000011;

import java.util.ArrayList;

/**
 *
 * @author MSI GTX
 */
public class Manager {

    public static ArrayList<Contact> lc = new ArrayList<>();
    Validation vd = new Validation();
    private int idDelete;
    int count = 0;
    String firstName;
    String lastName;
    String fullName;
    String[] fN;

    /**
     *Show Menu
     * 
     */
    public void menu() {
        System.out.println("CONTACT MANAGEMENT");
        System.out.println("1. Add a contact.");
        System.out.println("2. Display all contact.");
        System.out.println("3. Delete a contact.");
        System.out.println("4. Exit.");
        System.out.print("Enter your choice: ");
    }

    /**
     *Show choice
     * 
     */
    public void OpenMenu() {
        while (true) {
            menu();
            int choice = vd.checkInputIntLimit();
            switch (choice) {
                case 1:
                    Scanner();
                    break;
                case 2:
                    printAllContact();
                    break;
                case 3:
                    deleteContact();
                    break;
                case 4:
                    return;
            }
        }
    }

    /**
     *Enter the data and check if it is valid
     *
     */
    public void Scanner() {

        System.out.print("Enter id contact: ");
        int contactId = vd.checkInputInt();
        System.out.print("Enter first name: ");
        firstName = vd.checkInputWord();
        System.out.print("Enter last name: ");
        lastName = vd.checkInputWord();
        createFullName();
        System.out.print("Enter group: ");
        String group = vd.checkInputString();
        System.out.print("Enter address: ");
        String address = vd.checkInputString();
        System.out.print("Enter phone: ");
        String phone = vd.checkInputPhone();
        System.out.println("Add successful.");
        lc.add(new Contact(contactId, fullName, group, address,
                phone, firstName.trim().replaceAll("\\s+", " "), lastName.trim().replaceAll("\\s+", " ")));
        count++;

    }

    /**
     * create full name
     *
     */
    public void createFullName() {

        fullName = firstName.trim().concat(" " + lastName).replaceAll("\\s+", " ");

    }

    /**
     * Show list when added
     *
     */
    public void printAllContact() {
        System.out.println("+-----+-------------------------+--------------------+--------------------+--------------------+--------------------+--------------------+");
        System.out.printf("|%-5s|%-25s|%-20s|%-20s|%-20s|%-20s|%-20s|\n", "Id", "Name",
                "First name", "Last name", "Group", "Address", "Phone");
        System.out.println("+-----+-------------------------+--------------------+--------------------+--------------------+--------------------+--------------------+");
        //print infomation of contact from first to last list contact
        lc.forEach((contact) -> {
            System.out.printf("|%-5d|%-25s|%-20s|%-20s|%-20s|%-20s|%-20s|\n",
                    contact.getContactId(), contact.getFullName(),
                    contact.getFirstName(), contact.getLastName(),
                    contact.getGroup(), contact.getAddress(), contact.getPhone());
        });
        System.out.println("+-----+-------------------------+--------------------+--------------------+--------------------+--------------------+--------------------+");
    }

    /**
     * check if Id exist and delete them
     *
     */
    public void deleteContact() {
        if (count == 0) {
            System.out.println("You need to enter data before deletion!");
        } else {
            System.out.print("Enter id: ");
            idDelete = vd.checkInputInt();
            Contact contactDelete = getContactById();
            if (contactDelete == null) {
                System.out.println("Not found contact.");
            } else {
                System.out.println("Delete successful.");
                lc.remove(contactDelete);
                printAllContact();
            }
        }
    }

    /**
     *
     * get contact by id
     *
     * @return
     */
    public Contact getContactById() {
        for (Contact contact : lc) {
            if (contact.getContactId() == idDelete) {
                return contact;
            }
        }
        return null;
    }
}
Editor is loading...