Home (JAVA) Code has no errors, yet output ignores variables
Reply: 1

(JAVA) Code has no errors, yet output ignores variables

cat4hurricane
1#
cat4hurricane Published in 2017-12-07 21:25:48Z

I'm trying to get a running Vending Machine GUI to work, I have it all set up, all the buttons are clickable, yet the output doesn't show me what buttons I clicked (What I want to get) or how much it costs, or what card I've used to pay. It seems like it is ignoring variables.

My Entire code is here:

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

public class FinalProject extends JFrame implements ActionListener {
 private static final long serialVersionUID = 1 L;
 JLabel l1, l2, l3, l4, l5, l6, l7, l8, l9, l10, l11, l12, l13, l14, foodkiosk,
 healthkiosk, sundrieskiosk, shelf1, shelf2, shelf3, shelf4, shelf5, shelf6,
 shelf7, shelf8, shelf9;
 JButton b1, b2, b3, b4, b5;
 JButton potatoes, popcorn, crackers, chipmix, peanuts, chocolate, nutmix,
 healthbar, raisins, cookies, figbar, pretzels;
 JButton aspirin, acetom, vitC, vitB, bandaids, antibiotics, antiitch, gauze,
 toothpaste, toothbrush, dentfloss, adhesive;
 JButton sewingkit, nailkit, facetissue, tolietpaper, handcream,
 bodylotion, deodorant, antiperspirant, schoolshirt, sweatpants, pencilkit,
 handwipes;
 double[] price1 = {
  1.50,
  1.00,
  1.00,
  1.25,
  1.00,
  1.25,
  1.00,
  2.00,
  1.00,
  1.00,
  1.50,
  1.25
 };
 double[] price2 = {
  2.00,
  2.00,
  2.00,
  2.00,
  1.00,
  2.00,
  2.00,
  1.00,
  1.00,
  1.00,
  1.00,
  1.00
 };
 double[] price3 = {
  1.00,
  2.00,
  1.00,
  1.00,
  1.00,
  1.00,
  1.00,
  1.50,
  3.00,
  4.00,
  2.00,
  1.00
 };
 String item = "";
 JTextField t1, t2, t3;
 int potatoes1 = 0;
 int popcorn1 = 0;
 int crackers1 = 0;
 int chipmix1 = 0;
 int peanuts1 = 0;
 int chocolate1 = 0;
 int nutmix1 = 0;
 int healthbar1 = 0;
 int raisins1 = 0;
 int cookies1 = 0;
 int figbar1 = 0;
 int pretzels1 = 0;
 int aspirin1 = 0;
 int acetom1 = 0;
 int vitC1 = 0;
 int vitB1 = 0;
 int bandaids1 = 0;
 int antibiotics1 = 0;
 int antiitch1 = 0;
 int gauze1 = 0;
 int toothpaste1 = 0;
 int toothbrush1 = 0;
 int dentfloss1 = 0;
 int adhesive1 = 0;
 int sewingkit1 = 0;
 int nailkit1 = 0;
 int facetissue1 = 0;
 int tolietpaper1 = 0;
 int handcream1 = 0;
 int bodylotion1 = 0;
 int deodorant1 = 0;
 int antiperspirant1 = 0;
 int schoolshirt1 = 0;
 int sweatpants1 = 0;
 int pencilkit1 = 0;
 int handwipes1 = 0;
 private JRadioButton EDUcard, visa, discover;
 private ButtonGroup group;
 String[] food = {
  "Potato Chips",
  "Popcorn",
  "Crackers",
  "Chip 
  Mix ","
  Peanuts ", "
  Chocolate bar ","
  Mixed nuts ", "
  Health bar ", "
  Raisins ", 
  "Cookies",
  "Fig bars",
  "Pretzels"
 };
 String[] health = {
   "Aspirin",
   "Acetominophen",
   "Vitamin C",
   "Vitamin B 
   Complex ", "
   Bandaids ", "
   Antibiotic Ointment ", "
   Anit - Itch Ointment ", "
   Gauze
   Pad ", "
   Toothpaste ", "
   Toothbrush ", "
   Dental Floss ", "
   Adhesive Tape "};
   String[] sundries = {
    "Sewing Kit",
    "Nail Kit",
    "Facial Tissues",
    "Toliet 
    Tissue ","
    Hand Cream ","
    Body Lotion ","
    Deodorant ","
    Anti - Perspirant ","
    School Tee
    Shirt ","
    Sweat Pants ","
    Pencil Pen Stationary Kit ","
    Handy Wipes ",};

    FinalProject() {
     l1 = new JLabel(" Customer Name");
     l2 = new JLabel("  ");
     l3 = new JLabel(" amount to pay");
     l4 = new JLabel("  ");
     l5 = new JLabel("  ");
     l6 = new JLabel("  ");
     l7 = new JLabel("  ");
     l8 = new JLabel("  ");
     l9 = new JLabel("  ");
     l10 = new JLabel("  ");
     l11 = new JLabel("  ");
     l12 = new JLabel("  ");
     l13 = new JLabel("  ");
     l14 = new JLabel("  ");
     foodkiosk = new JLabel("Food Kiosk");
     healthkiosk = new JLabel("Health Kiosk");
     sundrieskiosk = new JLabel("Sundries Kiosk");

     shelf1 = new JLabel("Shelf 1");
     shelf2 = new JLabel("Shelf 2");
     shelf3 = new JLabel("Shelf 3");
     shelf4 = new JLabel("Shelf 4");
     shelf5 = new JLabel("Shelf 5");
     shelf6 = new JLabel("Shelf 6");
     shelf7 = new JLabel("Shelf 7");
     shelf8 = new JLabel("Shelf 8");
     shelf9 = new JLabel("Shelf 9");
     b1 = new JButton("COMPUTE");
     b2 = new JButton("EXIT");
     potatoes = new JButton("Potato Chips");
     popcorn = new JButton("Popcorn");
     crackers = new JButton("Crackers");
     chipmix = new JButton("Chip Mix");
     peanuts = new JButton("Peanuts");
     chocolate = new JButton("Chocolate");
     nutmix = new JButton("Mixed nuts");
     healthbar = new JButton("Health Bar");
     raisins = new JButton("Raisins");
     cookies = new JButton("Cookies");
     figbar = new JButton("Fig Bars");
     pretzels = new JButton("Pretzals");
     aspirin = new JButton("Aspirin");
     acetom = new JButton("Acetominophen");
     vitC = new JButton("Vitamin C");
     vitB = new JButton("Vitamin B Complex");
     bandaids = new JButton("Bandaids");
     antibiotics = new JButton("Antibiotic Ointment");
     antiitch = new JButton("Anit-Itch Ointment");
     gauze = new JButton("Gauze Pads");
     toothpaste = new JButton("Toothpaste");
     toothbrush = new JButton("Toothbrush");
     dentfloss = new JButton("Dental Floss");
     adhesive = new JButton("Adhesive Tape");
     sewingkit = new JButton("Sewing Kit");
     nailkit = new JButton("Nail Kit");
     facetissue = new JButton("Facial Tissues");
     tolietpaper = new JButton("Toliet Tissue");
     handcream = new JButton("Hand Cream");
     bodylotion = new JButton("Body Lotion");
     deodorant = new JButton("Deodorant");
     antiperspirant = new JButton("Anti-Perspirant");
     schoolshirt = new JButton("School Tee-Shirt");
     sweatpants = new JButton("Sweatpants");
     pencilkit = new JButton("Pencil Pen Stationary Kit");
     handwipes = new JButton("Handy Wipes");

     t1 = new JTextField(10);
     t2 = new JTextField(10);

     EDUcard = new JRadioButton("EDUcard", true);
     visa = new JRadioButton("visa", false);
     discover = new JRadioButton("discover", false);
     group = new ButtonGroup();
     group.add(EDUcard);
     group.add(visa);
     group.add(discover);

     add(l1);
     add(t1);
     add(foodkiosk);
     add(shelf1);
     add(potatoes);
     add(popcorn);
     add(crackers);
     add(chipmix);
     add(l4);
     add(shelf2);
     add(peanuts);
     add(chocolate);
     add(nutmix);
     add(healthbar);
     add(l5);
     add(shelf3);
     add(raisins);
     add(cookies);
     add(figbar);
     add(pretzels);
     add(l8);
     add(healthkiosk);
     add(shelf4);
     add(aspirin);
     add(acetom);
     add(vitC);
     add(vitB);
     add(l9);
     add(shelf5);
     add(bandaids);
     add(antibiotics);
     add(antiitch);
     add(gauze);
     add(l10);
     add(shelf6);
     add(toothpaste);
     add(toothbrush);
     add(dentfloss);
     add(adhesive);
     add(l11);
     add(sundrieskiosk);
     add(shelf7);
     add(sewingkit);
     add(nailkit);
     add(facetissue);
     add(tolietpaper);
     add(l12);
     add(shelf8);
     add(handcream);
     add(bodylotion);
     add(deodorant);
     add(antiperspirant);
     add(l13);
     add(shelf9);
     add(schoolshirt);
     add(sweatpants);
     add(pencilkit);
     add(handwipes);
     add(l7);
     add(EDUcard);
     add(discover);
     add(visa);
     add(l2);
     add(l6);
     add(b1);
     add(b2);
     b1.addActionListener(this);
     b2.addActionListener(e -> System.exit(0));
     potatoes.addActionListener(this);
     popcorn.addActionListener(this);
     crackers.addActionListener(this);
     chipmix.addActionListener(this);
     peanuts.addActionListener(this);
     chocolate.addActionListener(this);
     nutmix.addActionListener(this);
     healthbar.addActionListener(this);
     raisins.addActionListener(this);
     cookies.addActionListener(this);
     figbar.addActionListener(this);
     pretzels.addActionListener(this);
     aspirin.addActionListener(this);
     acetom.addActionListener(this);
     vitC.addActionListener(this);
     vitB.addActionListener(this);
     bandaids.addActionListener(this);
     antibiotics.addActionListener(this);
     antiitch.addActionListener(this);
     gauze.addActionListener(this);
     toothpaste.addActionListener(this);
     toothbrush.addActionListener(this);
     dentfloss.addActionListener(this);
     adhesive.addActionListener(this);
     sewingkit.addActionListener(this);
     nailkit.addActionListener(this);
     facetissue.addActionListener(this);
     tolietpaper.addActionListener(this);
     handcream.addActionListener(this);
     bodylotion.addActionListener(this);
     deodorant.addActionListener(this);
     antiperspirant.addActionListener(this);
     schoolshirt.addActionListener(this);
     sweatpants.addActionListener(this);
     pencilkit.addActionListener(this);
     handwipes.addActionListener(this);
     setSize(800, 800);
     setLayout(new GridLayout(10, 5));
     setTitle("Vending Machine");


     double total = 0.0;
     String card = "discover";
     if (chocolate1 < 5) {
      System.out.println("You need to restock back up to 10");
     }
     if (popcorn1 < 5) {
      System.out.println("You need to restock back up to 10");
     }
     if (peanuts1 < 5) {
      System.out.println("You need to restock back up to 10");
     }
     if (crackers1 < 5) {
      System.out.println("You need to restock back up to 10");
     }
     if (raisins1 < 5) {
      System.out.println("You need to restock back up to 10");
     }
     if (healthbar1 < 5) {
      System.out.println("You need to restock back up to 10");
     }
     if (chipmix1 < 5) {
      System.out.println("You need to restock back up to 10");
     }
     if (potatoes1 < 5) {
      System.out.println("You need to restock back up to 10");
     }
     if (cookies1 < 5) {
      System.out.println("You need to restock back up to 10");
     }
     if (nutmix1 < 5) {
      System.out.println("You need to restock back up to 10");
     }
     if (figbar1 < 5) {
      System.out.println("You need to restock back up to 10");
     }
     if (pretzels1 < 5) {
      System.out.println("You need to restock back up to 10");
     }
     if (adhesive1 < 5) {
      System.out.println("You need to restock back up to 10");
     }
     if (dentfloss1 < 5) {
      System.out.println("You need to restock back up to 10");
     }
     if (sweatpants1 < 5) {
      System.out.println("You need to restock back up to 10");
     }
     if (pencilkit1 < 5) {
      System.out.println("You need to restock back up to 10");
     }
     if (schoolshirt1 < 5) {
      System.out.println("You need to restock back up to 10");
     }
     if (antibiotics1 < 5) {
      System.out.println("You need to restock back up to 10");
     }
     if (antiitch1 < 5) {
      System.out.println("You need to restock back up to 10");
     }
     if (gauze1 < 5) {
      System.out.println("You need to restock back up to 10");
     }
     if (toothbrush1 < 5) {
      System.out.println("You need to restock back up to 10");
     }
     if (vitC1 < 5) {
      System.out.println("You need to restock back up to 10");
     }
     if (toothpaste1 < 5) {
      System.out.println("You need to restock back up to 10");
     }
     if (vitB1 < 5) {
      System.out.println("You need to restock back up to 10");
     }
     if (aspirin1 < 5) {
      System.out.println("You need to restock back up to 10");
     }
     if (acetom1 < 5) {
      System.out.println("You need to restock back up to 10");
     }
     if (bandaids1 < 5) {
      System.out.println("You need to restock back up to 10");
     }
     if (sewingkit1 < 5) {
      System.out.println("You need to restock back up to 10");
     }
     if (nailkit1 < 5) {
      System.out.println("You need to restock back up to 10");
     }
     if (facetissue1 < 5) {
      System.out.println("You need to restock back up to 10");
     }
     if (tolietpaper1 < 5) {
      System.out.println("You need to restock back up to 10");
     }
     if (handcream1 < 5) {
      System.out.println("You need to restock back up to 10");
     }
     if (bodylotion1 < 5) {
      System.out.println("You need to restock back up to 10");
     }
     if (deodorant1 < 5) {
      System.out.println("You need to restock back up to 10");
     }
     if (antiperspirant1 < 5) {
      System.out.println("You need to restock back up to 10");
     }

     if (EDUcard.isSelected() == true) {
      card += " EDUcard will be used to pay";
     }
     if (visa.isSelected() == true) {
      card += " Visa will be used to pay";
     }
     if (discover.isSelected() == true) {
      card += " discover will be used to pay";
     }
     if (popcorn.isSelected() == true) {
      System.out.println("Popcorn");
      total += total + price1[1];
      item = "Popcorn";

     }
     if (potatoes.isSelected() == true) {
      System.out.println("Potato chips");
      total = total + price1[0];
      item = "Potato chips";
     }
     if (crackers.isSelected() == true) {
      total = total + price1[2];
      System.out.println("Crackers");

      item = "Crackers";
     }
     if (chipmix.isSelected() == true) {
      System.out.println("Chip Mix");
      total = total + price1[3];
      item = "Chip Mix";
     }
     if (peanuts.isSelected() == true) {
      System.out.println("Peanuts");
      total = total + price1[4];
      item = "Peanuts";
     }
     if (chocolate.isSelected() == true) {
      System.out.println("Chocolate Bar");
      total = total + price1[5];
      item = "Chocolate Bar";
     }
     if (nutmix.isSelected() == true) {
      System.out.println("Mixed Nuts");
      total = total + price1[6];
      item = "Mixed Nuts";
     }
     if (healthbar.isSelected() == true) {
      System.out.println("Health Bar");
      total = total + price1[7];
      item = "Health Bar";
     }
     if (raisins.isSelected() == true) {
      System.out.println("Raisins");
      total = total + price1[8];
      item
       = "Raisins";
     }
     if (cookies.isSelected() == true) {
      System.out.println("Cookies");
      total = total + price1[9];
      item = "Cookies";
     }
     if (figbar.isSelected() == true) {
      System.out.println("Fig Bars");
      total = total + price1[10];
      item = "Fig Bars";
     }
     if (pretzels.isSelected() == true) {
      System.out.println("Pretzels");
      total = total + price1[11];
      item = "Pretzels";
     }
     if (aspirin.isSelected() == true) {
      System.out.println("Aspirin");
      total = total + price2[0];
      item = "Aspirin";
     }
     if (acetom.isSelected() == true) {
      System.out.println("Acetominophen");
      total = total + price2[1];
      item = "Acetominophen";
     }
     if (vitC.isSelected() == true) {
      System.out.println("Vitamin C");
      total = total + price2[2];
      item = "Vitamin C";
     }
     if (vitB.isSelected() == true) {
      System.out.println("Vitamin B Complex");
      total = total + price2[3];
      item = "Vitamin B Complex";
     }
     if (bandaids.isSelected() == true) {
      System.out.println("Bandaids");
      total = total + price2[4];
      item = "Bandaids";
     }
     if (antibiotics.isSelected() == true) {
      System.out.println("Antibiotic Ointment");
      total = total + price2[5];
      item = "Antibiotic Ointment";
     }
     if (antiitch.isSelected() == true) {
      System.out.println("Anti-Itch Ointment");
      total = total + price2[6];
      item = "Anti-Itch Ointment";
     }
     if (gauze.isSelected() == true) {
      System.out.println("Gauze Pads");
      total = total + price2[7];
      item = "Gauze Pads";
     }
     if (toothpaste.isSelected() == true) {
      System.out.println("Toothpaste");
      total = total + price2[8];
      item = "Toothpaste";
     }
     if (toothbrush.isSelected() == true) {
      System.out.println("Toothbrush");
      total = total + price2[9];
      item = "Toothbrush";
     }
     if (dentfloss.isSelected() == true) {
      System.out.println("Dental Floss");
      total = total + price2[10];
      item = "Dental Floss";
     }
     if (adhesive.isSelected() == true) {
      System.out.println("Adhesive Tape");
      total = total + price2[11];
      item = "Adhesive Tape";
     }
     if (sewingkit.isSelected() == true) {
      System.out.println("Sewing Kit");
      total = total + price3[0];
      item = "Sewing Kit";
     }
     if (nailkit.isSelected() == true) {
      System.out.println("Nail Kit");
      total = total + price3[1];
      item = "Nail Kit";
     }
     if (facetissue.isSelected() == true) {
      System.out.println("Facial Tissues");
      total = total + price3[2];
      item = "Facial Tissue";
     }
     if (tolietpaper.isSelected() == true) {
      System.out.println("Toliet Tissue");
      total = total + price3[3];
      item = "Toliet Tissue";
     }
     if (handcream.isSelected() == true) {
      System.out.println("Hand Cream");
      total = total + price3[4];
      item = "Hand Cream";
     }
     if (bodylotion.isSelected() == true) {
      System.out.println("Body Lotion");
      total = total + price3[5];
      item = "Body Lotion";
     }
     if (deodorant.isSelected() == true) {
      System.out.println("Deodorant");
      total = total + price3[6];
      item = "Deodorant";
     }
     if (antiperspirant.isSelected() == true) {
      System.out.println("Anti-perspirant");
      total = total + price3[7];
      item = "Anti-perspirant";
     }
     if (schoolshirt.isSelected() == true) {
      System.out.println("School Tee Shirt");
      total = total + price3[8];
      item = "School Tee Shirt";
     }
     if (sweatpants.isSelected() == true) {
      System.out.println("Sweatpants");
      total = total + price3[9];
      item = "Sweatpants";
     }
     if (pencilkit.isSelected() == true) {
      System.out.println("Pen Pencil Stationary Kit");
      total = total + price3[10];
      item = "Pen Pencil Stationary Kit";
     }
     if (handwipes.isSelected() == true) {
      System.out.println("Handy Wipes");
      total = total + price3[11];
      item = "Handy Wipes";
     }

    }
    String card = "";
    String a = "";
    int total = 0;
    String message = " Hello! your order is \t " + item + " on " + " " + "total 
    amount: $ " + total +"
    for: \t " +a +"
    you used card: "+ card;
    public void actionPerformed(ActionEvent ae) {
     if (ae.getSource() == b1) {
      String a1 = t1.getText();
      String a = "";

      message = " Hello! your order is \t " + item + " on " + " " + "total amount: $" + total + " for: \t" + a + "you used card: " + card;
      t2.setText("thank you: " + a);
      JOptionPane.showMessageDialog(null, "summary: \n" + message, "Order Summary", JOptionPane.PLAIN_MESSAGE);
      JOptionPane.showMessageDialog(null, "Summary: \n" + message,
       "Order Summary", JOptionPane.PLAIN_MESSAGE);
     }



     if (ae.getSource() == potatoes) {
      potatoes1 = potatoes1 + 1;
     }
     if (ae.getSource() == popcorn) {
      popcorn1 = popcorn1 + 1;
     }
     if (ae.getSource() == chipmix) {
      chipmix1 = chipmix1 + 1;
     }
     if (ae.getSource() == crackers) {
      crackers1 = crackers1 + 1;
     }
     if (ae.getSource() == peanuts) {
      peanuts1 = peanuts1 + 1;
     }
     if (ae.getSource() == chocolate) {
      chocolate1 = chocolate1 + 1;
     }
     if (ae.getSource() == nutmix) {
      nutmix1 = nutmix1 + 1;
     }
     if (ae.getSource() == healthbar) {
      healthbar1 = healthbar1 + 1;
     }
     if (ae.getSource() == raisins) {
      raisins1 = raisins1 + 1;
     }
     if (ae.getSource() == cookies) {
      cookies1 = cookies1 + 1;
     }
     if (ae.getSource() == figbar) {
      figbar1 = figbar1 + 1;
     }
     if (ae.getSource() == pretzels) {
      pretzels1 = pretzels1 + 1;
     }
     if (ae.getSource() == aspirin) {
      aspirin1 = aspirin1 + 1;
     }
     if (ae.getSource() == acetom) {
      acetom1 = acetom1 + 1;
     }
     if (ae.getSource() == vitC) {
      vitC1 = vitC1 + 1;
     }
     if (ae.getSource() == vitB) {
      vitB1 = vitB1 + 1;
     }
     if (ae.getSource() == bandaids) {
      bandaids1 = bandaids1 + 1;
     }
     if (ae.getSource() == antibiotics) {
      antibiotics1 = antibiotics1 + 1;
     }
     if (ae.getSource() == antiitch) {
      antiitch1 = antiitch1 + 1;
     }
     if (ae.getSource() == gauze) {
      gauze1 = gauze1 + 1;
     }
     if (ae.getSource() == toothpaste) {
      toothpaste1 = toothpaste1 + 1;
     }
     if (ae.getSource() == toothbrush) {
      toothbrush1 = toothbrush1 + 1;
     }
     if (ae.getSource() == dentfloss) {
      dentfloss1 = dentfloss1 + 1;
     }
     if (ae.getSource() == adhesive) {
      adhesive1 = adhesive1 + 1;
     }
     if (ae.getSource() == schoolshirt) {
      schoolshirt1 = schoolshirt1 + 1;
     }
     if (ae.getSource() == sweatpants) {
      sweatpants1 = sweatpants1 + 1;
     }
     if (ae.getSource() == sewingkit) {
      sewingkit1 = sewingkit1 + 1;
     }
     if (ae.getSource() == nailkit) {
      nailkit1 = nailkit1 + 1;
     }
     if (ae.getSource() == facetissue) {
      facetissue1 = facetissue1 + 1;
     }
     if (ae.getSource() == tolietpaper) {
      tolietpaper1 = tolietpaper1 + 1;
     }
     if (ae.getSource() == pencilkit) {
      pencilkit1 = pencilkit1 + 1;
     }
     if (ae.getSource() == handwipes) {
      handwipes1 = handwipes1 + 1;
     }
     if (ae.getSource() == handcream) {
      handcream1 = handcream1 + 1;
     }
     if (ae.getSource() == bodylotion) {
      bodylotion1 = bodylotion1 + 1;
     }
     if (ae.getSource() == deodorant) {
      deodorant1 = deodorant1 + 1;
     }
     if (ae.getSource() == antiperspirant) {
      antiperspirant1 = antiperspirant1 + 1;
     }
    }
    public static void main(String[] args) {
     FinalProject a = new FinalProject();
     a.setVisible(true);
     a.setLocation(200, 200);
    }
   } 

The code that is giving me issues, or the program is ignoring is this: https://imgur.com/F0c33AI I expect the results to be what items i've clicked, and what they cost me, plus what card I use to pay.

My code has no known errors, but here is what I get when I go to the compute button: https://imgur.com/shkNHPv

mckuok
2#
mckuok Reply to 2017-12-07 21:33:29Z

All the condition checks (whether the button is pressed or not) is done in the constructor, so the checks are only going to be done once when the object is created in main. What you need to do is to implement ActionListeners such that the checks are done when a button is pressed.

P.S. There is a lot of improvements you need to do for your code.

You need to login account before you can post.

About| Privacy statement| Terms of Service| Advertising| Contact us| Help| Sitemap|
Processed in 0.591356 second(s) , Gzip On .

© 2016 Powered by mzan.com design MATCHINFO