James - 1 year ago 171
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

...

The current output is:

Number Of Adult Tickets Sold: 5

...

``````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 ChildTickets = 10;
int StudentTickets = 15;
int SeniorTickets = 10;
double Child;
double Student;
double Senior;

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

inputStr = JOptionPane. showInputDialog ("Enter the total number of tickets sold for Adults");
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);

+ "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);
}
}
``````

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 numChildTicketsSold;
int numStudentTicketsSold;
int numSeniorTicketsSold;

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

inputStr = JOptionPane. showInputDialog ("Enter the total number of tickets sold for Adults");
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);