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

I am confused on how to construct the invoice object in the invoice class. A pet

ID: 3769318 • Letter: I

Question

I am confused on how to construct the invoice object in the invoice class.

A pet shop wants to give a discount to its clients if they buy one or more pets and at least five other items. The discount is equal to 20 percent of the cost of the other items, but not the pets.

Use a class Item to describe an item, with any needed methods and a constructor public Item(double price, boolean isPet, int quantity)

An invoice holds a collection of Item objects; use an array or array list to store them. In the Invoice class, implement methods

- public void add(Item anItem)

- public double getDiscount()

Write a program that prompts a cashier to enter each price and quantity, and then a Y for a pet or N for another item. Use a price of –1 as a sentinel. In the loop, call the add method; after the loop, call the getDiscount method and display the returned value.

Explanation / Answer

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import java.util.Scanner;

/**
*
*/

/**
* @author Srinivas Palli
*
*/
public class Item {

   double price;
   boolean isPet;
   int quantity;
   static List<Item> list = new ArrayList<Item>();

   /**
   * @param price
   * @param isPet
   * @param quantity
   */
   public Item(double price, boolean isPet, int quantity) {
       this.price = price;
       this.isPet = isPet;
       this.quantity = quantity;

   }

   /**
   * @param anItem
   */
   public static void add(Item anItem) {
       list.add(anItem);

   }

   /**
   * @return
   */
   public static double getDiscount() {
       double discount = 0.0;
       int petCount = 0, otherCount = 0;
       double totalOtherItemCost = 0.0;

       Iterator<Item> it = list.iterator();

       while (it.hasNext()) {
           Item item = it.next();
           if (item.isPet()) {
               petCount++;
           } else {
               totalOtherItemCost += item.getPrice();
               otherCount += item.getQuantity();
           }
       }

       if (petCount >= 1 && otherCount >= 5) {
           discount = totalOtherItemCost * (20d / 100d);

       }

       return discount;
   }

   /**
   * @return the price
   */
   public double getPrice() {
       return price;
   }

   /**
   * @return the isPet
   */
   public boolean isPet() {
       return isPet;
   }

   /**
   * @return the quantity
   */
   public int getQuantity() {
       return quantity;
   }

   /**
   * @param args
   */
   public static void main(String[] args) {
       // TODO Auto-generated method stub
       Scanner scanner = new Scanner(System.in);
       do {
           System.out.print("Enter the Price(-1 for exit):");
           double price = scanner.nextDouble();
           if (price == -1) {
               break;

           }

           System.out.print("Is Pet(Y/N):");
           String petFlag = scanner.next();
           boolean isPet;
           if (petFlag.equalsIgnoreCase("y")) {
               isPet = true;
           } else {
               isPet = false;
           }

           System.out.print("Enter Quantity:");
           int quantity = scanner.nextInt();
           Item item = new Item(price, isPet, quantity);
           Item.add(item);
       } while (true);

       System.out.println("Discount :" + Item.getDiscount());

   }

}

OUTPUT:

Test1: with 1 and 5 items quantity
Enter the Price(-1 for exit):234.23
Is Pet(Y/N):y
Enter Quantity:1
Enter the Price(-1 for exit):543.23
Is Pet(Y/N):n
Enter Quantity:3
Enter the Price(-1 for exit):432
Is Pet(Y/N):n
Enter Quantity:2
Enter the Price(-1 for exit):-1
Discount :195.04600000000002


Test2:with no pets
Enter the Price(-1 for exit):245
Is Pet(Y/N):n
Enter Quantity:3
Enter the Price(-1 for exit):765
Is Pet(Y/N):n
Enter Quantity:4
Enter the Price(-1 for exit):-1
Discount :0.0


Test1: with 1 and 4 items quantity
Enter the Price(-1 for exit):324.43
Is Pet(Y/N):y
Enter Quantity:1
Enter the Price(-1 for exit):432.1
Is Pet(Y/N):n
Enter Quantity:2
Enter the Price(-1 for exit):234.34
Is Pet(Y/N):n
Enter Quantity:2
Enter the Price(-1 for exit):-1
Discount :0.0