Skip to content

Instantly share code, notes, and snippets.

@jenndotcodes
Created September 22, 2014 19:06
Show Gist options
  • Save jenndotcodes/b15b610aaf5c6ffc9282 to your computer and use it in GitHub Desktop.
Save jenndotcodes/b15b610aaf5c6ffc9282 to your computer and use it in GitHub Desktop.
Here is the template for the Java TShirt order form, write in the if logic to calculate the price of an order.
package tshirt;
import java.awt.BorderLayout;
import java.awt.Dimension;
import java.awt.EventQueue;
import java.awt.Font;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.*;
import javax.swing.Box;
import javax.swing.BoxLayout;
import javax.swing.ButtonGroup;
import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JCheckBox;
import javax.swing.JComboBox;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JRadioButton;
import javax.swing.SwingConstants;
public class TShirtOrderForm
{
public static void main(String[] args)
{
EventQueue.invokeLater(new Runnable()
{
public void run()
{
TShirtOrderFrame tsf = new TShirtOrderFrame();
tsf.setTitle("T-Shirt Order Form");
tsf.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
tsf.setVisible(true);
}
});
}
}
class TShirtOrderFrame extends JFrame
{
public TShirtOrderFrame()
{
TShirtOrderPanel tsp = new TShirtOrderPanel();
add(tsp);
pack();
}
}
class TShirtOrderPanel extends JPanel implements ActionListener
{
// Arrays used for the values in the combo box
private String[] sizes = { "S", "M", "L", "XL" };
private String[] colors = { "Black", "White", "Red", "Pink", "Green",
"Yellow" };
private String[] quantities = { "1", "5", "10", "20", "100" };
// Save the IWriteCodeStamp.jpg in the root
// folder of your project
private ImageIcon shirtImage = new ImageIcon("IWriteCodeStamp.jpg");
// Declare all the controls that will be on the order form.
private JLabel imageLabel;
private JRadioButton mensRadioButton;
private JRadioButton womensRadioButton;
private JComboBox sizeCombo;
private JComboBox colorCombo;
private JLabel quantityLabel;
private JLabel labelTitle;
private JComboBox quantityCombo;
private JCheckBox baseballSleeves;
private JButton calcButton;
private JLabel resultLabel;
private JLabel sizeLabel;
private JLabel colorLabel;
public TShirtOrderPanel()
{
// Instantiate all the controls
imageLabel = new JLabel(shirtImage);
mensRadioButton = new JRadioButton("Mens");
womensRadioButton = new JRadioButton("Womens");
sizeCombo = new JComboBox(sizes);
colorCombo = new JComboBox(colors);
quantityLabel = new JLabel("Quantity");
labelTitle = new JLabel("TShirt Orderform");
quantityCombo = new JComboBox(quantities);
baseballSleeves = new JCheckBox("Baseball Sleeves?");
sizeLabel = new JLabel("Size:");
colorLabel = new JLabel("Color:");
calcButton = new JButton("Total");
resultLabel = new JLabel("");
// Pretty up the labels
labelTitle.setFont(new Font("Impact", Font.BOLD, 28));
labelTitle.setHorizontalAlignment(SwingConstants.CENTER);
resultLabel.setFont(new Font("Impact", Font.PLAIN, 16));
resultLabel.setHorizontalAlignment(SwingConstants.CENTER);
// Add the radio buttons to a button group so only one
// can be selected at a timemensRadioButton.setSelected(true);
ButtonGroup dept = new ButtonGroup();
dept.add(mensRadioButton);
dept.add(womensRadioButton);
// Arrange the controls onto the screen
this.setLayout(new BorderLayout());
this.add(labelTitle, BorderLayout.NORTH);
this.add(imageLabel, BorderLayout.WEST);
JPanel centerPanel = new JPanel();
this.add(centerPanel, BorderLayout.CENTER);
centerPanel.setLayout(new BoxLayout(centerPanel, BoxLayout.Y_AXIS));
JPanel deptPanel = new JPanel();
deptPanel.add(mensRadioButton);
deptPanel.add(womensRadioButton);
JPanel comboPanel = new JPanel();
comboPanel.add(sizeLabel);
comboPanel.add(sizeCombo);
comboPanel.add(colorLabel);
comboPanel.add(colorCombo);
JPanel quantPanel = new JPanel();
quantPanel.add(quantityLabel);
quantPanel.add(quantityCombo);
JPanel sleevesPanel = new JPanel();
sleevesPanel.add(baseballSleeves);
sleevesPanel.add(calcButton);
centerPanel.add(deptPanel);
centerPanel.add(comboPanel);
centerPanel.add(quantPanel);
centerPanel.add(sleevesPanel);
centerPanel.add(Box.createGlue());
add(resultLabel, BorderLayout.SOUTH);
// Make button responsive to clicks
calcButton.addActionListener(this);
}
@Override
public Dimension getPreferredSize()
{
return new Dimension(500, 300);
}
public void actionPerformed(ActionEvent arg0)
{
boolean mens = mensRadioButton.isSelected();
boolean womens = womensRadioButton.isSelected();
boolean sleeves = baseballSleeves.isSelected();
String size = sizeCombo.getSelectedItem().toString();
String color = colorCombo.getSelectedItem().toString();
int quantity = Integer.parseInt(quantityCombo.getSelectedItem()
.toString());
// Use decision statements to calculate the total for the
// Women's Small: 12.00
// Women's Medium: 13.00
// Women's Large: 14.00
// Women's XLarge: 15.00
// For men's sizes add 1.00
// For baseball sleeves add 2.00
// For the color red add 1.00
// If 5 shirts are ordered a 5% discount is applied
// if 10 shirts are ordered a 10% discount is applied
// If 20 shirts are ordered a 15% discount is applied
// If 100 shirts are ordered a 25% discount is applied
// Create a string that describes the details of the order including
// size, color, quantity, department, if baseball sleeves are included
// and order the total.
String orderDetails = "5 Men's Shirt(s) in Black with baseball sleeves. \n Total: 2,000$";
// Create a string with all theorder details
resultLabel.setText(orderDetails);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment