Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Hello, I\'m suppose to write an applet that simulates a soft drink vending machi

ID: 3816888 • Letter: H

Question

Hello, I'm suppose to write

an applet that simulates a soft drink vending machine.  The simulated machine dispenses the following soft drinks:  cola, lemon-lime soda, and bottled water.  These drinks cost $0.75 each to purchase.

When the applet starts, the drink machine should have a supply of 10 of each of the drinks.  The applet should have a text field where the user can enter the amount of money he or she is giving the machine.  The user can then click on a button to select a drink to dispense.  The applet should also display the amount of change it is giving back to the user.  The applet should keep track of its inventory of drinks and inform the user if he or she has selected a drink that is out of stock.  Be sure to handle operator errors such as selecting a drink with no money entered and selecting a drink with an inadequate amount of money entered.

I did get it set up, I've been running into issues with the program. Whenever I input $0.75 to place the purchase, it says that I have insufficient funds. Despite having the products limited to ten, it does work for both the cola and water, but it doesn't work for the lemon-lime.

Also, my applet must be embedded in a webpage. The webpage should have an appropriate title and description of the applet. Below is my program that is a work in progress. If you can help me resolve the issues I'm have, then that will be very appreciative. Thanks. :)

import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.JApplet;
import javax.swing.JButton;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTextField;
public class LeavinesDrinkMachine extends JApplet
{
   // components and variables needed
   int numberOfCola, numberOfLemon, numberOfBottledWater;
   private final double drinkCost = 0.75;
   double total;
   String amount;
  

   private JButton colaButton, lemonLimeSodaButton, waterBottleButton;
   private JLabel moneyLabel, stockLabel;
   private JTextField moneyTF;
   private JPanel buttonsPanel;
  
  
   // init method
@Override
public void init()
{
// number of drinks set to 10
numberOfCola = 10;
numberOfLemon = 10;
numberOfBottledWater = 10;
setSize(455,200);
// call to buildPanel method
buildPanel ();
  
}
       // buildPanel method
       private void buildPanel ()
       {
           // the button components
           colaButton = new JButton ("Cola");
           lemonLimeSodaButton = new JButton ("Lemon Lime Soda");
           waterBottleButton = new JButton("Water Bottle");
          
           // the label components
           moneyLabel = new JLabel("Please enter $0.75 to purchase the desired product: ");
           stockLabel = new JLabel();
          
           // the text field component
           moneyTF = new JTextField (8);
          
           // the panel
           buttonsPanel = new JPanel ();
          
           // set the layout
           buttonsPanel.setLayout(new GridLayout (9, 117));
          
           // components to the panel added
           buttonsPanel.add(moneyLabel);
           buttonsPanel.add(moneyTF);
           buttonsPanel.add(stockLabel);
           buttonsPanel.add(colaButton);
           buttonsPanel.add(lemonLimeSodaButton);
           buttonsPanel.add(waterBottleButton);

           // the action listener
           colaButton.addActionListener(new ButtonActionListner());
           lemonLimeSodaButton.addActionListener(new ButtonActionListner());
           waterBottleButton.addActionListener(new ButtonActionListner());
          
           // add the panel
           add(buttonsPanel);
       }
      
       private class ButtonActionListner implements ActionListener
       {
           public void actionPerformed (ActionEvent ae)
           {
               String str = ae.getActionCommand();
               try {
               total = Double.parseDouble
               (moneyTF.getText());
          
               } catch (Exception e)
               {
   // TODO: handle exception
   if(moneyTF.getText().trim().equalsIgnoreCase(""))
   {
   moneyTF.setText("Enter The Money");
   }
   else
   {
   moneyTF.setText("Sorry, insufficient money");
   return;
   }
   }
                  
               if (total > drinkCost)
               {
                   if (str.equals("Cola"))
                   {
                       if(numberOfCola > 0)
                       {
                           total = Double.parseDouble
                           (moneyTF.getText());
                           total = total - drinkCost;
                           amount = Double.toString(total);
                           numberOfCola--;
                       }
                       else
                       stockLabel.setText("Sorry, Cola is out of stock.");
               }
              
               else if(str.equals("Lemon Lime Soda"))
               {
                   if(numberOfLemon > 0)
                   {
                       total = Double.parseDouble
                       (moneyTF.getText());
                       total = total - drinkCost;
                       amount = Double.toString(total);
                   }
                   else
                   {
                       stockLabel.setText("Sorry, Lemon Lime Soda is out of stock.");
                   }
               }
              
               else if(str.equals("Water Bottle"))
               {
                   if(numberOfBottledWater > 0)
                   {
                       total = Double.parseDouble(moneyTF.getText());
                       total = total - drinkCost;
                       amount = Double.toString(total);
                       numberOfBottledWater--;
                   }
                   else
                   {
                       stockLabel.setText("Sorry, Water Bottle is out of stock.");
                   }
               }
               moneyTF.setText(amount);
           }
           else
           {
               moneyTF.setText("Sorry, insufficient money");
               return;
           }
       }
   }
}

Explanation / Answer

import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JApplet;
import javax.swing.JButton;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTextField;
public class LeavinesDrinkMachine extends JApplet
{
// components and variables needed
int numberOfCola, numberOfLemon, numberOfBottledWater;
private final double drinkCost = 0.75;
double total;
String amount;
  
private JButton colaButton, lemonLimeSodaButton, waterBottleButton;
private JLabel moneyLabel, stockLabel;
private JTextField moneyTF;
private JPanel buttonsPanel;
  
  
// init method
@Override
public void init()
{
// number of drinks set to 10
numberOfCola = 10;
numberOfLemon = 10;
numberOfBottledWater = 10;
setSize(455,200);
// call to buildPanel method
buildPanel ();
  
}
// buildPanel method
private void buildPanel ()
{
// the button components
colaButton = new JButton ("Cola");
lemonLimeSodaButton = new JButton ("Lemon Lime Soda");
waterBottleButton = new JButton("Water Bottle");
  
// the label components
moneyLabel = new JLabel("Please enter $0.75 to purchase the desired product: ");
stockLabel = new JLabel();
  
// the text field component
moneyTF = new JTextField (8);
  
// the panel
buttonsPanel = new JPanel ();
  
// set the layout
buttonsPanel.setLayout(new GridLayout (9, 117));
  
// components to the panel added
buttonsPanel.add(moneyLabel);
buttonsPanel.add(moneyTF);
buttonsPanel.add(stockLabel);
buttonsPanel.add(colaButton);
buttonsPanel.add(lemonLimeSodaButton);
buttonsPanel.add(waterBottleButton);
// the action listener
colaButton.addActionListener(new ButtonActionListner());
lemonLimeSodaButton.addActionListener(new ButtonActionListner());
waterBottleButton.addActionListener(new ButtonActionListner());
  
// add the panel
add(buttonsPanel);
}
  
private class ButtonActionListner implements ActionListener
{
public void actionPerformed (ActionEvent ae)
{
String str = ae.getActionCommand();
try {
total = Double.parseDouble
(moneyTF.getText());
  
} catch (Exception e)
{
// TODO: handle exception
if(moneyTF.getText().trim().equalsIgnoreCase(""))
{
moneyTF.setText("Enter The Money");
}
else
{
moneyTF.setText("Sorry, insufficient money");
return;
}
}
  
if (total - drinkCost <=0)
{
if (str.equals("Cola"))
{
if(numberOfCola > 0)
{
total = Double.parseDouble(moneyTF.getText());
total = total - drinkCost;
total= Math.abs(total);
if(amount!=null )
   total = total+Double.parseDouble(amount);
amount = Double.toString(total);
System.out.println(amount);
numberOfCola--;
}
else
stockLabel.setText("Sorry, Cola is out of stock.");
}
  
else if(str.equals("Lemon Lime Soda"))
{
if(numberOfLemon > 0)
{
total = Double.parseDouble
(moneyTF.getText());
total = total - drinkCost;
total= Math.abs(total);
if(amount!=null )
   total = total+Double.parseDouble(amount);
amount = Double.toString(total);
System.out.println(amount);
}
else
{
stockLabel.setText("Sorry, Lemon Lime Soda is out of stock.");
}
}
  
else if(str.equals("Water Bottle"))
{
if(numberOfBottledWater > 0)
{
total = Double.parseDouble(moneyTF.getText());
total = total - drinkCost;
total= Math.abs(total);
if(amount!=null )
   total = total+Double.parseDouble(amount);
amount = Double.toString(total);
System.out.println(amount);
numberOfBottledWater--;
}
else
{
stockLabel.setText("Sorry, Water Bottle is out of stock.");
}
}
moneyTF.setText(amount);
}
else
{
moneyTF.setText("Sorry, insufficient money");
return;
}
}
}
}

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote