Java Reference
In-Depth Information
int
[] listArray = yourChoices.getSelectedIndices();
double
localTax = 0.065;
double
tax;
double
subtotal = 0;
double
total;
Note that
listArray[0]
,
listArray[1]
,...,
listArray[listArray.length - 1]
contains the indices of the items selected from the menu list. Therefore, the
following
for
loop computes the total cost of the items selected from the menu:
for
(
int
index = 0; index < listArray.length; index++)
subTotal = subTotal + yourChoicesPrices[listArray[index]];
Next, we compute the tax and add it to
subTotal
to get the billing amount:
tax = localTax * subTotal;
total = subTotal + tax;
To display the bill, we append the necessary statements to the
JTextArea
and invoke
the
repaint
method to redraw the GUI components. To place another order, we
unselect the selected items. The definition of the method
displayBill
is:
// method to display the order and total cost
private void
displayBill()
{
int
[] listArray = yourChoices.getSelectedIndices();
double
localTax = 0.065;
double
tax;
double
subtotal = 0;
double
total;
//Set the text area to nonedit mode
//and start with an empty string.
bill.setEditable(
false
);
bill.setText("");
//Calculate the cost of the items ordered.
for
(
int
index = 0; index < listArray.length; index++)
subTotal = subTotal + yourChoicesPrices[listArray[index]];
1
2
tax = localTax * subTotal;
total = subTotal + tax;
//Display costs.
bill.append(" JAVA KIOSK A LA CARTE\n\n");
bill.append("--------------- Welcome ----------------\n\n");
for
(
int
index = 0; index < listArray.length; index++)
{
bill.append(yourChoicesItems[listArray[index]] + "\n");
}
Search WWH ::
Custom Search