Develop a simple program in Java that should calculate the amount of due for a u
ID: 3870766 • Letter: D
Question
Develop a simple program in Java that should calculate the amount of due for a user’s ticket order based upon the user’s selection. It should ask the user how many adult tickets and how many discounted (student/senior) tickets they would like to purchase. It should then give the user a menu of options for discounts to be applied. After the user has made their selection it should display a bill indicating the purchase price, the shipping costs (if any), the discount applied (if any) and the total due. Notes: The discount percentage and shipping rate should be constants (discount rate should be 10 percent and shipping should be $5). It should be that if the discount rate or shipping cost change, you would only have to make one change in your program – not multiple places throughout. Adult ticket prices are $10 and student/senior discounted tickets are $5. The output should look exactly like that shown in the examples (pay close attention to line breaks and spacing). Be sure to place your own name in the greeting. All currency should be displayed to 2 decimal places and percentages be displayed to 1 decimal place If the user makes an invalid selection from the menu, the program should automatically apply free shipping. You must use a switch statement to process the menu selection. Requirements: After the welcome screen is displayed, a menu selection has been made, and the number of tickets has been entered, your program should display enough blank lines to make it appear that the screen has been cleared before displaying whatever comes next. You should display the message “ -- Press Enter to Continue -- “ and wait for the user to press Enter after displaying the welcome screen. Upon pressing Enter, the program should continue running Write an algorithm/flowchartfor the solution to the problem. Be specific when mentioning calculations. Remember: your algorithm/flowchartis your road map –you will follow it when writing your code. DO NOT WRITE YOUR CODE FIRST AND THEN YOUR ALGORITHM/FLOWCHART! se your algorithm as a starting point for your comments throughout your programWrite your programone step at a time, i.e. make sure one thing works before going on to something else. omplete the documentationof the application by inserting comments and adhering to programming standards
Explanation / Answer
Answer : Some assumption has been made while coding as there was format of output in the code, the output format is similar to what mentioned in the text. The discount menu is assumed for some 3 options, can be edited to add more. discount values are same in all the 3 as it is written in the question that it should be 5. code is flexible is any minor changes needed.
Code:
import java.io.IOException;
import java.text.DecimalFormat;
import java.util.Scanner;
import java.util.logging.Level;
import java.util.logging.Logger;
public class Main13 {
//constant variable to be defined in single place, easy to change in one place only
public static final int discountRate = 10;
public static final int shippingCost = 5;
public static void main(String[] args) {
int adultTicketPrice = 10;
int discTicketPrice = 5;
System.out.println(" Welcome to the ticket booking App ");
System.out.println(" Enter the Number of Adults tickets : ");
Scanner in = new Scanner(System.in);
int adultTickets = in.nextInt();
System.out.println(" Enter the Number of Discounted tickets : ");
int discTickets = in.nextInt();
//Menu for discounts, can add more if needed
System.out.println("Please select the discount from the menu ");
System.out.println("1.New user offer ");
System.out.println("2.Festival offer ");
System.out.println("2.Winter Bonanza ");
int option = in.nextInt();
//current discount after menu selection to be updated in switch
int currentDiscount = discountRate;
//current shipping will be set to 0 if wrong option selected from menu
int currentShipping = shippingCost;
switch(option){
case 1:
//can set to different discount rates
currentDiscount = discountRate;
break;
case 2:
//can set to different discount rates
currentDiscount = discountRate;
break;
case 3:
//can set to different discount rates
currentDiscount = discountRate;
break;
default:
//shipping free for wrong choice
currentShipping = 0;
break;
}
double finalPrice = 0;
System.out.println(" Press Enter to Continue... ");
try {
System.in.read();
} catch (IOException ex) {
Logger.getLogger(Main13.class.getName()).log(Level.SEVERE, null, ex);
}
System.out.println(" ");
System.out.println(" Hi, Welcome to the ticket booking, Please find below your billing details ");
DecimalFormat df = new DecimalFormat("####0.00");
finalPrice = adultTicketPrice * adultTickets + discTicketPrice * discTickets;
System.out.println(" Purchase Price $" + df.format(finalPrice));
System.out.println(" Shipping Cost $" + df.format(currentShipping));
double totalDiscount = finalPrice * (currentDiscount/ 100.00);
System.out.println(" Total Discount $" + df.format(totalDiscount));
finalPrice = finalPrice + currentShipping - totalDiscount;
System.out.println(" Total Due $" + df.format(finalPrice));
System.out.println(" Please pay the due Amount for your tickets order is $" + df.format(finalPrice));
System.out.println(" ");
}
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.