Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save salamander2/a1fb5eb1473c06cdbc22 to your computer and use it in GitHub Desktop.
Save salamander2/a1fb5eb1473c06cdbc22 to your computer and use it in GitHub Desktop.
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.border.*;
public class Layout2 extends JApplet {
// 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
jbClear.addActionListener(new ClearButton());
jbClearAll.addActionListener(new ClearAllButton());
jcbMainItem1.addActionListener(new MainItemCB1());
jcbMainItem2.addActionListener(new MainItemCB2());
jcbMainItem3.addActionListener(new MainItemCB3());
jcbMainItem4.addActionListener(new MainItemCB4());
jcbMainItem5.addActionListener(new MainItemCB5());
jcbSideItem1.addActionListener(new SideItemCB1());
jcbSideItem2.addActionListener(new SideItemCB2());
jcbSideItem3.addActionListener(new SideItemCB3());
jcbSideItem4.addActionListener(new SideItemCB4());
jcbSideItem5.addActionListener(new SideItemCB5());
jcbDrinkItem1.addActionListener(new DrinkItemCB1());
jcbDrinkItem2.addActionListener(new DrinkItemCB2());
jcbDrinkItem3.addActionListener(new DrinkItemCB3());
jcbDrinkItem4.addActionListener(new DrinkItemCB4());
jcbDrinkItem5.addActionListener(new DrinkItemCB5());
jbTotal.addActionListener(new ButtonListener());
}
// 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 ClearButton 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;
}
}
class ClearAllButton implements ActionListener {
@Override
public void actionPerformed(ActionEvent e) {
receiptList[0] = null;
jlReceipt.updateUI();
jtfSubtotal.setText(null);
jtfSubtotal.updateUI();
subTotal = 0;
}
}
// main menu check box action listener
class MainItemCB1 implements ActionListener {
@Override
public void actionPerformed(ActionEvent e) {
updateReceipt();
}
}
class MainItemCB2 implements ActionListener {
@Override
public void actionPerformed(ActionEvent e) {
updateReceipt();
}
}
class MainItemCB3 implements ActionListener {
@Override
public void actionPerformed(ActionEvent e) {
updateReceipt();
}
}
class MainItemCB4 implements ActionListener {
@Override
public void actionPerformed(ActionEvent e) {
updateReceipt();
}
}
class MainItemCB5 implements ActionListener {
@Override
public void actionPerformed(ActionEvent e) {
updateReceipt();
}
}
class SideItemCB1 implements ActionListener {
@Override
public void actionPerformed(ActionEvent e) {
updateReceipt();
}
}
class SideItemCB2 implements ActionListener {
@Override
public void actionPerformed(ActionEvent e) {
updateReceipt();
}
}
class SideItemCB3 implements ActionListener {
@Override
public void actionPerformed(ActionEvent e) {
updateReceipt();
}
}
class SideItemCB4 implements ActionListener {
@Override
public void actionPerformed(ActionEvent e) {
updateReceipt();
}
}
class SideItemCB5 implements ActionListener {
@Override
public void actionPerformed(ActionEvent e) {
updateReceipt();
}
}
class DrinkItemCB1 implements ActionListener {
@Override
public void actionPerformed(ActionEvent e) {
updateReceipt();
}
}
class DrinkItemCB2 implements ActionListener {
@Override
public void actionPerformed(ActionEvent e) {
updateReceipt();
}
}
class DrinkItemCB3 implements ActionListener {
@Override
public void actionPerformed(ActionEvent e) {
updateReceipt();
}
}
class DrinkItemCB4 implements ActionListener {
@Override
public void actionPerformed(ActionEvent e) {
updateReceipt();
}
}
class DrinkItemCB5 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;
receiptIndex++;
subTotal += amount1;
jtfSubtotal.setText(String.format("$ %.2f", subTotal));
jlReceipt.updateUI();
}
if (jcbMainItem2.isSelected()) {
receiptList[receiptIndex] = "Fetuccine..........$" + amount2;
receiptIndex++;
subTotal += amount2;
jtfSubtotal.setText(String.format("$ %.2f", subTotal));
jlReceipt.updateUI();
}
if (jcbMainItem3.isSelected()) {
receiptList[receiptIndex] = "Ravioli..........$" + amount3;
// receiptIndex++;
subTotal += amount3;
jtfSubtotal.setText(String.format("$ %.2f", subTotal));
jlReceipt.updateUI();
}
if (jcbMainItem4.isSelected()) {
receiptList[receiptIndex] = "Grlc Chkn..........$" + amount4;
receiptIndex++;
subTotal += amount4;
jtfSubtotal.setText(String.format("$ %.2f", subTotal));
jlReceipt.updateUI();
}
if (jcbMainItem5.isSelected()) {
receiptList[receiptIndex] = "Soup..........$" + amount5;
receiptIndex++;
subTotal += amount5;
jtfSubtotal.setText(String.format("$ %.2f", subTotal));
jlReceipt.updateUI();
}
if (jcbSideItem1.isSelected()) {
receiptList[receiptIndex] = "Caesar Salad";
receiptIndex++;
jlReceipt.updateUI();
}
if (jcbSideItem2.isSelected()) {
receiptList[receiptIndex] = "Side Salad";
receiptIndex++;
jlReceipt.updateUI();
}
if (jcbSideItem3.isSelected()) {
receiptList[receiptIndex] = "Breadsticks";
receiptIndex++;
jlReceipt.updateUI();
}
if (jcbSideItem4.isSelected()) {
receiptList[receiptIndex] = "Garlic Bread";
receiptIndex++;
jlReceipt.updateUI();
}
if (jcbSideItem5.isSelected()) {
receiptList[receiptIndex] = "Doge";
receiptIndex++;
jlReceipt.updateUI();
}
if (jcbDrinkItem1.isSelected()) {
receiptList[receiptIndex] = "Soda Pop";
receiptIndex++;
jlReceipt.updateUI();
}
if (jcbDrinkItem2.isSelected()) {
receiptList[receiptIndex] = "Beer";
receiptIndex++;
jlReceipt.updateUI();
}
if (jcbDrinkItem3.isSelected()) {
receiptList[receiptIndex] = "Milk";
receiptIndex++;
jlReceipt.updateUI();
}
if (jcbDrinkItem4.isSelected()) {
receiptList[receiptIndex] = "Water";
receiptIndex++;
jlReceipt.updateUI();
}
if (jcbDrinkItem5.isSelected()) {
receiptList[receiptIndex] = "Juice";
receiptIndex++;
jlReceipt.updateUI();
}
}
}
@salamander2
Copy link
Author

Just coalesced the many actionListeners into three. Also removed duplicate code from updateReceipt()

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment