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

Write a class named ReceiptItem. The class should represent a single line item o

ID: 3801667 • Letter: W

Question

Write a 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: Receipt item(String name, int quantity, double price) 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 : Write a method named isMoreExpensiveThan that accepts a ReceiptItem as a parameter and returns a Boolean. The method should return true if the total cost of the receipt item that receives the invocation is more than the total cost of the parameter. Otherwise false.

Explanation / Answer

ReceiptItem.java

public class ReceiptItem {

   //Declaring instance variables
   private String name;
   private int quantity;
   private double price;


   //Parameterized constructor
   public ReceiptItem(String name, int quantity, double price) {
       super();
       this.name = name;
       this.quantity = quantity;
       this.price = price;
   }

   //Getters
   public String getName() {
       return name;
   }

   public int getQuantity() {
       return quantity;
   }

   public double getPrice() {
       return price;
   }

   //Calculating the total cost of Receipt Item
   public double getTotal() {
       return quantity * price;
   }

   //Checking the two Receipt objects are equal or not
   public boolean equals(ReceiptItem r) {

       if (name == null) {
           if (r.name != null)
               return false;
       } else if (!name.equals(r.name))
           return false;
       if (Double.doubleToLongBits(price) != Double.doubleToLongBits(r.price))
           return false;
       if (quantity != r.quantity)
           return false;
       return true;
   }

   //toString method is used to display the contents of an object inside it
   @Override
   public String toString() {
       return name + " x " + quantity+ " at " + price;
   }
  
   //Checking the which Receipt Item is more Expensive based on their total Cost
   public boolean isMoreExpensive(ReceiptItem r)
   {
       if(this.getTotal()> r.getTotal())
           return true;
       else
           return false;
   }

}

____________________

Driver.java

import java.util.Scanner;

public class Driver {

   public static void main(String[] args) {
      
       //Creating the Receipt Item one class object
       ReceiptItem r1=new ReceiptItem("Banana",3,0.50);
       System.out.println(r1.toString());
      
       //Creating the Receipt Item two class object      
       ReceiptItem r2=new ReceiptItem("Apple",4,0.75);
       System.out.println(r2.toString());
      
       System.out.println(" == Comparing Two ReceiptItem Obejcts ==");
       if(r1.equals(r2))
           System.out.println("** Two objects are equal **");
       else
           System.out.println("** Two objects are not equal **");
       System.out.println(" == Comparing Total Cost of Two ReceiptItem Obejcts ==");
       if(r1.isMoreExpensive(r2))
           System.out.println("Receipt Item one is expensive than Receipt Item Two");
       else
           System.out.println("Receipt two is expensive than Receipt Item One");
      
      
   }

}

___________________________

Output:

Banana x 3 at 0.5
Apple x 4 at 0.75

== Comparing Two ReceiptItem Obejcts ==
** Two objects are not equal **

== Comparing Total Cost of Two ReceiptItem Obejcts ==
Receipt two is expensive than Receipt Item One

_____________Thank You

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