0

My teacher wants me to create a Java program that functions like a vending machine. She wants it to have multidimensional arrays and one-dimensional arrays. I already created the program but I failed because it doesn't have any array in it. It works perfectly fine but there are no arrays in it that's why I failed. She gave me another chance until tomorrow morning. I am really bad at using arrays, to be honest, and I don't really know how to use them in GUI apps. Thank you so much in advance for your help. Below is my code and the output of the current program I have so far.

import java.awt.EventQueue;
import javax.swing.ImageIcon;
import javax.swing.JFrame;
import javax.swing.JPanel;
import java.awt.SystemColor;
import javax.swing.JLabel;
import javax.swing.JButton;
import java.awt.Font;
import java.awt.Cursor;
import javax.swing.JTextField;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
import javax.swing.JTextArea;
import javax.swing.JRadioButton;

public class Finals {

private JFrame frame;
private JTextField itemField;
private JTextField priceField;
private JButton pushButton, button1, button2, button3;
private JRadioButton  day1RB, day2RB, day3RB;
private JTextArea resultsTA;


//----Counter Variables for items A1, A2, and A3 for 3 days with 0 as the initial value
int A1d1=0; int A1d2=0; int A1d3=0;
int A2d1=0; int A2d2=0; int A2d3=0;
int A3d1=0; int A3d2=0; int A3d3=0;

//----Amount Variables for items A1, A2, and A3 for 3 days with 0 as the initial value
int A1d1Amount=0; int A1d2Amount=0; int A1d3Amount=0;
int A2d1Amount=0; int A2d2Amount=0; int A2d3Amount=0;
int A3d1Amount=0; int A3d2Amount=0; int A3d3Amount=0;

//----Variables for items B1, B2, and B3 for 3 days with 0 as the initial value
int B1d1=0; int B1d2=0; int B1d3=0;
int B2d1=0; int B2d2=0; int B2d3=0;
int B3d1=0; int B3d2=0; int B3d3=0;

//----Amount Variables for items B1, B2, and B3 for 3 days with 0 as the initial value
int B1d1Amount=0; int B1d2Amount=0; int B1d3Amount=0;
int B2d1Amount=0; int B2d2Amount=0; int B2d3Amount=0;
int B3d1Amount=0; int B3d2Amount=0; int B3d3Amount=0;

//----Variables used for storing total sales for all items for 3 days with 0 as the initial value
double A1total = 0; double A2total=0; double A3total=0;
double B1total = 0; double B2total=0; double B3total=0;


/**
 * Launching the application.
 */
public static void main(String[] args) {
    EventQueue.invokeLater(new Runnable() {
        public void run() {
            try {
                Finals window = new Finals();
                window.frame.setVisible(true);
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    });
}

/**
 * Creating the application.
 */
public Finals() {
    initialize();
}

/**
 * Initialize the contents of the frame.
 */
private void initialize() {
    frame = new JFrame();
    frame.setTitle("Vending Machine");
    frame.setResizable(false);
    frame.setBounds(100, 100, 914, 334);
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    frame.getContentPane().setLayout(null);
    
    JPanel itemsPanel = new JPanel();
    itemsPanel.setBackground(SystemColor.activeCaption);
    itemsPanel.setBounds(10, 11, 264, 191);
    frame.getContentPane().add(itemsPanel);
    itemsPanel.setLayout(null);
    
    JLabel a1Label = new JLabel();
    a1Label.setBounds(10, 11, 60, 57);
    ImageIcon a1Icon = new ImageIcon("cookie.png");
    a1Label.setIcon(a1Icon);
    itemsPanel.add(a1Label);
    
    JLabel a2Label = new JLabel();
    a2Label.setBounds(102, 11, 60, 57);
    ImageIcon a2Icon = new ImageIcon("gum.png");
    a2Label.setIcon(a2Icon);
    itemsPanel.add(a2Label);
    
    JLabel a3Label = new JLabel();
    a3Label.setBounds(195, 11, 60, 57);
    ImageIcon a3Icon = new ImageIcon("pretzel.png");
    a3Label.setIcon(a3Icon);
    itemsPanel.add(a3Label);
    
    JLabel b1Label = new JLabel("");
    b1Label.setBounds(10, 100, 60, 57);
    ImageIcon b1Icon = new ImageIcon("pretzel.png");
    b1Label.setIcon(b1Icon);
    itemsPanel.add(b1Label);
    
    JLabel b2Label = new JLabel();
    b2Label.setBounds(102, 100, 60, 57);
    ImageIcon b2Icon = new ImageIcon("cookie.png");
    b2Label.setIcon(b2Icon);
    itemsPanel.add(b2Label);
    
    JLabel b3Label = new JLabel();
    b3Label.setBounds(195, 100, 60, 57);
    ImageIcon b3Icon = new ImageIcon("soda.png");
    b3Label.setIcon(b3Icon);
    itemsPanel.add(b3Label);
    
    JLabel a1lbl = new JLabel("A1");
    a1lbl.setFont(new Font("Tahoma", Font.BOLD, 13));
    a1lbl.setBounds(27, 63, 25, 26);
    itemsPanel.add(a1lbl);
    
    JLabel a2lbl = new JLabel("A2");
    a2lbl.setFont(new Font("Tahoma", Font.BOLD, 13));
    a2lbl.setBounds(120, 63, 25, 26);
    itemsPanel.add(a2lbl);
    
    JLabel a3lbl = new JLabel("A3");
    a3lbl.setFont(new Font("Tahoma", Font.BOLD, 13));
    a3lbl.setBounds(213, 63, 25, 26);
    itemsPanel.add(a3lbl);
    
    JLabel b1lbl = new JLabel("B1");
    b1lbl.setFont(new Font("Tahoma", Font.BOLD, 13));
    b1lbl.setBounds(27, 154, 25, 26);
    itemsPanel.add(b1lbl);
    
    JLabel b2lbl = new JLabel("B2");
    b2lbl.setFont(new Font("Tahoma", Font.BOLD, 13));
    b2lbl.setBounds(120, 154, 25, 26);
    itemsPanel.add(b2lbl);
    
    JLabel b3lbl = new JLabel("B3");
    b3lbl.setFont(new Font("Tahoma", Font.BOLD, 13));
    b3lbl.setBounds(213, 154, 25, 26);
    itemsPanel.add(b3lbl);
    
    pushButton = new JButton("PUSH");
    pushButton.addActionListener(new ActionListener() {
        public void actionPerformed(ActionEvent event) {
            String selectedItem = itemField.getText();
            if(selectedItem.equals("A1") && day1RB.isSelected()) {
                A1d1Amount=A1d1Amount+10;
                A1d1+=1; A1total = A1d1Amount+A1d2Amount+A1d3Amount;
            }
            else if(selectedItem.equals("A1") && day2RB.isSelected()) {
                A1d2Amount=A1d2Amount+10;
                A1d2+=1; A1total = A1d1Amount+A1d2Amount+A1d3Amount;
            }else if(selectedItem.equals("A1") && day3RB.isSelected()) {
                A1d3Amount=A1d3Amount+10;
                A1d3+=1; A1total = A1d1Amount+A1d2Amount+A1d3Amount;
            }
            else if(selectedItem.equals("A2") && day1RB.isSelected()) {
                A2d1Amount=A2d1Amount+20;
                A2d1+=1; A2total = A2d1Amount+A2d2Amount+A2d3Amount;
            }
            else if(selectedItem.equals("A2") && day2RB.isSelected()) {
                A2d2Amount=A2d2Amount+20;
                A2d2+=1; A2total = A2d1Amount+A2d2Amount+A2d3Amount;
            }
            else if(selectedItem.equals("A2") && day3RB.isSelected()) {
                A2d3Amount=A2d3Amount+20;
                A2d3+=1; A2total = A2d1Amount+A2d2Amount+A2d3Amount;
            }
            else if(selectedItem.equals("A3") && day1RB.isSelected()) {
                A3d1Amount=A3d1Amount+30;
                A3d1+=1; A3total = A3d1Amount+A3d2Amount+A3d3Amount;
            }
            else if(selectedItem.equals("A3") && day2RB.isSelected()) {
                A3d2Amount=A3d2Amount+30;
                A3d2+=1; A3total = A3d1Amount+A3d2Amount+A3d3Amount;
            }
            else if(selectedItem.equals("A3") && day3RB.isSelected()) {
                A3d3Amount=A3d3Amount+30;
                A3d3+=1; A3total = A3d1Amount+A3d2Amount+A3d3Amount;
            }
            else if(selectedItem.equals("B1") && day1RB.isSelected()) {
                B1d1Amount=B1d1Amount+15;
                B1d1+=1; B1total = B1d1Amount+B1d2Amount+B1d3Amount;
            }
            else if(selectedItem.equals("B1") && day2RB.isSelected()) {
                B1d2Amount=B1d2Amount+15;
                B1d2+=1; B1total = B1d1Amount+B1d2Amount+B1d3Amount;
            }
            else if(selectedItem.equals("B1") && day3RB.isSelected()) {
                B1d3Amount=B1d3Amount+15;
                B1d3+=1; B1total = B1d1Amount+B1d2Amount+B1d3Amount;
            }
            else if(selectedItem.equals("B2") && day1RB.isSelected()) {
                B2d1Amount=B2d1Amount+25;
                B2d1+=1; B2total = B2d1Amount+B2d2Amount+B2d3Amount;
            }
            else if(selectedItem.equals("B2") && day2RB.isSelected()) {
                B2d2Amount=B2d2Amount+25;
                B2d2+=1; B2total = B2d1Amount+B2d2Amount+B2d3Amount;
            }
            else if(selectedItem.equals("B2") && day3RB.isSelected()) {
                B2d3Amount=B2d3Amount+25;
                B2d3+=1; B2total = B2d1Amount+B2d2Amount+B2d3Amount;
            }
            else if(selectedItem.equals("B3") && day1RB.isSelected()) {
                B3d1Amount=B3d1Amount+35;
                B3d1+=1; B3total = B3d1Amount+B3d2Amount+B3d3Amount;
            }
            else if(selectedItem.equals("B3") && day2RB.isSelected()) {
                B3d2Amount=B3d2Amount+35;
                B3d2+=1; B3total = B3d1Amount+B3d2Amount+B3d3Amount;
            }
            else if(selectedItem.equals("B3") && day3RB.isSelected()) {
                B3d3Amount=B3d3Amount+35;
                B3d3+=1; B3total = B3d1Amount+B3d2Amount+B3d3Amount;
            }
            
            double grandTotal = A1total+A2total+A3total+B1total+B2total+B3total;
            
            resultsTA.setText("Item        Day1        Day2        Day3        Sales Amount\n\n"
            +"A1            "+A1d1+"               "+A1d2+"               "+A1d3+"                ₱"+A1total+"0"
            +"\nA2            "+A2d1+"               "+A2d2+"               "+A2d3+"                ₱"+A2total+"0"
            +"\nA3            "+A3d1+"               "+A3d2+"               "+A3d3+"                ₱"+A3total+"0"
            +"\nB1            "+B1d1+"               "+B1d2+"               "+B1d3+"                ₱"+B1total+"0"
            +"\nB2            "+B2d1+"               "+B2d2+"               "+B2d3+"                ₱"+B2total+"0"
            +"\nB3            "+B3d1+"               "+B3d2+"               "+B3d3+"                ₱"+B3total+"0"
            +"\n\nTotal                                                             ₱"+grandTotal+"0"
            );
            clear();
        }
    });
    pushButton.setEnabled(false);
    pushButton.setCursor(Cursor.getPredefinedCursor(Cursor.HAND_CURSOR));
    pushButton.setFont(new Font("Tahoma", Font.BOLD, 16));
    pushButton.setToolTipText("Press to buy.");
    pushButton.setBounds(10, 207, 264, 34);
    frame.getContentPane().add(pushButton);
    
    itemField = new JTextField();
    itemField.setFont(new Font("Tahoma", Font.BOLD, 16));
    itemField.setEditable(false);
    itemField.setBounds(293, 11, 218, 34);
    frame.getContentPane().add(itemField);
    itemField.setColumns(10);
    
    priceField = new JTextField();
    priceField.setFont(new Font("Tahoma", Font.BOLD, 16));
    priceField.setEditable(false);
    priceField.setColumns(10);
    priceField.setBounds(293, 56, 218, 34);
    frame.getContentPane().add(priceField);
    
    JPanel panel = new JPanel();
    panel.setBackground(SystemColor.activeCaption);
    panel.setBounds(293, 101, 218, 140);
    frame.getContentPane().add(panel);
    panel.setLayout(null);
    
    JLabel label1 = new JLabel("Select a Day of sale first and make");
    label1.setFont(new Font("Tahoma", Font.BOLD, 12));
    label1.setBounds(2, 2, 216, 14);
    panel.add(label1);
    
    JButton aButton = new JButton("A");
    aButton.setEnabled(false);
    aButton.addActionListener(new ActionListener() {
        public void actionPerformed(ActionEvent event) {
            priceField.setText(null);
            pushButton.setEnabled(false);
            itemField.setText("A");
            button1.setEnabled(true);
            button2.setEnabled(true);
            button3.setEnabled(true);
        }
    });
    aButton.setCursor(Cursor.getPredefinedCursor(Cursor.HAND_CURSOR));
    aButton.setFont(new Font("Tahoma", Font.BOLD, 15));
    aButton.setBounds(62, 47, 46, 33);
    panel.add(aButton);
    
    JButton bButton = new JButton("B");
    bButton.setEnabled(false);
    bButton.addActionListener(new ActionListener() {
        public void actionPerformed(ActionEvent event) {
            priceField.setText(null);
            pushButton.setEnabled(false);
            itemField.setText("B");
            button1.setEnabled(true);
            button2.setEnabled(true);
            button3.setEnabled(true);
        }
    });
    bButton.setCursor(Cursor.getPredefinedCursor(Cursor.HAND_CURSOR));
    bButton.setFont(new Font("Tahoma", Font.BOLD, 15));
    bButton.setBounds(118, 47, 46, 33);
    panel.add(bButton);
    
    button1 = new JButton("1");
    button1.addActionListener(new ActionListener() {
        public void actionPerformed(ActionEvent event) {
            pushButton.setEnabled(true);
            String itemVal = itemField.getText();
            itemField.setText(itemVal+"1");
            button1.setEnabled(false);
            button2.setEnabled(false);
            button3.setEnabled(false);
            
            String newVal = itemField.getText();
            if(newVal.equals("A1")) {
                priceField.setText("₱10.00");
            }
            else {
                priceField.setText("₱15.00");
            }
        }
    });
    button1.setCursor(Cursor.getPredefinedCursor(Cursor.HAND_CURSOR));
    button1.setEnabled(false);
    button1.setFont(new Font("Tahoma", Font.BOLD, 15));
    button1.setBounds(10, 91, 46, 33);
    panel.add(button1);
    
    button2 = new JButton("2");
    button2.addActionListener(new ActionListener() {
        public void actionPerformed(ActionEvent event) {
            pushButton.setEnabled(true);
            String itemVal = itemField.getText();
            itemField.setText(itemVal+"2");
            button1.setEnabled(false);
            button2.setEnabled(false);
            button3.setEnabled(false);
            
            String newVal = itemField.getText();
            if(newVal.equals("A2")) {
                priceField.setText("₱20.00");
            }
            else {
                priceField.setText("₱25.00");
            }
        }
    });
    button2.setCursor(Cursor.getPredefinedCursor(Cursor.HAND_CURSOR));
    button2.setEnabled(false);
    button2.setFont(new Font("Tahoma", Font.BOLD, 15));
    button2.setBounds(87, 91, 46, 33);
    panel.add(button2);
    
    button3 = new JButton("3");
    button3.addActionListener(new ActionListener() {
        public void actionPerformed(ActionEvent event) {
            pushButton.setEnabled(true);
            String itemVal = itemField.getText();
            itemField.setText(itemVal+"3");
            button1.setEnabled(false);
            button2.setEnabled(false);
            button3.setEnabled(false);
            
            String newVal = itemField.getText();
            if(newVal.equals("A3")) {
                priceField.setText("₱30.00");
            }
            else {
                priceField.setText("₱35.00");
            }
        }
    });
    button3.setCursor(Cursor.getPredefinedCursor(Cursor.HAND_CURSOR));
    button3.setEnabled(false);
    button3.setFont(new Font("Tahoma", Font.BOLD, 15));
    button3.setBounds(162, 91, 46, 33);
    panel.add(button3);
    
    JLabel label2 = new JLabel("a selection from the items.");
    label2.setFont(new Font("Tahoma", Font.BOLD, 12));
    label2.setBounds(2, 22, 171, 14);
    panel.add(label2);
    
    resultsTA = new JTextArea();
    resultsTA.setEditable(false);
    resultsTA.setBounds(521, 11, 368, 230);
    frame.getContentPane().add(resultsTA);
    
    day1RB = new JRadioButton("Day 1");
    day1RB.addActionListener(new ActionListener() {
        public void actionPerformed(ActionEvent event) {
            aButton.setEnabled(true);
            bButton.setEnabled(true);
            day2RB.setSelected(false);
            day3RB.setSelected(false);
        }
    });
    day1RB.setCursor(Cursor.getPredefinedCursor(Cursor.HAND_CURSOR));
    day1RB.setFont(new Font("Tahoma", Font.BOLD, 12));
    day1RB.setBounds(531, 248, 64, 23);
    frame.getContentPane().add(day1RB);
    
    day2RB = new JRadioButton("Day 2");
    day2RB.addActionListener(new ActionListener() {
        public void actionPerformed(ActionEvent event) {
            aButton.setEnabled(true);
            bButton.setEnabled(true);
            day1RB.setSelected(false);
            day3RB.setSelected(false);
        }
    });
    day2RB.setCursor(Cursor.getPredefinedCursor(Cursor.HAND_CURSOR));
    day2RB.setFont(new Font("Tahoma", Font.BOLD, 12));
    day2RB.setBounds(665, 248, 64, 23);
    frame.getContentPane().add(day2RB);
    
    day3RB = new JRadioButton("Day 3");
    day3RB.addActionListener(new ActionListener() {
        public void actionPerformed(ActionEvent event) {
            aButton.setEnabled(true);
            bButton.setEnabled(true);
            day1RB.setSelected(false);
            day2RB.setSelected(false);
        }
    });
    day3RB.setCursor(Cursor.getPredefinedCursor(Cursor.HAND_CURSOR));
    day3RB.setFont(new Font("Tahoma", Font.BOLD, 12));
    day3RB.setBounds(797, 249, 64, 23);
    frame.getContentPane().add(day3RB);
}



//------Clearing everything after transaction
void clear() {
    itemField.setText(null);
    priceField.setText(null);
    button1.setEnabled(false);
    button2.setEnabled(false);
    button3.setEnabled(false);
    pushButton.setEnabled(false);
}

}

output

Nash
  • 3
  • 2
  • This is an array `int[] yourArray = {0, 0, 0, 0};`, and to replace the first value at index 0 with the number 5 we would use `yourArray[0] = 5;`. So for example rather than `A1d1+=1;` you would use `yourArray[0] +=1;` You need to follow the official java tutorial on collections to learn more about how an array works: https://docs.oracle.com/javase/tutorial/collections/intro/index.html – sorifiend Jun 17 '21 at 04:42
  • Oh, I see. Thanks for the response, Sir. I'll take note of that and hope to solve my issue little by little. – Nash Jun 17 '21 at 04:50
  • See this other answer for more information about how to create an array: https://stackoverflow.com/a/17559021/ – sorifiend Jun 17 '21 at 04:55
  • Thanks for giving me this link, Sir. I already checked it and got some idea on how to properly use arrays. I'm now starting to figure out how I can apply it to my program. – Nash Jun 17 '21 at 05:06
  • 1
    Numbered variables like `A1total` to `A3total` are indicators for one-dimensional arrays, double numbered variables like `A1d1` to `A3d3` are indicators for two dimensional arrays: `double[] Atotal = new double[3];` and `int[][] Acount = new int[3][3];`. One difference is that with numbered variables you can start numbering with 1 (or any other value you like), array indices always start at 0. – Thomas Kläger Jun 17 '21 at 05:11
  • That's a great idea, Sir. I can actually try that. Thanks for the example, Sir. It gave me additional useful information. – Nash Jun 17 '21 at 05:27

1 Answers1

1

As a tip to get you started, you could replace your 3 by 3 set of variabls:

int A1d1=0; int A1d2=0; int A1d3=0;
int A2d1=0; int A2d2=0; int A2d3=0;
int A3d1=0; int A3d2=0; int A3d3=0;

With a multi dimensional array with 3 columns and 3 rows:

int[][] num = new int[3][3];

Or like this:

int[][] num = {{0,0,0},
               {0,0,0},
               {0,0,0}};

Then to replace the first value in the second column and first row we use:

num[1][0] = newValue;

Be careful because the first value of an array is at position num[0][0] not num[1][1]. So for example, when editing/getting a value the last value in an array it would be at position num[2][2], not at num[3][3]

sorifiend
  • 5,927
  • 1
  • 28
  • 45
  • Wow. I never thought arrays are not that complicated. I'll take note of your answers, Sir, especially those useful examples. I'll be back after I try them in my program. Thanks a lot. – Nash Jun 17 '21 at 05:31
  • 1
    Finally! I made it! Thank you so many guys for all your help. I figured out already how to use arrays, both multidimensional and one-dimensional. Now I can pass this program to my teacher. I made it ahead of time, so thank you once again, Masters! – Nash Jun 17 '21 at 08:03