// 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;
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 );
// **** TODO ****** set up restaurantJLabel
JLabel restaurantJLabel = new JLabel("Restaurant");
restaurantJLabel.setFont(new Font("Times New Roman", Font.BOLD, 14));
restaurantJLabel.setBounds(30, 50, 100, 100);
JPanel waiterJPanel = new JPanel();
waiterJPanel.setLayout(null);
waiterJPanel.setBounds(20, 50, 100, 100);
waiterJPanel.setBorder(BorderFactory.createTitledBorder
(BorderFactory.createEtchedBorder(),"Waiter Information"));
JPanel menuItemsJPanel = new JPanel();
menuItemsJPanel.setLayout(null);
menuItemsJPanel.setBounds(20, 50, 200, 200);
waiterJPanel.setBorder(BorderFactory.createTitledBorder
(BorderFactory.createEtchedBorder(),"Menu Items"));
setLayout(null);
JButton calculateBillJButton = new JButton("Calculate Button");
calculateBillJButton.setBounds(250, 250, 50, 50);
// public void actionPerformed( ActionEvent event)
// {
// calculateBillJButtonActionPerformed( event );
// }
//
JLabel subtotalJLabel = new JLabel("Sub Total");
JTextField subtotalJTextField = new JTextField();
JLabel totalJLabel = new JLabel("Total");
JTextField 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()
{
// **** TODO ****** set up waiterJPanel
// **** TODO ****** set up tableNumberJLabel
// **** TODO ****** set up tableNumberJTextField
// **** TODO ****** set up waiterNameJLabel
// **** TODO ****** set up waiterNameJTextField
} // end method createWaiterJPanel
// **** TODO ****** 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
// **** TODO ****** add items to beverageJComboBox
// **** TODO ****** set up appetizerJLabel
// **** TODO ****** set up appetizerJComboBox
// **** TODO ****** add items to appetizerJComboBox
// **** TODO ****** set up mainCourseJLabel
// **** TODO ****** set up mainCourseJComboBox
mainCourseJComboBox = new JComboBox();
// **** TODO ****** add items to mainCourseJComboBox
// **** TODO ****** set up dessertJLabel
} // end method createMenuItemsJPanel
// **** TODO ****** 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();
} // end catch block
} // end method loadCategory
// **** TODO ****** user select beverage
private void beverageJComboBoxItemStateChanged( ItemEvent event )
{
} // end method beverageJComboBoxItemStateChanged
// **** TODO ****** user select appetizer
private void appetizerJComboBoxItemStateChanged( ItemEvent event )
{
} // end method appetizerJComboBoxItemStateChanged
// **** TODO ****** user select main course
private void mainCourseJComboBoxItemStateChanged(
ItemEvent event )
{
} // end method mainCourseJComboBoxItemStateChanged
// **** TODO ****** user select dessert
private void dessertJComboBoxItemStateChanged( ItemEvent event )
{
} // end method dessertJComboBoxItemStateChanged
// **** TODO ****** user click Calculate Bill JButton
private void calculateBillJButtonActionPerformed(
ActionEvent event )
{
} // 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