/*
* 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 Id;
int count = 0;
String firstName;
String lastName;
String fullName;
int coutId;
/**
* 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: ");
inputId();
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(Id, fullName, group, address, phone, firstName, lastName));
count++;
}
public void inputId() {
do {
Id = vd.checkInputInt();
if (checkIdExit(Id)) {
System.out.println("loi");
System.out.print("Enter again");
}
} while (checkIdExit(Id));
}
/**
* create full name
*
*/
public void createFullName() {
fullName = firstName.trim().concat(" " + lastName).replaceAll("\\s+", " ");
}
/**
* Show list when added
*
*/
public void printAllContact() {
if (count > 0) {
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.getId(), contact.getFullName(),
contact.getFirstName(), contact.getLastName(),
contact.getGroup(), contact.getAddress(), contact.getPhone());
});
System.out.println("+-----+-------------------------+--------------------+--------------------+--------------------+--------------------+--------------------+");
} else {
System.out.println("You need to enter data!");
}
}
/**
* 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);
}
}
}
/**
*
* get contact by id
*
* @return
*/
public Contact getContactById() {
for (Contact contact : lc) {
if (contact.getId() == idDelete) {
return contact;
}
}
return null;
}
public boolean checkIdExit(int Id) {
for (int i = 0; i < lc.size(); i++) {
if (lc.get(i).getId() == Id) {
return true;
}
}
return false;
}
}