diff --git a/src/main/java/danielcortes/xyz/controllers/CajaController.java b/src/main/java/danielcortes/xyz/controllers/CajaController.java deleted file mode 100644 index 0d142c9..0000000 --- a/src/main/java/danielcortes/xyz/controllers/CajaController.java +++ /dev/null @@ -1,106 +0,0 @@ -/* - * MIT License - * - * Copyright (c) 2018 Daniel Cortes - * - * Permission is hereby granted, free of charge, to any person obtaining a copy - * of this software and associated documentation files (the "Software"), to deal - * in the Software without restriction, including without limitation the rights - * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell - * copies of the Software, and to permit persons to whom the Software is - * furnished to do so, subject to the following conditions: - * - * The above copyright notice and this permission notice shall be included in all - * copies or substantial portions of the Software. - * - * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR - * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, - * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE - * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER - * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, - * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE - * SOFTWARE. - */ - -package danielcortes.xyz.controllers; - -import danielcortes.xyz.models.Egreso; -import danielcortes.xyz.models.EgresoDAO; -import danielcortes.xyz.models.TipoEgreso; -import danielcortes.xyz.models.TipoEgresoDAO; -import danielcortes.xyz.views.EgresosView; - -import javax.swing.*; -import java.awt.event.ComponentAdapter; -import java.awt.event.ComponentEvent; - -public class CajaController { - private EgresosView view; - private EgresoDAO egresoDAO; - private TipoEgresoDAO tipoEgresoDAO; - - public CajaController(EgresosView view, EgresoDAO egresoDAO, TipoEgresoDAO tipoEgresoDAO){ - this.view = view; - this.egresoDAO = egresoDAO; - this.tipoEgresoDAO = tipoEgresoDAO; - this.setUpViewEvents(); - this.fillEgresosTable(); - this.fillEgresosTipo(); - this.updateTotalEgresos(); - } - - private void fillEgresosTipo() { - JComboBox tipoCombo = view.getTipoCombo(); - for(TipoEgreso tipoEgreso : this.tipoEgresoDAO.findAll()){ - tipoCombo.addItem(tipoEgreso); - } - } - - private void fillEgresosTable() { - for(Egreso egreso: this.egresoDAO.findAll()){ - view.getEgresosTableModel().addRow(egreso); - } - } - - private void setUpViewEvents(){ - this.view.getGuardarButton().addActionListener(e -> guardarActionListener()); - this.view.getEliminarButton().addActionListener(e -> eliminarActionListener()); - } - - private void guardarActionListener(){ - String nro = this.view.getNroField().getText(); - String descripcion = this.view.getDescripcionField().getText(); - String valor = this.view.getValorField().getText(); - TipoEgreso tipo = (TipoEgreso) this.view.getTipoCombo().getSelectedItem(); - Egreso egreso = this.createEgreso(nro, descripcion, valor, tipo); - this.view.getEgresosTableModel().addRow(egreso); - this.updateTotalEgresos(); - } - - private void eliminarActionListener(){ - int selectedID = this.view.getEgresosTable().getSelectedRow(); - System.out.println(selectedID); - if(selectedID >= 0) { - Egreso egreso = this.view.getEgresosTableModel().getEgreso(selectedID); - this.view.getEgresosTableModel().removeRow(selectedID); - this.egresoDAO.deleteEgreso(egreso); - } - } - - private void updateTotalEgresos(){ - int total = this.egresoDAO.getTotalEgreso(); - this.view.getTotalEgresosField().setText(String.valueOf(total)); - } - - private Egreso createEgreso(String nro, String descripcion, String valor, TipoEgreso tipo){ - Egreso egreso = new Egreso(); - egreso.setValor(Integer.valueOf(valor)); - egreso.setDescripcion(descripcion); - egreso.setNro(nro); - egreso.setTipo(tipo); - egresoDAO.insertEgreso(egreso); - return egreso; - - } - -} diff --git a/src/main/java/danielcortes/xyz/controllers/EgresosController.java b/src/main/java/danielcortes/xyz/controllers/EgresosController.java new file mode 100644 index 0000000..7a803de --- /dev/null +++ b/src/main/java/danielcortes/xyz/controllers/EgresosController.java @@ -0,0 +1,211 @@ +/* + * MIT License + * + * Copyright (c) 2018 Daniel Cortes + * + * Permission is hereby granted, free of charge, to any person obtaining a copy + * of this software and associated documentation files (the "Software"), to deal + * in the Software without restriction, including without limitation the rights + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + * copies of the Software, and to permit persons to whom the Software is + * furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included in all + * copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE + * SOFTWARE. + */ + +package danielcortes.xyz.controllers; + +import danielcortes.xyz.models.Egreso; +import danielcortes.xyz.models.EgresoDAO; +import danielcortes.xyz.models.TipoEgreso; +import danielcortes.xyz.models.TipoEgresoDAO; +import danielcortes.xyz.views.EgresosView; + +import javax.swing.*; +import javax.swing.event.ListSelectionEvent; +import javax.swing.event.ListSelectionListener; +import java.awt.event.ActionEvent; +import java.awt.event.ActionListener; +import java.awt.event.ComponentAdapter; +import java.awt.event.ComponentEvent; + +public class EgresosController { + private EgresosView view; + private EgresoDAO egresoDAO; + private TipoEgresoDAO tipoEgresoDAO; + + public EgresosController(EgresosView view, EgresoDAO egresoDAO, TipoEgresoDAO tipoEgresoDAO){ + this.view = view; + this.egresoDAO = egresoDAO; + this.tipoEgresoDAO = tipoEgresoDAO; + this.setUpViewEvents(); + this.fillEgresosTable(); + this.fillEgresosTipo(); + this.updateTotalEgresos(); + } + + private void fillEgresosTipo() { + JComboBox tipoCombo = view.getTipoCombo(); + for(TipoEgreso tipoEgreso : this.tipoEgresoDAO.findAll()){ + tipoCombo.addItem(tipoEgreso); + } + } + + private void fillEgresosTable() { + for(Egreso egreso: this.egresoDAO.findAll()){ + view.getEgresosTableModel().addRow(egreso); + } + } + + private void setUpViewEvents(){ + this.view.getEgresosTable().getSelectionModel().addListSelectionListener(this::onSelectTableRowListener); + this.view.getGuardarButton().addActionListener(this::guardarActionListener); + this.view.getEliminarButton().addActionListener(this::eliminarActionListener); + this.view.getDescripcionField().addActionListener(this::guardarActionListener); + this.view.getNroField().addActionListener(this::guardarActionListener); + this.view.getValorField().addActionListener(this::guardarActionListener); + } + + private void eliminarActionListener(ActionEvent e){ + int selectedID = this.view.getEgresosTable().getSelectedRow(); + if(selectedID >= 0) { + Egreso egreso = this.view.getEgresosTableModel().getEgreso(selectedID); + this.view.getEgresosTableModel().removeRow(selectedID); + this.egresoDAO.deleteEgreso(egreso); + this.updateTotalEgresos(); + } + } + + private void guardarActionListener(ActionEvent e){ + String nro = this.view.getNroField().getText(); + String descripcion = this.view.getDescripcionField().getText(); + String valor = this.view.getValorField().getText(); + TipoEgreso tipo = (TipoEgreso) this.view.getTipoCombo().getSelectedItem(); + if(this.validateInput(nro, descripcion, valor, tipo)){ + Egreso egreso = this.createEgreso(nro, descripcion, valor, tipo); + this.view.getEgresosTableModel().addRow(egreso); + this.updateTotalEgresos(); + this.clearInputs(); + } + } + + private void onSelectTableRowListener(ListSelectionEvent e){ + this.view.getEliminarButton().setEnabled(true); + } + + private void updateTotalEgresos(){ + int total = this.egresoDAO.getTotalEgreso(); + this.view.getTotalEgresosField().setText(String.valueOf(total)); + } + + private Egreso createEgreso(String nro, String descripcion, String valor, TipoEgreso tipo){ + Egreso egreso = new Egreso(); + egreso.setValor(Integer.valueOf(valor)); + egreso.setDescripcion(descripcion); + egreso.setNro(nro); + egreso.setTipo(tipo); + egresoDAO.insertEgreso(egreso); + return egreso; + + } + + private boolean validateInput(String nro, String descripcion, String valor, TipoEgreso tipoEgreso){ + this.hideErrorMessages(); + + boolean nroValidation = this.validateNro(nro); + boolean descripcionValidation = this.validateDescripcion(descripcion); + boolean valorValidation = this.validateValor(valor); + boolean tipoEgresoValidation = this.validateTipoEgreso(tipoEgreso); + + return nroValidation && descripcionValidation && valorValidation && tipoEgresoValidation; + } + + private boolean validateNro(String nro){ + if(nro == null){ + this.view.getErrorNumero().setText("Hubo un problema con los datos"); + this.view.getErrorNumero().setVisible(true); + return false; + } + + nro = nro.trim(); + if(nro.isEmpty()){ + this.view.getErrorNumero().setText("El campo esta vacio"); + this.view.getErrorNumero().setVisible(true); + return false; + } + return true; + + } + + private boolean validateDescripcion(String descripcion){ + if(descripcion == null){ + this.view.getErrorDescripcion().setText("Hubo un problema con los datos"); + this.view.getErrorDescripcion().setVisible(true); + return false; + } + + descripcion = descripcion.trim(); + if(descripcion.isEmpty()){ + this.view.getErrorDescripcion().setText("El campo esta vacio"); + this.view.getErrorDescripcion().setVisible(true); + return false; + } + return true; + } + + private boolean validateValor(String valor){ + if(valor == null){ + this.view.getErrorValor().setText("Hubo un problema con los datos"); + this.view.getErrorValor().setVisible(true); + return false; + } + + valor = valor.trim(); + if(valor.isEmpty()){ + this.view.getErrorValor().setText("El campo esta vacio"); + this.view.getErrorValor().setVisible(true); + return false; + } + + if(!valor.chars().allMatch(Character::isDigit)){ + this.view.getErrorValor().setText("Deben ser numeros"); + this.view.getErrorValor().setVisible(true); + return false; + } + + return true; + + } + + private boolean validateTipoEgreso(TipoEgreso tipoEgreso){ + if(tipoEgreso == null){ + this.view.getErrorTipoEgreso().setText("Hubo un problema con los datos"); + this.view.getErrorTipoEgreso().setVisible(true); + return false; + } + return true; + } + + private void hideErrorMessages(){ + this.view.getErrorTipoEgreso().setVisible(false); + this.view.getErrorValor().setVisible(false); + this.view.getErrorDescripcion().setVisible(false); + this.view.getErrorNumero().setVisible(false); + } + + private void clearInputs(){ + this.view.getTipoCombo().setSelectedIndex(0); + this.view.getNroField().setText(""); + this.view.getValorField().setText(""); + this.view.getDescripcionField().setText(""); + } +} diff --git a/src/main/java/danielcortes/xyz/controllers/ManagerController.java b/src/main/java/danielcortes/xyz/controllers/ManagerController.java index 12f5f30..47d4dc4 100644 --- a/src/main/java/danielcortes/xyz/controllers/ManagerController.java +++ b/src/main/java/danielcortes/xyz/controllers/ManagerController.java @@ -67,6 +67,6 @@ public class ManagerController { this.view.getCardPanel().add(egresosView.getContentPanel(), "EGRESOS"); - CajaController cajaController = new CajaController(egresosView, egresoDAO, tipoEgresoDAO); + EgresosController egresosController = new EgresosController(egresosView, egresoDAO, tipoEgresoDAO); } } diff --git a/src/main/java/danielcortes/xyz/views/EgresosView.form b/src/main/java/danielcortes/xyz/views/EgresosView.form index 6d29f25..0b3c77f 100644 --- a/src/main/java/danielcortes/xyz/views/EgresosView.form +++ b/src/main/java/danielcortes/xyz/views/EgresosView.form @@ -8,7 +8,7 @@ - + @@ -82,7 +82,7 @@ - + @@ -95,7 +95,7 @@ - + @@ -103,7 +103,7 @@ - + @@ -111,7 +111,7 @@ - + @@ -121,12 +121,57 @@ - + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + diff --git a/src/main/java/danielcortes/xyz/views/EgresosView.java b/src/main/java/danielcortes/xyz/views/EgresosView.java index a783b21..1fbefac 100644 --- a/src/main/java/danielcortes/xyz/views/EgresosView.java +++ b/src/main/java/danielcortes/xyz/views/EgresosView.java @@ -44,6 +44,10 @@ public class EgresosView { private JButton eliminarButton; + private JLabel errorNumero; + private JLabel errorDescripcion; + private JLabel errorValor; + private JLabel errorTipoEgreso; private EgresosTableModel egresosTableModel; @@ -102,4 +106,19 @@ public class EgresosView { return egresosTableModel; } + public JLabel getErrorNumero() { + return errorNumero; + } + + public JLabel getErrorDescripcion() { + return errorDescripcion; + } + + public JLabel getErrorValor() { + return errorValor; + } + + public JLabel getErrorTipoEgreso() { + return errorTipoEgreso; + } }