SHARE
SPREAD
HELP

The Tradition of Sharing

Help your friends and juniors by posting answers to the questions that you know. Also post questions that are not available.


To start with, Sr2Jr’s first step is to reduce the expenses related to education. To achieve this goal Sr2Jr organized the textbook’s question and answers. Sr2Jr is community based and need your support to fill the question and answers. The question and answers posted will be available free of cost to all.

 

#
Authors:
Stuart Reges, Marty Stepp
Chapter:
Inheritance And Interfaces
Exercise:
Exercises
Question:9 | ISBN:9780136091813 | Edition: 2

Question

DiscountBill. Suppose a class GroceryBill keeps track of a list of items being purchased at a market:

Grocery bills interact with Item objects, each of which has the public methods that follow. A candy bar item might 
cost 1.35 with a discount of 0.25 for preferred customers, meaning that preferred customers get it for 1.10. (Some  items will have no discount, 0.0.) Currently the preceding classes do not consider discounts. Every item in a bill is charged full price, and item discounts are ignored.

Define a class DiscountBill that extends GroceryBill to compute discounts for preferred customers. Its constructor accepts a parameter for whether the customer should get the discount. Your class should also adjust the total reported for preferred customers. For example, if the total would have been $80 but a preferred customer is getting $20 in discounts, then getTotal should report the total as $60 for that customer. Also keep track of the number of items on which a customer is getting a nonzero discount and the sum of these discounts, both as a total amount and as a percentage of the original bill. Include the extra methods that follow, which allow a client to ask about the discount. Return 0.0 if the customer is not a preferred customer or if no items were discounted.

TextbookTextbookTextbookTextbookTextbookTextbookTextbookTextbookTextbookTextbookTextbookTextbookTextbookTextbookTextbookTextbookTextbook

Answer

// package inheritance;

public class DiscountBill extends GroceryBill {
 
	    
	    public DiscountBill(Employee emp, boolean preferred) {
	        super(emp);
	        this.discountCount = 0;
	        this.discountAmount = 0;
	        this.preferred = preferred;
	    }
	    
	    public void add(Item item) {
	        super.add(item);
	        
	        if(preferred && item.getDiscount() > 0) {
	            discountCount++;
	            discountAmount += item.getDiscount();
	        }
	    }
	    
	    public double getTotal() {
	        return super.getTotal() - discountAmount;
	    }
	    
	    public double getDiscountAmount() {
	        return discountAmount;
	    }
	    
	    public int getDiscountCount() {
	        return discountCount;
	    }
	    
	  
	    public double getDiscountPercent() {
	    	int total = super.getTotal();
	        return (discountAmount * 100) / total;
	    }
	}


 

0 0

Discussions

Post the discussion to improve the above solution.