diff --git a/.idea/workspace.xml b/.idea/workspace.xml
index e2051bb..9b80b7c 100644
--- a/.idea/workspace.xml
+++ b/.idea/workspace.xml
@@ -4,8 +4,22 @@
-
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
@@ -18,8 +32,8 @@
-
+
@@ -60,6 +74,12 @@
+
+
+
+
+
+
@@ -67,7 +87,7 @@
-
+
@@ -96,7 +116,7 @@
-
+
1669371969480
@@ -147,7 +167,14 @@
1669476308821
-
+
+ 1669481268777
+
+
+
+ 1669481268777
+
+
@@ -172,6 +199,33 @@
-
+
+
+
+
+
+
+
+ file://$PROJECT_DIR$/src/main/java/com/jorpelu/ServicioDeProductos/dao/Parser.java
+ 16
+
+
+
+ file://$PROJECT_DIR$/src/main/java/com/jorpelu/ServicioDeProductos/dao/Parser.java
+ 19
+
+
+
+ file://$PROJECT_DIR$/src/main/java/com/jorpelu/ServicioDeProductos/dao/Parser.java
+ 20
+
+
+
+
+
+
+
+
+
\ No newline at end of file
diff --git a/README.md b/README.md
index 9e7d23f..e940303 100644
--- a/README.md
+++ b/README.md
@@ -1,44 +1,68 @@
# Servicio de productos
### Introduccion
-Creo un SpringCore Con el servicio de consultar productos con sus precios y todas las caracteristicas.
+Creo un SpringCore Con el servicio de consultar productos
+con sus precios y todas las caracteristicas.
### Documentacion
### Files
+- *Parser* Clase que se encarga de leeer el archivo CSV donde estan guardados los datos
+- *productos_data.csv* Archivo de datos
- *Main.java* Clase Principal del programa
-- *IServicioProducto.java* Interfaz de producto
+- *ProductoDao.java* Interfaz de productos
+- *ProductosDaoImplMemory.java* implementa la memoria de la iinterfaz
- *Producto.java* Modelo del producto
-- *ServicioProducto.java* Clase para el control de los servicios del producto (ampliable en un futuro)
-- *beans.xml* Arvhivo que contiene los bean para la inyeccion de dependencias
-#### Dependencias importadas:
+- *ProductoService.java* Clase para el control de los servicios del producto
+- *beans.xml* Arvhivo que contiene los bean para la inyeccion de dependencias _(Ya no se Utiliza)_
+- *AppConfig.java* Para leer las anotaciones y configurar la app
+### Dependencias importadas:
~~~
org.springframework id:spring-context v: 6.0.0
+javax.annotation id:javax.annotation-api v:1.3.2
+org.springframework id:spring-core v:6.0.0
~~~
### Salida por pantalla
~~~
+id=0
+ nombre='Nevera'
+ descripcion='Producto para enfriar '
+ cantidad=200
+ precio=259.99}
id=1
- nombre='pr1'
- descripcion='ds1'
- cantidad=100
- precio=3.2}
+ nombre='Lampara'
+ descripcion='Ilumina tu cuarto'
+ cantidad=2500
+ precio=25.3}
id=2
- nombre='pr2'
- descripcion='ds2'
- cantidad=200
- precio=3.3}
+ nombre='Ordenador MSI'
+ descripcion='Portatil de Ultima generacion'
+ cantidad=100
+ precio=1199.99}
id=3
- nombre='pr3'
- descripcion='ds3'
- cantidad=300
- precio=3.4}
+ nombre='Pantalla Led'
+ descripcion='Mejor Imposible'
+ cantidad=250
+ precio=399.99}
id=4
- nombre='pr4'
- descripcion='ds4'
- cantidad=400
- precio=3.5}
+ nombre='Ratón'
+ descripcion='El mejor periférico'
+ cantidad=1500
+ precio=25.99}
+id=5
+ nombre='Teclado'
+ descripcion='La nueva tecnologia'
+ cantidad=200
+ precio=25.99}
+id=6
+ nombre='Regleta de luz'
+ descripcion='Solo eso, una regleta'
+ cantidad=1500
+ precio=9.99}
+id=7
+ nombre='Cargador 65W'
+ descripcion='MaximaVelocidad'
+ cantidad=500
+ precio=99.99}
-~~~
-### Explicación de la Actividad
+Process finished with exit code 0
-Por ahora, Solo crea y genera un producto a traves de un BEAN, donde se
-inicializa un BEAN con el producto y lo utiliza.
-En el main hemos llamado ahora al producto a traves de la interfaz.
+~~~
diff --git a/pom.xml b/pom.xml
index 3a899e0..ef20c4e 100644
--- a/pom.xml
+++ b/pom.xml
@@ -20,6 +20,11 @@
spring-context
6.0.0
+
+ javax.annotation
+ javax.annotation-api
+ 1.3.2
+
org.springframework
spring-core
diff --git a/src/main/java/com/jorpelu/ServicioDeProductos/ISercicioProductoDAO.java b/src/main/java/com/jorpelu/ServicioDeProductos/ISercicioProductoDAO.java
deleted file mode 100644
index 702e9d9..0000000
--- a/src/main/java/com/jorpelu/ServicioDeProductos/ISercicioProductoDAO.java
+++ /dev/null
@@ -1,12 +0,0 @@
-package com.jorpelu.ServicioDeProductos;
-
-import java.util.Collection;
-
-public interface ISercicioProductoDAO {
- public Producto findByIndex(int index);
- public Collection findAll();
- public void insert(Producto producto);
- public void edit(int index, Producto producto);
- public void delete(int index);
- public void delete(Producto producto);
-}
diff --git a/src/main/java/com/jorpelu/ServicioDeProductos/Main.java b/src/main/java/com/jorpelu/ServicioDeProductos/Main.java
index bbdf972..8a35e92 100644
--- a/src/main/java/com/jorpelu/ServicioDeProductos/Main.java
+++ b/src/main/java/com/jorpelu/ServicioDeProductos/Main.java
@@ -1,16 +1,16 @@
package com.jorpelu.ServicioDeProductos;
+import com.jorpelu.ServicioDeProductos.config.AppConfig;
+import com.jorpelu.ServicioDeProductos.dao.ProductoService;
import org.springframework.context.ApplicationContext;
-import org.springframework.context.support.ClassPathXmlApplicationContext;
+import org.springframework.context.annotation.AnnotationConfigApplicationContext;
public class Main {
public static void main(String[] args) {
- ApplicationContext appContext = new ClassPathXmlApplicationContext("beans.xml");
-// ServicioProductos servicioProductos = (ServicioProductos) appContext.getBean("productosService", ServicioProductos.class);
-
+ ApplicationContext appContext = new AnnotationConfigApplicationContext(AppConfig.class);
ProductoService servicioProductos = appContext.getBean(ProductoService.class);
servicioProductos.productoList().forEach(System.out::println);
- ((ClassPathXmlApplicationContext) appContext).close();
+ ((AnnotationConfigApplicationContext) appContext).close();
}
}
\ No newline at end of file
diff --git a/src/main/java/com/jorpelu/ServicioDeProductos/ProductoService.java b/src/main/java/com/jorpelu/ServicioDeProductos/ProductoService.java
deleted file mode 100644
index 4982da4..0000000
--- a/src/main/java/com/jorpelu/ServicioDeProductos/ProductoService.java
+++ /dev/null
@@ -1,22 +0,0 @@
-package com.jorpelu.ServicioDeProductos;
-
-import org.springframework.beans.factory.annotation.Autowired;
-import org.springframework.stereotype.Component;
-
-import java.awt.*;
-import java.util.Collection;
-
-@Component
-public class ProductoService {
- @Autowired
- private ISercicioProductoDAO productoDAO;
-
-
- public void setProductoDAO(ISercicioProductoDAO productoDAO) {
-
- this.productoDAO = productoDAO;
- }
- public Collection productoList(){
- return productoDAO.findAll();
- }
-}
diff --git a/src/main/java/com/jorpelu/ServicioDeProductos/ProductosDaoImplMemory.java b/src/main/java/com/jorpelu/ServicioDeProductos/ProductosDaoImplMemory.java
deleted file mode 100644
index 5e70523..0000000
--- a/src/main/java/com/jorpelu/ServicioDeProductos/ProductosDaoImplMemory.java
+++ /dev/null
@@ -1,47 +0,0 @@
-package com.jorpelu.ServicioDeProductos;
-
-import java.util.ArrayList;
-import java.util.List;
-
-public class ProductosDaoImplMemory implements ISercicioProductoDAO {
- List productos = new ArrayList<>();
-
-
- @Override
- public Producto findByIndex(int index) {
- return productos.get(index);
- }
-
- @Override
- public List findAll() {
- return productos;
- }
-
- @Override
- public void insert(Producto producto) {
- productos.add(producto);
- }
-
- @Override
- public void edit(int index, Producto producto) {
- productos.remove(index);
- productos.add(index,producto);
- }
-
- @Override
- public void delete(int index) {
- productos.remove(index);
- }
-
- @Override
- public void delete(Producto producto) {
- productos.remove(producto);
- }
-
- public void init() {
- insert(new Producto(1,"pr1", "ds1", 100 , (float) 3.2));
- insert(new Producto(2,"pr2", "ds2", 200 , (float) 3.3));
- insert(new Producto(3,"pr3", "ds3", 300 , (float) 3.4));
- insert(new Producto(4,"pr4", "ds4", 400 , (float) 3.5));
- }
-}
diff --git a/src/main/java/com/jorpelu/ServicioDeProductos/config/AppConfig.java b/src/main/java/com/jorpelu/ServicioDeProductos/config/AppConfig.java
new file mode 100644
index 0000000..1fe76c9
--- /dev/null
+++ b/src/main/java/com/jorpelu/ServicioDeProductos/config/AppConfig.java
@@ -0,0 +1,27 @@
+package com.jorpelu.ServicioDeProductos.config;
+
+
+import org.springframework.beans.factory.annotation.Value;
+import org.springframework.context.annotation.Bean;
+import org.springframework.context.annotation.ComponentScan;
+import org.springframework.context.annotation.Configuration;
+import org.springframework.context.annotation.PropertySource;
+
+@Configuration
+@ComponentScan(basePackages = "com.jorpelu.ServicioDeProductos")
+@PropertySource("classpath:/productos.properties")
+public class AppConfig {
+ @Value("${file.path}")
+ private String file;
+
+ @Value("${file.csv.separator}")
+ private String separator;
+
+ public String getFile() {
+ return file;
+ }
+
+ public String getSeparator() {
+ return separator;
+ }
+}
diff --git a/src/main/java/com/jorpelu/ServicioDeProductos/dao/Parser.java b/src/main/java/com/jorpelu/ServicioDeProductos/dao/Parser.java
new file mode 100644
index 0000000..b520c24
--- /dev/null
+++ b/src/main/java/com/jorpelu/ServicioDeProductos/dao/Parser.java
@@ -0,0 +1,30 @@
+package com.jorpelu.ServicioDeProductos.dao;
+
+import com.jorpelu.ServicioDeProductos.model.Producto;
+import org.springframework.util.ResourceUtils;
+
+import java.io.IOException;
+import java.nio.file.Files;
+import java.nio.file.Paths;
+import java.util.ArrayList;
+import java.util.List;
+import java.util.stream.Collectors;
+
+public class Parser {
+ public static List readFile(final String path, String separator) {
+ List result = new ArrayList<>();
+ try {
+ result = Files.lines(Paths.get(ResourceUtils.getFile(path).toURI())).
+ skip(1).
+ map(line->{
+ String[] values = line.split(separator);
+ return new Producto(Integer.parseInt(values[0]), values[1], values[2], Integer.parseInt(values[3]), Float.parseFloat(values[4]) );
+ }).collect(Collectors.toList());
+ } catch (IOException e) {
+ System.out.println("Error al leer el archivo"+ e.getMessage());
+ }
+
+
+ return result;
+ }
+}
diff --git a/src/main/java/com/jorpelu/ServicioDeProductos/dao/ProductoService.java b/src/main/java/com/jorpelu/ServicioDeProductos/dao/ProductoService.java
new file mode 100644
index 0000000..62a49a7
--- /dev/null
+++ b/src/main/java/com/jorpelu/ServicioDeProductos/dao/ProductoService.java
@@ -0,0 +1,17 @@
+package com.jorpelu.ServicioDeProductos.dao;
+
+import com.jorpelu.ServicioDeProductos.model.Producto;
+import org.springframework.beans.factory.annotation.Autowired;
+import org.springframework.stereotype.Service;
+
+import java.util.Collection;
+
+@Service
+public class ProductoService{
+ @Autowired
+ private ProductosDao productoDAO;
+
+ public Collection productoList(){
+ return productoDAO.findAll();
+ }
+}
diff --git a/src/main/java/com/jorpelu/ServicioDeProductos/dao/ProductosDao.java b/src/main/java/com/jorpelu/ServicioDeProductos/dao/ProductosDao.java
new file mode 100644
index 0000000..25806d0
--- /dev/null
+++ b/src/main/java/com/jorpelu/ServicioDeProductos/dao/ProductosDao.java
@@ -0,0 +1,15 @@
+package com.jorpelu.ServicioDeProductos.dao;
+
+
+import com.jorpelu.ServicioDeProductos.model.Producto;
+
+import java.util.Collection;
+public interface ProductosDao {
+ Producto findByIndex(int index);
+ Collection findAll();
+
+ void insert(Producto producto);
+ void edit(int index, Producto producto);
+ void delete(int index);
+ void delete(Producto producto);
+}
diff --git a/src/main/java/com/jorpelu/ServicioDeProductos/dao/ProductosDaoImplMemory.java b/src/main/java/com/jorpelu/ServicioDeProductos/dao/ProductosDaoImplMemory.java
new file mode 100644
index 0000000..85ca0af
--- /dev/null
+++ b/src/main/java/com/jorpelu/ServicioDeProductos/dao/ProductosDaoImplMemory.java
@@ -0,0 +1,57 @@
+package com.jorpelu.ServicioDeProductos.dao;
+
+import com.jorpelu.ServicioDeProductos.config.AppConfig;
+import com.jorpelu.ServicioDeProductos.model.Producto;
+import org.springframework.beans.factory.annotation.Autowired;
+import org.springframework.stereotype.Repository;
+
+import javax.annotation.PostConstruct;
+import java.util.ArrayList;
+import java.util.List;
+@Repository
+public class ProductosDaoImplMemory implements ProductosDao {
+ List productos = new ArrayList<>();
+
+ @Autowired
+ private AppConfig appConfig;
+
+ @Override
+ public Producto findByIndex(int index) {
+ return productos.get(index);
+ }
+
+ @Override
+ public List findAll() {
+ return productos;
+ }
+
+ @Override
+ public void insert(Producto producto) {
+ productos.add(producto);
+ }
+
+ @Override
+ public void edit(int index, Producto producto) {
+ productos.remove(index);
+ productos.add(index,producto);
+ }
+
+ @Override
+ public void delete(int index) {
+ productos.remove(index);
+ }
+
+ @Override
+ public void delete(Producto producto) {
+ productos.remove(producto);
+ }
+
+ @PostConstruct
+ public void init() {
+ productos = Parser.readFile(appConfig.getFile(), appConfig.getSeparator());
+// productos.add(new Producto(1,"pr1", "ds1", 100 , (float) 3.2));
+// productos.add(new Producto(2,"pr2", "ds2", 200 , (float) 3.3));
+// productos.add(new Producto(3,"pr3", "ds3", 300 , (float) 3.4));
+// productos.add(new Producto(4,"pr4", "ds4", 400 , (float) 3.5));
+ }
+}
diff --git a/src/main/java/com/jorpelu/ServicioDeProductos/Producto.java b/src/main/java/com/jorpelu/ServicioDeProductos/model/Producto.java
similarity index 97%
rename from src/main/java/com/jorpelu/ServicioDeProductos/Producto.java
rename to src/main/java/com/jorpelu/ServicioDeProductos/model/Producto.java
index f881a56..ae2e793 100644
--- a/src/main/java/com/jorpelu/ServicioDeProductos/Producto.java
+++ b/src/main/java/com/jorpelu/ServicioDeProductos/model/Producto.java
@@ -1,4 +1,4 @@
-package com.jorpelu.ServicioDeProductos;
+package com.jorpelu.ServicioDeProductos.model;
import java.util.Objects;
diff --git a/src/main/resources/beans.xml b/src/main/resources/beans.xml
index f26d371..5f60add 100644
--- a/src/main/resources/beans.xml
+++ b/src/main/resources/beans.xml
@@ -7,22 +7,10 @@
http://www.springframework.org/schema/context
https://www.springframework.org/schema/context/spring-context.xsd">
-
-
-
-
-
-
-
-
-
-
-
-
-
-
+
+
+
\ No newline at end of file
diff --git a/src/main/resources/productos.properties b/src/main/resources/productos.properties
new file mode 100644
index 0000000..42b5b81
--- /dev/null
+++ b/src/main/resources/productos.properties
@@ -0,0 +1,2 @@
+file.path=classpath:productos_data.csv
+file.csv.separator=;
\ No newline at end of file
diff --git a/src/main/resources/productos_data.csv b/src/main/resources/productos_data.csv
new file mode 100644
index 0000000..60c87ac
--- /dev/null
+++ b/src/main/resources/productos_data.csv
@@ -0,0 +1,9 @@
+id;nombre;descripcion;cantidad;precio
+0;Nevera;Producto para enfriar ;200;259.99
+1;Lampara;Ilumina tu cuarto;2500;25.3
+2;Ordenador MSI;Portatil de Ultima generacion;100;1199.99
+3;Pantalla Led;Mejor Imposible;250;399.99
+4;Ratón;El mejor periférico;1500;25.99
+5;Teclado;La nueva tecnologia;200;25.99
+6;Regleta de luz;Solo eso, una regleta;1500;9.99
+7;Cargador 65W;MaximaVelocidad;500;99.99