0

hellow im confusing about the loop that i used into my program because i used do while loop to loop if you want to buy or not . but the problem in this is when i click yes it will stack up . here is my code so far . please help me im very confused :3 .

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

import javax.swing.event.ListSelectionEvent;
import javax.swing.event.ListSelectionListener;
import java.awt.event.TextEvent;
import java.awt.event.TextListener;
public class brie extends JFrame implements ActionListener
{
    public static void main(String args [])
    {

        brie t1 = new brie();
        t1.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        t1.setSize(200,300);
        t1.setVisible(true);
        t1.setLocationRelativeTo(null);
        t1.setResizable(false);
    }
    JPanel panel3 = new JPanel();
    JLabel cos1 = new JLabel("Do you want to buy more ?");
    JButton yy = new JButton("Yes");
    JButton  nn = new JButton("No");
    JFrame want = new JFrame("Buy AGain");
    String namep[] = {
        "---------Chocolate---------", 
        "---------Cake----------", 
        "---------Candy---------", 
        "---------Ice Cream---------", 
        "---------Snacks---------"};
    int pricep [] = {1, 2, 3, 4, 5};
    JPanel panel2 = new JPanel();
    JLabel l = new JLabel("Enter your payment");
    JTextField f = new JTextField(15);
    JButton tru = new JButton("Yes");
    JFrame w = new JFrame("pay");
    JList lst = new JList(namep);
    JLabel l2 = new JLabel("Enter Quantity");
    JTextField t1 = new JTextField(15);
    JLabel l3 = new JLabel();
    JButton b2 = new JButton("Exit");
    JButton b1 = new JButton("Compute");
    JFrame ftinda = new JFrame("Item && Prices");

    double tet;
    Container cong = getContentPane();
    String namep1,totallist;
    double otpc;
    int paym,tot = 0;
    int quan,pricep1;
    int x,grandtotal,change;



    public brie()
    {

        totallist = "";
        namep1 = "";
        //      t1.setEditable(false);
        t1.setText("");             
        cong.setLayout(new FlowLayout());
        cong.add(new JScrollPane(lst));
        cong.add(l2);
        cong.add(t1);
        cong.add(b1);
        //cong.add(b2); 
        cong.add(l3);

        b1.addActionListener(this);
        //b2.addActionListener(this);
        tru.addActionListener(this);
        yy.addActionListener(this);
        nn.addActionListener(this);
        lst.setSelectedIndex(0);
        lst.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
        lst.addListSelectionListener(new ListSelectionListener()
        {
            public void valueChanged(ListSelectionEvent e)
            {
                t1.setEditable(true);
                x = lst.getSelectedIndex();
            }
        });
    }

    public void actionPerformed(ActionEvent e)
    {
        Object sc = e.getSource();
        do{
            if(sc == b1)
            {
                String t = t1.getText();

                if(t!=(""))
                {
                    quan = Integer.parseInt(t1.getText());
                    pricep1 = pricep[x];
                    namep1 = namep[x];
                    tot = pricep1*quan;
                    grandtotal += tot;
                    tet = tot;
                    totallist += namep1 + " "+quan+ "pcs = "+ tet +" pesos ";
                    t1.setText("");
                    t1.setEditable(true);
                    JOptionPane.showMessageDialog(null,
                        ""+namep1+" = "+pricep1+"php\n"+quan+"pcs = "+tot+"php");
                    setVisible(false);

                    panel3.setLayout(null);
                    cos1.setBounds(70,30,150,20);
                    yy.setBounds(80,65,150,20);
                    nn.setBounds(140,65,150,20);
                    yy.setSize(55,30);
                    nn.setSize(55,30);
                    panel3.add(cos1);
                    panel3.add(yy);
                    panel3.add(nn);

                    want.add(panel3);

                    want.setVisible(true);
                    want.setSize(300,200);
                    want.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
                    want.setLayout(new GridLayout());

                    return;
                }                                                                       
            }
            if(sc == nn)
                //if the user dont want to buy it will appear the total price.
            {
                setVisible(false);
                want.setVisible(false);

                JOptionPane.showMessageDialog(null,
                    "the product you choose is "+totallist+ 
                    "the total of"+ grandtotal);

                w.setLayout(new GridLayout());


                w.setSize(300,200);
                w.setVisible(true);
                w.setDefaultCloseOperation(EXIT_ON_CLOSE);
                panel2.setLayout(null);

                l.setBounds(90,20,150,20);
                f.setBounds(60,60,150,20);
                tru.setBounds(100,100,60,50);

                w.add(panel2);
                panel2.add(l);
                panel2.add(f);
                panel2.add(tru);
            }

            if(sc == tru)
            {
                w.setVisible(false);

                paym = Integer.parseInt(f.getText());
                change =paym-grandtotal;
                if(grandtotal <= paym)
                {
                    JOptionPane.showMessageDialog(null,
                        "the product you buy is"+totallist+ 
                        "the total of "+grandtotal+ 
                        "your payment is :"+f.getText() + 
                        "your change is :"+change);
                }

                else if(grandtotal >=  paym)
                {
                    System.out.print("Not enough");
                }

            }
        }while(sc == yy);
        // to loop if the costumer want to buy again.
    }
}
Marco13
  • 52,507
  • 9
  • 77
  • 154
Batusai
  • 149
  • 1
  • 1
  • 12
  • It's not clear what you're asking (what will "stack up"?). Could you try creating a smaller piece of code that still exhibits the problem? Make sure you're not calling addActionListener multiple times. – David Koelle Feb 16 '14 at 15:58
  • my code is runnable sir . look at the "do you want to buy more" part . if i click yes it needed to be loop because i declare in my while(sc == yy); and if i click no it will display the receipt . but if i click yes it will hang in my pc – Batusai Feb 16 '14 at 16:00
  • It's obviously hanging in the `do { ... } while` loop of the `actionPerformed` method. But the code is so poorly structured that it is hard to give a hint of how to resolve this issue without changing (i.e. cleaning up) most of the program. – Marco13 Feb 16 '14 at 21:03
  • @Marco is correct: GUI programming is event driven; your program should respond to the desired events such as the user changing a selection or value; [`Adder`](http://stackoverflow.com/a/8703807/230513) is a basic example. – trashgod Feb 16 '14 at 21:27

0 Answers0