/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
package skateshop;
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
package skateshop;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.text.DecimalFormat;
import javax.swing.BorderFactory;
import javax.swing.ImageIcon;
import javax.swing.JComboBox;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JList;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing. ListSelectionModel;
import javax.swing.border.Border;
import javax.swing.event. ListSelectionEvent;
import javax.swing.event. ListSelectionListener;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.text.DecimalFormat;
import javax.swing.BorderFactory;
import javax.swing.ImageIcon;
import javax.swing.JComboBox;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JList;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.
import javax.swing.border.Border;
import javax.swing.event.
import javax.swing.event.
public class SkateShop extends JFrame{
private JPanel panel1, panel2, panel3, panel4, panel5, panel6;
private JComboBox decks, trucks, wheels;
private JScrollPane scroll;
private JList Misc;
private ImageIcon skateboard;
private JLabel picture, subTotalOutput, taxOutput, totalOutput;
private String[] deck = {"The Master Thrasher $60", "The Dictator $45", "The Street King $50"};
private String[] truck = {"7.75 inch axle $35", "8 inch axle $40", "8.5 inch axle $45"};
private String[] wheel = {"51mm $20", "55mm $22", "58mm $24", "61mm $28"};
private String[] misc = {"Riser pads: $2", "Nuts & bolts kit: $3", "Grip tape: $10", "Bearings: $30"};
//Use a parallel array to match index from list to match price from this array.
private double[] prices = {2.0, 3.0, 10.0, 20.0, 22.0, 24.0, 28.0, 30.0, 35.0, 40.0, 45.0, 50.0, 60.0};
private double subtotal, tax, total;
private JComboBox decks, trucks, wheels;
private JScrollPane scroll;
private JList Misc;
private ImageIcon skateboard;
private JLabel picture, subTotalOutput, taxOutput, totalOutput;
private String[] deck = {"The Master Thrasher $60", "The Dictator $45", "The Street King $50"};
private String[] truck = {"7.75 inch axle $35", "8 inch axle $40", "8.5 inch axle $45"};
private String[] wheel = {"51mm $20", "55mm $22", "58mm $24", "61mm $28"};
private String[] misc = {"Riser pads: $2", "Nuts & bolts kit: $3", "Grip tape: $10", "Bearings: $30"};
//Use a parallel array to match index from list to match price from this array.
private double[] prices = {2.0, 3.0, 10.0, 20.0, 22.0, 24.0, 28.0, 30.0, 35.0, 40.0, 45.0, 50.0, 60.0};
private double subtotal, tax, total;
private DecimalFormat tenths = new DecimalFormat("#0.0#");
private Border b1 = BorderFactory. createTitledBorder("Choose a Deck");
private Border b2 = BorderFactory. createTitledBorder("Choose your Trucks");
private Border b3 = BorderFactory. createTitledBorder("Choose some Wheels");
private Border b4 = BorderFactory. createTitledBorder("Misc...");
private Border b6 = BorderFactory. createTitledBorder("Receipt");
public SkateShop(){
setLayout(new GridLayout(3, 2));
panel1 = new JPanel();
decks = new JComboBox(deck);
decks.addActionListener(new ComboListener());
panel1.setBorder(b1);
panel1.add(decks);
panel2 = new JPanel();
trucks = new JComboBox(truck);
trucks.addActionListener(new ComboListener());
panel2.setBorder(b2);
panel2.add(trucks);
panel3 = new JPanel();
wheels = new JComboBox(wheel);
wheels.addActionListener(new ComboListener());
panel3.setBorder(b3);
panel3.add(wheels);
panel4 = new JPanel();
Misc = new JList(misc);
Misc.setVisibleRowCount(3);
Misc.setSelectionMode( ListSelectionModel.MULTIPLE_ INTERVAL_SELECTION);
Misc. addListSelectionListener(new ListListener());
scroll = new JScrollPane(Misc);
panel4.setBorder(b4);
panel4.add(scroll);
panel5 = new JPanel();
skateboard = new ImageIcon("skateboard.png");
picture = new JLabel();
picture.setIcon(skateboard);
panel5.add(picture);
panel6 = new JPanel();
panel6.setLayout(new GridLayout(3, 0));
subTotalOutput = new JLabel();
taxOutput = new JLabel();
totalOutput = new JLabel();
panel6.setBorder(b6);
panel6.add(subTotalOutput);
panel6.add(taxOutput);
panel6.add(totalOutput);
add(panel1);
add(panel2);
add(panel3);
add(panel4);
add(panel5);
add(panel6);
setSize(400, 300);
setTitle("Kevin's Skateshop");
setVisible(true);
setDefaultCloseOperation( EXIT_ON_CLOSE);
}
public static void main(String[] args) {
new SkateShop();
}
private Border b1 = BorderFactory.
private Border b2 = BorderFactory.
private Border b3 = BorderFactory.
private Border b4 = BorderFactory.
private Border b6 = BorderFactory.
public SkateShop(){
setLayout(new GridLayout(3, 2));
panel1 = new JPanel();
decks = new JComboBox(deck);
decks.addActionListener(new ComboListener());
panel1.setBorder(b1);
panel1.add(decks);
panel2 = new JPanel();
trucks = new JComboBox(truck);
trucks.addActionListener(new ComboListener());
panel2.setBorder(b2);
panel2.add(trucks);
panel3 = new JPanel();
wheels = new JComboBox(wheel);
wheels.addActionListener(new ComboListener());
panel3.setBorder(b3);
panel3.add(wheels);
panel4 = new JPanel();
Misc = new JList(misc);
Misc.setVisibleRowCount(3);
Misc.setSelectionMode(
Misc.
scroll = new JScrollPane(Misc);
panel4.setBorder(b4);
panel4.add(scroll);
panel5 = new JPanel();
skateboard = new ImageIcon("skateboard.png");
picture = new JLabel();
picture.setIcon(skateboard);
panel5.add(picture);
panel6 = new JPanel();
panel6.setLayout(new GridLayout(3, 0));
subTotalOutput = new JLabel();
taxOutput = new JLabel();
totalOutput = new JLabel();
panel6.setBorder(b6);
panel6.add(subTotalOutput);
panel6.add(taxOutput);
panel6.add(totalOutput);
add(panel1);
add(panel2);
add(panel3);
add(panel4);
add(panel5);
add(panel6);
setSize(400, 300);
setTitle("Kevin's Skateshop");
setVisible(true);
setDefaultCloseOperation(
}
public static void main(String[] args) {
new SkateShop();
}
public class ListListener implements ListSelectionListener {
@Override
public void valueChanged( ListSelectionEvent e) {
int[] index = Misc.getSelectedIndices();
for(int i: index){
if (i == 0){
subtotal += prices[0];
}
else if (i == 1){
subtotal += prices[1];
}
else if (i == 2){
subtotal += prices[2];
}
else if (i == 3){
subtotal += prices[7];
}else{}
}
}
}
public class ComboListener implements ActionListener {
@Override
public void actionPerformed(ActionEvent e) {
if (e.getSource() == decks){
int index = decks.getSelectedIndex();
if(index == 0){
subtotal += prices[12];
}
else if(index == 1){
subtotal += prices[10];
}
else if(index == 2){
subtotal += prices[11];
}else{}
}
else if (e.getSource() == trucks){
int index = trucks.getSelectedIndex();
if(index == 0){
subtotal += prices[8];
}
else if(index == 1){
subtotal += prices[9];
}
else if(index == 2){
subtotal += prices[10];
}else{}
}
else if (e.getSource() == wheels){
int index = wheels.getSelectedIndex();
if(index == 0){
subtotal += prices[3];
}
else if(index == 1){
subtotal += prices[4];
}
else if(index == 2){
subtotal += prices[5];
}
else if(index == 3){
subtotal += prices[6];
}else{}
}else{}
subTotalOutput.setText(" Subtotal: " + "$" + tenths.format(subtotal));
tax = (subtotal * 0.07);
total = (subtotal + tax);
taxOutput.setText("Tax: " + "$" + tenths.format(tax));
totalOutput.setText("Total: " + "$" + tenths.format(total));
}
}
}
public void valueChanged(
int[] index = Misc.getSelectedIndices();
for(int i: index){
if (i == 0){
subtotal += prices[0];
}
else if (i == 1){
subtotal += prices[1];
}
else if (i == 2){
subtotal += prices[2];
}
else if (i == 3){
subtotal += prices[7];
}else{}
}
}
}
public class ComboListener implements ActionListener {
@Override
public void actionPerformed(ActionEvent e) {
if (e.getSource() == decks){
int index = decks.getSelectedIndex();
if(index == 0){
subtotal += prices[12];
}
else if(index == 1){
subtotal += prices[10];
}
else if(index == 2){
subtotal += prices[11];
}else{}
}
else if (e.getSource() == trucks){
int index = trucks.getSelectedIndex();
if(index == 0){
subtotal += prices[8];
}
else if(index == 1){
subtotal += prices[9];
}
else if(index == 2){
subtotal += prices[10];
}else{}
}
else if (e.getSource() == wheels){
int index = wheels.getSelectedIndex();
if(index == 0){
subtotal += prices[3];
}
else if(index == 1){
subtotal += prices[4];
}
else if(index == 2){
subtotal += prices[5];
}
else if(index == 3){
subtotal += prices[6];
}else{}
}else{}
subTotalOutput.setText("
tax = (subtotal * 0.07);
total = (subtotal + tax);
taxOutput.setText("Tax: " + "$" + tenths.format(tax));
totalOutput.setText("Total:
}
}
}
----
// other code to help fix
class ShopItem {
public final static DecimalFormat fmt = new DecimalFormat("#0.00");
private final String name;
private final double price;
public ShopItem(String name, double price) {
this.name = name;
this.price = price;
}
public String toString() { return name + " $" + fmt.format(price); }
public String getName() { return name; }
public double getPrice() { return price; }
}
class SkateShop extends JFrame{
private final int CB_DECK=0, CB_TRUCK=1, CB_WHEELS=2, CB_SIZE=3;
private final ShopItem[] singles = new ShopItem[CB_SIZE];
private final java.util.List<ShopItem> misc;
private final JLabel subTotalOutput, taxOutput, totalOutput;
public SkateShop(){
this.subTotalOutput = new JLabel();
this.taxOutput = new JLabel();
this.totalOutput = new JLabel();
setLayout(new GridLayout(3, 2));
JPanel panel1 = getItemPanel(
CB_DECK,
"Choose a Deck",
new ShopItem [] {
new ShopItem("The Master Thrasher", 60),
new ShopItem("The Dictator", 45),
new ShopItem("The Street King", 50),
});
this.misc = new java.util.ArrayList<ShopItem>( );
JPanel panel4 = getListItemPanel(this.misc, "Misc...",
new ShopItem [] {
new ShopItem("Riser pads", 2),
new ShopItem("Nuts & bolts kit", 3),
new ShopItem("Grip tape", 10),
new ShopItem("Bearings", 30),
});
JPanel panel6 = new JPanel();
panel6.setLayout(new GridLayout(3, 0));
panel6.setBorder( BorderFactory. createTitledBorder("Receipt")) ;
panel6.add(subTotalOutput);
panel6.add(taxOutput);
panel6.add(totalOutput);
add(panel1);
add(panel4);
add(panel6);
setSize(400, 300);
setTitle("Kevin's Skateshop");
setDefaultCloseOperation(EXIT_ ON_CLOSE);
setVisible(true);
}
private JPanel getItemPanel(final int index, String name, ShopItem [] items) {
JPanel panel = new JPanel();
panel.setBorder(BorderFactory. createTitledBorder(name));
final JComboBox<ShopItem> cb = new JComboBox<ShopItem>(items);
cb.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
singles[index] = (ShopItem)cb.getSelectedItem() ;
updateTotal();
}
});
panel.add(cb);
return panel;
}
private JPanel getListItemPanel(final java.util.List<ShopItem> chosen, String name, ShopItem [] items) {
JPanel panel = new JPanel();
panel.setBorder(BorderFactory. createTitledBorder(name));
final JList<ShopItem> jlist = new JList<ShopItem>(items);
jlist.setVisibleRowCount(3);
jlist.setSelectionMode( ListSelectionModel.MULTIPLE_ INTERVAL_SELECTION);
jlist. addListSelectionListener(new ListSelectionListener() {
public void valueChanged( ListSelectionEvent e) {
chosen.clear();
for(ShopItem item : jlist.getSelectedValuesList()) {
chosen.add(item);
}
updateTotal();
}
});
panel.add(new JScrollPane(jlist));
return panel;
}
private double getSubTotal() {
double tot = 0;
for(ShopItem item : singles) {
tot += (item==null) ? 0 : item.getPrice();
}
for(ShopItem item : misc) { tot += item.getPrice(); }
return tot;
}
private void displayValue(JLabel lab, String name, double value) {
lab.setText(name + ": " + "$" + ShopItem.fmt.format(value));
}
private void updateTotal() {
double subtotal = getSubTotal();
double tax = subtotal * 0.07;
displayValue(subTotalOutput, "Subtotal", subtotal);
displayValue(taxOutput, "Tax", tax);
displayValue(totalOutput, "Total", subtotal + tax);
}
}
No comments:
Post a Comment