Simulador de Crédito en Java Swing: Código Fuente Paso a Paso

Informática7,45 KB

Simulador de Crédito en Java con Swing

A continuación se presenta el código fuente de un simulador de crédito desarrollado en Java utilizando la biblioteca Swing. El programa permite ingresar el nombre del solicitante, el monto a solicitar y seleccionar el número de cuotas para calcular el valor de cada cuota y el monto total a pagar.

Código fuente

package simulador;
import javax.swing.*;
import java.awt.event.*;
import java.awt.*;

public class Respuesta extends JFrame
{
    private JLabel lblNombre, lblMonto;
    private JTextField txtNombre, txtMonto;
    private JRadioButton rbt1, rbt2, rbt3;
    private ButtonGroup grpCuotas;
    private JButton btnCalcular, btnLimpiar, btnSalir;
    private JPanel pnl1, pnl2, pnl3;

    public Respuesta()
    {
        super("Simulador de Crédito");
        lblNombre = new JLabel("Ingrese nombre ");
        lblMonto = new JLabel("Ingrese monto a solicitar ");
        txtNombre = new JTextField(10);
        txtMonto = new JTextField(10);
        rbt1 = new JRadioButton("entre 1 y 12 cuotas");
        rbt2 = new JRadioButton("entre 13 y 24 cuotas");
        rbt3 = new JRadioButton("entre 25 y 36 cuotas");
        grpCuotas = new ButtonGroup();
        grpCuotas.add(rbt1);
        grpCuotas.add(rbt2);
        grpCuotas.add(rbt3);
        btnCalcular = new JButton("Calcular");
        btnLimpiar = new JButton("Limpiar");
        btnSalir = new JButton("Salir");
        pnl1 = new JPanel();
        pnl1.setLayout(new GridLayout(2, 2));
        pnl1.add(lblNombre);
        pnl1.add(txtNombre);
        pnl1.add(lblMonto);
        pnl1.add(txtMonto);
        pnl2 = new JPanel();
        pnl2.setLayout(new FlowLayout());
        pnl2.add(rbt1);
        pnl2.add(rbt2);
        pnl2.add(rbt3);
        pnl3 = new JPanel();
        pnl3.setLayout(new FlowLayout());
        pnl3.add(btnCalcular);
        pnl3.add(btnLimpiar);
        pnl3.add(btnSalir);
        setLayout(new FlowLayout());
        add(pnl1);
        add(pnl2);
        add(pnl3);
        btnLimpiar.addActionListener(new Manejo());
        btnSalir.addActionListener(new Manejo());
        btnCalcular.addActionListener(new Manejo());
        super.setResizable(true);
        setSize(450, 250);
        setVisible(true);
    }

    public static void main(String args[])
    {
        Respuesta x = new Respuesta();
        x.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    }

    public class Manejo implements ActionListener
    {
        public void actionPerformed(ActionEvent x)
        {
            int m = 0, m_p = 0, cuota = 0, z = 0;
            if (x.getSource() == btnSalir)
            {
                System.exit(0);
            }
            if (x.getSource() == btnLimpiar)
            {
                grpCuotas.clearSelection();
                txtNombre.setText(" ");
                txtMonto.setText(" ");
            }
            if (x.getSource() == btnCalcular)
            {
                if (rbt1.isSelected())
                {
                    try
                    {
                        m = Integer.parseInt(txtMonto.getText());
                        if (m < 1000000)
                        {
                            m_p = m + (m * 10 / 100);
                            cuota = m_p / 12;
                        }
                        if (m >= 1000000 && m <= 3000000)
                        {
                            m_p = m + (m * 20 / 100);
                            cuota = m_p / 12;
                        }
                        if (m > 3000000)
                        {
                            m_p = m + (m * 30 / 100);
                            cuota = m_p / 12;
                        }
                    }
                    catch (NumberFormatException xx)
                    {
                        z = 1;
                        JOptionPane.showMessageDialog(null, "Ingrese un sueldo correcto");
                    }
                    if (z == 0)
                    {
                        JOptionPane.showMessageDialog(null, "Nombre: " + txtNombre.getText() + "\nMonto a solicitar: " + m + "\nNúmero de cuotas: " + rbt1.getText() + "\nValor de la cuota: " + cuota + "\nMonto total: " + m_p);
                    }
                }
            }
            if (x.getSource() == btnCalcular)
            {
                if (rbt2.isSelected())
                {
                    try
                    {
                        m = Integer.parseInt(txtMonto.getText());
                        if (m < 1000000)
                        {
                            m_p = m + (m * 20 / 100);
                            cuota = m_p / 24;
                        }
                        if (m >= 1000000 && m <= 3000000)
                        {
                            m_p = m + (m * 30 / 100);
                            cuota = m_p / 24;
                        }
                        if (m > 3000000)
                        {
                            m_p = m + (m * 40 / 100);
                            cuota = m_p / 24;
                        }
                    }
                    catch (NumberFormatException ss)
                    {
                        z = 1;
                        JOptionPane.showMessageDialog(null, "Ingrese un sueldo correcto");
                    }
                    if (z == 0)
                    {
                        JOptionPane.showMessageDialog(null, "Nombre: " + txtNombre.getText() + "\nMonto a solicitar: " + m + "\nNúmero de cuotas: " + rbt2.getText() + "\nValor de la cuota: " + cuota + "\nMonto total: " + m_p);
                    }
                }
            }
            if (x.getSource() == btnCalcular)
            {
                if (rbt3.isSelected())
                {
                    try
                    {
                        m = Integer.parseInt(txtMonto.getText());
                        if (m < 1000000)
                        {
                            m_p = m + (m * 30 / 100);
                            cuota = m_p / 36;
                        }
                        if (m >= 1000000 && m <= 3000000)
                        {
                            m_p = m + (m * 40 / 100);
                            cuota = m_p / 36;
                        }
                        if (m > 3000000)
                        {
                            m_p = m + (m * 50 / 100);
                            cuota = m_p / 36;
                        }
                    }
                    catch (NumberFormatException xx)
                    {
                        z = 1;
                        JOptionPane.showMessageDialog(null, "Ingrese un sueldo correcto");
                    }
                    if (z == 0)
                    {
                        JOptionPane.showMessageDialog(null, "Nombre: " + txtNombre.getText() + "\nMonto a solicitar: " + m + "\nNúmero de cuotas: " + rbt3.getText() + "\nValor de la cuota: " + cuota + "\nMonto total: " + m_p);
                    }
                }
            }
        }
    }
}

Notas del código

  • La clase Respuesta hereda de JFrame y construye la interfaz gráfica.
  • La clase interna Manejo implementa ActionListener para gestionar los eventos de los botones.
  • Los intereses aplicados varían según el monto solicitado y el número de cuotas seleccionado.

Descargar

PDF Word HTML