Skip to content

Commit 1d583e8

Browse files
committed
code
1 parent 48e1eb4 commit 1d583e8

14 files changed

Lines changed: 325 additions & 0 deletions

File tree

.idea/codeStyles

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

.idea/compiler.xml

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

.idea/misc.xml

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

.idea/workspace.xml

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

java-wishlist.iml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<module type="JAVA_MODULE" version="4" />

pom.xml

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<project xmlns="http://maven.apache.org/POM/4.0.0"
3+
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
4+
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
5+
<modelVersion>4.0.0</modelVersion>
6+
7+
<groupId>groupId</groupId>
8+
<artifactId>java-wishlist-part-1</artifactId>
9+
<version>1.0-SNAPSHOT</version>
10+
11+
<dependencies>
12+
<dependency>
13+
<groupId>org.hibernate</groupId>
14+
<artifactId>hibernate-core</artifactId>
15+
<version>5.4.2.Final</version>
16+
</dependency>
17+
<dependency>
18+
<groupId>org.postgresql</groupId>
19+
<artifactId>postgresql</artifactId>
20+
<version>42.2.5</version>
21+
</dependency>
22+
</dependencies>
23+
24+
25+
<properties>
26+
<java.version>11</java.version>
27+
<maven.compiler.source>${java.version}</maven.compiler.source>
28+
<maven.compiler.target>${java.version}</maven.compiler.target>
29+
</properties>
30+
31+
</project>

src/main/java/MainMenu.java

Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
1+
import java.io.IOException;
2+
import java.util.List;
3+
import java.util.Scanner;
4+
import javax.persistence.EntityManager;
5+
import javax.persistence.EntityManagerFactory;
6+
import javax.persistence.Persistence;
7+
8+
9+
public class MainMenu {
10+
private static Scanner input = new Scanner(System.in);
11+
private static EntityManagerFactory emf;
12+
private static EntityManager em;
13+
14+
public static void main(String[] args) throws IOException {
15+
menu();
16+
}
17+
18+
//Create a menu with options
19+
public static void menu(){
20+
emf = Persistence.createEntityManagerFactory("com.launchacademy.wishlist");
21+
em = emf.createEntityManager();
22+
String[] options = {". Add a new product", ". View wishlist", ". Exit"};
23+
while(true){
24+
for (int i = 0; i < options.length; i++){
25+
System.out.println((i + 1) + options[i]);
26+
}
27+
System.out.println("CHOOSE a number HUMAN");
28+
int choice = input.nextInt();
29+
if( choice == options.length){
30+
em.close();
31+
emf.close();
32+
System.exit(0);
33+
}
34+
else if (choice == 1){
35+
addProduct();
36+
}
37+
else if (choice == 2){
38+
listWishes();
39+
}
40+
}
41+
}
42+
43+
//Add a product
44+
private static void addProduct(){
45+
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());
53+
em.getTransaction().begin();
54+
em.persist(newProduct);
55+
em.getTransaction().commit();
56+
} finally {
57+
}
58+
}
59+
60+
//List all products in wishlist
61+
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());
65+
}
66+
}
67+
}

src/main/java/Product.java

Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
import javax.persistence.Column;
2+
import javax.persistence.Entity;
3+
import javax.persistence.GeneratedValue;
4+
import javax.persistence.GenerationType;
5+
import javax.persistence.Id;
6+
import javax.persistence.SequenceGenerator;
7+
import javax.persistence.Table;
8+
9+
@Entity
10+
@Table(name="products")
11+
public class Product {
12+
@Id
13+
@SequenceGenerator(name="product_generator", sequenceName = "products_id_seq", allocationSize = 1)
14+
@GeneratedValue(strategy = GenerationType.SEQUENCE, generator = "product_generator")
15+
@Column(name="id", nullable = false)
16+
private Long id;
17+
18+
@Column(name="name")
19+
private String name;
20+
21+
@Column(name="price")
22+
private float price;
23+
24+
@Column(name="url")
25+
private String url;
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 float getPrice() {
44+
return price;
45+
}
46+
47+
public void setPrice(float price) {
48+
this.price = price;
49+
}
50+
51+
public String getUrl() {
52+
return url;
53+
}
54+
55+
public void setUrl(String url) {
56+
this.url = url;
57+
}
58+
}
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
<persistence xmlns="http://xmlns.jcp.org/xml/ns/persistence"
2+
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" version="2.2"
3+
xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/persistence http://xmlns.jcp.org/xml/ns/persistence/persistence_2_2.xsd">
4+
<!-- Define persistence unit -->
5+
<persistence-unit name="com.launchacademy.wishlist">
6+
<provider>org.hibernate.jpa.HibernatePersistenceProvider</provider>
7+
<properties>
8+
<property name="javax.persistence.jdbc.url" value="jdbc:postgresql://localhost:5432/wishlist" />
9+
<property name="javax.persistence.jdbc.driver" value="org.postgresql.Driver" />
10+
<property name="javax.persistence.jdbc.user" value="postgres" />
11+
<property name="javax.persistence.jdbc.password" value="password" />
12+
</properties>
13+
</persistence-unit>
14+
</persistence>

src/main/resources/schema.sql

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
CREATE TABLE products(
2+
id SERIAL PRIMARY KEY,
3+
name VARCHAR(255) NOT NULL,
4+
price FLOAT,
5+
url VARCHAR(255) NOT NULL
6+
);

0 commit comments

Comments
 (0)