From ae5c699a6834edd640ab10bf7f0bc2128aad54bc Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?Eug=C3=AAnio=20Rocha=20Da=20Silva=20J=C3=BAnior?=
 <eugeniojunio99@gmail.com>
Date: Sun, 20 Aug 2023 02:53:17 +0000
Subject: [PATCH 1/2] Add Find and Insert Java

---
 dadosP.sql                                    |  7 +++
 .../com/Factories/ConnectionFactorie.java     | 15 ++++++
 src/main/java/com/example/App.java            | 19 ++++++-
 .../java/com/example/daos/ProductDao.java     | 52 +++++++++++++++++++
 src/main/java/com/example/models/Product.java | 25 +++++++++
 5 files changed, 117 insertions(+), 1 deletion(-)
 create mode 100644 dadosP.sql
 create mode 100644 src/main/java/com/Factories/ConnectionFactorie.java
 create mode 100644 src/main/java/com/example/daos/ProductDao.java
 create mode 100644 src/main/java/com/example/models/Product.java

diff --git a/dadosP.sql b/dadosP.sql
new file mode 100644
index 00000000..a9a80bb5
--- /dev/null
+++ b/dadosP.sql
@@ -0,0 +1,7 @@
+drop Table if EXISTS product;
+
+CREATE Table if NOT EXISTS product(
+    id SERIAL PRIMARY KEY NOT NULL UNIQUE,
+    name VARCHAR(45) NOT NULL,
+    price DECIMAL NOT NULL
+);
\ No newline at end of file
diff --git a/src/main/java/com/Factories/ConnectionFactorie.java b/src/main/java/com/Factories/ConnectionFactorie.java
new file mode 100644
index 00000000..854955dd
--- /dev/null
+++ b/src/main/java/com/Factories/ConnectionFactorie.java
@@ -0,0 +1,15 @@
+package com.Factories;
+
+import java.sql.Connection;
+import java.sql.DriverManager;
+import java.sql.SQLException;
+
+public class ConnectionFactorie {
+    private static final String PASSWORD = "";
+    private static final String USERNAME = "gitpod";
+    private static final String JDBC_URL = "jdbc:postgresql://localhost/postgres";
+    
+    public Connection connect() throws SQLException{
+        return DriverManager.getConnection(JDBC_URL, USERNAME, PASSWORD);
+    }
+}
diff --git a/src/main/java/com/example/App.java b/src/main/java/com/example/App.java
index 9407ea49..8736d50b 100644
--- a/src/main/java/com/example/App.java
+++ b/src/main/java/com/example/App.java
@@ -4,13 +4,30 @@
 import java.sql.DriverManager;
 import java.sql.SQLException;
 
+import com.example.daos.ProductDao;
+import com.example.models.Product;
+
 public class App {
     private static final String PASSWORD = "";
     private static final String USERNAME = "gitpod";
     private static final String JDBC_URL = "jdbc:postgresql://localhost/postgres";
 
     public static void main(String[] args) {
-        new App();
+        var product = new Product();
+        product.setName("Eugenio aprovado em Primeiro");
+        product.setPrice(15000);
+        try {
+            ProductDao dao = new ProductDao();
+            //dao.Create(product);
+            var products = dao.FindAll();
+            products.stream().filter(p -> p.getPrice() > 13000).forEach(item -> System.out.println(item.getName()));
+            //products.forEach(item -> System.out.println(item.getName()));
+
+        } catch (SQLException e) {
+            // TODO Auto-generated catch block
+            e.printStackTrace();
+        };
+
     }
 
     public App(){
diff --git a/src/main/java/com/example/daos/ProductDao.java b/src/main/java/com/example/daos/ProductDao.java
new file mode 100644
index 00000000..8241ac9b
--- /dev/null
+++ b/src/main/java/com/example/daos/ProductDao.java
@@ -0,0 +1,52 @@
+package com.example.daos;
+
+import java.sql.Connection;
+import java.sql.ResultSet;
+import java.sql.SQLException;
+import java.util.ArrayList;
+import java.util.List;
+
+import javax.xml.transform.Result;
+
+import com.Factories.ConnectionFactorie;
+import com.example.models.Product;
+
+public class ProductDao extends ConnectionFactorie{
+    private Connection connection;
+
+    public ProductDao() throws SQLException{
+        this.connection = connect();
+    }
+
+    public boolean Create(Product product) throws SQLException{
+        if(product == null)
+            return false;
+        String sql = "INSERT INTO product(name, price) VALUES (?, ?)";
+        var statement = connection.prepareStatement(sql);
+        try {
+            statement.setString(1, product.getName());
+            statement.setDouble(2, product.getPrice());
+            statement.execute();
+            statement.close();
+            return true;
+        } catch (Exception e) {
+            return false;
+        }
+    }
+
+    public List<Product> FindAll() throws SQLException{
+        String sql = "select * FROM product";
+        var statement = connect().prepareStatement(sql);
+        ResultSet result = statement.executeQuery();
+        var products = new ArrayList<Product>();
+        while(result.next()){
+            var product = new Product();
+            product.setId(result.getInt("id"));
+            product.setName(result.getString("name"));
+            product.setPrice(result.getDouble("price"));
+            products.add(product);
+        }
+        result.close();
+        return products;        
+    }
+}
diff --git a/src/main/java/com/example/models/Product.java b/src/main/java/com/example/models/Product.java
new file mode 100644
index 00000000..d57b55db
--- /dev/null
+++ b/src/main/java/com/example/models/Product.java
@@ -0,0 +1,25 @@
+package com.example.models;
+
+public class Product {
+    private int id;
+    private String name;
+    private double price;
+    public int getId() {
+        return id;
+    }
+    public void setId(int id) {
+        this.id = id;
+    }
+    public String getName() {
+        return name;
+    }
+    public void setName(String name) {
+        this.name = name;
+    }
+    public double getPrice() {
+        return price;
+    }
+    public void setPrice(double price) {
+        this.price = price;
+    } 
+}

From 142c691588a8413ff1ddb4c8bc0e034d7e88e40c Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?Eug=C3=AAnio=20Rocha=20Da=20Silva=20J=C3=BAnior?=
 <eugeniojunio99@gmail.com>
Date: Thu, 24 Aug 2023 00:19:46 +0000
Subject: [PATCH 2/2] Add OrderMethods

---
 src/main/java/com/example/App.java | 67 +++++++++++++++++++++++++++++-
 1 file changed, 66 insertions(+), 1 deletion(-)

diff --git a/src/main/java/com/example/App.java b/src/main/java/com/example/App.java
index 8736d50b..a840363d 100644
--- a/src/main/java/com/example/App.java
+++ b/src/main/java/com/example/App.java
@@ -1,8 +1,13 @@
 package com.example;
 
+import java.lang.reflect.Array;
 import java.sql.Connection;
 import java.sql.DriverManager;
 import java.sql.SQLException;
+import java.util.ArrayList;
+import java.util.Arrays;
+import java.util.Dictionary;
+import java.util.List;
 
 import com.example.daos.ProductDao;
 import com.example.models.Product;
@@ -13,7 +18,8 @@ public class App {
     private static final String JDBC_URL = "jdbc:postgresql://localhost/postgres";
 
     public static void main(String[] args) {
-        var product = new Product();
+        /*
+         var product = new Product();
         product.setName("Eugenio aprovado em Primeiro");
         product.setPrice(15000);
         try {
@@ -27,9 +33,68 @@ public static void main(String[] args) {
             // TODO Auto-generated catch block
             e.printStackTrace();
         };
+         */
+        int[] array = {10, 20, 50, 1, 5, 4, 6};
+        //buble_sort(array);
+        selection_sort(array);
+        System.out.print(Arrays.toString(array));
 
+        System.out.println(searchBinary(array, 90));
+        
     }
 
+    public static int searchBinary(int[] array, int target){
+        if(array.length <= 0) return -1;
+        int left = 0;
+        int right =array.length -1;
+        while(left <= right){
+            int mid = (left + right) / 2;
+            if(array[mid] == target){
+                return mid;
+            } else if(target > array[mid]){
+                left = mid + 1;
+            } else {
+                right = mid - 1;
+            }
+        }
+        return -1;
+
+    }
+
+    public static void selection_sort(int[] array){
+        if(array.length <= 0) return;
+        for(int i =0; i< array.length - 1; i++){
+            var minValueIndice = i;
+            for(int j=i+1; j < array.length; j++){
+                if(array[minValueIndice] > array[j]){
+                    minValueIndice = j;
+                }
+            }
+            var temp = array[i];
+            array[i] = array[minValueIndice];
+            array[minValueIndice] = temp;
+            System.out.println(Arrays.toString(array));
+        }
+    }
+    public static void buble_sort(int[] array){
+        if(array.length <= 0) return;
+        for (int i = 0 ; i < array.length -1 ; i++) {
+            boolean swap = false;
+            for (int j = 0; j < array.length - i - 1; j++){
+                if(array[j] > array[j+1]){
+                    int temp = array[j+1];
+                    array[j+1] = array[j];
+                    array[j] = temp;
+                    swap = true;
+                }
+                System.out.println(Arrays.toString(array));
+            }
+            if(!swap){
+                break;
+            }
+        } 
+        System.out.print(Arrays.toString(array));
+    }
     public App(){
         try(var conn = getConnection()){
             carregarDriverJDBC();
