// Project: RestaurantBillCalculator.java Calculates a table's bill.
// Import statements for different classes
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTextField;
import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JComponent;
import java.sql.Connection;
import java.sql.Statement;
import java.sql.ResultSet;
import java.util.ArrayList;
import java.awt.Container;
import javax.swing.BorderFactory;
import java.awt.Font;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.sql.DriverManager;
import java.sql.SQLException;
import java.sql.ResultSetMetaData;
import java.sql.ResultSet;
import java.awt.event.ItemEvent;
import java.awt.event.ItemListener;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import javax.swing.JOptionPane;
public class RestaurantBillCalculator extends JFrame
{
static final String DATABASE_URL = "jdbc:mysql://localhost/restaurant";
static final String DATABASE_USERNAME = "root";
static final String DATABASE_PASSWORD = "password";
// JLabel for Restaurant
private JLabel restaurantJLabel;
// JPanel to display waiter information
private JPanel waiterJPanel;
// JLabel and JTextField for table number
private JLabel tableNumberJLabel;
private JTextField tableNumberJTextField;
// JLabel and JTextField for waiter name
private JLabel waiterNameJLabel;
private JTextField waiterNameJTextField;
// JPanel to display menu items
private JPanel menuItemsJPanel;
// JLabel and JComboBox for beverage
private JLabel beverageJLabel;
private JComboBox beverageJComboBox;
// JLabel and JComboBox for appetizer
private JLabel appetizerJLabel;
private JComboBox appetizerJComboBox;
// JLabel and JComboBox for main course
private JLabel mainCourseJLabel;
private JComboBox mainCourseJComboBox;
// JLabel and JComboBox for dessert
private JLabel dessertJLabel;
private JComboBox dessertJComboBox;
// JButton for calculate bill
private JButton calculateBillJButton;
// JLabel and JTextField for subtotal
private JLabel subtotalJLabel;
private JTextField subtotalJTextField;
// JLabel and JTextField for tax
private JLabel taxJLabel;
private JTextField taxJTextField;
// JLabel and JTextField for total
private JLabel totalJLabel;
private JTextField totalJTextField;
// constant for tax rate
private final static double TAX_RATE = 0.05;
// declare instance variables for database processing
private Connection myConnection;
private Statement myStatement;
private ResultSet myResultSet;
// declare instance variable ArrayList to hold bill items
private ArrayList billItems = new ArrayList();
// Constructor
public RestaurantBillCalculator( String databaseUserName, String databasePassword )
{
try
{
myConnection = DriverManager.getConnection
(DATABASE_URL, DATABASE_USERNAME, DATABASE_PASSWORD);
myStatement = myConnection.createStatement();
myResultSet = myStatement.executeQuery(DATABASE_URL);
}
catch ( SQLException sqlException )
{
sqlException.printStackTrace();
System.exit( 1 );
} // end catch
finally
{
try
{
myResultSet.close();
myStatement.close();
myConnection.close();
} // end of try block
catch ( SQLException SQLexception )
{
SQLexception.printStackTrace();
} // end catch block
} // end of finally
} // end constructor
// create and position GUI components; register event handlers
private void createUserInterface()
{
// get content pane for attaching GUI components
Container contentPane = getContentPane();
// enable explicit positioning of GUI components
contentPane.setLayout( null );
// set up restaurantJLabel
restaurantJLabel = new JLabel("Restaurant");
restaurantJLabel.setFont(new Font("Times New Roman", Font.BOLD, 14));
restaurantJLabel.setBounds(30, 50, 100, 100);
waiterJPanel = new JPanel();
waiterJPanel.setLayout(null);
waiterJPanel.setBounds(20, 50, 100, 100);
waiterJPanel.setBorder(BorderFactory.createTitledBorder
(BorderFactory.createEtchedBorder(),"Waiter Information"));
menuItemsJPanel = new JPanel();
menuItemsJPanel.setLayout(null);
menuItemsJPanel.setBounds(20, 50, 200, 200);
waiterJPanel.setBorder(BorderFactory.createTitledBorder
(BorderFactory.createEtchedBorder(),"Menu Items"));
calculateBillJButton = new JButton("Calculate Button");
calculateBillJButton.setLayout(null);
calculateBillJButton.setBounds(250, 250, 50, 50);
// public void actionPerformed( ActionEvent event)
// {
// calculateBillJButtonActionPerformed( event );
// }
//
subtotalJLabel = new JLabel("Sub Total");
subtotalJTextField = new JTextField();
taxJLabel = new JLabel("Tax");
taxJTextField = new JTextField();
totalJLabel = new JLabel("Total");
totalJTextField = new JTextField();
// **** TODO ****** set properties of application's window
// **** TODO ****** ensure database connection is closed
// **** TODO ****** when user quits application
addWindowListener(
new WindowAdapter()anonymous inner class
{
// event handler called when close button is clicked
public void windowClosing( WindowEvent event )
{
frameWindowClosing( event );
}
} // end anonymous inner class
); // end addWindowListener
} // end method createUserInterface
// **** TODO ****** set up waiterJPanel
private void createWaiterJPanel()
{
// set up waiterJPanel
waiterJPanel = new JPanel();
waiterJPanel.setLayout( null );
waiterJPanel.setBounds( 20, 152, 232, 152 );
waiterJPanel.setBorder( BorderFactory.createTitledBorder(
BorderFactory.createEtchedBorder(), "Waiter Information" ) );
// set up tableNumberJLabel
tableNumberJLabel = new JLabel();
tableNumberJLabel.setBounds( 8, 24, 80, 24 );
tableNumberJLabel.setText( "Table Number:" );
waiterJPanel.add( tableNumberJLabel );
// set up tableNumberJTextField
tableNumberJTextField = new JTextField();
tableNumberJTextField.setBounds(10, 30, 90, 30);
waiterJPanel.add( tableNumberJTextField );
// set up waiterNameJLabel
waiterNameJLabel = new JLabel();
waiterNameJLabel.setBounds( 8, 24, 80, 24 );
waiterNameJLabel.setText( "Waiter Name:" );
waiterJPanel.add( waiterNameJLabel );
// set up waiterNameJTextField
waiterNameJTextField = new JTextField();
waiterNameJTextField.setBounds(10, 30, 90, 30);
waiterJPanel.add( waiterNameJTextField );
} // end method createWaiterJPanel
// create menuItemsJPanel
private void createMenuItemsJPanel()
{
// set up menuItemsJPanel
menuItemsJPanel = new JPanel();
menuItemsJPanel.setBounds( 20, 152, 232, 152 );
menuItemsJPanel.setBorder( BorderFactory.createTitledBorder(
BorderFactory.createEtchedBorder(), "Menu Items" ) );
menuItemsJPanel.setLayout( null );
//*******************************************************************
// set up beverageJLabel
beverageJLabel = new JLabel();
beverageJLabel.setBounds( 8, 24, 80, 24 );
beverageJLabel.setText( "Beverage:" );
menuItemsJPanel.add( beverageJLabel );
// set up beverageJComboBox
beverageJComboBox = new JComboBox();
beverageJComboBox.setBounds( 88, 24, 128, 25 );
menuItemsJPanel.add( beverageJComboBox );
beverageJComboBox.addItemListener(
new ItemListener() // **** TODO ****** anonymous inner class
{
// **** TODO ****** event handler called when item in beverageJComboBox
// **** TODO ****** is selected
public void itemStateChanged( ItemEvent event )
{
beverageJComboBoxItemStateChanged( event );
}
} // end anonymous inner class
); // end addItemListener
// add items to beverageJComboBox
beverageJComboBox.addItem("Minerals");
beverageJComboBox.addItem("Tea");
beverageJComboBox.addItem("Coffee");
beverageJComboBox.addItem("Minerla Water");
beverageJComboBox.addItem("Fruit Juice");
beverageJComboBox.addItem("Milk");
//******************************************************************
// set up appetizerJLabel
appetizerJLabel = new JLabel();
appetizerJLabel.setBounds( 8, 24, 80, 24 );
appetizerJLabel.setText( "Appetizer:" );
menuItemsJPanel.add( appetizerJLabel );
// set up appetizerJComboBox
appetizerJComboBox = new JComboBox();
appetizerJComboBox.setBounds( 88, 24, 128, 25 );
menuItemsJPanel.add( appetizerJComboBox );
beverageJComboBox.addItemListener(
new ItemListener() // **** TODO ****** anonymous inner class
{
// **** TODO ****** event handler called when item in beverageJComboBox
// **** TODO ****** is selected
public void itemStateChanged( ItemEvent event )
{
appetizerJComboBoxItemStateChanged( event );
}
} // end anonymous inner class
); // end addItemListener
// add items to appetizerJComboBox
appetizerJComboBox.addItem("Chicken Wings");
appetizerJComboBox.addItem("Pate and Toast");
appetizerJComboBox.addItem("Potato Skins");
appetizerJComboBox.addItem("Nachos");
appetizerJComboBox.addItem("Garlic Mushrooms");
appetizerJComboBox.addItem("SeaFood Cocktail");
appetizerJComboBox.addItem("Brie Cheese");
//****************************************************************
// set up mainCourseJLabel
mainCourseJLabel = new JLabel();
mainCourseJLabel.setBounds( 8, 24, 80, 24 );
mainCourseJLabel.setText( "Main Course:" );
menuItemsJPanel.add( mainCourseJLabel );
// set up mainCourseJComboBox
mainCourseJComboBox = new JComboBox();
mainCourseJComboBox.setBounds( 88, 24, 128, 25 );
menuItemsJPanel.add( mainCourseJComboBox );
mainCourseJComboBox.addItemListener(
new ItemListener() // **** TODO ****** anonymous inner class
{
// **** TODO ****** event handler called when item in beverageJComboBox
// **** TODO ****** is selected
public void itemStateChanged( ItemEvent event )
{
mainCourseJComboBoxItemStateChanged( event );
}
} // end anonymous inner class
); // end addItemListener
// add items to mainCourseJComboBox
mainCourseJComboBox.addItem("Seafood Alfredo");
mainCourseJComboBox.addItem("Chicken Alfredo");
mainCourseJComboBox.addItem("Lasagne");
mainCourseJComboBox.addItem("Turkey Club");
mainCourseJComboBox.addItem("Lobster Pie");
mainCourseJComboBox.addItem("Rib Steak");
mainCourseJComboBox.addItem("Scampi");
mainCourseJComboBox.addItem("Turkey & Ham");
mainCourseJComboBox.addItem("Chicken Kiev");
//**********************************************************
// set up dessertJLabel
dessertJLabel = new JLabel();
dessertJLabel.setBounds( 8, 24, 80, 24 );
dessertJLabel.setText( "Main Course:" );
menuItemsJPanel.add( dessertJLabel );
// set up dessertJComboBox
dessertJComboBox = new JComboBox();
dessertJComboBox.setBounds( 88, 24, 128, 25 );
menuItemsJPanel.add( dessertJComboBox );
dessertJComboBox.addItemListener(
new ItemListener() // **** TODO ****** anonymous inner class
{
// **** TODO ****** event handler called when item in beverageJComboBox
// **** TODO ****** is selected
public void itemStateChanged( ItemEvent event )
{
dessertJComboBoxItemStateChanged( event );
}
} // end anonymous inner class
); // end addItemListener
// add items to dessertJComboBox
dessertJComboBox.addItem("Apple Pie");
dessertJComboBox.addItem("Sundae");
dessertJComboBox.addItem("Carrot Cake");
dessertJComboBox.addItem("Mud Pie");
dessertJComboBox.addItem("Pavlova");
} // end method createMenuItemsJPanel
//********************************************************************
// add items to JComboBox
private void loadCategory(
String category, JComboBox categoryJComboBox )
{
loadCategory("beverage", beverageJComboBox);
loadCategory("appetizer", appetizerJComboBox);
loadCategory("main course", mainCourseJComboBox);
loadCategory("dessert", dessertJComboBox);
try
{
myResultSet = myStatement.executeQuery("SELECT name FROM" +
"Menu WHERE category = "" + category + ");
while(myResultSet.next())
{
categoryJComboBox.addItem(myResultSet.getString("name"));
}
myResultSet.close();
} // end try block
catch ( SQLException SQLexception )
{
SQLexception.printStackTrace();
System.out.println("Cannot load Categories");
} // end catch block
} // end method loadCategory
// user select beverage
private void beverageJComboBoxItemStateChanged( ItemEvent event )
{
// select an item
if(event.getStateChange() == ItemEvent.SELECTED)
{
billItems.add((String)beverageJComboBox.getSelectedItem());
}
} // end of beverageJComboBoxItemStateChanged
// user select appetizer
private void appetizerJComboBoxItemStateChanged( ItemEvent event )
{
if(event.getStateChange() == ItemEvent.SELECTED)
{
billItems.add((String)appetizerJComboBox.getSelectedItem());
}
} // end method appetizerJComboBoxItemStateChanged
// user select main course
private void mainCourseJComboBoxItemStateChanged(
ItemEvent event )
{
if(event.getStateChange() == ItemEvent.SELECTED)
{
billItems.add((String)mainCourseJComboBox.getSelectedItem());
}
} // end method mainCourseJComboBoxItemStateChanged
// user select dessert
private void dessertJComboBoxItemStateChanged( ItemEvent event )
{
if(event.getStateChange() == ItemEvent.SELECTED)
{
billItems.add((String)dessertJComboBox.getSelectedItem());
}
} // end method dessertJComboBoxItemStateChanged
// user click Calculate Bill JButton
private void calculateBillJButtonActionPerformed(
ActionEvent event )
{
String tableNumber = tableNumberJTextField.getText().trim();
String waiterName = waiterNameJTextField.getText().trim();
if (tableNumber.length() == 0 || waiterName.length() == 0)
{
JOptionPane.showInputDialog("Information in Table Number and Waiter Name "
+ "must be entered");
}
else
{
}
}// end method calculateBillJButtonActionPerformed
// **** TODO ****** calculate subtotal
private double calculateSubtotal()
{
return 0;
} // end method calculateSubtotal
// **** TODO ****** user close window
private void frameWindowClosing( WindowEvent event )
{
} // end method frameWindowClosing
// **** TODO ****** method main
public static void main( String[] args )
{
JFrame createUserInterface = new JFrame();
createUserInterface.setSize(500, 500);
createUserInterface.setVisible(true);
createUserInterface.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
createUserInterface.setLocationRelativeTo(null);
// **** TODO ****** check command-line arguments
if ( args.length == 2 )
{
// **** TODO ****** get command-line arguments
String databaseUserName = args[ 0 ];
String databasePassword = args[ 1 ];
// **** TODO ****** create new RestaurantBillCalculator
RestaurantBillCalculator application = new RestaurantBillCalculator (
databaseUserName, databasePassword );
}
else
{
System.out.println( "Usage: java " +
"RestaurantBillCalculator databaseUserName databasePassword" );
}
} // end method main
} // end class RestaurantBillCalculator