Ask Question

Write a class called Product. The class should have fields called name, amount, and price, holding the product's name, the number of items of that product in stock, and the regular price of the product. There should be a method get_price that receives the number of items to be bought and returns as the cost of buying that many items, where the regular price is charged for orders of less than 10 items, a 10% discount is applied for orders of between 10 and 99 items, and a 20% discount is applied for orders of 100 or more items. There should also be a method called make_purchase that receives the number of items to be bought and decreases amount by that much.

Answers (1)
  1. 26 December, 00:10
    public class Product {

    //Declaring the fields

    private String name;

    private int amount;

    private double price;


    public Product (String name, int amount, double price) {

    this. name = name;

    this. amount = amount;

    this. price = price;


    / / The method get_price

    public double get_price (int amount) {

    / / amount of goods less than 10

    if (amount<10) {

    double zeroDiscPrice;

    zeroDiscPrice = this. price;

    return zeroDiscPrice;


    / / amount of goods less than 100

    else if (amount>=10 && amount<100) {

    double tenPercentDiscPrice;

    tenPercentDiscPrice = this. price - (this. price*0.1);

    return tenPercentDiscPrice;


    / / amount of goods greater than 100


    double twentyPercentDiscPrice;

    twentyPercentDiscPrice = this. price - (this. price*0.2);

    return twentyPercentDiscPrice;





    The class is implemented in Java programming language

    Three member variables (fields) are declared as described in the question

    A constructor is created to initialize an instance of the class

    The method get_price () as created uses a combination of if ... else if ... else To determing the price based on the amount of items bought as described in the question.
Know the Answer?