http://practiceit.cs.washington.edu/problem.jsp?category=Building+Java+Programs%
ID: 3565089 • Letter: H
Question
http://practiceit.cs.washington.edu/problem.jsp?category=Building+Java+Programs%2C+3rd+edition%2FBJP3+Chapter+9&problem=bjp3-9-e10-DiscountBill
Suppose a class GroceryBill keeps track of a list of items being purchased at a market:
GroceryBill objects interact with Item objects. An Item has the following public methods:
For example, 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 above 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. The constructor for DiscountBill accepts a parameter for whether the customer should get the discount.
Your class should adjust the amount reported by getTotal 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. You should also keep track of how many items a customer is getting a non-zero discount for and the overall discount, both as a total amount and as a percentage of the original bill. Include the extra methods below that allow a client to ask about the discount:
If the customer is not a preferred customer the DiscountBill behaves at all times as if there is a total discount of 0.0 and no items have been discounted.
Related Files:
Method/Constructor Description public GroceryBill(Employee clerk) constructs a GroceryBill object for the given clerk public void add(Item i) adds i to this bill's total public double getTotal() returns the cost of these items public void printReceipt() prints a list of itemsExplanation / Answer
Here is the code for the question with sample output.
Employee.java
public class Employee {
private String name;
public Employee(String name)
{
this.name = name;
}
public String getName()
{
return name;
}
}
Item.java
public class Item {
private String name ;
private double price = 0.0d;
private double discount = 0.0d;
public Item(String name, double price, double discount) {
this.name = name;
this.price = price;
this.discount = discount;
}
public double getPrice() {
return price;
}
public double getDiscount() {
return discount;
}
public String getName()
{
return name;
}
}
GroceryBill.java
import java.util.ArrayList;
import java.util.List;
public class GroceryBill {
protected List<Item> itemList = new ArrayList<>();
protected Employee clerk;
public GroceryBill(Employee clerk) {
this.clerk = clerk;
}
public void add(Item i) {
itemList.add(i);
}
public double getTotal() {
double retTotal = 0.0d;
for(int i = 0; i < itemList.size(); i++) {
retTotal += itemList.get(i).getPrice();
}
return retTotal;
}
public void printReceipt() {
System.out.println(" Bill for " + clerk.getName());
System.out.printf(" %-15s %8s %10s%n", "Item Name", "Price", "Discount");
for(int i = 0; i < itemList.size(); i++) {
Item item = itemList.get(i);
System.out.printf("%d) %-15s %8.2f %10.2f%n", (i+1), item.getName(), item.getPrice(), item.getDiscount());
}
}
}
DiscountBill.java
public class DiscountBill extends GroceryBill {
private boolean isPreferredCustomer = false;
public DiscountBill(Employee clerk, boolean preferred) {
super(clerk);
isPreferredCustomer = preferred;
}
public int getDiscountCount() {
int retDiscount = 0;
if(isPreferredCustomer) {
for(int i = 0; i < itemList.size(); i++) {
if(itemList.get(i).getDiscount() > 0)
retDiscount ++;
}
}
return retDiscount;
}
public double getDiscountAmount() {
int retAmount = 0;
if(isPreferredCustomer) {
for(int i = 0; i < itemList.size(); i++) {
retAmount += itemList.get(i).getDiscount();
}
}
return retAmount;
}
public double getDiscountPercent() {
double grossTotal = super.getTotal();
return (getDiscountAmount() / grossTotal) * 100;
}
@Override
public double getTotal() {
if(!isPreferredCustomer) {
return super.getTotal();
}
return super.getTotal() - getDiscountAmount();
}
@Override
public void printReceipt() {
System.out.println(" Bill for " + clerk.getName());
System.out.printf(" %-15s %8s %10s%n", "Item Name", "Price", "Discount");
for(int i = 0; i < itemList.size(); i++) {
Item item = itemList.get(i);
System.out.printf("%d) %-15s %8.2f %10.2f%n", (i+1), item.getName(), item.getPrice(), item.getDiscount());
}
System.out.printf("No.Discounted Items %5d %n", getDiscountCount());
System.out.printf("Total Amount %3.2f ", super.getTotal());
System.out.printf("Bill after Discount %3.2f ", getTotal());
System.out.printf("Total Savings %3.2f (%2.2f%%) %n", getDiscountAmount(), getDiscountPercent());
}
}
Main.java
public class Main {
public static void main(String[] args) {
Item i1 = new Item("Shampoo",10.0, 0.0);
Item i2 = new Item("Tomatoes",5.0, 2.0);
DiscountBill dbill1 = new DiscountBill(new Employee("John"), true);
dbill1.add(i1);
dbill1.add(i2);
DiscountBill dbill2 = new DiscountBill(new Employee("Bob"), false);
dbill2.add(i1);
dbill2.add(i2);
dbill1.printReceipt();
dbill2.printReceipt();
}
}
output
Bill for John
Item Name Price Discount
1) Shampoo 10.00 0.00
2) Tomatoes 5.00 2.00
No.Discounted Items 1
Total Amount 15.00
Bill after Discount 13.00
Total Savings 2.00 (13.33%)
Bill for Bob
Item Name Price Discount
1) Shampoo 10.00 0.00
2) Tomatoes 5.00 2.00
No.Discounted Items 0
Total Amount 15.00
Bill after Discount 15.00
Total Savings 0.00 (0.00%)
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.