import java.awt.BorderLayout;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.BorderFactory;
import javax.swing.ButtonGroup;
import javax.swing.JButton;
import javax.swing.JDialog;
import javax.swing.JFrame;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JRadioButton;
//optional for formatting the output nicer for the prices
import javax.swing.JTextArea;
import java.util.*;
import java.text.*;
//optional for formatting the output nicer for the prices
public class MovieStore2 {
private String[] Movie = {"The Tiki Temple", "Wrong Turn", "Revenge of the Nerds", "Sharly and Me"};
private final double[] MoviePrice = { 13.00, 12.75, 12.50, 12.55 };
private JRadioButton[] MovieButtons;
private String[] MovieSnacks ={"Popcorn", "Chocolate", "Sour Candies", "Pretzels"};
private final double[] SnacksPrice = { 7.50, 2.50 , 2.00, 6.00 };
private JRadioButton[] MovieSnacksButtons;
private String[] MovieDrinks ={"Juice", "Soda Pop", "Milkshake", "Water"};
private final double[] DrinksPrice = {2.25 , 2.50 , 4.50, 1.00};
private JRadioButton[] MovieDrinksButtons;
//optional to display amounts in the US dollar format rather than just numbers
private Locale usa = new Locale("en", "US");
// Create a Currency instance for the Locale
private Currency dollars = Currency.getInstance(usa);
// Create a formatter given the Locale
private NumberFormat dollarFormat = NumberFormat.getCurrencyInstance(usa);
//end optional display - if not needed, take out the java.util.* and java.text.* imports above
// Format the Number into a Currency String
public MovieStore2()
{
JFrame frame = new JFrame();
frame.setTitle("MOVIE STORE");
frame.setSize(500, 200);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.add(getMoviePanel(), BorderLayout.WEST);
frame.add(getSnacksPanel(), BorderLayout.EAST);
frame.add(getDrinksPanel(), BorderLayout.CENTER);
frame.add(getTotalPanel(frame), BorderLayout.SOUTH);
frame.setVisible(true);
}
private JPanel getMoviePanel()
{
/** create the panel with a border and a 4x1 grid */
JPanel moviePanel = new JPanel();
moviePanel.setLayout(new GridLayout(4, 1));
/** add a border around the panel */
moviePanel.setBorder(BorderFactory.createTitledBorder("Movie"));
/** Create an array of radio buttons */
MovieButtons = new JRadioButton[Movie.length];
ButtonGroup group = new ButtonGroup();
/** initialize each radio button within the array */
for(int index = 0; index < MovieButtons.length; index++) {
MovieButtons[index] = new JRadioButton(Movie[index]);
/** group the radio buttons */
group.add(MovieButtons[index]);
/** add the radio buttons to the panel */
moviePanel.add(MovieButtons[index]);
}
return moviePanel;
}
private double getMovieCost() {
double MovieCost = 0.0;
for(int index = 0; index < MovieButtons.length; index++) {
if(MovieButtons[index].isSelected()) {
MovieCost = MoviePrice[index];
}
}
return MovieCost;
}
private JPanel getSnacksPanel()
{
/** create the panel with a border and a 4x1 grid */
JPanel snacksPanel = new JPanel();
snacksPanel.setLayout(new GridLayout(4, 1));
/** add a border around the panel */
snacksPanel.setBorder(BorderFactory.createTitledBorder("Snacks"));
/** Create an array of radio buttons */
MovieSnacksButtons = new JRadioButton[MovieSnacks.length];
ButtonGroup group = new ButtonGroup();
/** initialize each radio button within the array */
for(int index = 0; index < MovieSnacksButtons.length; index++) {
MovieSnacksButtons[index] = new JRadioButton(MovieSnacks[index]);
/** group the radio buttons */
group.add(MovieSnacksButtons[index]);
/** add the radio buttons to the panel */
snacksPanel.add(MovieSnacksButtons[index]);
}
return snacksPanel;
}
private double getSnacksCost() {
double MovieCost = 0.0;
for(int index = 0; index < MovieSnacksButtons.length; index++) {
if(MovieSnacksButtons[index].isSelected()) {
MovieCost = SnacksPrice[index];
}
}
return MovieCost;
}
private JPanel getDrinksPanel()
{
/** create the panel with a border and a 4x1 grid */
JPanel drinksPanel = new JPanel();
drinksPanel.setLayout(new GridLayout(4, 1));
/** add a border around the panel */
drinksPanel.setBorder(BorderFactory.createTitledBorder("Drinks"));
/** Create an array of radio buttons */
MovieDrinksButtons = new JRadioButton[MovieDrinks.length];
ButtonGroup group = new ButtonGroup();
/** initialize each radio button within the array */
for(int index = 0; index < MovieDrinksButtons.length; index++) {
MovieDrinksButtons[index] = new JRadioButton(MovieDrinks[index]);
/** group the radio buttons */
group.add(MovieDrinksButtons[index]);
/** add the radio buttons to the panel */
drinksPanel.add(MovieDrinksButtons[index]);
}
return drinksPanel;
}
private double getDrinksCost() {
double DrinksCost = 0.0;
for(int index = 0; index < MovieDrinksButtons.length; index++) {
if(MovieDrinksButtons[index].isSelected()) {
DrinksCost = DrinksPrice[index];
}
}
return DrinksCost;
}
private JPanel getTotalPanel(JFrame frame)
{
JPanel getTotalPanel = new JPanel();
getTotalPanel.setLayout(new GridLayout(1, 1));
JButton getTotalButton = new JButton("Show Total");
getTotalButton.addActionListener((ActionListener) new ActionListener()
{
public void actionPerformed(ActionEvent e)
{
getTotalCost(e);
//@Override
//show jdialog when button is clicked
//JDialog dialog = new JDialog(frame, "Welcome to StackHowTo", true);
//dialog.setLocationRelativeTo(frame);
//dialog.setVisible(true);
}
});
getTotalPanel.add(getTotalButton);
return getTotalPanel;
}
/*********************************************************************
/** TODO Set up a GUI containing one button to calculate the total.
*********************************************************************/
/** TODO create getTotalPanel method */
/*********************************************************************/
public void getTotalCost(ActionEvent e) {
// JOptionPane.showMessageDialog(null, "You called getTotalCost", "InfoBox: " , JOptionPane.INFORMATION_MESSAGE);
/** Sales tax rate */
final double TAX_RATE = 0.06;
String costMessage = "";
double subtotal, tax, total;
subtotal = getMovieCost() +
getSnacksCost() +
getDrinksCost();
tax = subtotal * TAX_RATE;
total = subtotal + tax;
//System.out.println(dollars.getDisplayName() + ": " + dollarFormat.format(currencyAmount));
costMessage = "The total charges for today are:\n\n";
costMessage += "Movie: \t" + dollarFormat.format(getMovieCost()) + "\n";
costMessage += "Drinks: \t" + dollarFormat.format(getDrinksCost()) + "\n";
costMessage += "Snacks: \t" +dollarFormat.format(getSnacksCost())+ "\n";
costMessage += "Subtotal: \t" +dollarFormat.format(subtotal)+ "\n";
costMessage += "Tax: \t" +dollarFormat.format(tax)+ "\n";
costMessage += "Total: \t" + dollarFormat.format(total);
//this one has a little nice formatting with tabs
JOptionPane.showMessageDialog(null, new JTextArea(costMessage), "Total Charges" , JOptionPane.INFORMATION_MESSAGE);
//this one is just plain text without tabs
JOptionPane.showMessageDialog(null, costMessage, "Total Charges" , JOptionPane.INFORMATION_MESSAGE);
/** TODO Display the charges using JOptionPane.showMessageDialog */
}
/*********************************************************************
* Launches the application.
*********************************************************************/
public static void main(String[] args)
{
new MovieStore2();
}
}