In: Computer Science
Use Java
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 (i.e. -1 means all values have been entered). In the loop, call the addmethod; after the loop, call the getDiscount method and display the returned value.
Code:
//Item.java:
public class Item {
double price;
boolean isPet;
int quantity;
public Item(double price, boolean isPet, int quantity){
this.price = price;
this.isPet = isPet;
this.quantity = quantity;
}
public double getPrice() {
return price;
}
public double getQuantity() {
return quantity;
}
public boolean isPet(){
return isPet;
}
public double getTotal(){
return price*quantity;
}
@Override
public String toString() {
return isPet() + "\t\t" + getQuantity()+ "\t" + getPrice() + "\tTotal: " + getTotal();
}
}
//Invoice.java:
import java.util.ArrayList;
public class Invoice {
ArrayList<Item> itemList = new ArrayList<>();
public void add(Item anItem) {
itemList.add(anItem);
}
public double getTotalAmount(){
double amt = 0.0;
for (int i = 0; i < itemList.size(); i++)
{
Item it = itemList.get(i);
amt += it.getTotal();
}
return amt;
}
public double getDiscount() {
double sumPriceofItems = 0.0;
double discount;
int items = 0, pets = 0;
for (int i = 0; i < itemList.size(); i++)
{
Item it = itemList.get(i);
if(it.isPet()){
pets += it.getQuantity();
}
else{
items += it.getQuantity();
sumPriceofItems += it.getTotal();
}
}
if(items > 4 && pets > 0)
{
discount = 0.2 * sumPriceofItems;
}
else discount = 0.0;
return discount;
}
public void printInvoice() {
for (int i = 0; i < itemList.size(); i++) {
System.out.println(itemList.get(i).toString());
}
}
}
//NewMain.java:
import java.util.Scanner;
public class NewMain {
public static void main(String[] args) {
double totalAmount;
double price;
int quan;
boolean isPet;
String type;
Item itemObject;
Invoice itemInvoice = new Invoice();
Scanner scan = new Scanner(System.in);
System.out.print("Enter price of an item: ");
price = scan.nextDouble();
while (price != -1) {
System.out.print("Enter quantity of items: ");
quan = scan.nextInt();
System.out.print("Enter y if pet: ");
type = scan.next();
if(type.equalsIgnoreCase("y")) {
isPet = true;
}
else {
isPet = false;
}
itemObject = new Item(price, isPet, quan);
itemInvoice.add(itemObject);
System.out.print("Enter price of an item: ");
price = scan.nextDouble();
}
totalAmount = itemInvoice.getTotalAmount();
System.out.println("\n\n--PRINT INVOICE--");
itemInvoice.printInvoice();
System.out.println("Discount: " + itemInvoice.getDiscount());
System.out.println("Total: $" + (totalAmount - itemInvoice.getDiscount()));
}
}
Output: