Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Write a java class named ReceiptItem. The class should represent a single line i

ID: 3801925 • Letter: W

Question

Write a java class named ReceiptItem.

The class should represent a single line item on a store receipt. It should keep track of the name of the item that was purchased, the quantity, and the price.

The Class should have a single constructor:

It should have getters (but not setters) for the item name, the quantity and price.

It should have a method named getTotal() that returns the price times the quantity.

It should have a method named equals(ReceiptItem r) that returns true if the parameter item has a name, quantity, and price that match the object that equals is invoked on.

It should have a method named toString() that returns a string with the following format:

x at :

for example, for a particular receipt item toString might return "Banana x 3 at 0.50: 1.50"

Explanation / Answer

ReceiptItem.java


public class ReceiptItem {
   private String name;
   private int quantity;
   private double price;
   public ReceiptItem(String name, int quantity, double price){
       this.name = name;
       this.quantity = quantity;
       this.price = price;
   }
   public String getName() {
       return name;
   }
   public void setName(String name) {
       this.name = name;
   }
   public int getQuantity() {
       return quantity;
   }
   public void setQuantity(int quantity) {
       this.quantity = quantity;
   }
   public double getPrice() {
       return price;
   }
   public void setPrice(double price) {
       this.price = price;
   }
   public double getTotal() {
       return quantity * price;
   }
   public boolean equals(ReceiptItem r) {
       if(r.price == price && r.name.equals(name) && r.quantity == quantity){
           return true;
       }
       else{
           return false;
       }
   }
   public String toString() {
       return name+" X "+quantity+" at "+price+" = "+getTotal();
   }
}

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote