Problem with accumulator in Java

0

This is the first time I have requested your help, I am doing this code as a task and I have not managed to get the accumulator "totaldia" to save the data that the "Shutdown" variable sends. I would appreciate it if you make me see where I fail. Thanks.

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


public class Pupuseria_La_Ansiedad extends JFrame {

    public static void main(String[] args) {

        JLabel bienvenida, indicacion, pupusas1, pupusas2, pupusas3, orden, venta;
        JTextField pupas1, pupas2, pupas3;
        JLabel pago, total;


        JFrame frame = new JFrame("Pupusas La Ansiedad");
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.setSize(400, 350);

        JPanel entradaPanel = new JPanel();
        entradaPanel.setBounds(40,20,300,50);
        bienvenida = new JLabel("SISTEMA DE 'PUPUSERIA LA ANSIEDAD'", JLabel.CENTER);
        entradaPanel.add(bienvenida);
        indicacion = new JLabel("INGRESE CANTIDAD DE PUPUSAS", JLabel.CENTER);
        entradaPanel.add(indicacion);

        JPanel pupusasPanel = new JPanel();
        pupusasPanel.setLayout(new GridLayout(3,1));
        pupusasPanel.setBounds(40,80,220,100);
        pupusas1 = new JLabel("Revueltas y Frijol con Queso");
        pupusas1.setFont(new Font ("Verdana",Font.BOLD,12));
        pupusasPanel.add(pupusas1);
        pupusas2 = new JLabel("Queso y Chicharron con Queso");
        pupusas2.setFont(new Font ("Verdana",Font.BOLD,12));
        pupusasPanel.add(pupusas2);
        pupusas3 = new JLabel("Especialidad");
        pupusas3.setFont(new Font ("Verdana",Font.BOLD,12));
        pupusasPanel.add(pupusas3);

        JPanel cantidadPanel = new JPanel();
        cantidadPanel.setLayout(new GridLayout(3,1));
        cantidadPanel.setBounds(270,80,70,100);
        pupas1 = new JTextField("");
        pupas1.setHorizontalAlignment(JTextField.CENTER);
        cantidadPanel.add(pupas1);
        pupas2 = new JTextField("");
        pupas2.setHorizontalAlignment(JTextField.CENTER);
        cantidadPanel.add(pupas2);
        pupas3 = new JTextField("");
        pupas3.setHorizontalAlignment(JTextField.CENTER);
        cantidadPanel.add(pupas3);

        JPanel botonPanel = new JPanel();
        botonPanel.setBounds(60,200,120,50);
        JButton botonOrdenar=new JButton("Enviar Orden");
        botonPanel.add(botonOrdenar);

        JPanel boton2Panel = new JPanel();
        boton2Panel.setBounds(200,200,120,50);
        JButton botonLimpiar=new JButton("Limpiar Orden");
        boton2Panel.add(botonLimpiar);


        JPanel ordenPanel = new JPanel();
        ordenPanel.setBounds(40,250,90,40);
        orden = new JLabel("Esta Orden:");
        orden.setFont(new Font ("Verdana",Font.BOLD,12));
        ordenPanel.add(orden);

        JPanel pagoPanel = new JPanel();
        pagoPanel.setBounds(130,250,50,40);
        pago = new JLabel("");
        pago.setFont(new Font ("Verdana",Font.BOLD,12));
        pagoPanel.add(pago);

        JPanel ventaPanel = new JPanel();
        ventaPanel.setBounds(200,250,80,40);
        venta = new JLabel("Total Día:");
        venta.setFont(new Font ("Verdana",Font.BOLD,12));
        ventaPanel.add(venta);

        JPanel totalPanel = new JPanel();
        totalPanel.setBounds(270,250,80,40);
        total = new JLabel();
        total.setFont(new Font ("Verdana",Font.BOLD,12));
        totalPanel.add(total);

        //Agregar elementos al frame
        Container contenedor=frame.getContentPane();
        contenedor.setLayout(null);
        frame.add(pupusasPanel);
        frame.add(entradaPanel);
        frame.add(cantidadPanel);
        frame.add(botonPanel);
        frame.add(boton2Panel);
        frame.add(ordenPanel);
        frame.add(pagoPanel);
        frame.add(ventaPanel);
        frame.add(totalPanel);

        botonOrdenar.addActionListener(new ActionListener(){
            public void actionPerformed(ActionEvent e){

                int p1, p2, p3;
                double totaldia = 0;

                if(pupas1.getText().length()==0){
                    p1=0;
                }
                else{
                    p1= Integer.parseInt(pupas1.getText());
                }

                if(pupas2.getText().length()==0){
                    p2=0;
                }
                else{
                    p2= Integer.parseInt(pupas2.getText());
                }

                if(pupas3.getText().length()==0){
                    p3=0;
                }
                else{
                    p3= Integer.parseInt(pupas3.getText());
                }

                double Apagar = ((p1*0.50)+(p2*0.55)+(p3*0.75));
                pago.setText("$"+Apagar);

                totaldia = totaldia+Apagar;

                total.setText("$"+totaldia);

                pupas1.setText("");
                pupas2.setText("");
                pupas3.setText("");
            }
        });

        botonLimpiar.addActionListener(new ActionListener(){
            public void actionPerformed(ActionEvent e){
               pago.setText("");
                //total.setText("");
            }
        });

        frame.setVisible(true);
    }
}
    
asked by Jonathan Velasco 28.04.2018 в 16:35
source

1 answer

0

Each time the actionPerformed method is called, the totaldia variable is created and initialized to zero. If what you want is that every time that method is called it is updated all day long you must make a small trap, create a class dedicated only to contain the value of totaldia. Something like this:

public class Totaldia {
private double total;
public Totaldia(double total) {
    // TODO Auto-generated constructor stub
    this.total = total;
}
public double getTotal() {
    return total;
}
public void setTotal(double total) {
    this.total = total;
}

}

Then use a final instance of the Totaldia class instead of your double primitive variable.

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


public class Pupuseria_La_Ansiedad extends JFrame {


public static void main(String[] args) {

    JLabel bienvenida, indicacion, pupusas1, pupusas2, pupusas3, orden, venta;
    JTextField pupas1, pupas2, pupas3;
    JLabel pago, total;
    final Totaldia totaldia = new Totaldia(0);


    JFrame frame = new JFrame("Pupusas La Ansiedad");
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    frame.setSize(400, 350);

    JPanel entradaPanel = new JPanel();
    entradaPanel.setBounds(40,20,300,50);
    bienvenida = new JLabel("SISTEMA DE 'PUPUSERIA LA ANSIEDAD'", JLabel.CENTER);
    entradaPanel.add(bienvenida);
    indicacion = new JLabel("INGRESE CANTIDAD DE PUPUSAS", JLabel.CENTER);
    entradaPanel.add(indicacion);

    JPanel pupusasPanel = new JPanel();
    pupusasPanel.setLayout(new GridLayout(3,1));
    pupusasPanel.setBounds(40,80,220,100);
    pupusas1 = new JLabel("Revueltas y Frijol con Queso");
    pupusas1.setFont(new Font ("Verdana",Font.BOLD,12));
    pupusasPanel.add(pupusas1);
    pupusas2 = new JLabel("Queso y Chicharron con Queso");
    pupusas2.setFont(new Font ("Verdana",Font.BOLD,12));
    pupusasPanel.add(pupusas2);
    pupusas3 = new JLabel("Especialidad");
    pupusas3.setFont(new Font ("Verdana",Font.BOLD,12));
    pupusasPanel.add(pupusas3);

    JPanel cantidadPanel = new JPanel();
    cantidadPanel.setLayout(new GridLayout(3,1));
    cantidadPanel.setBounds(270,80,70,100);
    pupas1 = new JTextField("");
    pupas1.setHorizontalAlignment(JTextField.CENTER);
    cantidadPanel.add(pupas1);
    pupas2 = new JTextField("");
    pupas2.setHorizontalAlignment(JTextField.CENTER);
    cantidadPanel.add(pupas2);
    pupas3 = new JTextField("");
    pupas3.setHorizontalAlignment(JTextField.CENTER);
    cantidadPanel.add(pupas3);

    JPanel botonPanel = new JPanel();
    botonPanel.setBounds(60,200,120,50);
    JButton botonOrdenar=new JButton("Enviar Orden");
    botonPanel.add(botonOrdenar);

    JPanel boton2Panel = new JPanel();
    boton2Panel.setBounds(200,200,120,50);
    JButton botonLimpiar=new JButton("Limpiar Orden");
    boton2Panel.add(botonLimpiar);


    JPanel ordenPanel = new JPanel();
    ordenPanel.setBounds(40,250,90,40);
    orden = new JLabel("Esta Orden:");
    orden.setFont(new Font ("Verdana",Font.BOLD,12));
    ordenPanel.add(orden);

    JPanel pagoPanel = new JPanel();
    pagoPanel.setBounds(130,250,50,40);
    pago = new JLabel("");
    pago.setFont(new Font ("Verdana",Font.BOLD,12));
    pagoPanel.add(pago);

    JPanel ventaPanel = new JPanel();
    ventaPanel.setBounds(200,250,80,40);
    venta = new JLabel("Total Día:");
    venta.setFont(new Font ("Verdana",Font.BOLD,12));
    ventaPanel.add(venta);

    JPanel totalPanel = new JPanel();
    totalPanel.setBounds(270,250,80,40);
    total = new JLabel();
    total.setFont(new Font ("Verdana",Font.BOLD,12));
    totalPanel.add(total);

    //Agregar elementos al frame
    Container contenedor=frame.getContentPane();
    contenedor.setLayout(null);
    frame.add(pupusasPanel);
    frame.add(entradaPanel);
    frame.add(cantidadPanel);
    frame.add(botonPanel);
    frame.add(boton2Panel);
    frame.add(ordenPanel);
    frame.add(pagoPanel);
    frame.add(ventaPanel);
    frame.add(totalPanel);

    botonOrdenar.addActionListener(new ActionListener(){
        public void actionPerformed(ActionEvent e){

            int p1, p2, p3;


            if(pupas1.getText().length()==0){
                p1=0;
            }
            else{
                p1= Integer.parseInt(pupas1.getText());
            }

            if(pupas2.getText().length()==0){
                p2=0;
            }
            else{
                p2= Integer.parseInt(pupas2.getText());
            }

            if(pupas3.getText().length()==0){
                p3=0;
            }
            else{
                p3= Integer.parseInt(pupas3.getText());
            }

            double Apagar = ((p1*0.50)+(p2*0.55)+(p3*0.75));
            pago.setText("$"+Apagar);


            totaldia.setTotal(totaldia.getTotal()+Apagar);

            total.setText("$"+totaldia);

            pupas1.setText("");
            pupas2.setText("");
            pupas3.setText("");
        }
    });

    botonLimpiar.addActionListener(new ActionListener(){
        public void actionPerformed(ActionEvent e){
           pago.setText("");
            //total.setText("");
        }
    });

    frame.setVisible(true);
}
}

I hope it serves you.

Greetings

    
answered by 28.04.2018 / 16:49
source