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

Design a New Java Project named DrinkMachine . //Write an applet that simulates

ID: 3821971 • Letter: D

Question

Design a New Java Project named DrinkMachine.

//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.

//NOTE:  Your applet must be embedded in a webpage.  The webpage should have an appropriate title and description of the applet.  Feel free to add color and excitement to your webpage.  Be sure to follow the instructions on “How to Create and Deploy an Applet” to add your webpage to your project.  Export your project as usual for submission.

Explanation / Answer

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

public class VendingApp
{
   int order = 0;
   public static void main(String[] args)
   {
       JFrame frame = new VendingFrame();
       frame.setVisible(true);
   }//end of main
}//end VendingApp

class VendingFrame extends JFrame
{
   public VendingFrame()
   {
       // create a constructor to set the frame title
       setTitle("Vending Machine");
       // make the frame not resizeable; make the frame exit on close
       setResizable(false);
       setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
       // create a new VendingPanel and add it to the frame
       JPanel panel = new VendingPanel();
       this.add(panel);
       // use the pack method to set the frame size
       this.pack();
       centerWindow(this);
   }//end of VendingFrame

   private void centerWindow(Window w)
   {
       // code the centerWindow method
       Toolkit tk = Toolkit.getDefaultToolkit();
       Dimension d = tk.getScreenSize();
       setLocation((d.width-w.getWidth())/2, (d.height-w.getHeight())/2);
   }//end of centerWindow
}//end class VendingFrame


class VendingPanel extends JPanel implements ActionListener

{
   Machine m = new Machine(1.00);
   // code the instance variables for the text fields, labels, buttons, radio buttons and check boxes

   private JButton   colaButton,
                       lemonlimeButton,
                       bottledwaterButton,
                       refundButton;
   private JLabel   priceLabel;
   private JTextField amountTextField;
NumberFormat currency = NumberFormat.getCurrencyInstance();

   public VendingPanel()
   {

       m.lbeverages(0, new Beverage("Cola", m.QUANITY));
               m.lbeverages(1, new Beverage("Lemon Lime Soda", m.QUANITY));
               m.lbeverages(2, new Beverage("Bottled Water", m.QUANITY));

       // set your flow layout
       setLayout(new GridBagLayout());

       Border loweredBorder =
           BorderFactory.createBevelBorder(BevelBorder.LOWERED);


       // create and add a Vending panel with the approriate components
       JPanel VendingPanel = new JPanel();
       VendingPanel.setLayout(new GridBagLayout());
       VendingPanel.setBorder(BorderFactory.createTitledBorder(loweredBorder, "Sodas"));


       //soda selection
       colaButton = new JButton("Cola");
       setWidth(colaButton, 125);
       colaButton.addActionListener(this);
       VendingPanel.add(colaButton,
           getConstraints(1,0,1,1, GridBagConstraints.WEST));
   ///*******

       lemonlimeButton = new JButton("Lemon Lime Soda");
       lemonlimeButton.addActionListener(this);
       setWidth(lemonlimeButton, 125);
       VendingPanel.add(lemonlimeButton,
           getConstraints(1,1,1,1, GridBagConstraints.WEST));
   //***

       bottledwaterButton = new JButton("Bottled Water");
       bottledwaterButton.addActionListener(this);
       setWidth(bottledwaterButton, 125);
       VendingPanel.add(bottledwaterButton,
           getConstraints(1,2,1,1, GridBagConstraints.WEST));


       refundButton = new JButton("Refund");
       refundButton.addActionListener(this);
       setWidth(refundButton, 100);
       VendingPanel.add(refundButton,
           getConstraints(0,3,1,1, GridBagConstraints.WEST));

       // create and add a price text box
               amountTextField = new JTextField(1);
               amountTextField.setEditable(false);
               VendingPanel.add(amountTextField,
                   getConstraints(0,2,1,1, GridBagConstraints.WEST));


       this.add(VendingPanel,
           getConstraints(0,1,3,1, GridBagConstraints.WEST));

       setAddEditMode(false);

   }//end of VendingPanel

   // a method for setting grid bag constraints
   private GridBagConstraints getConstraints(int gridx, int gridy,
   int gridwidth, int gridheight, int anchor)
   {
       // code the grid constraints and return the result
       GridBagConstraints c = new GridBagConstraints();
       c.insets = new Insets(5,5,5,5);
       c.ipadx = 0;
       c.ipady = 0;
       c.gridx = gridx;
       c.gridy = gridy;
       c.gridwidth = gridwidth;
       c.gridheight = gridheight;
       c.anchor = anchor;
       return c;
   }
private void setWidth(JComponent c, int width)
       {
           c.setPreferredSize(new Dimension(width,25));
       }
public void setAddEditMode(boolean e)
{
   colaButton.setEnabled(e);
   lemonlimeButton.setEnabled(e);
   bottledwaterButton.setEnabled(e);
}

public void actionPerformed(ActionEvent e)
{
   Object source = e.getSource();

if (source == colaButton)
{
m.money +=.75;
   order(0);
m.getMoney(0);
amountTextField.setText(currency.format(m.money));

}
if (source == lemonlimeButton)
{
m.money +=.75;
   order(1);
m.getMoney(1);
amountTextField.setText(currency.format(m.money));

}
if (source == bottledwaterButton)
{
m.money +=.75;
   order(2);
m.getMoney(2);
amountTextField.setText(currency.format(m.money));
}
if (source == refundButton)
{
   m.refund();
   amountTextField.setText(currency.format(m.money));
}
}
public void order(int orders)
{
if(m.money >= m.cost)
{
   JOptionPane.showMessageDialog(this, "Enjoy your" + m.getName(orders));
   amountTextField.setText(currency.format(m.money));
}
else if(m.money < m.cost)
{
   setAddEditMode(false);
JOptionPane.showMessageDialog(this, "Enter more money");
}
}