JAVA Application: I need help to create the Process tab and its corresponding fu
ID: 3695372 • Letter: J
Question
JAVA Application: I need help to create the Process tab and its corresponding functions. Please help me with any errors within the current code itself as well. The ending application should look something like the pictures depicted below as Instructions.
My code so far: See pastebin link at bottom for neater version.
package icecreamorder;
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import java.text.DecimalFormat;
/**
*
*/
public class IceCreamOrder extends JPanel {
public final double VANILLA = 2.25;
public final double CHOCOLATE = 2.25;
public final double STRAWBERRY = 2.25;
private JRadioButton vanillaFlavor;
private JRadioButton chocolateFlavor;
private JRadioButton strawberryFlavor;
private ButtonGroup bg;
}
public IceCreamOrder()
{
setLayout(new GridLayout(1, 3));
/*(setTitle("Joel Jacob - Ice Cream Order");
setSize(WINDOW_WIDTH, WINDOW_HEIGHT);
SetDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setVisible(true); */
vanillaFlavor = new JRadioButton("Vanilla", true);
chocolateFlavor = new JRadioButton("Chocolate");
strawberryFlavor = new JRadioButton("Strawberry");
bg = new ButtonGroup();
bg.add(vanillaFlavor);
bg.add(chocolateFlavor);
bg.add(strawberryFlavor);
add(vanillaFlavor);
add(chocolateFlavor);
add(strawberryFlavor);
}
public double getIceCreamCost()
{
double iceCreamCost = 0.0;
if (vanillaFlavor.isSelected())
{
iceCreamCost = VANILLA;
}
if (chocolateFlavor.isSelected())
{
iceCreamCost = CHOCOLATE;
}
if (strawberryFlavor.isSelected())
{
iceCreamCost = STRAWBERRY;
}
return IceCreamCost;
}
public class Extras etends JPanel
{
public final double NUTS = 0.50;
public final double CHERRIES = 0.50;
private JCheckBox nuts;
private JCheckBox cherries;
}
public Extras
{
setLayout(new GridLayout(1, 2));
nuts = new JCheckBox("Nuts");
cherries = new JCheckBox("Cherries");
add(nuts);
add(cherries);
}
public double getExtrasCost()
{
double extrasCost = 0.0;
if (nuts.isSelected())
extrasCost = NUTS;
if (cherries.isSelected())
extrasCost = CHERRIES;
return extrasCost;
}
public static void main(String[] args)
{
// TODO code application logic here
}
}
http://pastebin.com/5wfsq5iQ
Explanation / Answer
//importing packages
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import java.text.DecimalFormat;
import java.io.*;`
//IceCream.java
public class IceCream extends JFrame
{
//Constants for window width and height
private final int WINDOW_WIDTH = 500;
private final int WINDOW_HEIGHT = 250;
//Ice cream flavors
private JRadioButton bVan; //Vanilla Radio button
private JRadioButton bStraw; //Strawberry Radio button
private JRadioButton bChoc; //Chocolate radio button
//Nuts
private JCheckBox cbNuts; //Checkbox for Nuts
private JCheckBox cbCherr; //Checkbox for Cherries
//Syrup
private JRadioButton bChocSyrup; //Radio button for Chocolate syrup
private JRadioButton bCaramSyrup; //Radio button for Caramel syrup
private JRadioButton bNoSyrup; //Radio button for No syrup
//Buttons for Open, Save and Close features.
private JButton Save;
private JButton Open;
private JButton Total;
//Button Groups for Ice Cream flavors and Syrup flavors
private ButtonGroup iceCreamGroup;
private ButtonGroup syrupGroup;
//Panels for the button groups
private JPanel iceCreamPanel; //IceCream flavor Panel
private JPanel syrupPanel; //Syrup flavors panel
private JPanel buttonPanel; //Panel for Save, Open, and Total buttons
//CONSTRUCTOR
public IceCream()
{
//Window title
setTitle("Icecream Orders");
//Set size
setSize(WINDOW_WIDTH,WINDOW_HEIGHT);
//Default close operation
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
//Calling buildPanel method
buildPanels();
setLayout(new GridLayout(2,2));
//Adding the panel to JFrame
add(iceCreamPanel);
add(nutsPanel);
add(syrupPanel);
add(buttonPanel);
//Visibility
setVisible(true);
}
private void buildPanels()
{
//Initializing the flavor radio butons and adding them to the Buton Group
bVan = new JRadioButton("Vanilla", true);
bStraw = new JRadioButton("Strawberry");
bChoc = new JRadioButton("Chocolate");
iceCreamGroup = new ButtonGroup();
iceCreamGroup.add(bVan);
iceCreamGroup.add(bStraw);
iceCreamGroup.add(bChoc);
//Initializing the Checkbox for nuts and cherries
cbNuts = new JCheckBox("Nuts?");
cbCherr = new JCheckBox("Cherries?");
//Initializing the Syrup radio buttons and adding them to the Button Group
bChocSyrup = new JRadioButton("Chocolate", true);
bCaramSyrup = new JRadioButton("Caramel");
bNoSyrup = new JRadioButton("None");
syrupGroup = new ButtonGroup();
syrupGroup.add(bChocSyrup);
syrupGroup.add(bCaramSyrup);
syrupGroup.add(bNoSyrup);
//Initializing the Buttons
Save = new JButton("Save Order");
Open = new JButton("Open Order");
Total = new JButton("Total");
//Associating action listener to the total button
Total.addActionListener(new TotalButtonListener());
//Initializing the panels
iceCreamPanel = new JPanel();
nutsPanel = new JPanel();
syrupPanel = new JPanel();
buttonPanel = new JPanel();
//Setting borders of the panels
iceCreamPanel.setBorder(BorderFactory.createTitledBorder("Ice Cream Flavor"));
nutsPanel.setBorder(BorderFactory.createTitledBorder("Nuts?"));
syrupPanel.setBorder(BorderFactory.createTitledBorder("Syrup Flavor"));
//Adding the objecs to the respective panels
iceCreamPanel.add(bVan);
iceCreamPanel.add(bStraw);
iceCreamPanel.add(bChoc);
nutsPanel.add(cbNuts);
nutsPanel.add(cbCherr);
syrupPanel.add(bChocSyrup);
syrupPanel.add(bCaramSyrup);
syrupPanel.add(bNoSyrup);
buttonPanel.add(Save);
buttonPanel.add(Open);
buttonPanel.add(Total);
}//End buildPanels
private class TotalButtonListener implements ActionListener
{
public void actionPerformed(ActionEvent e)
{
double total = 1.5;
//Creating a new deciaml format using DecimalFormater
DecimalFormat formatter = new DecimalFormat("##.##");
//Calculating total price using .isSelcted method
if(cbNuts.isSelected())
total += 0.75;
if(cbCherr.isSelected())
total += 0.50;
if(bChocSyrup.isSelected())
total += 0.75;
if(bCaramSyrup.isSelected())
total += 0.75;
//Calculating sales tax
double tax = total * 0.06;
JOptionPane.showMessageDialog(null, "Total: $" + (formatter.format(total + tax)) +
" ($" + formatter.format(total) + " + $"
+ formatter.format(tax) + " tax)");
} //End actionPermormed
} //End TotalButtonListener
//Embedded Main method
public static void main(String[] args)
{
new IceCream();
} //End Main
} //End Class
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.