package com.technoelevate.phone.
dao;
import java.util.List;
import java.util.Scanner;
import javax.persistence.EntityManager;
import javax.persistence.EntityManagerFactory;
import javax.persistence.EntityTransaction;
import javax.persistence.Persistence;
import javax.persistence.Query;
import com.technoelevate.dto.Calling.Calling;
public class Call {
static EntityManagerFactory entityManagerFactory = null;
static EntityManager entityManager = null;
static EntityTransaction entityTransaction = null;
static Scanner sc = new Scanner(System.in);
public static void operation() {
entityManagerFactory =
Persistence.createEntityManagerFactory("phone12");
entityManager = entityManagerFactory.createEntityManager();
entityTransaction = entityManager.getTransaction();
public static void menu() {
System.out.println("Welcome to Developers contact list :))");
System.out.println("Select an option \n1.Show All Contacts \n2.Search
Contact \n3.Operate contact");
int input = sc.nextInt();
switch (input) {
case 1:
showContacts();
break;
case 2 :
searchContact();
break;
case 3 :
operate();
break;
}
public static void showContacts() {
operation();
String query = "from Calling";
Query createQuery = entityManager.createQuery(query);
List resultList = createQuery.getResultList();
for (Object object : resultList) {
System.out.println(object);
}
public static void searchContact() {
operation();
System.out.println("Enter the contact name");
sc.nextLine();
String snm = sc.nextLine();
String query = "Select name from Calling";
Query createQuery = entityManager.createQuery(query);
List resultList = createQuery.getResultList();
int no = 0;
for (Object object : resultList) {
if(((String) object).equalsIgnoreCase(snm)) {
System.out.println("Playing current song = "+ snm);
no++;
}
}
if(no == 1) {
System.out.println("Choose any option \n1.Call Contact 2.Text
Contact");
int cap = sc.nextInt();
switch (cap) {
case 1:
callContact(snm , createQuery);
break;
case 2:
textContact(snm , createQuery);
break;
default:
System.out.println("Invalid Input");
break;
}
}
if(no !=1) {
System.out.println("Contact is not available");
}
greeting();
}
public static void callContact(String snm , Query createQuery) {
operation();
String query = "Select phn_no from Calling where name='"+ snm+"'";
Query createQuery2 = entityManager.createQuery(query);
Object singleResult = createQuery2.getSingleResult();
System.out.println("Calling...."+ snm + "\n" + singleResult);
public static void textContact(String snm , Query createQuery) {
operation();
String query = "Select name from Calling where name='"+ snm+"'";
Query createQuery3 = entityManager.createQuery(query);
Object singleResult = createQuery3.getSingleResult();
System.out.println("Enter Message");
String msg = sc.next();
System.out.println("Sending Message to .......>"+snm + "\nsent
successfully" );
public static void operate() {
System.out.println("Please Select the Options: \n1.Add Contact \
n2.Delete Contact");
int in = sc.nextInt();
if(in==1) {
addContact();
} else if(in==2) {
deleteContact();
}
public static void addContact() {
int executeUpdate;
operation();
entityTransaction.begin();
System.out.println("Enter phone no");
long phoneno = sc.nextLong();
System.out.println("Enter Name");
String namee = sc.next();
System.out.println("Enter Id");
int id = sc.nextInt();
System.out.println("Enter Email");
String em = sc.next();
Calling cap =new Calling(phoneno, namee, id, em);
entityManager.persist(cap);
entityTransaction.commit();
System.out.println("Added Successfully.....:))");
greeting();
}
public static void deleteContact() {
int executeUpdate;
operation();
entityTransaction.begin();
System.out.println("Enter the name to delete");
String sn = sc.next();
String query = "delete from Calling where name='"+ sn+"'";
Query createQuery = entityManager.createQuery(query);
executeUpdate = createQuery.executeUpdate();
if(executeUpdate != 0) {
System.out.println(executeUpdate+"Row affected");
}
System.out.println("Contact Deleted Successfully ...:))");
entityTransaction.commit();
greeting();
}
public static void greeting() {
System.out.println("Please Select the Options: \n1.Main menu \
n2.exit");
int in = sc.nextInt();
if(in==1) {
menu();
}else if (in==2) {
System.out.println("******************************\nThank you ,I hope
you enjoyed :))\n******************************");
System.exit(0);
}
public static void main(String[] args) {
menu();
}