Skip to content

Commit

Permalink
REFACTOR: Modify date expired and price.
Browse files Browse the repository at this point in the history
  • Loading branch information
chanchobrujo committed Sep 20, 2023
1 parent c8a982e commit 78f1023
Show file tree
Hide file tree
Showing 12 changed files with 219 additions and 169 deletions.
Binary file modified bd/db.db
Binary file not shown.
4 changes: 2 additions & 2 deletions nbproject/private/private.xml
Original file line number Diff line number Diff line change
Expand Up @@ -13,14 +13,14 @@
<url>src/modules/ModuleProduct.java</url>
<bookmark id="2">
<name/>
<line>75</line>
<line>76</line>
<key/>
</bookmark>
</file>
</editor-bookmarks>
<open-files xmlns="http://www.netbeans.org/ns/projectui-open-files/2">
<group>
<file>file:/C:/Users/User/Documents/Ambientes%20de%20desarrollo/BOTICA-CAUTIVO/src/views/reports/ReportSales.java</file>
<file>file:/C:/Users/User/Documents/Ambientes%20de%20desarrollo/BOTICA-CAUTIVO/src/views/maintenance/products_views.java</file>
</group>
</open-files>
</project-private>
2 changes: 1 addition & 1 deletion src/Constans/Headers/HeadersTableSwing.java
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ public class HeadersTableSwing {

public static final String headres_product[] =
new String[] {"ID", "NOMBRE", "MARCA", "PRECIO", "STOCK", "CATEGORIA"
, "ESTADO"};
, "ESTADO", "FECHA DE VENC."};

public static final String headres_productstock[] = new String[] {"ID", "PRODUCTO", "PRECIO", "STOCK", "CATEGORIA"};

Expand Down
3 changes: 2 additions & 1 deletion src/entities/Product.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
package entities;
package entities;

import lombok.AllArgsConstructor;
import lombok.Builder;
Expand Down Expand Up @@ -31,6 +31,7 @@ public class Product {
private Integer stock;
private Category category;
private Boolean state;
private String date;

public Product(String name, String brand, Double price, Integer stock,
Category category) {
Expand Down
2 changes: 2 additions & 0 deletions src/model/ModelProduct.java
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
*/
package model;

import java.util.Date;
import lombok.AllArgsConstructor;
import lombok.Builder;
import lombok.Getter;
Expand All @@ -29,6 +30,7 @@ public class ModelProduct {
private Double price;
private Integer stock;
private String category;
private Date date;
private Boolean state;

}
4 changes: 2 additions & 2 deletions src/modules/ModuleProduct.java
Original file line number Diff line number Diff line change
Expand Up @@ -70,11 +70,11 @@ public List<Product> findAll_Products_Active() {
}

public String saveProduct(Integer id, String name, String brand,
Double price, Integer Stock, String category) {
Double price, Integer Stock, String category, String date) {
boolean verify = Commons.StringsIsEmpty(name, brand, category)
|| Commons.IntegerIsEmpty(Stock) || Commons.DoublesIsEmpty(price);

return verify ? messageError : this.productService.save(id, name, brand, price, Stock, category);
return verify ? messageError : this.productService.save(id, name, brand, price, Stock, category, date);
}

public String changeStateProduct(Integer id) {
Expand Down
5 changes: 2 additions & 3 deletions src/render/table/Entity/TableModelProduct.java
Original file line number Diff line number Diff line change
Expand Up @@ -63,10 +63,9 @@ private void renderTable(JTable table, List<Product> array){
row.add(product.getStock());
row.add(product.getCategory().getName());
row.add(Commons.BooleanToString(product.getState()));
row.add(product.getDate());
return row;
}).forEachOrdered(row -> {
((DefaultTableModel) table.getModel()).addRow(row);
});
}).forEachOrdered(row -> ((DefaultTableModel) table.getModel()).addRow(row));
table.getColumnModel().getColumn(0).setMaxWidth(0);
table.getColumnModel().getColumn(3).setMaxWidth(50);
table.getColumnModel().getColumn(4).setMaxWidth(50);
Expand Down
12 changes: 6 additions & 6 deletions src/repository/ProductRepository.java
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
import java.util.List;
import java.util.Objects;
import java.util.stream.Collectors;
import java.util.stream.Stream;
import util.Commons;
import util.GestorBd;
import util.Mapper;
Expand All @@ -34,12 +35,12 @@ public ProductRepository(){
}

public String insert(Product product) {
String sql = "INSERT INTO product (name, brand, price, stock, id_category, state) VALUES('"
String sql = "INSERT INTO product (name, brand, price, stock, id_category, state, expired) VALUES('"
+product.getName()+"','"+product.getBrand()+"',"
+product.getPrice()+","+product.getStock()+","
+product.getCategory().getId()+","
+Commons.BooleanToInteger(product.getState()) +");";

+Commons.BooleanToInteger(product.getState()) +",'"
+product.getDate() + "');";
return GestorBd.execute(sql);
}

Expand All @@ -48,9 +49,9 @@ public String update(Product product) {
+ "brand = '"+product.getBrand()+"', "
+ "price = "+product.getPrice()+", "
+ "stock = "+product.getStock()+", "
+ "expired = '"+product.getDate()+"', "
+ "id_category = "+product.getCategory().getId()
+ " WHERE id = "+product.getId();

return GestorBd.execute(sql);
}

Expand Down Expand Up @@ -79,8 +80,7 @@ public String changeState(int id, Boolean state) {

public List<Product> findAll() {
List list = GestorBd.findAll(SCRIPT_FINDALL);
List<Product> findAll = new ArrayList<>();

List<Product> findAll = new ArrayList<>();
if (Commons.collectionNonEmptyOrNull(list))
list.stream()
.map(mapper -> {
Expand Down
36 changes: 14 additions & 22 deletions src/services/ProductService.java
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
package services;

import entities.Category;
import entities.Product;
import entities.Product;

import java.util.List;
import java.util.Objects;
Expand Down Expand Up @@ -38,32 +38,27 @@ private Optional<Category> findByName_Categories(String name){
.findFirst();
}

public String save(int id, String name, String brand, Double price,
Integer Stock, String category){
public String save(Integer id, String name, String brand, Double price,
Integer Stock, String category, String date){
String message = Constans.Enums.ErrorMessage.REPETED_VALUES.getValue();
Optional<Category> _category = this.findByName_Categories(category);

Boolean verify = this.verifyByNameOrBrand(name, brand).isPresent();

if (verify) return message;

Product product = Product.builder()
.name(name)
.brand(brand)
.price(price)
.stock(Stock)
.category(_category.get())
.state(Boolean.TRUE)
.date(date)
.build();

switch(id){
case 0:
message = this.productRepository.insert(product);
break;
default:
product.setId(id);
message = this.productRepository.update(product);
break;
if (id == null || id == 0) {
Boolean verify = this.verifyByNameOrBrand(name, brand).isPresent();
if (verify) return message;
message = this.productRepository.insert(product);
} else {
product.setId(id);
product.setDate(date);
message = this.productRepository.update(product);
}
return message;
}
Expand Down Expand Up @@ -128,11 +123,8 @@ public List<Product> searchProduct(String value){

public List<Product> findAllByStockMin(){
return this.productRepository.findAll().stream()
.filter(predicate -> predicate.getStock() <= 10)
.map(mapper -> {
this.productRepository.changeState(mapper.getId(), Boolean.FALSE);
return mapper;
})
.filter(predicate -> predicate.getStock() == 0)
.peek(mapper -> this.productRepository.changeState(mapper.getId(), false))
.collect(Collectors.toList());
}
}
11 changes: 6 additions & 5 deletions src/util/Mapper.java
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
import model.ModelSale;

import Constans.Constan;
import java.util.Objects;

/**
*
Expand All @@ -28,7 +29,6 @@ public static Rol mapperRol(Object object) {
.id(id)
.name(row[1].toString())
.build();

}

public static Category mapperCategory(Object obj) {
Expand Down Expand Up @@ -60,24 +60,25 @@ public static User mapperUser(Object obj) {

public static Product mapperProduct(Object obj) {
Object[] row = (Object[]) obj;

Integer id = Integer.parseInt(row[0].toString());
String name = row[1].toString();
String brand = row[2].toString();
Double price = Double.parseDouble(row[3].toString());
Integer stock = Integer.parseInt(row[4].toString());

Boolean state = Commons.IntegerToBoolean(Integer.parseInt(row[6].toString()));

return Product.builder()
Product prod = Product.builder()
.id(id)
.name(name)
.brand(brand)
.price(price)
.stock(stock)
.state(state)
.build();

if (Objects.nonNull(row[7])) {
prod.setDate(row[7].toString());
}
return prod;
}

public static ModelSale mapperSale(Object object) {
Expand Down
Loading

0 comments on commit 78f1023

Please sign in to comment.