Answer to Question #246901 in Java | JSP | JSF for taps

Question #246901

Create a subclass called TicketSales that extends the Tickets class. The TicketSales class must contain a constructor to accept the customer name, movie title, customer age and price of the movie as parameters. Write code for the print_tickets method which displays the customer name, movie title, movie price, discount and final cost due. A discount of 10% is applied if the customer’s age is greater than or equal to 65. If the customer is under 65 years old, no discount is given. Finally, write a Movie_Tickets class to instantiate the TicketSales class. Sample output is shown below and you may use the same values to test your application


1
Expert's answer
2021-10-05T23:13:59-0400
package com.company;

import java.util.*;

// Base Class Ticket
class Ticket{

    // Private Fields
    private String customer_name;
    private int customer_age;
    
    // Parameterized Constructor of the base class
    public Ticket(String customer_name, int customer_age) {
        this.customer_name = customer_name;
        this.customer_age = customer_age;
    }

    // public method to print details
    public void printDetails() {
        System.out.println("\t\tCustomer name: " + customer_name);
        System.out.println("\t\tCustomer age: " + customer_age);
    }

}
// Derived Class Ticket_sales
class Ticket_sales extends Ticket {
    // Private field
    private String movie_title;
    private int price;
    private int discount;

    // Parameterized Ticket_sales Constructor
    public Ticket_sales(String customer_name, int customer_age, String movie_title,  int price) {
        //calling parent class constructor
        super(customer_name, customer_age);
        this.movie_title = movie_title;
        this.price=price;
        
    }

    public void print_tickets() {
        //calling print method of the from parent class
        printDetails();
        System.out.println("\t\tMovie Title: " + movie_title);
        System.out.println("\t\tOriginal Movie Price : " + price);
    }

}

class Main {

    public static void main(String[] args) {

//MEnu
        Scanner sc = new Scanner(System.in); //System.in is a standard input stream

        System.out.println("\nWelcome to Dkay movie shop\n");
        System.out.println("Provide the details below to buy a movie");

        System.out.println("Enter your name: ");
        String name = sc.nextLine();
        System.out.println("Enter the movie Title: ");
        String movie_title = sc.nextLine();
        System.out.println("Enter your age: ");
        int age = sc.nextInt();

        System.out.println("Enter the movie price");
        int price = sc.nextInt();
        //creation of Ticket_sales Object
        Ticket_sales my_student = new Ticket_sales(name, age, movie_title, price);
        my_student.print_tickets(); //calling method to print details
        //Now calculate the discount and final price
        if (age == 65 || age > 65) {
            int discount = (10 * price) / 100;
            int final_price = price - discount;
            System.out.printf("\t\tDiscount is " + discount);
            System.out.println("\n\t\tFinal price is " + final_price);
        } else {
            System.out.printf("\t\tAge below 65, no discount given");
        }
    }
}

Need a fast expert's response?

Submit order

and get a quick answer at the best price

for any assignment or question with DETAILED EXPLANATIONS!

Comments

No comments. Be the first!

Leave a comment

LATEST TUTORIALS
New on Blog
APPROVED BY CLIENTS