44import javax .persistence .EntityManager ;
55import javax .persistence .EntityManagerFactory ;
66import javax .persistence .Persistence ;
7+ import javax .persistence .TypedQuery ;
78
89
910public class MainMenu {
11+
1012 private static Scanner input = new Scanner (System .in );
1113 private static EntityManagerFactory emf ;
1214 private static EntityManager em ;
1315
1416 public static void main (String [] args ) throws IOException {
1517 menu ();
1618 }
17-
1819 //Create a menu with options
1920 public static void menu (){
2021 emf = Persistence .createEntityManagerFactory ("com.launchacademy.wishlist" );
2122 em = emf .createEntityManager ();
22- String [] options = {". Add a new product " , ". View wishlist" , ". Exit" };
23+ String [] options = {". Add a new item " , ". View wishlist" , ". Exit" };
2324 while (true ){
24- for (int i = 0 ; i < options .length ; i ++){
25+ for (int i = 0 ; i < options .length ; i ++){
2526 System .out .println ((i + 1 ) + options [i ]);
2627 }
27- System .out .println ("CHOOSE a number HUMAN" );
28+ System .out .println ("CHOOSE A NUMBER HUMAN! " );
2829 int choice = input .nextInt ();
29- if ( choice == options .length ){
30+ if (choice == options .length ){
31+ System .out .println ("Goodbye" );
3032 em .close ();
3133 emf .close ();
3234 System .exit (0 );
@@ -40,28 +42,78 @@ else if (choice == 2){
4042 }
4143 }
4244
45+
4346 //Add a product
44- private static void addProduct (){
47+ public static void addProduct () {
48+ String name = "" ;
49+ String price = "" ;
50+ String url = "" ;
51+ String category = "" ;
52+
53+ System .out .println ("Name of product" );
54+ Scanner userInputName = new Scanner (System .in );
55+ name = userInputName .nextLine ();
56+
57+ System .out .println ("Price of product" );
58+ Scanner userInputPrice = new Scanner (System .in );
59+ price = userInputPrice .nextLine ();
60+ Float floatPrice = Float .parseFloat (price );
61+
62+ System .out .println ("Url to product" );
63+ Scanner userInputUrl = new Scanner (System .in );
64+ url = userInputUrl .nextLine ();
65+
66+ System .out .println ("Would you like to add a category? (type 'none' for no" );
67+ Scanner inputCategory = new Scanner (System .in );
68+ category = inputCategory .nextLine ();
69+
70+ Product product = new Product ();
71+ Category newCategory = new Category ();
72+
73+ TypedQuery <Category > categoryQuery = em .createQuery ("SELECT c FROM Category c" , Category .class );
74+ List <Category > categories = categoryQuery .getResultList ();
75+
4576 try {
46- Product newProduct = new Product ();
47- System .out .println ("What product would you like to add?" );
48- newProduct .setName (input .next ());
49- System .out .println ("What is the product's price?" );
50- newProduct .setPrice (input .nextFloat ());
51- System .out .println ("What is the product's url?" );
52- newProduct .setUrl (input .next ());
77+
78+ newCategory .setName (category );
79+ em .getTransaction ().begin ();
80+ em .persist (newCategory );
81+ em .getTransaction ().commit ();
82+
83+ em .getTransaction ().begin ();
84+ product .setName (name );
85+ product .setPrice (floatPrice );
86+ product .setUrl (url );
87+ product .setCategory (newCategory );
88+
89+ em .persist (product );
90+ em .getTransaction ().commit ();
91+
92+ } catch (Exception ex ){
93+ System .out .println ("Duplicate Category Name Error" );
5394 em .getTransaction ().begin ();
54- em .persist (newProduct );
95+ product .setName (name );
96+ product .setPrice (floatPrice );
97+ product .setUrl (url );
98+ for (Category eachCategory : categories ){
99+ if (category .equals (eachCategory .getName ())){
100+ product .setCategory (eachCategory );
101+ }
102+ }
103+ em .persist (product );
55104 em .getTransaction ().commit ();
56- } finally {
57105 }
58106 }
59107
60108 //List all products in wishlist
61109 private static void listWishes (){
62- List <Product > wishes = em .createQuery ("SELECT p FROM Product p" , Product .class ).getResultList ();
63- for (Product product : wishes ){
64- System .out .println (product .getName () + " " + product .getPrice () + " " + product .getUrl ());
110+ TypedQuery <Product > query = em .createQuery ("SELECT p FROM Product p" , Product .class );
111+ List <Product > products = query .getResultList ();
112+
113+ for (Product product : products ){
114+ Category eachCategory = product .getCategory ();
115+ System .out .println ("Product: " + product .getName () + "\n " + "Price: " + product .getPrice ()
116+ + "\n " + "Url: " + product .getUrl () + "\n " + " Category: " + eachCategory .getName () + "\n " );
65117 }
66118 }
67119}
0 commit comments