/**
* File: Project3.java (Originally Project2.java, Fall 2008)
* Author: D. Frey
* Date: October 7, 2008
* Modified: S. Mitchell, October 25, 2009
* Modified: S. Mitchell, November 4, 2012
*
* This file contains the GUI for the Snack Machine project
*
* DO NOT edit this file
* DO NOT submit this file
*
*/
package proj3;
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
public class Project3
{
public static void main (String [] args)
{
Project3 p3 = new Project3( );
}
private SnackMachine snackMachine;
private JSpinner spinNickels, spinDimes, spinQrts;
private JFrame mainFrame;
public Project3 ( )
{
snackMachine = new SnackMachine( );
// spinners for coins inserted
mainFrame = new JFrame("CMSC 202 Project 3");
mainFrame.setLayout(new BorderLayout());
mainFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
mainFrame.setSize(1000, 500);
mainFrame.setLocation(100, 50);
// Maintenance Menus
JMenuBar jmb = new JMenuBar( );
// Create the Candy Maintenance Menu System
JMenu jmCandy = new JMenu("Snack Maintenance");
JMenuItem jmiAddChocChipCookies = new JMenuItem("Add Chocolate Chip Cookies");
JMenuItem jmiAddOatmealCookies = new JMenuItem("Add Oatmeal Cookies");
JMenuItem jmiAddLemonCookies = new JMenuItem("Add Lemon Cookies");
JMenuItem jmiAddMints = new JMenuItem("Add Mints");
JMenuItem jmiNrMints = new JMenuItem("Mint Packages Count");
JMenuItem jmiNrCookies = new JMenuItem("Cookie Packages Count");
// attach action listeners for each menu choice
jmiAddChocChipCookies.addActionListener(new AddChocChipCookies());
jmiAddOatmealCookies.addActionListener(new AddOatmealCookies());
jmiAddLemonCookies.addActionListener(new AddLemonCookies());
jmiAddMints.addActionListener(new AddMints());
jmiNrMints.addActionListener(new NrMints());
jmiNrCookies.addActionListener(new NrCookies());
// add menu items to CandyMaintenance Menu
jmCandy.add( jmiAddChocChipCookies);
jmCandy.add( jmiAddOatmealCookies);
jmCandy.add( jmiAddLemonCookies);
jmCandy.add( jmiAddMints);
jmCandy.add( jmiNrMints);
jmCandy.add( jmiNrCookies);
// Register Maintenance Menu
JMenu jmRegister = new JMenu("Cash Register Maintenance");
JMenuItem jmiCashOnHand = new JMenuItem("Report Cash on Hand");
// add action listeners for menu items
jmiCashOnHand.addActionListener( new CashOnHand());
jmRegister.add(jmiCashOnHand);
// add menu lists to menu bar
jmb.add(jmCandy);
jmb.add(jmRegister);
// add menu bar to frame
mainFrame.setJMenuBar(jmb);
// Marquee in the NORTH
JLabel jlMarquee = new JLabel( "Programmer's Late Night Snack Machine");
jlMarquee.setFont(new Font("Times New Roman", Font.BOLD, 24));
jlMarquee.setHorizontalAlignment(JTextField.CENTER);
mainFrame.add(jlMarquee, BorderLayout.NORTH);
// Buttons for purchases in the CENTER of the frame
JPanel jpButtons = new JPanel( );
jpButtons.setLayout(new GridLayout(1, 2));
JButton jbMints = new JButton( "
Buy Mints35 cents
");
jbMints.setBackground(Color.ORANGE);
jbMints.setFont( new Font("Times New Roman", Font.BOLD, 42));
jbMints.setFocusPainted(false);
jbMints.addActionListener(new BuyMints());
JButton jbCookies = new JButton( "Buy Cookies65 cents
");
jbCookies.setBackground(Color.MAGENTA);
jbCookies.setFont( new Font("Times New Roman", Font.BOLD, 42));
jbCookies.setFocusPainted(false);
jbCookies.addActionListener(new BuyCookies());
jpButtons.add( jbMints);
jpButtons.add( jbCookies);
// Spinners for coin insertion in the SOUTH
JPanel jpCoins = new JPanel( ); // to encapsulate labels and spinners
Dimension spinnerDim = new Dimension(50, 20);
Font labelFont = new Font("Times New Roman", Font.BOLD, 24);
SpinnerNumberModel spnmNickels = new SpinnerNumberModel(0, 0, 20, 1);
spinNickels = new JSpinner( spnmNickels );
spinNickels.setPreferredSize(spinnerDim);
SpinnerNumberModel spnmDimess = new SpinnerNumberModel(0, 0, 10, 1);
spinDimes = new JSpinner( spnmDimess );
spinDimes.setPreferredSize(spinnerDim);
SpinnerNumberModel spnmQtrs = new SpinnerNumberModel(0, 0, 4, 1);
spinQrts = new JSpinner( spnmQtrs );
spinQrts.setPreferredSize(spinnerDim);
JLabel jlInsertCoins = new JLabel("Insert Coins Here ");
jlInsertCoins.setFont(new Font("Times New Roman", Font.BOLD, 24));
jpCoins.add(jlInsertCoins);
JLabel jlNickels = new JLabel("Nickels");
jlNickels.setFont(labelFont);
jlNickels.setHorizontalAlignment(JLabel.RIGHT);
jpCoins.add(jlNickels);
jpCoins.add(spinNickels);
JLabel jlDimes = new JLabel("Dimes");
jlDimes.setFont(labelFont);
jlDimes.setHorizontalAlignment(JLabel.RIGHT);
jpCoins.add(jlDimes);
jpCoins.add(spinDimes);
JLabel jlQrts = new JLabel("Quarters");
jlQrts.setFont(labelFont);
jlQrts.setHorizontalAlignment(JLabel.RIGHT);
jpCoins.add(jlQrts);
jpCoins.add(spinQrts);
// populate the main frame
mainFrame.add(jpButtons, BorderLayout.CENTER);
mainFrame.add(jpCoins, BorderLayout.SOUTH);
mainFrame.setVisible( true );
}
// -- helper methods
private void resetSpinners( )
{
spinNickels.setValue(new Integer(0));
spinDimes.setValue(new Integer(0));
spinQrts.setValue( new Integer(0));
}
//---- event handlers --- //
// Buy Mints Button
private class BuyMints implements ActionListener
{
public void actionPerformed(ActionEvent ae)
{
// read the coins inserted
int nrNickels, nrDimes, nrQrts;
nrNickels = ( (Integer)spinNickels.getValue()).intValue();
nrDimes = ( (Integer)spinDimes.getValue()).intValue();
nrQrts = ( (Integer)spinQrts.getValue()).intValue();
// buy the Mints, get Mints in return
Mints m = snackMachine.buyMints(new Money(nrNickels, nrDimes, nrQrts));
if ( m == null ) // sold out or not exact change
JOptionPane.showMessageDialog(mainFrame, "Mints purchase failed");
else {
// purchase successful
RoundIcon mints = new RoundIcon(m.getColor(), 100);
String msg = "Congratulation! You bought this package of Mints";
JOptionPane.showMessageDialog(null, msg,
"Mints Purchase", JOptionPane.INFORMATION_MESSAGE, mints);
}
// reset coin spinners
resetSpinners( );
}
}
// Buy Cookies Button
private class BuyCookies implements ActionListener
{
public void actionPerformed(ActionEvent ae)
{
// read the coins inserted
int nrNickels, nrDimes, nrQrts;
nrNickels = (Integer)spinNickels.getValue();
nrDimes = (Integer)spinDimes.getValue();
nrQrts = (Integer)spinQrts.getValue();
// buy the Cookies, get Cookies in return
Cookies c = snackMachine.buyCookies(new Money(nrNickels, nrDimes, nrQrts));
if ( c == null ) // purchase failed
JOptionPane.showMessageDialog(mainFrame, "Cookies purchase failed");
else {
// purchase successful
String msg = "Congratulations! You bought a package of "
+ c.getFlavor() + " cookies.";
JOptionPane.showMessageDialog(null, msg);
}
// reset coin spinners
resetSpinners( );
}
}
// Candy Maintenance Menu -- add chocolate chip cookies
private class AddChocChipCookies implements ActionListener
{
public void actionPerformed(ActionEvent ae)
{
AddCookies( CookieFlavors.CHOCOLATE_CHIP );
}
}
// Candy Maintenance Menu -- add oatmeal cookies
private class AddOatmealCookies implements ActionListener
{
public void actionPerformed(ActionEvent ae)
{
AddCookies( CookieFlavors.OATMEAL );
}
}
// Candy Maintenance Menu -- add lemon cookies
private class AddLemonCookies implements ActionListener
{
public void actionPerformed(ActionEvent ae)
{
AddCookies( CookieFlavors.LEMON );
}
}
// some stuff marked "final" so they can be used within anonymous ActionListener
private void AddCookies(final CookieFlavors flavor)
{
// pop a frame and ask how many
final JFrame frame = new JFrame( );
frame.setResizable(false);
frame.setLocation(300, 300);
frame.setSize(400, 150);
frame.setDefaultCloseOperation(JFrame.HIDE_ON_CLOSE);
SpinnerNumberModel spnmNrCookies = new SpinnerNumberModel(1, 1, 50, 1);
final JSpinner spinNrCookies = new JSpinner( spnmNrCookies );
spinNrCookies.setPreferredSize(new Dimension(50, 20));
JButton ok = new JButton("OK");
ok.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent ae) {
int nrCookies = (Integer)spinNrCookies.getValue();
snackMachine.addCookies(flavor, nrCookies);
JOptionPane.showMessageDialog(frame,
nrCookies + " bags of " + flavor +
" cookies have been added to the machine");
}
});
JPanel subPanel1 = new JPanel();
subPanel1.add( new JLabel("Enter Number of Bags of " + flavor +
" Cookies to Add"));
subPanel1.add(spinNrCookies);
JPanel subPanel2 = new JPanel();
subPanel2.add(ok);
JPanel panel = new JPanel();
panel.setLayout(new BorderLayout());
panel.add( subPanel1, BorderLayout.NORTH );
panel.add( subPanel2, BorderLayout.CENTER);
panel.setBackground(Color.MAGENTA);
frame.add(panel, BorderLayout.CENTER);
frame.setVisible(true);
}
// Candy Maintenance Menu -- add mints
private class AddMints implements ActionListener
{
public void actionPerformed(ActionEvent ae)
{
// pop a frame and ask how many
final JFrame frame = new JFrame( );
frame.setResizable(false);
frame.setLocation(200, 200);
frame.setSize(500, 500);
frame.setDefaultCloseOperation(JFrame.HIDE_ON_CLOSE);
// color chooser
JPanel subPanel1 = new JPanel();
JLabel jlCount =new JLabel("Enter Number of Packages of Mints to Add");
jlCount.setFont(new Font("Times New Roman", Font.BOLD, 18));
SpinnerNumberModel spnmNrMints = new SpinnerNumberModel(1, 1, 50, 1);
final JSpinner spinNrMints = new JSpinner( spnmNrMints );
spinNrMints.setPreferredSize(new Dimension(50, 20));
subPanel1.add(jlCount);
subPanel1.add(spinNrMints);
JPanel subPanel2 = new JPanel();
JLabel jlColor = new JLabel("Choose mints color");
jlColor.setFont(new Font("Times New Roman", Font.BOLD, 18));
final JColorChooser jccMintsColor = new JColorChooser( );
subPanel2.add (jlColor);
subPanel2.add(jccMintsColor);
JPanel subPanel3 = new JPanel();
JButton ok = new JButton("OK");
ok.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent ae) {
int nrMints = (Integer)spinNrMints.getValue();
Color mColor = jccMintsColor.getColor();
snackMachine.addMints(mColor, nrMints);
JOptionPane.showMessageDialog(frame, nrMints +
" packages of mints added to the machine");
}
});
subPanel3.add(ok);
JPanel panel = new JPanel();
panel.setLayout(new BorderLayout());
panel.add( subPanel1, BorderLayout.NORTH );
panel.add( subPanel2, BorderLayout.CENTER);
panel.add( subPanel3, BorderLayout.SOUTH);
panel.setBackground(Color.MAGENTA);
frame.add(panel, BorderLayout.CENTER);
frame.setVisible(true);
}
}
// Candy Maintenance Menu -- how many packages of mints in the machine?
private class NrMints implements ActionListener
{
public void actionPerformed(ActionEvent ae)
{
// create a frame to display the number of packages of mints
int nrMints = snackMachine.getNrMints();
String info = "The snack machine currently contains " +
nrMints + " packages of mints";
JOptionPane.showMessageDialog(mainFrame, info);
}
}
// Candy Maintenance Menu -- how many bags of cookies in the machine?
private class NrCookies implements ActionListener
{
public void actionPerformed(ActionEvent ae)
{
// create a frame to display the number of cookies
int nrCookies = snackMachine.getNrCookies();
String info = "The snack machine currently contains " +
nrCookies + " packages of cookies";
JOptionPane.showMessageDialog(mainFrame, info);
}
}
// Cash Maintenance Menu -- report cash on hand
private class CashOnHand implements ActionListener
{
public void actionPerformed(ActionEvent ae)
{
// call snack machine method
// report coins in machine -- share with RemoveFunds
Money cashOnHand = snackMachine.getCashOnHand();
String info = "The Cash Register contains\n" + cashOnHand.toString();
JOptionPane.showMessageDialog(mainFrame, info);
}
}
/**
* a round Icon with transparent corners
* Used to draw Mints
*/
public static class RoundIcon implements Icon {
private Color color;
private int diameter;
public RoundIcon(Color color, int diameter) {
this.color = color;
this.diameter = diameter;
}
public int getIconHeight() {
return diameter;
}
public int getIconWidth() {
return diameter;
}
public void paintIcon(Component c, Graphics g, int x, int y) {
g.setColor(color);
g.fillArc(x, y, diameter, diameter, 0, 360);
}
}
}