diff --git a/database/create.sql b/database/create.sql index f7383cb..5a0e730 100644 --- a/database/create.sql +++ b/database/create.sql @@ -24,20 +24,36 @@ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell drop table if exists egresos; drop table if exists tipos_egreso; +drop table if exists ingresos; +drop table if exists tipos_ingreso; + +create table tipos_egreso( + id int(10) unsigned primary key auto_increment, + nombre varchar(191) not null +); create table egresos( id int(10) unsigned primary key auto_increment, nro varchar(191) not null, descripcion varchar(191) not null, valor int(10) not null, - tipo_id int(10) not null + tipo_egreso_id int(10) unsigned not null, + foreign key fk_tipo_id(tipo_egreso_id) references tipos_egreso(id) on update cascade on delete restrict ); -create table tipos_egreso( + +create table tipos_ingreso( id int(10) unsigned primary key auto_increment, nombre varchar(191) not null ); +create table ingresos( + id int(10) unsigned primary key auto_increment, + valor int(10) not null, + tipo_ingreso_id int(10) unsigned not null, + foreign key fk_tipo_ingreso(tipo_ingreso_id) references tipos_ingreso(id) on update cascade on delete restrict +); + insert into tipos_egreso (nombre) values ('Guia Materia Prima'), ('Factura Materia Prima'), @@ -50,3 +66,9 @@ insert into tipos_egreso (nombre) values ('Anticipo Personal'), ('Retiros Gerencia'), ('Otro'); + +insert into tipos_ingreso (nombre) values +('Boletas Fiscales'), +('Boletas Manuales'), +('Facturas'), +('Guias') diff --git a/src/main/java/danielcortes/xyz/Main.java b/src/main/java/danielcortes/xyz/Main.java index cbd5520..d8588eb 100644 --- a/src/main/java/danielcortes/xyz/Main.java +++ b/src/main/java/danielcortes/xyz/Main.java @@ -40,12 +40,13 @@ public class Main { ManagerView view = new ManagerView(); + ManagerController managerController = new ManagerController(view); + JFrame frame = new JFrame("Caja"); frame.setContentPane(view.getContentPanel()); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); frame.setSize(1300,700); frame.setLocationRelativeTo(null); - ManagerController managerController = new ManagerController(view); frame.setVisible(true); diff --git a/src/main/java/danielcortes/xyz/controllers/EgresosController.java b/src/main/java/danielcortes/xyz/controllers/EgresosController.java index 7a803de..d55b9fb 100644 --- a/src/main/java/danielcortes/xyz/controllers/EgresosController.java +++ b/src/main/java/danielcortes/xyz/controllers/EgresosController.java @@ -24,19 +24,18 @@ 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.models.egreso.Egreso; +import danielcortes.xyz.models.egreso.EgresoDAO; +import danielcortes.xyz.models.tipo_egreso.TipoEgreso; +import danielcortes.xyz.models.tipo_egreso.TipoEgresoDAO; import danielcortes.xyz.views.EgresosView; +import danielcortes.xyz.views.components.EgresosTableModel; 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; +import java.awt.event.KeyAdapter; +import java.awt.event.KeyEvent; public class EgresosController { private EgresosView view; @@ -49,11 +48,12 @@ public class EgresosController { this.tipoEgresoDAO = tipoEgresoDAO; this.setUpViewEvents(); this.fillEgresosTable(); - this.fillEgresosTipo(); + this.fillTipoEgresoCombo(); this.updateTotalEgresos(); + this.updateEliminarButton(); } - private void fillEgresosTipo() { + private void fillTipoEgresoCombo() { JComboBox tipoCombo = view.getTipoCombo(); for(TipoEgreso tipoEgreso : this.tipoEgresoDAO.findAll()){ tipoCombo.addItem(tipoEgreso); @@ -61,31 +61,30 @@ public class EgresosController { } private void fillEgresosTable() { + EgresosTableModel egresosTableModel = view.getEgresosTableModel(); for(Egreso egreso: this.egresoDAO.findAll()){ - view.getEgresosTableModel().addRow(egreso); + egresosTableModel.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); + this.view.getEgresosTable().getSelectionModel().addListSelectionListener(e -> onSelectTableRowListener()); + this.view.getGuardarButton().addActionListener(e -> guardarActionListener()); + this.view.getEliminarButton().addActionListener(e -> eliminarActionListener()); + this.view.getDescripcionField().addActionListener(e -> guardarActionListener()); + this.view.getNroField().addActionListener(e -> guardarActionListener()); + this.view.getValorField().addActionListener(e -> guardarActionListener()); + this.view.getTipoCombo().addKeyListener(new KeyAdapter() { + @Override + public void keyPressed(KeyEvent e) { + if(e.getKeyCode() == KeyEvent.VK_ENTER){ + 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){ + private void guardarActionListener(){ String nro = this.view.getNroField().getText(); String descripcion = this.view.getDescripcionField().getText(); String valor = this.view.getValorField().getText(); @@ -98,7 +97,18 @@ public class EgresosController { } } - private void onSelectTableRowListener(ListSelectionEvent e){ + private void eliminarActionListener(){ + 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(); + this.updateEliminarButton(); + } + } + + private void onSelectTableRowListener(){ this.view.getEliminarButton().setEnabled(true); } @@ -107,6 +117,14 @@ public class EgresosController { this.view.getTotalEgresosField().setText(String.valueOf(total)); } + private void updateEliminarButton() { + if(this.view.getEgresosTable().getSelectedRow()>=0){ + this.view.getEliminarButton().setEnabled(true); + }else{ + this.view.getEliminarButton().setEnabled(false); + } + } + private Egreso createEgreso(String nro, String descripcion, String valor, TipoEgreso tipo){ Egreso egreso = new Egreso(); egreso.setValor(Integer.valueOf(valor)); @@ -182,6 +200,12 @@ public class EgresosController { return false; } + if(valor.length() > 10){ + this.view.getErrorValor().setText("El numero ingresado es demasiado largo"); + this.view.getErrorValor().setVisible(true); + return false; + } + return true; } diff --git a/src/main/java/danielcortes/xyz/controllers/IngresosController.java b/src/main/java/danielcortes/xyz/controllers/IngresosController.java new file mode 100644 index 0000000..c5bc802 --- /dev/null +++ b/src/main/java/danielcortes/xyz/controllers/IngresosController.java @@ -0,0 +1,194 @@ +/* + * 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.ingreso.Ingreso; +import danielcortes.xyz.models.ingreso.IngresoDAO; +import danielcortes.xyz.models.tipo_ingreso.TipoIngreso; +import danielcortes.xyz.models.tipo_ingreso.TipoIngresoDAO; +import danielcortes.xyz.views.IngresosView; +import danielcortes.xyz.views.components.IngresosTableModel; + +import javax.swing.*; +import javax.swing.event.ListSelectionEvent; +import javax.xml.bind.SchemaOutputResolver; +import java.awt.event.ActionEvent; +import java.awt.event.KeyAdapter; +import java.awt.event.KeyEvent; + +public class IngresosController { + private IngresosView view; + private IngresoDAO ingresoDAO; + private TipoIngresoDAO tipoIngresoDAO; + + public IngresosController(IngresosView view, IngresoDAO ingresoDAO, TipoIngresoDAO tipoIngresoDAO) { + this.view = view; + this.ingresoDAO = ingresoDAO; + this.tipoIngresoDAO = tipoIngresoDAO; + this.fillTipoIngresoCombo(); + this.fillIngresosTable(); + this.setupViewEvents(); + this.updateTotalIngresos(); + this.updateEliminarButton(); + } + + private void fillTipoIngresoCombo() { + JComboBox tipoCombo = this.view.getTipoCombo(); + for (TipoIngreso tipo : this.tipoIngresoDAO.findAll()) { + tipoCombo.addItem(tipo); + } + } + + private void fillIngresosTable() { + IngresosTableModel ingresosTableModel = this.view.getIngresosTableModel(); + for (Ingreso ingreso : this.ingresoDAO.findAll()) { + ingresosTableModel.addRow(ingreso); + } + } + + private void setupViewEvents() { + this.view.getIngresosTable().getSelectionModel().addListSelectionListener(e -> onSelectTableRowListener()); + this.view.getGuardarButton().addActionListener(e -> guardarActionListener()); + this.view.getValorField().addActionListener(e -> guardarActionListener()); + this.view.getEliminarButton().addActionListener(e -> eliminarActionListener()); + + this.view.getTipoCombo().addKeyListener(new KeyAdapter() { + @Override + public void keyPressed(KeyEvent e) { + if(e.getKeyCode() == KeyEvent.VK_ENTER){ + guardarActionListener(); + } + } + }); + } + + private void guardarActionListener() { + String valor = this.view.getValorField().getText(); + TipoIngreso tipoIngreso = (TipoIngreso) this.view.getTipoCombo().getSelectedItem(); + + if(this.validateInput(valor, tipoIngreso)){ + Ingreso ingreso = this.createIngreso(valor,tipoIngreso); + this.view.getIngresosTableModel().addRow(ingreso); + this.clearInputs(); + this.updateTotalIngresos(); + } + } + + private void eliminarActionListener() { + int selectedId = this.view.getIngresosTable().getSelectedRow(); + if(selectedId >= 0){ + Ingreso ingreso = this.view.getIngresosTableModel().getIngreso(selectedId); + this.view.getIngresosTableModel().removeRow(selectedId); + this.ingresoDAO.deleteIngreso(ingreso); + this.updateTotalIngresos(); + this.updateEliminarButton(); + } + } + + private void onSelectTableRowListener(){ + this.view.getEliminarButton().setEnabled(true); + } + + private void updateTotalIngresos(){ + int total = this.ingresoDAO.getTotalIngreso(); + this.view.getTotalIngresoField().setText(String.valueOf(total)); + } + + private void updateEliminarButton() { + if(this.view.getIngresosTable().getSelectedRow()>=0){ + this.view.getEliminarButton().setEnabled(true); + }else{ + this.view.getEliminarButton().setEnabled(false); + } + } + + private Ingreso createIngreso(String valor, TipoIngreso tipoIngreso) { + Ingreso ingreso = new Ingreso(); + ingreso.setTipoIngreso(tipoIngreso); + ingreso.setValor(Integer.valueOf(valor)); + + this.ingresoDAO.insertIngreso(ingreso); + + return ingreso; + } + + private boolean validateInput(String valor, TipoIngreso tipoIngreso) { + this.hideErrorMessages(); + + boolean valorValidation = this.validateValor(valor); + boolean tipoIngresoValidation = this.validateTipoIngreso(tipoIngreso); + + return valorValidation && tipoIngresoValidation; + } + + 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; + } + + if(valor.length() > 10){ + this.view.getErrorValor().setText("El numero ingresado es demasiado largo"); + this.view.getErrorValor().setVisible(true); + return false; + } + + return true; + + } + + private boolean validateTipoIngreso(TipoIngreso tipoIngreso) { + if (tipoIngreso == null) { + this.view.getErrorTipoIngreso().setText("Hubo un problema con los datos"); + this.view.getErrorTipoIngreso().setVisible(true); + return false; + } + return true; + } + + private void hideErrorMessages() { + this.view.getErrorTipoIngreso().setVisible(false); + this.view.getErrorValor().setVisible(false); + } + + private void clearInputs() { + this.view.getTipoCombo().setSelectedIndex(0); + this.view.getValorField().setText(""); + } +} diff --git a/src/main/java/danielcortes/xyz/controllers/ManagerController.java b/src/main/java/danielcortes/xyz/controllers/ManagerController.java index 47d4dc4..10d4526 100644 --- a/src/main/java/danielcortes/xyz/controllers/ManagerController.java +++ b/src/main/java/danielcortes/xyz/controllers/ManagerController.java @@ -24,14 +24,19 @@ package danielcortes.xyz.controllers; -import danielcortes.xyz.models.EgresoDAO; -import danielcortes.xyz.models.TipoEgresoDAO; -import danielcortes.xyz.models.mysql.MysqlEgresoDAO; -import danielcortes.xyz.models.mysql.MysqlTipoEgresoDAO; +import danielcortes.xyz.models.egreso.EgresoDAO; +import danielcortes.xyz.models.ingreso.IngresoDAO; +import danielcortes.xyz.models.ingreso.MysqlIngresoDAO; +import danielcortes.xyz.models.tipo_egreso.TipoEgresoDAO; +import danielcortes.xyz.models.egreso.MysqlEgresoDAO; +import danielcortes.xyz.models.tipo_egreso.MysqlTipoEgresoDAO; +import danielcortes.xyz.models.tipo_ingreso.MysqlTipoIngresoDAO; +import danielcortes.xyz.models.tipo_ingreso.TipoIngreso; +import danielcortes.xyz.models.tipo_ingreso.TipoIngresoDAO; import danielcortes.xyz.views.EgresosView; +import danielcortes.xyz.views.IngresosView; import danielcortes.xyz.views.ManagerView; -import javax.swing.*; import java.awt.*; @@ -51,13 +56,24 @@ public class ManagerController { }); this.view.getIngresosButton().addActionListener(e -> { CardLayout layout = (CardLayout) this.view.getCardPanel().getLayout(); - layout.show(this.view.getCardPanel(),"NONE"); + layout.show(this.view.getCardPanel(),"INGRESOS"); }); } private void loadCardContents(){ - this.view.getCardPanel().add(new JPanel(), "NONE"); + //this.view.getCardPanel().add(new JPanel(), "NONE"); this.loadEgresosView(); + this.loadIngresosView(); + } + + private void loadIngresosView() { + IngresosView ingresosView = new IngresosView(); + IngresoDAO ingresoDAO = new MysqlIngresoDAO(); + TipoIngresoDAO tipoIngresoDAO = new MysqlTipoIngresoDAO(); + + this.view.getCardPanel().add(ingresosView.getContentPanel(), "INGRESOS"); + + IngresosController ingresosController = new IngresosController(ingresosView,ingresoDAO,tipoIngresoDAO); } private void loadEgresosView(){ diff --git a/src/main/java/danielcortes/xyz/models/Egreso.java b/src/main/java/danielcortes/xyz/models/egreso/Egreso.java similarity index 96% rename from src/main/java/danielcortes/xyz/models/Egreso.java rename to src/main/java/danielcortes/xyz/models/egreso/Egreso.java index dd5ca0e..1018459 100644 --- a/src/main/java/danielcortes/xyz/models/Egreso.java +++ b/src/main/java/danielcortes/xyz/models/egreso/Egreso.java @@ -22,7 +22,9 @@ * SOFTWARE. */ -package danielcortes.xyz.models; +package danielcortes.xyz.models.egreso; + +import danielcortes.xyz.models.tipo_egreso.TipoEgreso; public class Egreso { diff --git a/src/main/java/danielcortes/xyz/models/EgresoDAO.java b/src/main/java/danielcortes/xyz/models/egreso/EgresoDAO.java similarity index 94% rename from src/main/java/danielcortes/xyz/models/EgresoDAO.java rename to src/main/java/danielcortes/xyz/models/egreso/EgresoDAO.java index 41de979..aad8278 100644 --- a/src/main/java/danielcortes/xyz/models/EgresoDAO.java +++ b/src/main/java/danielcortes/xyz/models/egreso/EgresoDAO.java @@ -22,7 +22,9 @@ * SOFTWARE. */ -package danielcortes.xyz.models; +package danielcortes.xyz.models.egreso; + +import danielcortes.xyz.models.egreso.Egreso; import java.util.List; diff --git a/src/main/java/danielcortes/xyz/models/mysql/MysqlEgresoDAO.java b/src/main/java/danielcortes/xyz/models/egreso/MysqlEgresoDAO.java similarity index 87% rename from src/main/java/danielcortes/xyz/models/mysql/MysqlEgresoDAO.java rename to src/main/java/danielcortes/xyz/models/egreso/MysqlEgresoDAO.java index 7f5a074..9d8628a 100644 --- a/src/main/java/danielcortes/xyz/models/mysql/MysqlEgresoDAO.java +++ b/src/main/java/danielcortes/xyz/models/egreso/MysqlEgresoDAO.java @@ -22,13 +22,12 @@ * SOFTWARE. */ -package danielcortes.xyz.models.mysql; +package danielcortes.xyz.models.egreso; import danielcortes.xyz.data.MysqlConnection; -import danielcortes.xyz.models.Egreso; -import danielcortes.xyz.models.EgresoDAO; -import danielcortes.xyz.models.TipoEgreso; -import danielcortes.xyz.models.TipoEgresoDAO; +import danielcortes.xyz.models.tipo_egreso.TipoEgreso; +import danielcortes.xyz.models.tipo_egreso.TipoEgresoDAO; +import danielcortes.xyz.models.tipo_egreso.MysqlTipoEgresoDAO; import java.sql.Connection; import java.sql.PreparedStatement; @@ -52,7 +51,7 @@ public class MysqlEgresoDAO implements EgresoDAO { PreparedStatement ps = conn.prepareStatement("select * from egresos"); ResultSet rs = ps.executeQuery(); - egresoList = this.EgresosFromResultSet(rs); + egresoList = this.egresosFromResultSet(rs); rs.close(); ps.close(); @@ -72,7 +71,7 @@ public class MysqlEgresoDAO implements EgresoDAO { ps.setInt(1,id); ResultSet rs = ps.executeQuery(); - egresoList = this.EgresosFromResultSet(rs); + egresoList = this.egresosFromResultSet(rs); rs.close(); ps.close(); @@ -92,7 +91,7 @@ public class MysqlEgresoDAO implements EgresoDAO { ps.setString(1, nro); ResultSet rs = ps.executeQuery(); - egresoList = this.EgresosFromResultSet(rs); + egresoList = this.egresosFromResultSet(rs); rs.close(); ps.close(); @@ -108,13 +107,19 @@ public class MysqlEgresoDAO implements EgresoDAO { int updates; try { Connection conn = mysqlConnection.getConnection(); - PreparedStatement ps = conn.prepareStatement("insert into egresos (nro, descripcion, valor, tipo_id) values (?,?,?,?)"); + PreparedStatement ps = conn.prepareStatement("insert into egresos (nro, descripcion, valor, tipo_egreso_id) values (?,?,?,?)"); ps.setString(1,egreso.getNro()); ps.setString(2,egreso.getDescripcion()); ps.setInt(3,egreso.getValor()); ps.setInt(4,egreso.getTipo().getId()); updates = ps.executeUpdate(); ps.close(); + + ps = conn.prepareStatement("select last_insert_id()"); + ResultSet rs = ps.executeQuery(); + rs.next(); + egreso.setId(rs.getInt(1)); + conn.close(); } catch (SQLException e) { e.printStackTrace(); @@ -128,7 +133,7 @@ public class MysqlEgresoDAO implements EgresoDAO { int updates; try { Connection conn = mysqlConnection.getConnection(); - PreparedStatement ps = conn.prepareStatement("update egresos set nro = ?, descripcion = ?, valor = ?, tipo_id = ? where id = ? "); + PreparedStatement ps = conn.prepareStatement("update egresos set nro = ?, descripcion = ?, valor = ?, tipo_egreso_id = ? where id = ? "); ps.setString(1,egreso.getNro()); ps.setString(2,egreso.getDescripcion()); ps.setInt(3,egreso.getValor()); @@ -181,10 +186,10 @@ public class MysqlEgresoDAO implements EgresoDAO { return total; } - private List EgresosFromResultSet(ResultSet rs) throws SQLException { + private List egresosFromResultSet(ResultSet rs) throws SQLException { ArrayList egresoList = new ArrayList<>(); while(rs.next()){ - int tipoEgresoId = rs.getInt("tipo_id"); + int tipoEgresoId = rs.getInt("tipo_egreso_id"); TipoEgresoDAO tipoEgresoDAO = new MysqlTipoEgresoDAO(); TipoEgreso tipoEgreso = tipoEgresoDAO.findById(tipoEgresoId).get(0); diff --git a/src/main/java/danielcortes/xyz/models/ingreso/Ingreso.java b/src/main/java/danielcortes/xyz/models/ingreso/Ingreso.java new file mode 100644 index 0000000..0cfd06f --- /dev/null +++ b/src/main/java/danielcortes/xyz/models/ingreso/Ingreso.java @@ -0,0 +1,70 @@ +/* + * 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.models.ingreso; + +import danielcortes.xyz.models.tipo_ingreso.TipoIngreso; + +public class Ingreso { + private int id; + private int valor; + private TipoIngreso tipoIngreso; + + public Ingreso(int id, int valor, TipoIngreso tipoIngreso ) { + this.id = id; + this.valor = valor; + this.tipoIngreso = tipoIngreso; + } + + public Ingreso( int valor, TipoIngreso tipoIngreso) { + this.valor = valor; + this.tipoIngreso = tipoIngreso; + } + + public Ingreso(){} + + public int getId() { + return id; + } + + public void setId(int id) { + this.id = id; + } + + public TipoIngreso getTipoIngreso() { + return tipoIngreso; + } + + public void setTipoIngreso(TipoIngreso tipoIngreso) { + this.tipoIngreso = tipoIngreso; + } + + public int getValor() { + return valor; + } + + public void setValor(int valor) { + this.valor = valor; + } +} diff --git a/src/main/java/danielcortes/xyz/models/TipoEgreso.java b/src/main/java/danielcortes/xyz/models/ingreso/IngresoDAO.java similarity index 67% rename from src/main/java/danielcortes/xyz/models/TipoEgreso.java rename to src/main/java/danielcortes/xyz/models/ingreso/IngresoDAO.java index 03270a2..dec3a70 100644 --- a/src/main/java/danielcortes/xyz/models/TipoEgreso.java +++ b/src/main/java/danielcortes/xyz/models/ingreso/IngresoDAO.java @@ -22,36 +22,19 @@ * SOFTWARE. */ -package danielcortes.xyz.models; -public class TipoEgreso { - private int id; - private String nombre; +package danielcortes.xyz.models.ingreso; - public TipoEgreso(int id, String nombre) { - this.nombre = nombre; - } +import danielcortes.xyz.models.tipo_ingreso.TipoIngreso; - public TipoEgreso(){} +import java.util.List; - public String getNombre() { - return nombre; - } - - public void setNombre(String nombre) { - this.nombre = nombre; - } - - public int getId() { - return id; - } - - public void setId(int id) { - this.id = id; - } - - @Override - public String toString(){ - return this.nombre; - } +public interface IngresoDAO { + List findAll(); + List findById(int id); + List findByTipoIngreso(TipoIngreso tipoIngreso); + boolean insertIngreso(Ingreso ingreso); + boolean updateIngreso(Ingreso ingreso); + boolean deleteIngreso(Ingreso ingreso); + int getTotalIngreso(); } diff --git a/src/main/java/danielcortes/xyz/models/ingreso/MysqlIngresoDAO.java b/src/main/java/danielcortes/xyz/models/ingreso/MysqlIngresoDAO.java new file mode 100644 index 0000000..b0c8cb6 --- /dev/null +++ b/src/main/java/danielcortes/xyz/models/ingreso/MysqlIngresoDAO.java @@ -0,0 +1,203 @@ +/* + * 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.models.ingreso; + +import danielcortes.xyz.data.MysqlConnection; +import danielcortes.xyz.models.tipo_ingreso.MysqlTipoIngresoDAO; +import danielcortes.xyz.models.tipo_ingreso.TipoIngreso; +import danielcortes.xyz.models.tipo_ingreso.TipoIngresoDAO; + +import java.sql.Connection; +import java.sql.PreparedStatement; +import java.sql.ResultSet; +import java.sql.SQLException; +import java.util.ArrayList; +import java.util.List; + +public class MysqlIngresoDAO implements IngresoDAO{ + private MysqlConnection mysqlConnection; + + public MysqlIngresoDAO(){ + this.mysqlConnection = new MysqlConnection(); + } + + @Override + public List findAll() { + List ingresosList = new ArrayList<>(); + try { + Connection conn = mysqlConnection.getConnection(); + PreparedStatement ps = conn.prepareStatement("select * from ingresos"); + ResultSet rs = ps.executeQuery(); + + ingresosList = this.ingresosFromResultSet(rs); + + rs.close(); + ps.close(); + conn.close(); + } catch (SQLException e) { + e.printStackTrace(); + } + return ingresosList; + } + + @Override + public List findById(int id) { + List ingresosList = new ArrayList<>(); + try { + Connection conn = mysqlConnection.getConnection(); + PreparedStatement ps = conn.prepareStatement("select * from ingresos where id = ?"); + ps.setInt(1, id); + ResultSet rs = ps.executeQuery(); + + ingresosList = this.ingresosFromResultSet(rs); + + rs.close(); + ps.close(); + conn.close(); + } catch (SQLException e) { + e.printStackTrace(); + } + return ingresosList; + } + + @Override + public List findByTipoIngreso(TipoIngreso tipoIngreso) { + List ingresosList = new ArrayList<>(); + try { + Connection conn = mysqlConnection.getConnection(); + PreparedStatement ps = conn.prepareStatement("select ingresos.* from ingresos inner join tipos_ingreso on (ingresos.tipo_ingreso_id = tipos_ingreso.id) where ingresos.tipo_ingreso_id = ?"); + ps.setInt(1, tipoIngreso.getId()); + ResultSet rs = ps.executeQuery(); + + ingresosList = this.ingresosFromResultSet(rs); + + rs.close(); + ps.close(); + conn.close(); + } catch (SQLException e) { + e.printStackTrace(); + } + return ingresosList; + } + + @Override + public boolean insertIngreso(Ingreso ingreso) { + int updates; + try { + Connection conn = mysqlConnection.getConnection(); + PreparedStatement ps = conn.prepareStatement("insert into ingresos (valor, tipo_ingreso_id) values (?,?)"); + ps.setInt(1, ingreso.getValor()); + ps.setInt(2, ingreso.getTipoIngreso().getId()); + + updates = ps.executeUpdate(); + ps.close(); + + ps = conn.prepareStatement("select last_insert_id()"); + ResultSet rs = ps.executeQuery(); + rs.next(); + ingreso.setId(rs.getInt(1)); + + conn.close(); + } catch (SQLException e) { + e.printStackTrace(); + return false; + } + return updates > 0; + } + + @Override + public boolean updateIngreso(Ingreso ingreso) { + int updates; + try { + Connection conn = mysqlConnection.getConnection(); + PreparedStatement ps = conn.prepareStatement("update ingresos set valor = ? , tipo_ingreso_id = ? where id = ?"); + ps.setInt(1,ingreso.getValor()); + ps.setInt(2, ingreso.getTipoIngreso().getId()); + ps.setInt(3, ingreso.getId()); + updates = ps.executeUpdate(); + ps.close(); + conn.close(); + } catch (SQLException e) { + e.printStackTrace(); + return false; + } + return updates > 0; + } + + @Override + public boolean deleteIngreso(Ingreso ingreso) { + int updates; + try { + Connection conn = mysqlConnection.getConnection(); + PreparedStatement ps = conn.prepareStatement("delete from ingresos where id = ?"); + ps.setInt(1,ingreso.getId()); + updates = ps.executeUpdate(); + ps.close(); + conn.close(); + } catch (SQLException e) { + e.printStackTrace(); + return false; + } + return updates > 0; + } + + @Override + public int getTotalIngreso() { + int total = 0; + try { + Connection conn = mysqlConnection.getConnection(); + PreparedStatement ps = conn.prepareStatement("select sum(valor) from ingresos"); + ResultSet rs = ps.executeQuery(); + + rs.next(); + total = rs.getInt(1); + + rs.close(); + ps.close(); + conn.close(); + } catch (SQLException e) { + e.printStackTrace(); + } + return total; + } + + private List ingresosFromResultSet(ResultSet rs) throws SQLException { + ArrayList ingresosList = new ArrayList<>(); + while(rs.next()){ + int tipoIngresoId = rs.getInt("tipo_ingreso_id"); + TipoIngresoDAO tipoEgresoDAO = new MysqlTipoIngresoDAO(); + TipoIngreso tipoIngreso = tipoEgresoDAO.findById(tipoIngresoId).get(0); + + Ingreso ingreso = new Ingreso(); + + ingreso.setId(rs.getInt("id")); + ingreso.setValor(rs.getInt("valor")); + ingreso.setTipoIngreso(tipoIngreso); + + ingresosList.add(ingreso); + } + return ingresosList; + } +} diff --git a/src/main/java/danielcortes/xyz/models/mysql/MysqlTipoEgresoDAO.java b/src/main/java/danielcortes/xyz/models/tipo_egreso/MysqlTipoEgresoDAO.java similarity index 96% rename from src/main/java/danielcortes/xyz/models/mysql/MysqlTipoEgresoDAO.java rename to src/main/java/danielcortes/xyz/models/tipo_egreso/MysqlTipoEgresoDAO.java index b212480..397c4c4 100644 --- a/src/main/java/danielcortes/xyz/models/mysql/MysqlTipoEgresoDAO.java +++ b/src/main/java/danielcortes/xyz/models/tipo_egreso/MysqlTipoEgresoDAO.java @@ -22,12 +22,9 @@ * SOFTWARE. */ -package danielcortes.xyz.models.mysql; +package danielcortes.xyz.models.tipo_egreso; import danielcortes.xyz.data.MysqlConnection; -import danielcortes.xyz.models.Egreso; -import danielcortes.xyz.models.TipoEgreso; -import danielcortes.xyz.models.TipoEgresoDAO; import java.sql.Connection; import java.sql.PreparedStatement; diff --git a/src/main/java/danielcortes/xyz/models/tipo_egreso/TipoEgreso.java b/src/main/java/danielcortes/xyz/models/tipo_egreso/TipoEgreso.java new file mode 100644 index 0000000..1bf43e1 --- /dev/null +++ b/src/main/java/danielcortes/xyz/models/tipo_egreso/TipoEgreso.java @@ -0,0 +1,86 @@ +/* + * 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. + */ + +/* + * 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.models.tipo_egreso; + +public class TipoEgreso { + private int id; + private String nombre; + + public TipoEgreso(int id, String nombre) { + this.id = id; + this.nombre = nombre; + } + + public TipoEgreso(String nombre) { + this.nombre = nombre; + } + + public TipoEgreso(){} + + public String getNombre() { + return nombre; + } + + public void setNombre(String nombre) { + this.nombre = nombre; + } + + public int getId() { + return id; + } + + public void setId(int id) { + this.id = id; + } + + @Override + public String toString(){ + return this.nombre; + } +} diff --git a/src/main/java/danielcortes/xyz/models/tipo_egreso/TipoEgresoDAO.java b/src/main/java/danielcortes/xyz/models/tipo_egreso/TipoEgresoDAO.java new file mode 100644 index 0000000..cce1816 --- /dev/null +++ b/src/main/java/danielcortes/xyz/models/tipo_egreso/TipoEgresoDAO.java @@ -0,0 +1,60 @@ +/* + * 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. + */ + +/* + * 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.models.tipo_egreso; + +import java.util.List; + +public interface TipoEgresoDAO { + List findAll(); + List findById(int id); + List findByNombre(String nombre); + boolean insertTipoEgreso(TipoEgreso tipoEgreso); + boolean updateTipoEgreso(TipoEgreso tipoEgreso); + boolean deleteTipoEgreso(TipoEgreso tipoEgreso); +} diff --git a/src/main/java/danielcortes/xyz/models/tipo_ingreso/MysqlTipoIngresoDAO.java b/src/main/java/danielcortes/xyz/models/tipo_ingreso/MysqlTipoIngresoDAO.java new file mode 100644 index 0000000..d169cc3 --- /dev/null +++ b/src/main/java/danielcortes/xyz/models/tipo_ingreso/MysqlTipoIngresoDAO.java @@ -0,0 +1,128 @@ +/* + * 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.models.tipo_ingreso; + +import danielcortes.xyz.data.MysqlConnection; + +import java.sql.Connection; +import java.sql.PreparedStatement; +import java.sql.ResultSet; +import java.sql.SQLException; +import java.util.ArrayList; +import java.util.List; + +public class MysqlTipoIngresoDAO implements TipoIngresoDAO{ + private MysqlConnection mysqlConnection; + + public MysqlTipoIngresoDAO(){ + this.mysqlConnection = new MysqlConnection(); + } + + @Override + public List findAll() { + List tiposIngresoList = new ArrayList<>(); + try { + Connection conn = mysqlConnection.getConnection(); + PreparedStatement ps = conn.prepareStatement("select * from tipos_ingreso"); + ResultSet rs = ps.executeQuery(); + + tiposIngresoList = this.tiposIngresoFromResultSet(rs); + + rs.close(); + ps.close(); + conn.close(); + } catch (SQLException e) { + e.printStackTrace(); + } + return tiposIngresoList; + } + + @Override + public List findById(int id) { + List tiposIngresoList = new ArrayList<>(); + try { + Connection conn = mysqlConnection.getConnection(); + PreparedStatement ps = conn.prepareStatement("select * from tipos_ingreso where id = ?"); + ps.setInt(1,id); + ResultSet rs = ps.executeQuery(); + + tiposIngresoList = this.tiposIngresoFromResultSet(rs); + + rs.close(); + ps.close(); + conn.close(); + } catch (SQLException e) { + e.printStackTrace(); + } + return tiposIngresoList; + } + + @Override + public List findByNombre(String nombre) { + List tiposIngresoList = new ArrayList<>(); + try { + Connection conn = mysqlConnection.getConnection(); + PreparedStatement ps = conn.prepareStatement("select * from tipos_ingreso where nombre = ?"); + ps.setString(1,nombre); + ResultSet rs = ps.executeQuery(); + + tiposIngresoList = this.tiposIngresoFromResultSet(rs); + + rs.close(); + ps.close(); + conn.close(); + } catch (SQLException e) { + e.printStackTrace(); + } + return tiposIngresoList; + } + + @Override + public boolean insertTipoIngreso(TipoIngreso tipoEgreso) { + return false; + } + + @Override + public boolean updateTipoIngreso(TipoIngreso tipoEgreso) { + return false; + } + + @Override + public boolean deleteTipoIngreso(TipoIngreso tipoEgreso) { + return false; + } + + + private List tiposIngresoFromResultSet(ResultSet rs) throws SQLException { + ArrayList tiposIngresoList = new ArrayList<>(); + while(rs.next()){ + TipoIngreso tipoIngreso = new TipoIngreso(); + tipoIngreso.setId(rs.getInt("id")); + tipoIngreso.setNombre(rs.getString("nombre")); + tiposIngresoList.add(tipoIngreso); + } + return tiposIngresoList; + } +} diff --git a/src/main/java/danielcortes/xyz/models/tipo_ingreso/TipoIngreso.java b/src/main/java/danielcortes/xyz/models/tipo_ingreso/TipoIngreso.java new file mode 100644 index 0000000..f48f5e5 --- /dev/null +++ b/src/main/java/danielcortes/xyz/models/tipo_ingreso/TipoIngreso.java @@ -0,0 +1,87 @@ +/* + * 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. + */ + +/* + * 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.models.tipo_ingreso; + +public class TipoIngreso { + private int id; + private String nombre; + + public TipoIngreso(int id, String nombre) { + this.id = id; + this.nombre = nombre; + } + + public TipoIngreso(String nombre) { + this.nombre = nombre; + } + + public TipoIngreso() { + } + + public int getId() { + return id; + } + + public void setId(int id) { + this.id = id; + } + + public String getNombre() { + return nombre; + } + + public void setNombre(String nombre) { + this.nombre = nombre; + } + + @Override + public String toString() { + return this.nombre; + } +} diff --git a/src/main/java/danielcortes/xyz/models/TipoEgresoDAO.java b/src/main/java/danielcortes/xyz/models/tipo_ingreso/TipoIngresoDAO.java similarity index 75% rename from src/main/java/danielcortes/xyz/models/TipoEgresoDAO.java rename to src/main/java/danielcortes/xyz/models/tipo_ingreso/TipoIngresoDAO.java index 384a86b..5db3b3a 100644 --- a/src/main/java/danielcortes/xyz/models/TipoEgresoDAO.java +++ b/src/main/java/danielcortes/xyz/models/tipo_ingreso/TipoIngresoDAO.java @@ -22,15 +22,15 @@ * SOFTWARE. */ -package danielcortes.xyz.models; +package danielcortes.xyz.models.tipo_ingreso; import java.util.List; -public interface TipoEgresoDAO { - List findAll(); - List findById(int id); - List findByNombre(String nombre); - boolean insertTipoEgreso(TipoEgreso tipoEgreso); - boolean updateTipoEgreso(TipoEgreso tipoEgreso); - boolean deleteTipoEgreso(TipoEgreso tipoEgreso); +public interface TipoIngresoDAO { + List findAll(); + List findById(int id); + List findByNombre(String nombre); + boolean insertTipoIngreso(TipoIngreso tipoEgreso); + boolean updateTipoIngreso(TipoIngreso tipoEgreso); + boolean deleteTipoIngreso(TipoIngreso tipoEgreso); } diff --git a/src/main/java/danielcortes/xyz/views/EgresosView.form b/src/main/java/danielcortes/xyz/views/EgresosView.form index 0b3c77f..ad28e7d 100644 --- a/src/main/java/danielcortes/xyz/views/EgresosView.form +++ b/src/main/java/danielcortes/xyz/views/EgresosView.form @@ -8,7 +8,7 @@ - + @@ -16,73 +16,9 @@ - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - + @@ -93,85 +29,198 @@ - + + - + - - - - - + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + - + - - - - - - - - + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + diff --git a/src/main/java/danielcortes/xyz/views/EgresosView.java b/src/main/java/danielcortes/xyz/views/EgresosView.java index 1fbefac..60e0dc7 100644 --- a/src/main/java/danielcortes/xyz/views/EgresosView.java +++ b/src/main/java/danielcortes/xyz/views/EgresosView.java @@ -24,16 +24,13 @@ package danielcortes.xyz.views; -import danielcortes.xyz.models.TipoEgreso; +import danielcortes.xyz.models.tipo_egreso.TipoEgreso; import danielcortes.xyz.views.components.EgresosTableModel; import javax.swing.*; -import javax.swing.plaf.basic.BasicComboBoxRenderer; -import java.awt.*; public class EgresosView { public JPanel contentPanel; - private JPanel egresosPanel; private JTable egresosTable; private JButton guardarButton; private JTextField valorField; @@ -98,10 +95,6 @@ public class EgresosView { return egresosTable; } - public void setEgresosTable(JTable egresosTable) { - this.egresosTable = egresosTable; - } - public EgresosTableModel getEgresosTableModel() { return egresosTableModel; } diff --git a/src/main/java/danielcortes/xyz/views/IngresosView.form b/src/main/java/danielcortes/xyz/views/IngresosView.form new file mode 100644 index 0000000..738983e --- /dev/null +++ b/src/main/java/danielcortes/xyz/views/IngresosView.form @@ -0,0 +1,175 @@ + +
+ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +
diff --git a/src/main/java/danielcortes/xyz/views/IngresosView.java b/src/main/java/danielcortes/xyz/views/IngresosView.java new file mode 100644 index 0000000..1429f46 --- /dev/null +++ b/src/main/java/danielcortes/xyz/views/IngresosView.java @@ -0,0 +1,95 @@ +/* + * 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.views; + +import danielcortes.xyz.models.tipo_ingreso.TipoIngreso; +import danielcortes.xyz.views.components.IngresosTableModel; + +import javax.swing.*; + +public class IngresosView { + private JPanel contentPanel; + private JTable ingresosTable; + private JButton guardarButton; + private JButton eliminarButton; + private JTextField totalIngresoField; + private JTextField valorField; + private JComboBox tipoCombo; + private JLabel errorTipoIngreso; + private JLabel errorValor; + + private IngresosTableModel ingresosTableModel; + + private void createUIComponents() { + this.createIngresosTable(); + } + + private void createIngresosTable(){ + this.ingresosTableModel = new IngresosTableModel(); + this.ingresosTable = new JTable(ingresosTableModel); + this.ingresosTable.setAutoCreateRowSorter(true); + this.ingresosTable.setSelectionMode(ListSelectionModel.SINGLE_SELECTION); + } + + public JPanel getContentPanel() { + return contentPanel; + } + + public JTable getIngresosTable() { + return ingresosTable; + } + + public JButton getGuardarButton() { + return guardarButton; + } + + public JButton getEliminarButton() { + return eliminarButton; + } + + public JTextField getTotalIngresoField() { + return totalIngresoField; + } + + public JTextField getValorField() { + return valorField; + } + + public JComboBox getTipoCombo() { + return tipoCombo; + } + + public IngresosTableModel getIngresosTableModel() { + return ingresosTableModel; + } + + public JLabel getErrorTipoIngreso() { + return errorTipoIngreso; + } + + public JLabel getErrorValor() { + return errorValor; + } +} diff --git a/src/main/java/danielcortes/xyz/views/components/EgresosTableModel.java b/src/main/java/danielcortes/xyz/views/components/EgresosTableModel.java index b74d0ee..add81d9 100644 --- a/src/main/java/danielcortes/xyz/views/components/EgresosTableModel.java +++ b/src/main/java/danielcortes/xyz/views/components/EgresosTableModel.java @@ -24,7 +24,7 @@ package danielcortes.xyz.views.components; -import danielcortes.xyz.models.Egreso; +import danielcortes.xyz.models.egreso.Egreso; import javax.swing.table.AbstractTableModel; import java.util.ArrayList; diff --git a/src/main/java/danielcortes/xyz/views/components/IngresosTableModel.java b/src/main/java/danielcortes/xyz/views/components/IngresosTableModel.java new file mode 100644 index 0000000..4e43a69 --- /dev/null +++ b/src/main/java/danielcortes/xyz/views/components/IngresosTableModel.java @@ -0,0 +1,78 @@ +/* + * 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.views.components; + +import danielcortes.xyz.models.ingreso.Ingreso; + +import javax.swing.table.AbstractTableModel; +import java.util.ArrayList; + +public class IngresosTableModel extends AbstractTableModel { + private String[] columns; + private ArrayList rows; + + public IngresosTableModel(){ + super(); + this.columns = new String[]{"Valor", "Tipo"}; + this.rows = new ArrayList<>(); + } + + public String getColumnName(int col) { + return this.columns[col]; + } + + public int getColumnCount() { + return this.columns.length; + } + + public int getRowCount() { + return this.rows.size(); + } + + public void addRow(Ingreso ingreso) { + this.rows.add(ingreso); + this.fireTableRowsInserted(0,getRowCount()-1); + } + + public void removeRow(int row){ + this.rows.remove(row); + this.fireTableRowsDeleted(0,getRowCount()-1); + } + + public Object getValueAt(int row, int col) { + switch (col) { + case 0: + return this.rows.get(row).getValor(); + case 1: + return this.rows.get(row).getTipoIngreso().getNombre(); + } + return null; + } + + public Ingreso getIngreso(int row){ + return this.rows.get(row); + } + +}