From 9c0765b81fc8eee117ed5265c56e65256a5158da Mon Sep 17 00:00:00 2001 From: Daniel Cortes Date: Thu, 7 Mar 2019 16:16:01 -0300 Subject: [PATCH] La palaba get es mas bonita que find --- .../xyz/controllers/ArqueoController.java | 4 +- .../controllers/CalcularFondoController.java | 2 +- .../models/calculo_fondo/CalculoFondoDAO.java | 11 ++-- .../calculo_fondo/SQLiteCalculoFondoDAO.java | 61 +++++++++---------- .../xyz/models/documentos/DocumentosDAO.java | 11 ++-- .../documentos/SQLiteDocumentosDAO.java | 51 ++++++++-------- .../xyz/models/efectivo/EfectivoDAO.java | 9 +-- .../models/efectivo/SQLiteEfectivoDAO.java | 51 ++++++++-------- 8 files changed, 100 insertions(+), 100 deletions(-) diff --git a/src/danielcortes/xyz/controllers/ArqueoController.java b/src/danielcortes/xyz/controllers/ArqueoController.java index 1a31b58..d23fc9e 100644 --- a/src/danielcortes/xyz/controllers/ArqueoController.java +++ b/src/danielcortes/xyz/controllers/ArqueoController.java @@ -75,7 +75,7 @@ public class ArqueoController extends BaseController { * Rellena los campos del efectivo con la instancia de efectivo que pertenece a la caja */ private void fillEfectivo() { - this.efectivo = DAOManager.getEfectivoDAO().findByCaja(this.caja).orElse(Efectivo.EMPTY); + this.efectivo = DAOManager.getEfectivoDAO().getByCaja(this.caja).orElse(Efectivo.EMPTY); this.view.getVeinteMilField().setValue(efectivo.getVeinteMil()); this.view.getDiezMilField().setValue(efectivo.getDiezMil()); this.view.getCincoMilField().setValue(efectivo.getCincoMil()); @@ -91,7 +91,7 @@ public class ArqueoController extends BaseController { * Rellea los campos de documentos con la instancia de documentos que pertenece a la caja */ private void fillDocumentos() { - this.documentos = DAOManager.getDocumentosDAO().findByCaja(caja).orElse(Documentos.EMPTY); + this.documentos = DAOManager.getDocumentosDAO().getByCaja(caja).orElse(Documentos.EMPTY); this.view.getTarjetasField().setValue(documentos.getTarjetas()); this.view.getChequesField().setValue(documentos.getCheques()); this.view.getRetiroField().setValue(documentos.getRetiros()); diff --git a/src/danielcortes/xyz/controllers/CalcularFondoController.java b/src/danielcortes/xyz/controllers/CalcularFondoController.java index 2539f80..95c98c6 100644 --- a/src/danielcortes/xyz/controllers/CalcularFondoController.java +++ b/src/danielcortes/xyz/controllers/CalcularFondoController.java @@ -33,7 +33,7 @@ public class CalcularFondoController extends BaseController { private void fillTable() { FondoTableModel tableModel = this.view.getTableModel(); tableModel.removeRows(); - for (CalculoFondo calculoFondo : DAOManager.getCalculoFondoDAO().findByCaja(this.caja)) { + for (CalculoFondo calculoFondo : DAOManager.getCalculoFondoDAO().getByCaja(this.caja)) { tableModel.addRow(calculoFondo); } } diff --git a/src/danielcortes/xyz/models/calculo_fondo/CalculoFondoDAO.java b/src/danielcortes/xyz/models/calculo_fondo/CalculoFondoDAO.java index a32e3d8..dc3b482 100644 --- a/src/danielcortes/xyz/models/calculo_fondo/CalculoFondoDAO.java +++ b/src/danielcortes/xyz/models/calculo_fondo/CalculoFondoDAO.java @@ -30,18 +30,17 @@ import java.util.Optional; public interface CalculoFondoDAO { - List findAll(); + List getAll(); - List findByCaja(Caja caja); + List getByCaja(Caja caja); - Optional findById(int id); + Optional getById(int id); + + int getTotalCalculoFondo(Caja caja); void insertCalculoFondo(CalculoFondo calculoFondo); void updateCalculoFondo(CalculoFondo calculoFondo); void deleteCalculoFondo(CalculoFondo calculoFondo); - - int getTotalCalculoFondo(Caja caja); - } diff --git a/src/danielcortes/xyz/models/calculo_fondo/SQLiteCalculoFondoDAO.java b/src/danielcortes/xyz/models/calculo_fondo/SQLiteCalculoFondoDAO.java index 99dc295..ec5d73b 100644 --- a/src/danielcortes/xyz/models/calculo_fondo/SQLiteCalculoFondoDAO.java +++ b/src/danielcortes/xyz/models/calculo_fondo/SQLiteCalculoFondoDAO.java @@ -49,7 +49,7 @@ public class SQLiteCalculoFondoDAO implements CalculoFondoDAO { } @Override - public List findAll() { + public List getAll() { List calculoFondoList = new ArrayList<>(); String query = "select * from calculo_fondo"; try (Connection conn = connectionHolder.getConnection()) { @@ -79,7 +79,7 @@ public class SQLiteCalculoFondoDAO implements CalculoFondoDAO { } @Override - public List findByCaja(Caja caja) { + public List getByCaja(Caja caja) { List calculoFondoList = new ArrayList<>(); if(Caja.EMPTY == caja){ return calculoFondoList; @@ -111,7 +111,7 @@ public class SQLiteCalculoFondoDAO implements CalculoFondoDAO { } @Override - public Optional findById(int id) { + public Optional getById(int id) { CalculoFondo calculoFondo = null; String query = "select * from calculo_fondo where id = ?"; try (Connection conn = connectionHolder.getConnection()) { @@ -138,6 +138,33 @@ public class SQLiteCalculoFondoDAO implements CalculoFondoDAO { return Optional.ofNullable(calculoFondo); } + @Override + public int getTotalCalculoFondo(Caja caja) { + int sum = 0; + + if (Caja.EMPTY == caja) { + return sum; + } + + String query = "select sum(valor) from calculo_fondo where caja_id = ?"; + try (Connection conn = connectionHolder.getConnection()) { + try (PreparedStatement ps = conn.prepareStatement(query)) { + ps.setInt(1, caja.getId()); + try (ResultSet rs = ps.executeQuery()) { + LOGGER.log(Level.FINE, "QUERY: {0} | values: {1}", new Object[]{query, caja.getId()}); + + if (rs.next()) { + sum = rs.getInt(1); + } + } + } + } catch (SQLException e) { + LOGGER.log(Level.SEVERE, e.toString(), e); + } + + return sum; + } + @Override public void insertCalculoFondo(CalculoFondo calculoFondo) { String query = "insert into calculo_fondo (valor, descripcion, caja_id) values (?, ?, ?)"; @@ -192,32 +219,4 @@ public class SQLiteCalculoFondoDAO implements CalculoFondoDAO { LOGGER.log(Level.SEVERE, e.toString(), e); } } - - @Override - public int getTotalCalculoFondo(Caja caja) { - int sum = 0; - - if (Caja.EMPTY == caja) { - return sum; - } - - String query = "select sum(valor) from calculo_fondo where caja_id = ?"; - try (Connection conn = connectionHolder.getConnection()) { - try (PreparedStatement ps = conn.prepareStatement(query)) { - ps.setInt(1, caja.getId()); - try (ResultSet rs = ps.executeQuery()) { - LOGGER.log(Level.FINE, "QUERY: {0} | values: {1}", new Object[]{query, caja.getId()}); - - if (rs.next()) { - sum = rs.getInt(1); - } - } - } - } catch (SQLException e) { - LOGGER.log(Level.SEVERE, e.toString(), e); - } - - return sum; - } - } diff --git a/src/danielcortes/xyz/models/documentos/DocumentosDAO.java b/src/danielcortes/xyz/models/documentos/DocumentosDAO.java index 0db7c08..347816d 100644 --- a/src/danielcortes/xyz/models/documentos/DocumentosDAO.java +++ b/src/danielcortes/xyz/models/documentos/DocumentosDAO.java @@ -30,11 +30,13 @@ import java.util.Optional; public interface DocumentosDAO { - List findAll(); + List getAll(); - Optional findById(int id); + Optional getById(int id); - Optional findByCaja(Caja caja); + Optional getByCaja(Caja caja); + + int getTotalDocumentos(Caja caja); void insertDocumentos(Documentos documentos); @@ -43,7 +45,4 @@ public interface DocumentosDAO { void updateDocumentos(Documentos documentos); void deleteDocumentos(Documentos documentos); - - int getTotalDocumentos(Caja caja); - } diff --git a/src/danielcortes/xyz/models/documentos/SQLiteDocumentosDAO.java b/src/danielcortes/xyz/models/documentos/SQLiteDocumentosDAO.java index 073734e..2f4d92a 100644 --- a/src/danielcortes/xyz/models/documentos/SQLiteDocumentosDAO.java +++ b/src/danielcortes/xyz/models/documentos/SQLiteDocumentosDAO.java @@ -49,7 +49,7 @@ public class SQLiteDocumentosDAO implements DocumentosDAO { } @Override - public List findAll() { + public List getAll() { List documentosList = new ArrayList<>(); String query = "select * from documentos"; try (Connection conn = connectionHolder.getConnection()) { @@ -80,7 +80,7 @@ public class SQLiteDocumentosDAO implements DocumentosDAO { } @Override - public Optional findById(int id) { + public Optional getById(int id) { Documentos documentos = null; try (Connection conn = connectionHolder.getConnection()) { @@ -111,7 +111,7 @@ public class SQLiteDocumentosDAO implements DocumentosDAO { } @Override - public Optional findByCaja(Caja caja) { + public Optional getByCaja(Caja caja) { Documentos documentos = null; if (Caja.EMPTY == caja) { @@ -141,6 +141,29 @@ public class SQLiteDocumentosDAO implements DocumentosDAO { return Optional.ofNullable(documentos); } + @Override + public int getTotalDocumentos(Caja caja) { + int total = 0; + if (Caja.EMPTY == caja) { + return total; + } + + String query = "select cheques + tarjetas + retiros from documentos where caja_id = ?"; + try (Connection conn = connectionHolder.getConnection()) { + try (PreparedStatement ps = conn.prepareStatement(query)) { + ps.setInt(1, caja.getId()); + try (ResultSet rs = ps.executeQuery()) { + if (rs.next()) { + total = rs.getInt(1); + } + } + } + } catch (SQLException e) { + LOGGER.log(Level.SEVERE, e.toString(), e); + } + return total; + } + @Override public void insertDocumentos(Documentos documentos) { String query = "insert into documentos (cheques, tarjetas, retiros, caja_id) values (?,?,?,?)"; @@ -213,26 +236,4 @@ public class SQLiteDocumentosDAO implements DocumentosDAO { } } - @Override - public int getTotalDocumentos(Caja caja) { - int total = 0; - if (Caja.EMPTY == caja) { - return total; - } - - String query = "select cheques + tarjetas + retiros from documentos where caja_id = ?"; - try (Connection conn = connectionHolder.getConnection()) { - try (PreparedStatement ps = conn.prepareStatement(query)) { - ps.setInt(1, caja.getId()); - try (ResultSet rs = ps.executeQuery()) { - if (rs.next()) { - total = rs.getInt(1); - } - } - } - } catch (SQLException e) { - LOGGER.log(Level.SEVERE, e.toString(), e); - } - return total; - } } diff --git a/src/danielcortes/xyz/models/efectivo/EfectivoDAO.java b/src/danielcortes/xyz/models/efectivo/EfectivoDAO.java index 7e91d80..8a84343 100644 --- a/src/danielcortes/xyz/models/efectivo/EfectivoDAO.java +++ b/src/danielcortes/xyz/models/efectivo/EfectivoDAO.java @@ -30,11 +30,13 @@ import java.util.Optional; public interface EfectivoDAO { - List findAll(); + List getAll(); - Optional findById(int id); + Optional getById(int id); - Optional findByCaja(Caja caja); + Optional getByCaja(Caja caja); + + int getTotalEfectivo(Caja caja); void insertEfectivo(Efectivo efectivo); @@ -44,5 +46,4 @@ public interface EfectivoDAO { void deleteEfectivo(Efectivo efectivo); - int getTotalEfectivo(Caja caja); } diff --git a/src/danielcortes/xyz/models/efectivo/SQLiteEfectivoDAO.java b/src/danielcortes/xyz/models/efectivo/SQLiteEfectivoDAO.java index 6b541e9..d354a53 100644 --- a/src/danielcortes/xyz/models/efectivo/SQLiteEfectivoDAO.java +++ b/src/danielcortes/xyz/models/efectivo/SQLiteEfectivoDAO.java @@ -49,7 +49,7 @@ public class SQLiteEfectivoDAO implements EfectivoDAO { } @Override - public List findAll() { + public List getAll() { List efectivoList = new ArrayList<>(); String query = "select * from efectivos"; try (Connection conn = connectionHolder.getConnection()) { @@ -86,7 +86,7 @@ public class SQLiteEfectivoDAO implements EfectivoDAO { } @Override - public Optional findById(int id) { + public Optional getById(int id) { Efectivo efectivo = null; String query = "select * from efectivos where id = ?"; try (Connection conn = connectionHolder.getConnection()) { @@ -122,7 +122,7 @@ public class SQLiteEfectivoDAO implements EfectivoDAO { } @Override - public Optional findByCaja(Caja caja) { + public Optional getByCaja(Caja caja) { Efectivo efectivo = null; if (Caja.EMPTY == caja) { return Optional.ofNullable(efectivo); @@ -156,6 +156,29 @@ public class SQLiteEfectivoDAO implements EfectivoDAO { return Optional.ofNullable(efectivo); } + @Override + public int getTotalEfectivo(Caja caja) { + int total = 0; + if (Caja.EMPTY == caja) { + return total; + } + + String query = "select veinte_mil + diez_mil + cinco_mil + dos_mil + mil + quinientos + cien + cincuenta + diez from efectivos where caja_id = ?"; + try (Connection conn = connectionHolder.getConnection()) { + try (PreparedStatement ps = conn.prepareStatement(query)) { + ps.setInt(1, caja.getId()); + try (ResultSet rs = ps.executeQuery()) { + if (rs.next()) { + total = rs.getInt(1); + } + } + } + } catch (SQLException e) { + LOGGER.log(Level.SEVERE, e.toString(), e); + } + return total; + } + @Override public void insertEfectivo(Efectivo efectivo) { String query = "insert into efectivos (veinte_mil, diez_mil, cinco_mil, dos_mil, mil, quinientos, cien, cincuenta, diez, caja_id) values (?,?,?,?,?,?,?,?,?,?)"; @@ -240,26 +263,4 @@ public class SQLiteEfectivoDAO implements EfectivoDAO { } } - @Override - public int getTotalEfectivo(Caja caja) { - int total = 0; - if (Caja.EMPTY == caja) { - return total; - } - - String query = "select veinte_mil + diez_mil + cinco_mil + dos_mil + mil + quinientos + cien + cincuenta + diez from efectivos where caja_id = ?"; - try (Connection conn = connectionHolder.getConnection()) { - try (PreparedStatement ps = conn.prepareStatement(query)) { - ps.setInt(1, caja.getId()); - try (ResultSet rs = ps.executeQuery()) { - if (rs.next()) { - total = rs.getInt(1); - } - } - } - } catch (SQLException e) { - LOGGER.log(Level.SEVERE, e.toString(), e); - } - return total; - } }