I need help with this program. I have a code that was sent to me but I cant get
ID: 3634392 • Letter: I
Question
I need help with this program. I have a code that was sent to me but I cant get it to work. I even tried breaking it down into different classes and still wouldnt compile for me. I will post the description of problem first, then will post the code I have.
Project: Skateboard Designer
Problem Description:
The skate shop sells the skateboard products listed on the table below:
Decks
Truck Assemblies
Wheels
The Master Thrasher $ 60
7.75- inch axle $ 35
51 mm $ 20
The Dictator $ 45
8- inch axle $ 40
55 mm $ 22
The Street King $ 50
8.5- inch axle $ 45
58 mm $ 24
61 mm $ 28
In addition, the Skate Shop sells the following miscellaneous products and services:
Create an application that allows the user to select one deck, one truck assembly, and one wheel set from either list components or combo boxes. The application should also have a list component that allows the user to select multiple miscellaneous products. The application should display the subtotal, the amount of sales tax ( at 6%), and the total of the order.
Analysis:
(Describe the purpose, processing, input and output in your own words.)
Design:
(Draw the user interface diagram.)
Testing: (Describe how you test this program)
Code I have
import javax.swing.*;
import javax.swing.event.*;
import java.awt.*;
public class DeckPanel extends JPanel
{
private JPanel deckPanel;
private JList deckList;
private JList selectedDeckList;
private String[] decks = {"The Master Trasher" , "The Dictator", "The Street King"};
public DeckPanel()
{
setBorder(BorderFactory.createTitledBorder("Decks"));
setLayout(new GridLayout(3, 1));
}
}
// AssembliesPanel
public class AssembliesPanel extends JPanel
{
public final double AXLE1 = 35.00;
public final double AXLE2 = 40.00;
public final double AXLE3 = 45.00;
private JCheckBox axle1;
private JCheckBox axle2;
private JCheckBox axle3;
public AssembliesPanel()
{
setLayout(new GridLayout(3, 1));
axle1 = new JCheckBox("7.75 inch axle");
axle2 = new JCheckBox("8 inch axle");
axle3 = new JCheckBox("8.5 inch axle");
setBorder(BorderFactory.createTitledBorder("Truck Assemblies"));
add(axle1);
add(axle2);
add(axle3);
}
}
// WheelsPanel:
public class WheelsPanel extends JPanel
{
public final double WHEEL1 = 20.0;
public final double WHEEL2 = 22.00;
public final double WHEEL3 = 24.00;
public final double WHEEL4 = 28.00;
private JRadioButton wheel1;
private JRadioButton wheel2;
private JRadioButton wheel3;
private JRadioButton wheel4;
private ButtonGroup bg;
public WheelsPanel()
{
setLayout(new GridLayout(4, 1));
wheel1 = new JRadioButton("51 mm", true);
wheel2 = new JRadioButton("55 mm");
wheel3 = new JRadioButton("58 mm");
wheel4 = new JRadioButton("61 mm");
bg = new ButtonGroup();
bg.add(wheel1);
bg.add(wheel2);
bg.add(wheel3);
bg.add(wheel4);
setBorder(BorderFactory.createTitledBorder("Wheels"));
add(wheel1);
add(wheel2);
add(wheel3);
add(wheel4);
}
}
// MiscellaneousPanel
public class MiscellaneousPanel extends JPanel
{
public final double TAPE = 10.0;
public final double BEARINGS = 30.00;
public final double PADS = 2.00;
public final double NUTS = 28.00;
private JRadioButton tape;
private JRadioButton bearings;
private JRadioButton pads;
private JRadioButton nuts;
private ButtonGroup bg;
public MiscellaneousPanel()
{
setLayout(new GridLayout(4, 1));
tape = new JRadioButton("Grip Tape", true);
bearings = new JRadioButton("Bearings");
pads = new JRadioButton("Riser Pads");
nuts = new JRadioButton("Nuts & Bolts Kit");
bg = new ButtonGroup();
bg.add(tape);
bg.add(bearings);
bg.add(pads);
bg.add(nuts);
setBorder(BorderFactory.createTitledBorder
("Miscellaneous Products"));
add(tape);
add(bearings);
add(pads);
add(nuts);
}
}
public class OrderCalculatorGUI extends JFrame
{
private final int WINDOW_WIDTH = 400;
private final int WINDOW_HEIGHT = 200;
private DeckPanel decks; // Deck panel
private AssembliesPanel assemblies;
private WheelsPanel wheels;
private MiscellaneousPanel miscellaneous;
private JPanel buttonPanel = new JPanel(); private JButton calcButton;
private JButton exitButton;
private final double TAX_RATE = 0.06;
public OrderCalculatorGUI()
{
setTitle("Skateboard Designer");
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setSize(WINDOW_WIDTH, WINDOW_HEIGHT);
setLayout(new GridLayout(3, 4));
miscellaneous = new MiscellaneousPanel();
decks = new DeckPanel();
assemblies = new AssembliesPanel();
wheels = new WheelsPanel();
buildButtonPanel();
add(decks);
add(assemblies);
add(wheels);
add(miscellaneous);
add(buttonPanel);
pack();
setVisible(true);
}
private void buildButtonPanel()
{
calcButton = new JButton("Calculate");
exitButton = new JButton("Exit");
buttonPanel.add(calcButton);
buttonPanel.add(exitButton);
}
private class ExitButtonListener implements ActionListener
{
public void actionPerformed(ActionEvent e)
{
System.exit(0);
}
}
}
// DeckPanel
public class DeckPanel extends JPanel implements ListSelectionListener
{
private JPanel deckPanel;
private JList deckList;
String[] decks = {"The Master Trasher" , "The Dictator", "The Street King"};
public DeckPanel()
{
setBorder(BorderFactory.createTitledBorder("Decks"));
deckList = new JList(decks);
add(deckList);
JScrollPane scrollbar = new JScrollPane(deckList);
scrollbar.setVerticalScrollBarPolicy(ScrollPaneConstants.VERTICAL_SCROLLBAR_ALWAYS);
scrollbar.setHorizontalScrollBarPolicy(ScrollPaneConstants.HORIZONTAL_SCROLLBAR_NEVER);
add(scrollbar);
deckList.setVisibleRowCount(2);
deckList.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
deckList.addListSelectionListener(this);
setLayout(new GridLayout(3, 1));
}
public void valueChanged(ListSelectionEvent e)
{
if (!e.getValueIsAdjusting())
{
String selection = (String) deckList.getSelectedValue();
System.out.println(selection);
}
}
}
Decks
Truck Assemblies
Wheels
The Master Thrasher $ 60
7.75- inch axle $ 35
51 mm $ 20
The Dictator $ 45
8- inch axle $ 40
55 mm $ 22
The Street King $ 50
8.5- inch axle $ 45
58 mm $ 24
61 mm $ 28
Explanation / Answer
/** 02 This program creates an instance of the OrderCalculatorGUI class 03 which displays the GUI for the Skateboard Designer application. 04 */ 05 06 public class SkateboardDesigner 07 { 08 public static void main(String[] args) 09 { 10 new OrderCalculatorGUI(); 11 } 12 } import javax.swing.*; 02 import javax.swing.event.*; 03 import java.awt.*; 04 05 /** 06 The DeckPanel class allows the user to select either Master trasher, dictator, or street king. 07 */ 08 09 public class DeckPanel extends JPanel 10 { 11 // The following constants are used to indicate 12 // The different kind of decks 13 14 15 private JPanel deckPanel; 16 private JList deckList; 17 private JList selectedDeckList; 18 19 private String[] decks = {"The Master Trasher" , "The Dictator", "The Street King"}; 20 21 /** 22 Constructor 23 */ 24 25 public DeckPanel() 26 { 27 // Add a border around the panel. 28 setBorder(BorderFactory.createTitledBorder("Decks")); 29 30 // Create a GridLayout manager with 31 // Two rows and one column 32 setLayout(new GridLayout(3, 1)); 33 34 } 35 } 01 import javax.swing.*; 02 import java.awt.*; 03 04 /** 05 The AssembliesPanel class allows the user to select 06 the axles for the skateboard 07 */ 08 09 public class AssembliesPanel extends JPanel 10 { 11 // The following constants are used to indicate 12 // The cost of the assemblies. 13 public final double AXLE1 = 35.00; 14 public final double AXLE2 = 40.00; 15 public final double AXLE3 = 45.00; 16 17 private JCheckBox axle1; // To select axle1 18 private JCheckBox axle2; // To select axle2 19 private JCheckBox axle3; // To select axle3 20 21 /** 22 Constructor 23 */ 24 25 public AssembliesPanel() 26 { 27 // Create a GridLayout manager with 28 // four rows and one column. 29 setLayout(new GridLayout(3, 1)); 30 31 // Create the check boxes. 32 axle1 = new JCheckBox("7.75 inch axle"); 33 axle2 = new JCheckBox("8 inch axle"); 34 axle3 = new JCheckBox("8.5 inch axle"); 35 36 // Add a border around the panel. 37 setBorder(BorderFactory.createTitledBorder("Truck Assemblies")); 38 39 // Add the check boxes to the panel. 40 add(axle1); 41 add(axle2); 42 add(axle3); 43 } 44 45 } 01 import javax.swing.*; 02 import java.awt.*; 03 04 /** 05 The WheelsPanel class allows the user to select different wheels. 06 */ 07 08 public class WheelsPanel extends JPanel 09 { 10 // The following constants are used to indicate 11 // The cost of wheels. 12 public final double WHEEL1 = 20.0; 13 public final double WHEEL2 = 22.00; 14 public final double WHEEL3 = 24.00; 15 public final double WHEEL4 = 28.00; 16 17 private JRadioButton wheel1; // To select wheel1. 18 private JRadioButton wheel2; // To select wheel2. 19 private JRadioButton wheel3; // To select wheel3. 20 private JRadioButton wheel4; // To select wheel4. 21 private ButtonGroup bg; // Radio button group 22 23 /** 24 Constructor 25 */ 26 27 public WheelsPanel() 28 { 29 // Create a GridLayout manager with 30 // four rows and one column. 31 setLayout(new GridLayout(4, 1)); 32 33 // Create the radio buttons. 34 wheel1 = new JRadioButton("51 mm", true); 35 wheel2 = new JRadioButton("55 mm"); 36 wheel3 = new JRadioButton("58 mm"); 37 wheel4 = new JRadioButton("61 mm"); 38 39 // Group the redio buttons 40 bg = new ButtonGroup(); 41 bg.add(wheel1); 42 bg.add(wheel2); 43 bg.add(wheel3); 44 bg.add(wheel4); 45 46 // Add a border around the panel. 47 setBorder(BorderFactory.createTitledBorder("Wheels")); 48 49 // Add the radio buttons to the panel. 50 add(wheel1); 51 add(wheel2); 52 add(wheel3); 53 add(wheel4); 54 } 55 56 } 01 import javax.swing.*; 02 import java.awt.*; 03 04 /** 05 The MiscellaneousPanel class allows the user to select miscellaneous items. 06 */ 07 08 public class MiscellaneousPanel extends JPanel 09 { 10 // The following constants are used to indicate 11 // The cost of miscellaneous items. 12 public final double TAPE = 10.0; 13 public final double BEARINGS = 30.00; 14 public final double PADS = 2.00; 15 public final double NUTS = 28.00; 16 17 private JRadioButton tape; // To select tape. 18 private JRadioButton bearings; // To select bearings. 19 private JRadioButton pads; // To select pads. 20 private JRadioButton nuts; // To select nuts. 21 private ButtonGroup bg; // Radio button group 22 23 /** 24 Constructor 25 */ 26 27 public MiscellaneousPanel() 28 { 29 // Create a GridLayout manager with 30 // four rows and one column. 31 setLayout(new GridLayout(4, 1)); 32 33 // Create the radio buttons. 34 tape = new JRadioButton("Grip Tape", true); 35 bearings = new JRadioButton("Bearings"); 36 pads = new JRadioButton("Riser Pads"); 37 nuts = new JRadioButton("Nuts & Bolts Kit"); 38 39 // Group the redio buttons 40 bg = new ButtonGroup(); 41 bg.add(tape); 42 bg.add(bearings); 43 bg.add(pads); 44 bg.add(nuts); 45 46 // Add a border around the panel. 47 setBorder(BorderFactory.createTitledBorder("Miscellaneous Products")); 48 49 // Add the radio buttons to the panel. 50 add(tape); 51 add(bearings); 52 add(pads); 53 add(nuts); 54 } 55 } import javax.swing.*; 02 import java.awt.*; 03 import java.awt.event.*; 04 import java.text.DecimalFormat; 05 06 /** 07 The OrderCalculatorGUI class creates the GUI for the 08 Brandi's Bagel House application. 09 */ 10 11 public class OrderCalculatorGUI extends JFrame 12 { 13 private final int WINDOW_WIDTH = 400; // window width 14 private final int WINDOW_HEIGHT = 200; // window height 15 private DeckPanel decks; // Deck panel 16 private AssembliesPanel assemblies; // Assemblies panel 17 private WheelsPanel wheels; // Wheels panel 18 private MiscellaneousPanel miscellaneous; // To display a greeting 19 private JPanel buttonPanel = new JPanel(); // To hold the buttons 20 private JButton calcButton; // To calculate the cost 21 private JButton exitButton; // To exit the application 22 private final double TAX_RATE = 0.06; // Sales tax rate 23 24 public OrderCalculatorGUI() 25 { 26 // Display a title. 27 setTitle("Skateboard Designer"); 28 29 // Specify an action for the close button. 30 setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 31 32 // set size of the window 33 setSize(WINDOW_WIDTH, WINDOW_HEIGHT); 34 35 // add gridlayout manager. 36 setLayout(new GridLayout(3, 4)); 37 38 // Create the custom panels. 39 miscellaneous = new MiscellaneousPanel(); 40 decks = new DeckPanel(); 41 assemblies = new AssembliesPanel(); 42 wheels = new WheelsPanel(); 43 44 // Create the button panel. 45 buildButtonPanel(); 46 47 // Add the components to the content pane. 48 add(decks); 49 add(assemblies); 50 add(wheels); 51 add(miscellaneous); 52 add(buttonPanel); 53 54 // Pack the contents of the window and display it. 55 pack(); 56 setVisible(true); 57 } 58 59 /** 60 The buildButtonPanel method builds the button panel. 61 */ 62 63 private void buildButtonPanel() 64 { 65 // Create a panel for the buttons. 66 calcButton = new JButton("Calculate"); 67 exitButton = new JButton("Exit"); 68 69 /** Register the action listeners. 70 calcButton.addActionListener(new CalcButtonListener()); 71 exitButton.addActionListener(new ExitButtonListener()); 72 */ 73 74 // Add the buttons to the button panel. 75 buttonPanel.add(calcButton); 76 buttonPanel.add(exitButton); 77 } 78 79 80 /** 81 Private inner class that handles the event when 82 the user clicks the Exit button. 83 */ 84 85 private class ExitButtonListener implements ActionListener 86 { 87 public void actionPerformed(ActionEvent e) 88 { 89 System.exit(0); 90 } 91 } 92 } You need to implement 'Listeners' in your code such as in the deckPanel view source print? 01 import javax.swing.*; 02 import javax.swing.event.*; 03 04 import java.awt.*; 05 06 /** 07 The DeckPanel class allows the user to select either Master trasher, dictator, or street king. 08 */ 09 10 public class DeckPanel extends JPanel implements ListSelectionListener 11 { 12 // The following constants are used to indicate 13 // The different kind of decks 14 15 16 private JPanel deckPanel; 17 private JList deckList; 18 19 20 String[] decks = {"The Master Trasher" , "The Dictator", "The Street King"}; 21 22 23 /** 24 Constructor 25 */ 26 27 public DeckPanel() 28 { 29 // Add a border around the panel. 30 setBorder(BorderFactory.createTitledBorder("Decks")); 31 32 deckList = new JList(decks); 33 add(deckList); 34 JScrollPane scrollbar = new JScrollPane(deckList); 35 36 scrollbar.setVerticalScrollBarPolicy(ScrollPaneConstants.VERTICAL_SCROLLBAR_ALWAYS); 37 scrollbar.setHorizontalScrollBarPolicy(ScrollPaneConstants.HORIZONTAL_SCROLLBAR_NEVER); 38 add(scrollbar); 39 deckList.setVisibleRowCount(2); 40 deckList.setSelectionMode(ListSelectionModel.SINGLE_SELECTION); 41 deckList.addListSelectionListener(this); 42 // Create a GridLayout manager with 43 // Two rows and one column 44 setLayout(new GridLayout(3, 1)); 45 46 47 48 } 49 50 51 public void valueChanged(ListSelectionEvent e) 52 { 53 if (!e.getValueIsAdjusting()){ 54 String selection = (String) deckList.getSelectedValue(); 55 System.out.println(selection); 56 } 57 58 } 59 }