package sklep.przyklady;

import java.math.BigDecimal;
import java.util.List;
import java.util.Scanner;

import javax.persistence.EntityManager;
import javax.persistence.EntityManagerFactory;
import javax.persistence.Persistence;
import javax.persistence.TypedQuery;

import sklep.model.Product;

public class Parametry {

	public static void main(String[] args) {
		EntityManagerFactory emf = null;
		EntityManager em = null;
		try {
			Scanner sc = new Scanner(System.in);
			emf = Persistence.createEntityManagerFactory("sklep");
			em = emf.createEntityManager();

			System.out.print("Podaj  minimalną cenę: ");
			BigDecimal min = sc.nextBigDecimal();
			
			System.out.print("Podaj maksymalną cenę: ");
			BigDecimal max = sc.nextBigDecimal();
			
			TypedQuery<Product> query = em.createQuery(
					"SELECT p FROM Product p WHERE p.price BETWEEN :minprice AND :maxprice",
					Product.class);
			query.setParameter("minprice", min);
			query.setParameter("maxprice", max);
			List<Product> products = query.getResultList();
			System.out.println("Odczytano " + products.size() + " rekordów:");
			for (Product product : products) {
				System.out.println(" * " + product.getProductName() + " za " + product.getPrice());
			}
		} finally {
			if(em != null)
				em.close();
			if(emf != null)
				emf.close();
		}
	}

}
