Skip to content

Commit 740a746

Browse files
committed
code
1 parent c1f8e13 commit 740a746

16 files changed

Lines changed: 300 additions & 58 deletions

.idea/workspace.xml

Lines changed: 44 additions & 28 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

pom.xml

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,4 +28,19 @@
2828
<maven.compiler.target>${java.version}</maven.compiler.target>
2929
</properties>
3030

31+
<build>
32+
<plugins>
33+
<plugin>
34+
<groupId>org.flywaydb</groupId>
35+
<artifactId>flyway-maven-plugin</artifactId>
36+
<version>5.2.4</version>
37+
<configuration>
38+
<url>jdbc:postgresql://localhost:5432/wishlist</url>
39+
<user>postgres</user>
40+
<password>password</password>
41+
</configuration>
42+
</plugin>
43+
</plugins>
44+
</build>
45+
3146
</project>

src/main/java/Category.java

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
import java.util.ArrayList;
2+
import java.util.List;
3+
import javax.persistence.Column;
4+
import javax.persistence.Entity;
5+
import javax.persistence.GeneratedValue;
6+
import javax.persistence.GenerationType;
7+
import javax.persistence.Id;
8+
import javax.persistence.OneToMany;
9+
import javax.persistence.SequenceGenerator;
10+
import javax.persistence.Table;
11+
12+
@Entity
13+
@Table(name = "categories")
14+
public class Category {
15+
@Id
16+
@SequenceGenerator(name = "category_generator", sequenceName = "categories_id_seq", allocationSize = 1)
17+
@GeneratedValue(strategy = GenerationType.SEQUENCE, generator = "category_generator")
18+
@Column(name = "id", nullable = false, unique = true)
19+
private Long id;
20+
21+
@Column(name = "name", nullable = false, unique = true)
22+
private String name;
23+
24+
@OneToMany(mappedBy = "category")
25+
private List<Product> products = new ArrayList<>();
26+
27+
public Long getId() {
28+
return id;
29+
}
30+
31+
public void setId(Long id) {
32+
this.id = id;
33+
}
34+
35+
public String getName() {
36+
return name;
37+
}
38+
39+
public void setName(String name) {
40+
this.name = name;
41+
}
42+
43+
public List<Product> getProducts() {
44+
return products;
45+
}
46+
47+
public void setProducts(List<Product> products) {
48+
this.products = products;
49+
}
50+
}

src/main/java/MainMenu.java

Lines changed: 70 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -4,29 +4,31 @@
44
import javax.persistence.EntityManager;
55
import javax.persistence.EntityManagerFactory;
66
import javax.persistence.Persistence;
7+
import javax.persistence.TypedQuery;
78

89

910
public 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

Comments
 (0)