Question

In: Computer Science

Use Java A pet shop wants to give a discount to its clients if they buy...

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.

Solutions

Expert Solution

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:



Related Solutions

If an investor wants to buy a coffee shop for $1million, and wants to make assumptions...
If an investor wants to buy a coffee shop for $1million, and wants to make assumptions on its working capital, what could those assumptions be? Ps: the investment has a horizon of 10 years.
JAVA In this PoD you will use an ArrayList to store different pet names (there are...
JAVA In this PoD you will use an ArrayList to store different pet names (there are no repeats in this list). This PoD can be done in your demo program (where your main method is) – you don’t have to create a separate class for today. Details Create an arraylist of Strings, then using a Scanner object you will first read in a number that will tell you how many pet names (one word) you will add to the arraylist....
A certain store gives its customers a discount on the goods they buy based on a...
A certain store gives its customers a discount on the goods they buy based on a peculiar manner. The discount percentage is equal to each customer's remainder of weight to age, weight % age. The maximum discount percentage allowed is 10. If the remainder is greater than 10, the discount percentage is recomputed as the remainder of 10. For example, a customer whose weight is 175 lbs and age 61, buys an item costing $60.97. The discount percentage = 175...
An asset manager wants to buy 100,000 apple shares at 2% discount below the current market...
An asset manager wants to buy 100,000 apple shares at 2% discount below the current market price in about a month. She may submit a limit order and wait but may never be able to purchase at that price if apple does not go down by 2% or more. How can she take a more proactive options strategy to achieve her goal in the next month? please describe the option stratey and the outcome if in a month, apple price...
Susan wants to buy a house for $200,000. US Bank will give her the loan at...
Susan wants to buy a house for $200,000. US Bank will give her the loan at 3.5% for 30 years if she puts 20% down and Dacotah Bank will give her the loan at 3.8% for 30 years if she puts 10% down. There are NO other options. (15 PTS) a. Which loan should she sign up for? Pick one, no explanation needed. What is her monthly payment? (5 PTS) b. After 5 years, Susan is approached by a philanthropic...
Susan wants to buy a house for $200,000. US Bank will give her the loan at...
Susan wants to buy a house for $200,000. US Bank will give her the loan at 3.5% for 30 years if she puts 20% down and Dacotah Bank will give her the loan at 3.8% for 30 years if she puts 10% down. There are NO other options. (15 PTS) a-Which loan should she sign up for? Pick one, no explanation needed. What is her monthly payment? (5 PTS) b-After 5 years, Susan is approached by a philanthropic group known...
Cloud Nine provides its clients with a monthly newsletter that offers recommendations about stocks to buy...
Cloud Nine provides its clients with a monthly newsletter that offers recommendations about stocks to buy or sell. Doug Layton, Cloud Nine’s president, has asked your opinion on whether dot-com stocks might be good investments for the future. He specifically mentioned Google, eBay, Amazon.com, and Yahoo!, but he said you could suggest other companies. Doug wants you to do some Internet research to learn more about these Web-based companies and their prospects. You can use a search engine or start...
Midwest Company manufactures lamps. Shop Smart, a large retail merchandiser, wants to buy 200,000 lamps from...
Midwest Company manufactures lamps. Shop Smart, a large retail merchandiser, wants to buy 200,000 lamps from Midwest Company for $12 each. The lamp would carry Shop Smart’s name and would be sold in its stores. Midwest Company normally sells 420,000 lamps a year at $16 each; its production capacity is a total of 450,000 units a year. Cost information for the lamps is as follows: Production costs: Variable production costs $6 per unit Fixed manufacturing overhead ($2,100,000 / 420,000 units)...
A wine shop wants to serve its white wine at a mean temperature of 50 degrees....
A wine shop wants to serve its white wine at a mean temperature of 50 degrees. In a sample of 36 glasses of wine, the mean was found to be 53 with a standard deviation of 3. The wine shop is interested to see if the evidence indicates that the wine shop is not meeting its temperature standard. (a) Using a significance level of α=0.05, perform a two-tailed hypothesis test to determine if the wine shop is not meeting its...
Lopez Pizza shop wants to determine the best forecast technique for its dough. It evaluates the...
Lopez Pizza shop wants to determine the best forecast technique for its dough. It evaluates the "exponential smoothing" and "Weighted moving average" methods. By using MAPE, assess forecasting techniques and provide your advice/explanation for your choice.. a) For "exponential smoothing" consider smoothing constant is ALPHA = 0.5. The firm assumes the initial forecast for month 1 was 11 units (F1 = 11) b) For the "Weighted Moving average" , the weights used will be 6, and 3. (CONSIDER RECENT TO...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT