📄 restaurantbillcalculator.java
字号:
loadCategory( "Beverage", beverageJComboBox );
// set up appetizerJLabel
appetizerJLabel = new JLabel();
appetizerJLabel.setBounds( 8, 56, 80, 24 );
appetizerJLabel.setText( "Appetizer:" );
menuItemsJPanel.add( appetizerJLabel );
// set up appetizerJComboBox
appetizerJComboBox = new JComboBox();
appetizerJComboBox.setBounds( 88, 56, 128, 25 );
appetizerJComboBox.setEnabled( false );
menuItemsJPanel.add( appetizerJComboBox );
appetizerJComboBox.addItemListener(
new ItemListener() // anonymous inner class
{
// event handler called when item in appetizerJComboBox
// is selected
public void itemStateChanged( ItemEvent event )
{
appetizerJComboBoxItemStateChanged( event );
}
} // end anonymous inner class
); // end addItemListener
// add items to appetizerJComboBox
appetizerJComboBox.addItem( "" );
loadCategory( "Appetizer", appetizerJComboBox );
// set up mainCourseJLabel
mainCourseJLabel = new JLabel();
mainCourseJLabel.setBounds( 8, 88, 80, 24 );
mainCourseJLabel.setText( "Main Course:" );
menuItemsJPanel.add( mainCourseJLabel );
// set up mainCourseJComboBox
mainCourseJComboBox = new JComboBox();
mainCourseJComboBox.setBounds( 88, 88, 128, 25 );
mainCourseJComboBox.setEnabled( false );
menuItemsJPanel.add( mainCourseJComboBox );
mainCourseJComboBox.addItemListener(
new ItemListener() // anonymous inner class
{
// event handler called when item in mainCourseJComboBox
// is selected
public void itemStateChanged( ItemEvent event )
{
mainCourseJComboBoxItemStateChanged( event );
}
} // end anonymous inner class
); // end addItemListener
// add items to mainCourseJComboBox
mainCourseJComboBox.addItem( "" );
loadCategory( "Main Course", mainCourseJComboBox );
// set up dessertJLabel
dessertJLabel = new JLabel();
dessertJLabel.setBounds( 8, 120, 80, 24 );
dessertJLabel.setText( "Dessert:" );
menuItemsJPanel.add( dessertJLabel );
// set up dessertJComboBox
dessertJComboBox = new JComboBox();
dessertJComboBox.setBounds( 88, 120, 128, 25 );
dessertJComboBox.setEnabled( false );
menuItemsJPanel.add( dessertJComboBox );
dessertJComboBox.addItemListener(
new ItemListener() // anonymous inner class
{
// event handler called when item in dessertJComboBox
// is selected
public void itemStateChanged( ItemEvent event )
{
dessertJComboBoxItemStateChanged( event );
}
} // end anonymous inner class
); // end addItemListener
// add items to dessertJComboBox
dessertJComboBox.addItem( "" );
loadCategory( "Dessert", dessertJComboBox );
} // end method createMenuItemsJPanel
// add numbers to tableNumberJComboBox
private void loadTableNumbers()
{
} // end method loadTableNumbers
// add items to JComboBox
private void loadCategory(
String category, JComboBox categoryJComboBox )
{
// read all items from database for specified category
try
{
// obtain all items in specified category
myResultSet = myStatement.executeQuery( "SELECT name FROM "
+ "Menu WHERE category = '" + category + "'" );
// add items to JComboBox
while ( myResultSet.next() == true )
{
categoryJComboBox.addItem(
myResultSet.getString( "name" ) );
}
myResultSet.close(); // close myResultSet
} // end try
// catch SQLException
catch ( SQLException exception )
{
exception.printStackTrace();
}
} // end method loadCategory
// user select table
private void tableNumberJComboBoxItemStateChanged(
ItemEvent event )
{
} // end method tableNumberJComboBoxItemStateChanged
// user select beverage
private void beverageJComboBoxItemStateChanged( ItemEvent event )
{
// select an item
if ( event.getStateChange() == ItemEvent.SELECTED )
{
billItems.add(
( String ) beverageJComboBox.getSelectedItem() );
}
} // end method beverageJComboBoxItemStateChanged
// user select appetizer
private void appetizerJComboBoxItemStateChanged( ItemEvent event )
{
// select an item
if ( event.getStateChange() == ItemEvent.SELECTED )
{
billItems.add(
( String ) appetizerJComboBox.getSelectedItem() );
}
} // end method appetizerJComboBoxItemStateChanged
// user select main course
private void mainCourseJComboBoxItemStateChanged(
ItemEvent event )
{
// select an item
if ( event.getStateChange() == ItemEvent.SELECTED )
{
billItems.add(
( String ) mainCourseJComboBox.getSelectedItem() );
}
} // end method mainCourseJComboBoxItemStateChanged
// user select dessert
private void dessertJComboBoxItemStateChanged( ItemEvent event )
{
// select an item
if ( event.getStateChange() == ItemEvent.SELECTED )
{
billItems.add(
( String ) dessertJComboBox.getSelectedItem() );
}
} // end method dessertJComboBoxItemStateChanged
// user click saveTableJButton
private void saveTableJButtonActionPerformed( ActionEvent event )
{
} // end method saveTableJButtonActionPerformed
// user click payBillJButton
private void payBillJButtonActionPerformed( ActionEvent event )
{
} // end method payBillJButtonActionPerformed
// reset JFrame
private void resetJFrame()
{
// reset instance variable
billItems = new ArrayList();
// reset and disable menuItemsJPanel
menuItemsJPanel.setEnabled( false );
beverageJComboBox.setSelectedIndex( 0 );
appetizerJComboBox.setSelectedIndex( 0 );
mainCourseJComboBox.setSelectedIndex( 0 );
dessertJComboBox.setSelectedIndex( 0 );
beverageJComboBox.setEnabled( false );
appetizerJComboBox.setEnabled( false );
mainCourseJComboBox.setEnabled( false );
dessertJComboBox.setEnabled( false );
// reset and enable waiterJPanel
waiterJPanel.setEnabled( true );
tableNumberJComboBox.setEnabled( true );
tableNumberJComboBox.setSelectedIndex( 0 );
waiterNameJTextField.setText( "" );
// clear JTextFields
subtotalJTextField.setText( "" );
taxJTextField.setText( "" );
totalJTextField.setText( "" );
// disable JButtons
saveTableJButton.setEnabled( false );
calculateBillJButton.setEnabled( false );
payBillJButton.setEnabled( false );
} // end method resetJFrame
// user click Calculate Bill button
private void calculateBillJButtonActionPerformed(
ActionEvent event )
{
double total = calculateSubtotal();
// display subtotal, tax and total
displayTotal( total );
} // end method calculateBillJButtonActionPerformed
// display subtotal, tax and total
private void displayTotal( double total )
{
// define display format
DecimalFormat dollars = new DecimalFormat( "$0.00" );
// display subtotal
subtotalJTextField.setText( dollars.format( total ) );
// calculate and display tax
double tax = total * TAX_RATE;
taxJTextField.setText( dollars.format( tax ) );
// display total
totalJTextField.setText(
dollars.format( total + tax ) );
} // end method displayTotal
// calculate subtotal
private double calculateSubtotal()
{
double total = subtotal;
Object[] items = billItems.toArray();
// get data from database
try
{
// get price for each item in items array
for ( int i = 0; i < items.length; i++ )
{
// execute query to get price
myResultSet = myStatement.executeQuery( "SELECT price " +
"FROM Menu WHERE name = '" + ( String ) items[ i ] +
"'" );
// myResultSet not empty
if ( myResultSet.next() == true )
{
total += myResultSet.getDouble( "price" );
}
myResultSet.close(); // close myResultSet
} // end for
} // end try
// catch SQLException
catch ( SQLException exception )
{
exception.printStackTrace();
}
return total;
} // end method calculateSubtotal
// user close window
private void frameWindowClosing( WindowEvent event )
{
// close myStatement and database connection
try
{
myStatement.close();
myConnection.close();
}
catch ( SQLException sqlException )
{
sqlException.printStackTrace();
}
finally
{
System.exit( 0 );
}
} // end method frameWindowClosing
// main method
public static void main( String[] args )
{
// check command-line arguments
if ( args.length == 2 )
{
// get command-line arguments
String databaseDriver = args[ 0 ];
String databaseURL = args[ 1 ];
// create new RestaurantBillCalculator
RestaurantBillCalculator application =
new RestaurantBillCalculator(
databaseDriver, databaseURL );
}
else
{
System.out.println( "Usage: java " +
"RestaurantBillCalculator databaseDriver databaseURL" );
}
} // end method main
} // end class RestaurantBillCalculator
/**************************************************************************
* (C) Copyright 1992-2004 by Deitel & Associates, Inc. and *
* Pearson Education, Inc. All Rights Reserved. *
* *
* DISCLAIMER: The authors and publisher of this book have used their *
* best efforts in preparing the book. These efforts include the *
* development, research, and testing of the theories and programs *
* to determine their effectiveness. The authors and publisher make *
* no warranty of any kind, expressed or implied, with regard to these *
* programs or to the documentation contained in these books. The authors *
* and publisher shall not be liable in any event for incidental or *
* consequential damages in connection with, or arising out of, the *
* furnishing, performance, or use of these programs. *
**************************************************************************/
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -