how can i display the information of an array in a window in different method?

i am a second year student and I was asked to do a book purchasing window in java using JFrame, by creating a window that contains a JComboBox with book genres, one JTextField for the user to input the amount of book they need, 3 JRadioButtons for selecting memberships, and two buttons "add" and "buy", when the user is going to choose all the information and click add, all the information should kept in an array, after that they should click on buy and all the kept information shall be displayed in new window and the total price of the books they chose that is multiplied by the amount with a specific discount should be given based on the membership they chose. I did everything except the part where i should display all the information that is written in the array lists inside TextField() method to appear in the empty window that will appear when the customer press "buy" button. can you please help me figure out how to do this

import java.util.ArrayList;
import java.util.Arrays;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.ButtonGroup;
import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JRadioButton;
import javax.swing.JTextField;

public class Assignment1 extends JFrame implements ActionListener {
    private JButton btnAdd,btnBuy;
    private JLabel lblAmount,lblGenre;
    public static  JTextField txt1;
    private JRadioButton rdSilver,rdGold,rdNon;
    public JComboBox C1;
    private JFrame secondFrame;
    static String currentPattern;

    public Assignment1() {

        

        //setting the layout
  
        setLayout(null);
        //buttons
        btnAdd = new JButton("Add");
        btnBuy = new JButton("Buy");
        btnAdd.setSize(100,25);
        btnAdd.setLocation(500,60);//(R/L,U/D)
        btnBuy.setSize(100,25);
        btnBuy.setLocation(500,120);
        add(btnAdd);
        add(btnBuy);
        btnAdd.addActionListener(this);
        btnBuy.addActionListener(this);

        //labels
        lblAmount = new JLabel("Amount");
        lblGenre = new JLabel("Genre");
        lblAmount.setSize(100,25);
        lblAmount.setLocation(300,30);
        lblGenre.setSize(100,25);
        lblGenre.setLocation(40,30);
        add(lblAmount);
        add(lblGenre);
        //textfield
        txt1 = new JTextField();
        txt1.setSize(120,25);
        txt1.setLocation(300,60);
        add(txt1);
        //radioButtons
        rdGold = new JRadioButton("Golden Member");
        rdSilver = new JRadioButton("Silver Member");
        rdNon = new JRadioButton("Non-member");
        rdGold.setSize(150,150);
        rdGold.setLocation(30,130);
        rdSilver.setSize(150,150);
        rdSilver.setLocation(190,130);
        rdNon.setSize(150,150);
        rdNon.setLocation(340,130);
        
        
        
        // sellecting only one
        
        ButtonGroup makeRadioGroup = new ButtonGroup();
        makeRadioGroup.add(rdGold);
        makeRadioGroup.add(rdSilver);
        makeRadioGroup.add(rdNon);
            
        add(rdGold);
        add(rdSilver);
        add(rdNon);
    
        //comboBox
        String Combo1[]={"Sience[10p]","History[15p]","Drama[5p]","Romance[7p]","Action[13p]"};
        JComboBox C1 = new JComboBox(Combo1);
        C1.setSize(130,25);
        C1.setLocation(40,60);
        add(C1);
        currentPattern = Combo1[0];
        C1.addActionListener(this);
        
      
    
        
        
        
// designing the window
        setSize(650,300);
        setVisible(true);
        setTitle("Book Store");
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    }
    
     public static void TextField() {
    
            
            try {
                
                
                String strOne= txt1.getText().trim();
                int nmr= Integer.parseInt(strOne);
                //System.out.println(nmr);
                ArrayList<Integer> arrayList2=new ArrayList<Integer>();
                ArrayList<String> arrayList=new ArrayList<String>();
                
            
                int price=0;
                if (currentPattern=="Sience[10p]") {
                    price=nmr*10;
                    arrayList.add("Book Genre: "+currentPattern+"\n\tThe amount: "+nmr+ "\n\tThe price: "+price);
                    arrayList2.add(price);
                }
                
                else if (currentPattern=="History[15p]") {
                    price=nmr*15;
                    arrayList.add("Book Genre: "+currentPattern+"\n\tThe amount: "+nmr+ "\n\tThe price: "+price);
                    arrayList2.add(price);


                }
                else if (currentPattern=="Romance[7p]") {
                    price=nmr*7;
                    arrayList.add("Book Genre: "+currentPattern+"\n\tThe amount: "+nmr+ "\n\tThe price: "+price);
                    arrayList2.add(price);
                }

                else if(currentPattern=="Drama[5p]") {
                    price=nmr*5;
                    arrayList.add("Book Genre: "+currentPattern+"\n\tThe amount: "+nmr+ "\n\tThe price: "+price);
                    arrayList2.add(price);
                }

                else if (currentPattern=="Action[13p]") {
                    price=nmr*13;
                    arrayList.add("Book Genre: "+currentPattern+"\n\tThe amount: "+nmr+ "\n\tThe price: "+price);
                    arrayList2.add(price);
                }   
            for(String i:arrayList)
                System.out.println(i);
            
            }
            
            catch (NumberFormatException e1) {
                    // TODO: handle exception
                    JOptionPane.showMessageDialog(null, "Please enter the amount", "Warning", JOptionPane.ERROR_MESSAGE);
                
                    }
            
     }
    public static void main(String[] args) {
        // TODO Auto-generated method stub
        new Assignment1();


    }
    

    
    public static void Confirmation() {
        //JOptionPane.showMessageDialog (null,TextField());
        JFrame secondFrame = new JFrame();
          secondFrame.setSize(300,300);
          secondFrame.setVisible(true);
          secondFrame.setTitle("Confirmation");
          secondFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
          
    }
        /*
            int sum = 0;
            for (int i :arrayList2) {
                sum+=i;
    }
        //System.out.println(sum);
            double discount;       
            double total;
            if (rdGold.isSelected()) {
                discount=(100-15);
                total=(discount*sum)/100;
                System.out.println("The total price with 15% discount: "+total);
    }
            else if (rdSilver.isSelected()) {
                discount=(100-10);
                total=(discount*sum)/100;
                System.out.println("The total price with 10% discount: "+total);

    }
            else if (rdNon.isSelected()) {
                total=sum;
            System.out.println("No Discount: "+total);
            }
}
*/
    
    
    
        public void actionPerformed(ActionEvent arg0) {
        // TODO Auto-generated method stub
            Object source = arg0.getSource();
            if (source instanceof JButton) {
                if (arg0.getSource()==btnAdd ) {
                    TextField();
                }
                
                else if (arg0.getSource()== btnBuy) {
                    Confirmation();
                    add(secondFrame);

                }


            
                    
                }
                
            
            else if (source instanceof JComboBox) {
                    JComboBox cb = (JComboBox)arg0.getSource();
                    String newSelcetion = (String) cb.getSelectedItem();
                    currentPattern = newSelcetion;
                    //System.out.println(currentPattern);
            }
                
            }
        }


    
        


Read more here: https://stackoverflow.com/questions/64946584/how-can-i-display-the-information-of-an-array-in-a-window-in-different-method

Content Attribution

This content was originally published by Ghazal lutfi at Recent Questions - Stack Overflow, and is syndicated here via their RSS feed. You can read the original post over there.

%d bloggers like this: