Skip to content

Instantly share code, notes, and snippets.

@salamander2
Forked from gabempayne/gist:5563438f05a6427c7dda
Last active August 29, 2015 14:01
Show Gist options
  • Save salamander2/3089dcaf997561e5efb2 to your computer and use it in GitHub Desktop.
Save salamander2/3089dcaf997561e5efb2 to your computer and use it in GitHub Desktop.
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.border.*;
// Make it a JFrame instead
//public class Layout2 extends JApplet {
public class Layout2 extends JFrame {
public static void main(String args[]){
new Layout2();
}
// string array for jlist
private String[] receiptList = new String[15];
{
receiptList[0] = "No Items Selected ";
}
private int receiptIndex = 0;
private double subTotal = 0.0;
private double total = 0.0;
// buttons for calculating total cost and removing items from the receipt
private JButton jbTotal = new JButton("Total");
private JButton jbClearAll = new JButton("Clear All");
private JButton jbClear = new JButton("Clear");
// list for receipt and text area for dietary needs
private JList jlReceipt = new JList(receiptList);
private JTextArea jtaDiet = new JTextArea();
// text fields for menu item quantities
private JTextField jtfMainItem1 = new JTextField();
private JTextField jtfMainItem2 = new JTextField();
private JTextField jtfMainItem3 = new JTextField();
private JTextField jtfMainItem4 = new JTextField();
private JTextField jtfMainItem5 = new JTextField();
private JTextField jtfSideItem1 = new JTextField(1);
private JTextField jtfSideItem2 = new JTextField(1);
private JTextField jtfSideItem3 = new JTextField(1);
private JTextField jtfSideItem4 = new JTextField(1);
private JTextField jtfSideItem5 = new JTextField(1);
private JTextField jtfDrinkItem1 = new JTextField();
private JTextField jtfDrinkItem2 = new JTextField();
private JTextField jtfDrinkItem3 = new JTextField();
private JTextField jtfDrinkItem4 = new JTextField();
private JTextField jtfDrinkItem5 = new JTextField();
private JTextField jtfSubtotal = new JTextField();
private JTextField jtfTotal = new JTextField();
// menu item check boxes
private JCheckBox jcbMainItem1 = new JCheckBox();
private JCheckBox jcbMainItem2 = new JCheckBox();
private JCheckBox jcbMainItem3 = new JCheckBox();
private JCheckBox jcbMainItem4 = new JCheckBox();
private JCheckBox jcbMainItem5 = new JCheckBox();
// main menu item combo boxes
private JComboBox jcbItemSelct1 = new JComboBox(new Object[] { "Red Sauce",
"Alfredo", "Meat Sauce", "Plain" });
private JComboBox jcbItemSelct2 = new JComboBox(new Object[] { "Red Sauce",
"Alfredo", "Meat Sauce", "Plain" });
private JComboBox jcbItemSelct3 = new JComboBox(new Object[] { "Red Sauce",
"Plain" });
private JComboBox jcbItemSelct4 = new JComboBox(new Object[] {
"Xtra Sauce", "Plain" });
private JComboBox jcbItemSelct5 = new JComboBox(new Object[] {
"Minestrone", "Zuppa", "Brocolli", "Italian" });
// sides menu check boxes
private JCheckBox jcbSideItem1 = new JCheckBox();
private JCheckBox jcbSideItem2 = new JCheckBox();
private JCheckBox jcbSideItem3 = new JCheckBox();
private JCheckBox jcbSideItem4 = new JCheckBox();
private JCheckBox jcbSideItem5 = new JCheckBox();
// drinks check boxes
private JCheckBox jcbDrinkItem1 = new JCheckBox();
private JCheckBox jcbDrinkItem2 = new JCheckBox();
private JCheckBox jcbDrinkItem3 = new JCheckBox();
private JCheckBox jcbDrinkItem4 = new JCheckBox();
private JCheckBox jcbDrinkItem5 = new JCheckBox();
// payment type radio buttons
private JRadioButton jrbCheck = new JRadioButton("Check");
private JRadioButton jrbCreditDebit = new JRadioButton("Credit/Debit");
private JRadioButton jrbCash = new JRadioButton("Cash");
public Layout2() {
// scroll bars for diet needs and receipt
JScrollPane jspDrinksietScroll = new JScrollPane(jtaDiet);
JScrollPane jspReceiptScroll = new JScrollPane(jlReceipt);
jspReceiptScroll.setSize(250, 40);
ButtonGroup bgPay = new ButtonGroup();
bgPay.add(jrbCheck);
bgPay.add(jrbCreditDebit);
bgPay.add(jrbCash);
// main menu panel
JPanel pMain = new JPanel(new GridLayout(5, 4));
pMain.add(new JLabel("Spaghetti"));
pMain.add(jcbItemSelct1);
pMain.add(jcbMainItem1);
pMain.add(jtfMainItem1);
pMain.add(new JLabel("Fettucine"));
pMain.add(jcbItemSelct2);
pMain.add(jcbMainItem2);
pMain.add(jtfMainItem2);
pMain.add(new JLabel("Ravioli"));
pMain.add(jcbItemSelct3);
pMain.add(jcbMainItem3);
pMain.add(jtfMainItem3);
pMain.add(new JLabel("Grlc Chkn"));
pMain.add(jcbItemSelct4);
pMain.add(jcbMainItem4);
pMain.add(jtfMainItem4);
pMain.add(new JLabel("Soup"));
pMain.add(jcbItemSelct5);
pMain.add(jcbMainItem5);
pMain.add(jtfMainItem5);
pMain.setBorder(new TitledBorder("Main Menu"));
// sides menu
JPanel pSides = new JPanel(new GridLayout(5, 3));
pSides.add(new JLabel("Caesar Salad"));
pSides.add(jcbSideItem1);
pSides.add(jtfSideItem1);
pSides.add(new JLabel("Side Salad"));
pSides.add(jcbSideItem2);
pSides.add(jtfSideItem2);
pSides.add(new JLabel("Breadsticks"));
pSides.add(jcbSideItem3);
pSides.add(jtfSideItem3);
pSides.add(new JLabel("Garlic Bread"));
pSides.add(jcbSideItem4);
pSides.add(jtfSideItem4);
pSides.add(new JLabel("Doge"));
pSides.add(jcbSideItem5);
pSides.add(jtfSideItem5);
pSides.setBorder(new TitledBorder("Sides"));
// Drink Menu
JPanel pDrinks = new JPanel(new GridLayout(5, 3));
pDrinks.add(new JLabel("Soda Pop"));
pDrinks.add(jcbDrinkItem1);
pDrinks.add(jtfDrinkItem1);
pDrinks.add(new JLabel("Beer"));
pDrinks.add(jcbDrinkItem2);
pDrinks.add(jtfDrinkItem2);
pDrinks.add(new JLabel("Milk"));
pDrinks.add(jcbDrinkItem3);
pDrinks.add(jtfDrinkItem3);
pDrinks.add(new JLabel("Water"));
pDrinks.add(jcbDrinkItem4);
pDrinks.add(jtfDrinkItem4);
pDrinks.add(new JLabel("Juice"));
pDrinks.add(jcbDrinkItem5);
pDrinks.add(jtfDrinkItem5);
pDrinks.setBorder(new TitledBorder("Drinks"));
// diet panel
JPanel pDrinksiet = new JPanel(new GridLayout(1, 1));
pDrinksiet.add(jspDrinksietScroll);
pDrinksiet.setBorder(new TitledBorder(
"Please Specify Any Dietary Preferences"));
// receipt panels
JPanel pClear = new JPanel(new GridLayout());
pClear.add(jbClear);
pClear.add(jbClearAll);
JPanel pList = new JPanel(new FlowLayout());
pList.add(jspReceiptScroll);
JPanel pTotal = new JPanel(new GridLayout(2, 2));
pTotal.add(new Label("SUBTOTAL"));
pTotal.add(jtfSubtotal);
pTotal.add(jbTotal);
pTotal.add(jtfTotal);
JPanel pPay = new JPanel(new FlowLayout());
pPay.add(jrbCheck);
pPay.add(jrbCreditDebit);
pPay.add(jrbCash);
JPanel pSidesubReceipt = new JPanel(new BorderLayout());
pSidesubReceipt.add(pClear, BorderLayout.NORTH);
pSidesubReceipt.add(pList, BorderLayout.CENTER);
JPanel pSidesubReceipt2 = new JPanel(new BorderLayout());
pSidesubReceipt2.add(pTotal, BorderLayout.CENTER);
pSidesubReceipt2.add(pPay, BorderLayout.SOUTH);
JPanel pReceipt = new JPanel(new BorderLayout());
pReceipt.setPreferredSize(new Dimension(250, 250));
pReceipt.add(pSidesubReceipt, BorderLayout.CENTER);
pReceipt.add(pSidesubReceipt2, BorderLayout.SOUTH);
// large panel groupSides
JPanel pBottom = new JPanel(new BorderLayout());
pBottom.add(pDrinksiet, BorderLayout.CENTER);
pBottom.add(pReceipt, BorderLayout.EAST);
// menu panel
JPanel pMenu = new JPanel(new GridLayout(0, 3));
pMenu.add(pSides);
pMenu.add(pMain);
pMenu.add(pDrinks);
// assembly
add(pMenu, BorderLayout.CENTER);
add(pBottom, BorderLayout.SOUTH);
add(new Label("Da Resturant"), BorderLayout.NORTH);
// Button Listeners
ActionListener clearAL = new ClearButtonAL();
ActionListener mainAL = new MainItemAL();
jbClear.addActionListener(clearAL);
jbClearAll.addActionListener(clearAL);
jcbMainItem1.addActionListener(mainAL);
jcbMainItem2.addActionListener(mainAL);
jcbMainItem3.addActionListener(mainAL);
jcbMainItem4.addActionListener(mainAL);
jcbMainItem5.addActionListener(mainAL);
jcbSideItem1.addActionListener(mainAL);
jcbSideItem2.addActionListener(mainAL);
jcbSideItem3.addActionListener(mainAL);
jcbSideItem4.addActionListener(mainAL);
jcbSideItem5.addActionListener(mainAL);;
jcbDrinkItem1.addActionListener(mainAL);
jcbDrinkItem2.addActionListener(mainAL);
jcbDrinkItem3.addActionListener(mainAL);
jcbDrinkItem4.addActionListener(mainAL);
jcbDrinkItem5.addActionListener(mainAL);
jbTotal.addActionListener(new ButtonListener());
//for JFrame
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
this.setSize(900,500);
this.validate();
this.setVisible(true);
}
// total amount
private class ButtonListener implements ActionListener {
@Override
public void actionPerformed(ActionEvent arg0) {
total = subTotal + (subTotal * .075);
jtfTotal.setText(String.format("$ %.2f", total));
// jlReceipt.updateUI();
}
}
class ClearButtonAL implements ActionListener {
@Override
public void actionPerformed(ActionEvent e) {
receiptList[0] = null;
jlReceipt.updateUI();
jtfSubtotal.setText(null);// added to clear subtotal using clear button.
jtfSubtotal.updateUI();//it only clears one item and doesn't update to allow re-adding items.
subTotal = 0;
}
}
// main menu check box action listener
class MainItemAL implements ActionListener {
@Override
public void actionPerformed(ActionEvent e) {
updateReceipt();
}
}
private void updateReceipt() {
double amount1 = 10.00;
double amount2 = 9.00;
double amount3 = 7.00;
double amount4 = 5.00;
double amount5 = 8.00;
// update receipt()
// replace with a switch-case?
if (jcbMainItem1.isSelected()) {
receiptList[receiptIndex] = "Spaghetti..........$" + amount1;
subTotal += amount1;
jtfSubtotal.setText(String.format("$ %.2f", subTotal));
}
if (jcbMainItem2.isSelected()) {
receiptList[receiptIndex] = "Fetuccine..........$" + amount2;
subTotal += amount2;
jtfSubtotal.setText(String.format("$ %.2f", subTotal));
}
if (jcbMainItem3.isSelected()) {
receiptList[receiptIndex] = "Ravioli..........$" + amount3;
subTotal += amount3;
jtfSubtotal.setText(String.format("$ %.2f", subTotal));
}
if (jcbMainItem4.isSelected()) {
receiptList[receiptIndex] = "Grlc Chkn..........$" + amount4;
subTotal += amount4;
jtfSubtotal.setText(String.format("$ %.2f", subTotal));
}
if (jcbMainItem5.isSelected()) {
receiptList[receiptIndex] = "Soup..........$" + amount5;
subTotal += amount5;
jtfSubtotal.setText(String.format("$ %.2f", subTotal));
}
if (jcbSideItem1.isSelected()) {
receiptList[receiptIndex] = "Caesar Salad";
}
if (jcbSideItem2.isSelected()) {
receiptList[receiptIndex] = "Side Salad";
}
if (jcbSideItem3.isSelected()) {
receiptList[receiptIndex] = "Breadsticks";
}
if (jcbSideItem4.isSelected()) {
receiptList[receiptIndex] = "Garlic Bread";
}
if (jcbSideItem5.isSelected()) {
receiptList[receiptIndex] = "Doge";
}
if (jcbDrinkItem1.isSelected()) {
receiptList[receiptIndex] = "Soda Pop";
}
if (jcbDrinkItem2.isSelected()) {
receiptList[receiptIndex] = "Beer";
}
if (jcbDrinkItem3.isSelected()) {
receiptList[receiptIndex] = "Milk";
}
if (jcbDrinkItem4.isSelected()) {
receiptList[receiptIndex] = "Water";
}
if (jcbDrinkItem5.isSelected()) {
receiptList[receiptIndex] = "Juice";
}
receiptIndex++;
jlReceipt.updateUI();
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment