CCCPaste

Inheritance

//Main Class
public class Main {
    public static void main(String[] args) {
        //object creation and invoking constructor of Class "Book"
        Book myBook = new Book("The Great Gatsby", "F. Scott Fitzgerald", 2425.00);

        System.out.println("--- Book Details ---");
        myBook.displayDetails();
        
        System.out.println("\n--- Discount Calculation ---");
        myBook.applyDiscount();
    }
}

class Book {
    private String title;
    private String author;
    private double price;
    //Constructor with parameters
    public Book(String title, String author, double price) {
        this.title = title;
        this.author = author;
        this.price = price;
    }
    //method that print author and price
    public void displayDetails() {
        System.out.println("Title: " + this.title);
        System.out.println("Author: " + this.author);
        System.out.println("Price: P" + String.format("%.2f", this.price));
    }

    public void applyDiscount() {
        final double DISCOUNT_RATE = 0.10;
        double discountAmount = this.price * DISCOUNT_RATE;
        double discountedPrice = this.price - discountAmount;
        
        System.out.println("Original Price: P" + String.format("%.2f", this.price));
        System.out.println("Discount Rate: " + (DISCOUNT_RATE * 100) + "%");
        System.out.println("Discount Amount: P" + String.format("%.2f", discountAmount));
        System.out.println("New Discounted Price: P" + String.format("%.2f", discountedPrice));
    }
}