The manager of a football stadium wants you to write a programthat calculates th
ID: 3640695 • Letter: T
Question
The manager of a football stadium wants you to write a programthat calculates the total ticket sales after each game. There arefour types of tickets - box, sideline, premium, and generaladmission. After each game, data is stored in a file in thefollowing form:ticketPrice numberOfTicketsSold
...
Sample data are shown below:
250 5750
100 28000
50 35750
25 18750
The first line indicated that the ticket price is $250 andthat 5750 tickets were sold at that price. Output the number oftickets sold and the total sale amount. Format your output with twodecimal places.
Could you please leave comments. Thanks
Explanation / Answer
700 karma pts question?
import java.io.FileReader;
import java.util.Scanner;
public class Program {
public static void main(String[] args) {
//Variables needed
double ticketPrice;
int ticketsSold;
int totalTicketsSold;
double totalSaleAmount;
//Get data fle name (omit this part if you use a give fileName
Scanner scanner = new Scanner(System.in);
System.out.print("Enter data file's name: ");
String fileName = scanner.nextLine();
//Try to open and load data file to scanner
try {
scanner = new Scanner(new FileReader(fileName)); //or change fileName to your given fileName, ex. "sales.dat"
}
catch (Exception e) {
System.out.println("ERROR: File not found" + e.getMessage());
System.exit(1);
}
//Initialize totalTicketsSold and totalSaleAmount
totalTicketsSold = 0;
totalSaleAmount = 0.0;
//Read sale records
while (scanner.hasNext()) {
//First item is ticketPrice (type double)
ticketPrice = scanner.nextDouble();
//Seond item is ticketsSold (type int)
ticketsSold = scanner.nextInt();
//Add ticketsSold to totalTicketsSold
totalTicketsSold += ticketsSold;
//Add sale amount to totalSaleAmount
totalSaleAmount += ticketPrice * ticketsSold;
//repeat for next line
}
//Output the number oftickets sold and the total sale amount
System.out.println("Number oftickets sold: " + totalTicketsSold);
System.out.printf("Total sale amount: $%.2f ", totalSaleAmount);
//Close data file before exiting
scanner.close();
}
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.