James James - 9 days ago 4
Java Question

change the total price for each ticket sold

I need to make the program change the total price depending on the amount of tickets sold. For example if 5 adult tickets are sold then it should say $100 for adults. I'm just Confused on how to do the calculation for the program. like what do I have to do to actually make the program add.




The Output that i need:

Number Of Adult Tickets Sold: 5

Adult total: $100

...




The current output is:

Number Of Adult Tickets Sold: 5

Adult total: $20

...




package Ticket.V3;

import javax.swing.JOptionPane;
import java.io.*;
import java.util.*;

public class Ticket
{
public static void main(String[] args)
{
String inputStr;
String outputStr;

int AdultTickets = 20;
int ChildTickets = 10;
int StudentTickets = 15;
int SeniorTickets = 10;
double Adult;
double Child;
double Student;
double Senior;

Adult = AdultTickets;
Child = ChildTickets;
Student = StudentTickets;
Senior = SeniorTickets;

inputStr = JOptionPane. showInputDialog ("Enter the total number of tickets sold for Adults");
AdultTickets= Integer.parseInt(inputStr);
inputStr = JOptionPane. showInputDialog ("Enter the total number of tickets sold for Childs");
ChildTickets = Integer.parseInt(inputStr);
inputStr = JOptionPane. showInputDialog ("Enter the total number of tickets sold for Students");
StudentTickets = Integer.parseInt(inputStr);
inputStr = JOptionPane. showInputDialog ("Enter the total number of tickets sold for Seniors");
SeniorTickets = Integer.parseInt(inputStr);

outputStr = "Number Of Adult Tickets Sold: " + (AdultTickets) + "\n" + "Adult Total: $" + String.format("%.2f", Adult) + " \n"
+ "Number Of Child Tickets Sold: " + (ChildTickets) + "\n" + "Child Total: $" + String.format("%.2f", Child) + "\n"
+ "Number Of Student Tickets Sold: " + (StudentTickets) + "\n"+ "Student Total: $" + String.format("%.2f", Student) + "\n"
+ "Number Of Senior Tickets Sold: " + (SeniorTickets) + "\n"+ "Senior Total: $" + String.format("%.2f", Senior) + "\n";
JOptionPane.showMessageDialog(null, outputStr, "Total Ticket Sales", JOptionPane.INFORMATION_MESSAGE);

System.exit(0);
}
}

Answer

You need to multiply the amount of tickets sold by their cost, like so:

 outputStr = "Number Of Adult Tickets Sold: " + (AdultTickets) + "\n" + "Adult Total: $" + String.format("%.2f", Adult * AdultTickets) + " \n" 
    + "Number Of Child Tickets Sold: " + (ChildTickets) + "\n" + "Child Total: $" + String.format("%.2f", Child * ChildTickets) + "\n" 
    + "Number Of Student Tickets Sold: " + (StudentTickets) + "\n"+ "Student Total: $" + String.format("%.2f", Student * StudentTickets) + "\n"
    + "Number Of Senior Tickets Sold: " + (SeniorTickets) + "\n"+ "Senior Total: $" + String.format("%.2f", Senior * SeniorTickets) + "\n";

It'd make a lot more sense with better names for the variables, like this:

public static void main(String[] args) {
    String inputStr;
    String outputStr;

    int numAdultTicketsSold;
    int numChildTicketsSold;
    int numStudentTicketsSold;
    int numSeniorTicketsSold;

    double adultTicketPrice = 20;
    double childTicketPrice = 10;
    double studentTicketPrice = 15;
    double seniorTicketPrice = 10;

    inputStr = JOptionPane. showInputDialog ("Enter the total number of tickets sold for Adults");
    numAdultTicketsSold= Integer.parseInt(inputStr);
    inputStr = JOptionPane. showInputDialog ("Enter the total number of tickets sold for Childs");
    numChildTicketsSold = Integer.parseInt(inputStr);
    inputStr = JOptionPane. showInputDialog ("Enter the total number of tickets sold for Students");
    numStudentTicketsSold = Integer.parseInt(inputStr);
    inputStr = JOptionPane. showInputDialog ("Enter the total number of tickets sold for Seniors");
    numSeniorTicketsSold = Integer.parseInt(inputStr);

    outputStr = "Number Of Adult Tickets Sold: " + (numAdultTicketsSold) + "\n" + "Adult Total: $" + String.format("%.2f", numAdultTicketsSold * adultTicketPrice) + " \n"
            + "Number Of Child Tickets Sold: " + (numChildTicketsSold) + "\n" + "Child Total: $" + String.format("%.2f", numChildTicketsSold * childTicketPrice) + "\n"
            + "Number Of Student Tickets Sold: " + (numStudentTicketsSold) + "\n"+ "Student Total: $" + String.format("%.2f", numStudentTicketsSold * studentTicketPrice) + "\n"
            + "Number Of Senior Tickets Sold: " + (numSeniorTicketsSold) + "\n"+ "Senior Total: $" + String.format("%.2f", numSeniorTicketsSold * seniorTicketPrice) + "\n";
    JOptionPane.showMessageDialog(null, outputStr, "Total Ticket Sales", JOptionPane.INFORMATION_MESSAGE);

    System.exit(0);
}