diff --git a/.mes b/.mes new file mode 100644 index 0000000..60608bd Binary files /dev/null and b/.mes differ diff --git a/bin/.gitignore b/bin/.gitignore index 9bbe4d5..cbd64bd 100644 --- a/bin/.gitignore +++ b/bin/.gitignore @@ -1,3 +1,4 @@ /IniciarSesion/ /VistaControlador/ /Logica/ +/Ficheros/ diff --git a/bin/Logica/Gestion.class b/bin/Logica/Gestion.class index e72e9f5..2cc913e 100644 Binary files a/bin/Logica/Gestion.class and b/bin/Logica/Gestion.class differ diff --git a/bin/Logica/Transaccion.class b/bin/Logica/Transaccion.class index dd3367c..b1cd4c7 100644 Binary files a/bin/Logica/Transaccion.class and b/bin/Logica/Transaccion.class differ diff --git a/src/Ficheros/FicheroBinario.java b/src/Ficheros/FicheroBinario.java new file mode 100644 index 0000000..00bbd60 --- /dev/null +++ b/src/Ficheros/FicheroBinario.java @@ -0,0 +1,39 @@ +package Ficheros; + +import java.io.File; +import java.io.FileInputStream; +import java.io.FileNotFoundException; +import java.io.FileOutputStream; +import java.io.IOException; +import java.io.ObjectInputStream; +import java.io.ObjectOutputStream; + +import Logica.Gestion; +import Logica.Meses; + +public class FicheroBinario { + String directorioGestiones; + String directorioTransacciones; + String directorioMeses; + public FicheroBinario(String nombre) { + this.directorioGestiones=nombre; + } + + public void guardarMeses(Meses mes) { + + } + + public void guardarGestiones(Gestion gestion) throws IOException { + FileOutputStream ficheroSalida = new FileOutputStream(new File(this.directorioGestiones)); + ObjectOutputStream escritor = new ObjectOutputStream(ficheroSalida); + escritor.writeObject(gestion); + escritor.close(); + ficheroSalida.close(); + } + + public void cargarGestiones(Meses meses) throws IOException { + FileInputStream ficheroEntrada = new FileInputStream(new File(this.directorioGestiones)); + ObjectInputStream lector = new ObjectInputStream(ficheroEntrada); + //meses.aniadirGestion(nombre, anio, mes, isPositivo) + } +} diff --git a/src/Logica/Gestion.java b/src/Logica/Gestion.java index 9b683fd..94add3a 100644 --- a/src/Logica/Gestion.java +++ b/src/Logica/Gestion.java @@ -1,14 +1,11 @@ package Logica; -import java.time.Month; -import java.time.Year; +import java.io.Serializable; import java.util.Vector; -public class Gestion{ +public class Gestion implements Serializable{ private Vector gestiones; private float suma; private float total; - private Month mes; - private Year anio; private boolean isPositivo; private String nombre; @@ -55,6 +52,10 @@ public class Gestion{ } } + public boolean esIngreso() { + return this.isPositivo; + } + public void alterarVisibilidad(int elemento) { if(this.gestiones.get(elemento).alterarVisivilidad()) { this.suma += this.gestiones.get(elemento).getDinero(); diff --git a/src/Logica/Meses.java b/src/Logica/Meses.java index 58285a9..1ce8e33 100644 --- a/src/Logica/Meses.java +++ b/src/Logica/Meses.java @@ -1,5 +1,13 @@ package Logica; +import java.io.EOFException; +import java.io.FileInputStream; +import java.io.FileNotFoundException; +import java.io.FileOutputStream; +import java.io.IOException; +import java.io.ObjectInputStream; +import java.io.ObjectOutputStream; +import java.io.Serializable; import java.time.Month; import java.util.ArrayList; @@ -66,9 +74,44 @@ private ArrayList meses; return this.meses.get(this.mesActual).getTotal(); } + public void guardarMeses(String nombre) throws IOException { + FileOutputStream fichero = new FileOutputStream(nombre); + ObjectOutputStream escritor = new ObjectOutputStream(fichero); + for(Mes mes:this.meses) { + for(Gestion gestion:mes.getGestiones()) { + for(Transaccion transaccion:gestion.getElementos()) { + escritor.writeObject(transaccion); + } + } + + } + escritor.close(); + fichero.close(); + } + + public void cargarMeses(String nombre) throws IOException, ClassNotFoundException { + FileInputStream fichero; + try { + fichero = new FileInputStream(nombre); + }catch(FileNotFoundException e){ + return; + } + + ObjectInputStream lector = new ObjectInputStream(fichero); + try { + while(true) { + Transaccion transaccion = (Transaccion)lector.readObject(); + this.aniadirTransaccion(transaccion, transaccion.getGestion().getNombre(), transaccion.getGestion().esIngreso()); + } + }catch(EOFException e) { + lector.close(); + fichero.close(); + } + } + } -class Mes{ +class Mes implements Serializable{ private int anio; private Month mes; ArrayList gestiones; diff --git a/src/Logica/Transaccion.java b/src/Logica/Transaccion.java index 2dc7689..0cb8030 100644 --- a/src/Logica/Transaccion.java +++ b/src/Logica/Transaccion.java @@ -1,25 +1,25 @@ package Logica; +import java.io.Serializable; import java.time.LocalDate; -public class Transaccion { +public class Transaccion implements Serializable{ + /** + * + */ + private static final long serialVersionUID = 7544113192927092049L; private String nombre; private float dinero; private boolean visible; private LocalDate dia; + private Gestion gestion; - public Transaccion(String nombre, float dinero){ - this.nombre = nombre; - this.dinero = dinero; - this.visible = true; - this.dia = LocalDate.now(); - } - - public Transaccion(String nombre, float dinero, LocalDate dia){ + public Transaccion(String nombre, float dinero, LocalDate dia, Gestion gestion){ this.nombre = nombre; this.dinero = dinero; this.visible = true; this.dia = dia; + this.gestion=gestion; } public String toString() { @@ -42,4 +42,12 @@ public class Transaccion { public LocalDate getDia() { return this.dia; } + + public void aniadirse() { + this.gestion.aniadirGasto(this); + } + + public Gestion getGestion() { + return this.gestion; + } } diff --git a/src/VistaControlador/ControladorAniadirVisualizar.java b/src/VistaControlador/ControladorAniadirVisualizar.java index bf0cfff..498c4eb 100644 --- a/src/VistaControlador/ControladorAniadirVisualizar.java +++ b/src/VistaControlador/ControladorAniadirVisualizar.java @@ -18,49 +18,52 @@ public class ControladorAniadirVisualizar implements ActionListener,ChangeListen } public void actionPerformed(ActionEvent e) { - if(e.getActionCommand().equals(this.vista.boton.getActionCommand())) { - String nombre=JOptionPane.showInputDialog("Introduce un nuevo gasto o ingreso"); - if(nombre == null) return; - if(nombre.equals("")) { - JOptionPane.showMessageDialog(null, "Debe introducir algo", "error", JOptionPane.WARNING_MESSAGE); - return; - } - String dinero = JOptionPane.showInputDialog("Introduce a cuanto asciende"); - if(dinero == null) return; - if(dinero.equals("")) { - JOptionPane.showMessageDialog(null, "Debe introducir algo", "error", JOptionPane.WARNING_MESSAGE); - return; - } - - try { - this.vista.aniadirElemento(nombre, Float.parseFloat(dinero),this); - this.vista.menu.panel.actualizarDatos(this.vista.gestiones); - this.vista.menu.panel.revalidate(); - this.vista.menu.panel.repaint(); - }catch (NumberFormatException ex) { - JOptionPane.showMessageDialog(null, "Debe introducir un numero", "error", JOptionPane.WARNING_MESSAGE); - } - }else { - JCheckBox pulsado = (JCheckBox)e.getSource(); - for(int i = 0; i < vista.gestiones.getElementos().size();i++) { - if(vista.gestiones.getElementos().get(i).toString().equals(pulsado.getText())) { - vista.gestiones.alterarVisibilidad(i); + switch(e.getActionCommand()) { + case "Aniadir":{ + String nombre=JOptionPane.showInputDialog("Introduce un nuevo gasto o ingreso"); + if(nombre == null) return; + if(nombre.equals("")) { + JOptionPane.showMessageDialog(null, "Debe introducir algo", "error", JOptionPane.WARNING_MESSAGE); + return; + } + String dinero = JOptionPane.showInputDialog("Introduce a cuanto asciende"); + if(dinero == null) return; + if(dinero.equals("")) { + JOptionPane.showMessageDialog(null, "Debe introducir algo", "error", JOptionPane.WARNING_MESSAGE); + return; + } + + try { + this.vista.aniadirElemento(nombre, Float.parseFloat(dinero),this); this.vista.menu.panel.actualizarDatos(this.vista.gestiones); this.vista.menu.panel.revalidate(); this.vista.menu.panel.repaint(); + }catch (NumberFormatException ex) { + JOptionPane.showMessageDialog(null, "Debe introducir un numero", "error", JOptionPane.WARNING_MESSAGE); + } + break; + } + case "pulsar":{ + JCheckBox pulsado = (JCheckBox)e.getSource(); + for(int i = 0; i < vista.gestiones.getElementos().size();i++) { + if(vista.gestiones.getElementos().get(i).toString().equals(pulsado.getText())) { + vista.gestiones.alterarVisibilidad(i); + this.vista.menu.panel.actualizarDatos(this.vista.gestiones); + this.vista.menu.panel.revalidate(); + this.vista.menu.panel.repaint(); + } } } + } } private void aniadirListeners() { vista.boton.addActionListener(this); vista.boton.setActionCommand("Aniadir"); - int contador = 0; for(JCheckBox check:this.vista.transacciones) { check.addActionListener(this); - check.setActionCommand("pulsar "+contador); - contador++; + check.setActionCommand("pulsar"); } } diff --git a/src/VistaControlador/ControladorPanelLateral.java b/src/VistaControlador/ControladorPanelLateral.java index ffa5cfa..d3d4312 100644 --- a/src/VistaControlador/ControladorPanelLateral.java +++ b/src/VistaControlador/ControladorPanelLateral.java @@ -99,7 +99,6 @@ public class ControladorPanelLateral implements ActionListener, DateChangeListen public void dateChanged(DateChangeEvent arg0) { if(this.mes!=VistaPanelLateral.elegirMes.getDate().getMonth() || this.anio!=VistaPanelLateral.elegirMes.getDate().getYear()) { - System.out.println("Cambia mes"); this.menu.cargarGestiones(VistaPanelLateral.elegirMes.getDate().getYear(), VistaPanelLateral.elegirMes.getDate().getMonth()); this.mes=VistaPanelLateral.elegirMes.getDate().getMonth(); this.anio=VistaPanelLateral.elegirMes.getDate().getYear(); diff --git a/src/VistaControlador/Menu.java b/src/VistaControlador/Menu.java index cf9c71e..5420474 100644 --- a/src/VistaControlador/Menu.java +++ b/src/VistaControlador/Menu.java @@ -3,12 +3,14 @@ package VistaControlador; import java.awt.Dimension; import java.awt.GridBagConstraints; import java.awt.GridBagLayout; +import java.io.IOException; import java.time.LocalDate; import java.time.Month; import java.util.ArrayList; import javax.swing.JButton; import javax.swing.JFrame; +import javax.swing.JOptionPane; import javax.swing.JPanel; import javax.swing.JTabbedPane; import javax.swing.JTextArea; @@ -17,6 +19,7 @@ import javax.swing.event.ChangeListener; import Logica.Gestion; import Logica.Meses; +import Logica.Transaccion; public class Menu extends JFrame{ protected JPanel panelCentral; @@ -24,20 +27,30 @@ public class Menu extends JFrame{ protected VistaAniadirVisualizar ingresos; protected VistaAniadirVisualizar gastos; protected VistaPanelLateral panel; + String nombreDatos; Meses meses; ArrayList pestanias; ArrayList controladores; protected Gestion datosGastos; protected Gestion datosIngresos; public Menu() { + this.nombreDatos = ".mes"; + setDefaultCloseOperation(javax.swing.WindowConstants.DO_NOTHING_ON_CLOSE); + + addWindowListener(new java.awt.event.WindowAdapter() { + public void windowClosing(java.awt.event.WindowEvent evt) { + close(); + } + }); this.pestania = new JTabbedPane(); + GridBagConstraints constrain = new GridBagConstraints(); + this.cargarGestiones(LocalDate.now().getYear(), LocalDate.now().getMonth()); this.cargarPestanias(); this.setLayout(new GridBagLayout()); - GridBagConstraints constrain = new GridBagConstraints(); this.panel = new VistaPanelLateral(constrain, this.meses); this.panelCentral = new JPanel(); - + constrain.fill = GridBagConstraints.VERTICAL; constrain.gridx = 0; constrain.gridy = 0; @@ -49,16 +62,26 @@ public class Menu extends JFrame{ setDefaultCloseOperation(3); setLocationRelativeTo(null); ControladorPanelLateral controlador3 = new ControladorPanelLateral(this.panel, this); - this.pestania.addChangeListener((ChangeListener)->{ - if(this.pestania.getTabCount()<0) { - this.panel.actualizarDatos(meses.getGestionesActuales().get(this.pestania.getSelectedIndex())); - } - }); + this.listenerPestania(); this.add(this.panel); } + private void close(){ + if (JOptionPane.showConfirmDialog(rootPane, "¿Desea guardar el estado?", + "Salir del sistema", JOptionPane.YES_NO_OPTION) == JOptionPane.YES_OPTION) { + try { + this.meses.guardarMeses(this.nombreDatos); + } catch (IOException e) { + // TODO Auto-generated catch block + e.printStackTrace(); + } + } + System.exit(0); + } + private void iniciarMes(int anio, Month mes) { this.pestania.removeAll(); + this.listenerPestania(); meses.aniadirGestion("Ingresos", anio, mes, true); meses.aniadirGestion("Gastos", anio, mes, false); this.pestanias=new ArrayList(); @@ -73,6 +96,15 @@ public class Menu extends JFrame{ void cargarGestiones(int anio, Month mes) { if(this.meses==null) { this.meses=new Meses(); + try { + meses.cargarMeses(this.nombreDatos); + } catch (ClassNotFoundException e) { + // TODO Auto-generated catch block + e.printStackTrace(); + } catch (IOException e) { + // TODO Auto-generated catch block + e.printStackTrace(); + } } this.meses.elegirMes(anio, mes); if(this.meses.getGestionesActuales().size() == 0) { @@ -84,8 +116,17 @@ public class Menu extends JFrame{ private void cargarMes() { this.pestania.removeAll(); - this.pestanias.clear(); - this.controladores.clear(); + this.listenerPestania(); + if(this.pestanias == null) { + this.pestanias = new ArrayList(); + }else { + this.pestanias.clear(); + } + if(this.controladores == null) { + this.controladores = new ArrayList(); + }else { + this.controladores.clear(); + } for(Gestion gestion:this.meses.getGestionesActuales()) { VistaAniadirVisualizar vista = new VistaAniadirVisualizar(this, gestion); ControladorAniadirVisualizar controlador = new ControladorAniadirVisualizar(vista); @@ -98,9 +139,11 @@ public class Menu extends JFrame{ void cargarPestanias() { this.pestania.removeAll(); + this.listenerPestania(); for(VistaAniadirVisualizar vista:this.pestanias) { this.pestania.addTab(vista.getName(),vista); } + this.pestania.setSelectedIndex(0); } void aniadirGestion(String nombre, boolean sumaOResta) { @@ -111,4 +154,19 @@ public class Menu extends JFrame{ this.pestania.addTab(vista.getName(),vista); } + void listenerPestania(){ + if(this.pestania.getChangeListeners().length == 1) { + this.pestania.addChangeListener((ChangeListener)->{ + if(this.pestania.getTabCount()>0) { + try { + this.panel.actualizarDatos(meses.getGestionesActuales().get(this.pestania.getSelectedIndex())); + }catch (Exception e) { + // TODO: handle exception + } + } + }); + } + + } + } diff --git a/src/VistaControlador/VistaAniadirVisualizar.java b/src/VistaControlador/VistaAniadirVisualizar.java index 3f9deea..52f9146 100644 --- a/src/VistaControlador/VistaAniadirVisualizar.java +++ b/src/VistaControlador/VistaAniadirVisualizar.java @@ -54,7 +54,7 @@ public class VistaAniadirVisualizar extends JPanel{ } public void aniadirElemento(String nombre, float dinero, ControladorAniadirVisualizar controlador) { - Transaccion transaccion = new Transaccion(nombre, dinero,panelLateral.getDate()); + Transaccion transaccion = new Transaccion(nombre, dinero,panelLateral.getDate(),this.gestiones); this.gestiones.aniadirGasto(transaccion); JCheckBox check = new JCheckBox(transaccion.toString()); check.setSelected(true); diff --git a/src/VistaControlador/VistaPanelLateral.java b/src/VistaControlador/VistaPanelLateral.java index d1591fd..e992e18 100644 --- a/src/VistaControlador/VistaPanelLateral.java +++ b/src/VistaControlador/VistaPanelLateral.java @@ -25,7 +25,7 @@ public class VistaPanelLateral extends JPanel{ VistaPanelLateral(GridBagConstraints constrain, Meses meses){ this.setPreferredSize(new Dimension(200,200)); this.meses=meses; - this.total = new JTextArea(); + this.total = new JTextArea(String.valueOf(meses.getTotal())); this.gastoEnvio = new JTextArea(); this.mostrarEstadisticas = new JButton("Mostrar grafico del mes"); this.aniadirGestion = new JButton("Aniadir nueva gestión");