Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

(TCOs 1–6) TicketsRUs needs an application to calculate ticket prices. There are

ID: 3710375 • Letter: #

Question

(TCOs 1–6) TicketsRUs needs an application to calculate ticket prices. There are three ticket prices: • Orchestra $85 each • Mezzanine $70 each • Balcony $45 each There is also a 15% discount on matinee performances. Your application has the GUI shown below. With the following named components: Component Type Purpose txtNum JTextField Input for number of tickets chkMatinee JCheckBox Check if matinee performance radOrchestra JRadioButton Check for orchestra tickets radMezzanine JRadioButton Check for mezzanine tickets radBalcony JRadioButton Check for balcony tickets btnCalc JButton Click to calculate price txtEach JTextField Displays price of each ticket txtTotal JTextField Displays total price Clicking the CalcPrice button should determine the price per ticket and the total price based on the user’s input and display in txtEach and txtTotal. You should make sure the number of tickets is entered and a ticket type is selected, otherwise give an error message. The action listener for btnCalc is set up as follows. btnCalc.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent e) { calcPrice(); //write the code for this method } }); Write the calcPrice method that is called by the action listener. This class method has access to all of the GUI components. You DO NOT HAVE TO CODE THE GUI. ONLY write the code for this method which does all the work. The header for the method is: private void calcPrice()

Explanation / Answer

private void calcPrice()

{
*********** variable declaration***********
int totalTickets;
boolean isMatinee= false;
int selectedShowCost=0;
int totalCost;
int discountAmt;
********* variable declaration ends*********

if(!txtNum .getText().trim().isEmpty())
{
if(radOrchestra .isSelected() || radMezzanine.isSelected() || radBalcony .isSelected())
{
   try
   {
    totalTickets= Integer.parseInt(txtNum .getText());
    }
   catch (NumberFormatException e)
   {
    // Number of tickets field has an invalid entry
   }

   Initialize(); // method to intialize variables
    ComputeCost(); // method to compute totalPrice
   }
}
else()
{
// no show is selected
}
else ()
{
// invalid entry in totaltickets
}


private void Initialize()
{
if(chkMatinee .isSelected())
{
Matinee=true;
}
if(radOrchestra .isSelected()) { selectedShowCost=90; }
else if (radMezzanine.isSelected() ) { selectedShowCost=75; }
else if (radBalcony .isSelected() ) { selectedShowCost=50; }
}

private void ComputeCost()
{
if(!isMatinee)
totalcost=totalTickets * selectedShowCost;
else
discountAmt=(20/100)*(totalTickets * selectedShowCost);
totalcost=totalTickets * selectedShowCost-discountAmt;
txtEach.setText(selectedShowCost);
txtTotal.setText(totalcost);
}
}