Question

In: Computer Science

Define a class Product to hold the name and price of items in a grocery store....

Define a class Product to hold the name and price of items in a grocery store. Encapsulate the fields and provide getters and setters. Create an application for a grocery store to calculate the total bill for each customer. Such program will read a list of products purchased and the quantity of each item. Each line in the bill consists of: ProductName ProductPrice Quantity/Weight

The list of items will be terminated by the “end” keyword. The data will look like this: Juice 10 7 Apples 3 3.5 Cereal 5 3 Gum 1 15 Pears 3.5 5 ends The program should read the data from the console, calculate the total price of the items purchased and print it out. For each line create an object of class Product and initialize its fields in the constructor. Then calculate the price for given quantity using the getter of the object.

Solutions

Expert Solution

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

public class product {
        String name;
        float price;
        float quantity;
        static ArrayList<product> productList=new ArrayList<product>();             //contains all products and their details

        //constructor
        public product(String name,float price, float quantity) {
                this.name=name;
                this.price=price;
                this.quantity=quantity;
        }
        
        //getter and setter functions
        public String getName() {
                return name;
        }

        public void setName(String name) {
                this.name = name;
        }

        public float getPrice() {
                return price;
        }

        public void setPrice(float price) {
                this.price = price;
        }

        public float getQuantity() {
                return quantity;
        }

        public void setQuantity(float quantity) {
                this.quantity = quantity;
        }
        public static void main(String[] args) {
                float totalPrice=0;
                String input;
                Scanner sc=new Scanner(System.in);
                System.out.println("Enter the product name, product price and quantity of the product:");
                while(true) {
                        input=sc.nextLine();
                        if(input.equalsIgnoreCase("end"))
                                break;                  //terminate the loop and stop taking more inputs
                        else {
                                StringTokenizer st=new StringTokenizer(input, " ");
                                
                                //split the input line (name,price and quantity) and pass them as parameters to the constructor
                                product temp=new product(st.nextToken(),Float.parseFloat(st.nextToken()),Float.parseFloat(st.nextToken()));
                                productList.add(temp);          //add the object to the list
                        }
                }
                System.out.println("Input completed");
                Iterator it=productList.iterator();
                while(it.hasNext()) {   //iterates over the product list and calculates price of each product whcih is added to the 'totalPrice'
                        product temp=(product) it.next();
                        totalPrice+=(temp.getPrice()*temp.getQuantity());
                }
                System.out.println("Total bill is: "+totalPrice);
                sc.close();
        }
}

The java code is given above. the screenshot of the output has also been attached for your reference.

the code has been written as per the requirements given in the question. Relevant comments have been added which should help to undertsand the code.

Hope it helps.


Related Solutions

Goals Understand class structure and encapsulation Description Define a class Product to hold the name and...
Goals Understand class structure and encapsulation Description Define a class Product to hold the name and price of items in a grocery store. Encapsulate the fields and provide getters and setters. Create an application for a grocery store to calculate the total bill for each customer. Such program will read a list of products purchased and the quantity of each item. Each line in the bill consists of: ProductName ProductPrice Quantity/Weight The list of items will be terminated by “end”...
Here are some prices for randomly selected grocery items from the grocery store: Items Prices: Cheese...
Here are some prices for randomly selected grocery items from the grocery store: Items Prices: Cheese $3.29 Butter $4.99 Eggs $3.49 Yogurt $3.49 Juice $3.89 Tea $3.69 Chips $3.99 Soda $1.99 Pastry $2.99 Cerrial $4.99 Oats $3.29 Almond Milk $2.79 Almonds $4.39 Popcorn $3.29 Crackers $3.59 Ice Cream $6.99 Cookies $2.99 Jam $3.69 Peanut Butter $3.29 Coffee $3.19 Green Tea $4.99 BBQ Sauce $2.99 Oil $6.69 Mayonnaise $4.59 Mustard $2.99 1. Compute the sample mean x and the sample standard...
Here are some prices for randomly selected grocery items from the grocery store: Items Prices: Cheese...
Here are some prices for randomly selected grocery items from the grocery store: Items Prices: Cheese $3.29 Butter $4.99 Eggs $3.49 Yogurt $3.49 Juice $3.89 Tea $3.69 Chips $3.99 Soda $1.99 Pastry $2.99 Cerrial $4.99 Oats $3.29 Almond Milk $2.79 Almonds $4.39 Popcorn $3.29 Crackers $3.59 Ice Cream $6.99 Cookies $2.99 Jam $3.69 Peanut Butter $3.29 Coffee $3.19 Green Tea $4.99 BBQ Sauce $2.99 Oil $6.69 Mayonnaise $4.59 Mustard $2.99 1. Compute the sample mean x and the sample standard...
Here are some prices for randomly selected grocery items from the grocery store: Items Prices: Cheese...
Here are some prices for randomly selected grocery items from the grocery store: Items Prices: Cheese $3.29 Butter $4.99 Eggs $3.49 Yogurt $3.49 Juice $3.89 Tea $3.69 Chips $3.99 Soda $1.99 Pastry $2.99 Cerrial $4.99 Oats $3.29 Almond Milk $2.79 Almonds $4.39 Popcorn $3.29 Crackers $3.59 Ice Cream $6.99 Cookies $2.99 Jam $3.69 Peanut Butter $3.29 Coffee $3.19 Green Tea $4.99 BBQ Sauce $2.99 Oil $6.69 Mayonnaise $4.59 Mustard $2.99 1. Compute the sample mean x and the sample standard...
Here are some prices for randomly selected grocery items from the grocery store: Items Prices: Cheese...
Here are some prices for randomly selected grocery items from the grocery store: Items Prices: Cheese $3.29 Butter $4.99 Eggs $3.49 Yogurt $3.49 Juice $3.89 Tea $3.69 Chips $3.99 Soda $1.99 Pastry $2.99 Cerrial $4.99 Oats $3.29 Almond Milk $2.79 Almonds $4.39 Popcorn $3.29 Crackers $3.59 Ice Cream $6.99 Cookies $2.99 Jam $3.69 Peanut Butter $3.29 Coffee $3.19 Green Tea $4.99 BBQ Sauce $2.99 Oil $6.69 Mayonnaise $4.59 Mustard $2.99 1. Compute the sample mean x and the sample standard...
Grocery Store using Java Arraylist Requirements: 1. Need Product Class and Stock Class 2. Use arraylist...
Grocery Store using Java Arraylist Requirements: 1. Need Product Class and Stock Class 2. Use arraylist to store/ add product in stock 3. Product must have name, code number and quantity in stock 4. Print list of the products and their stock quantity 5. Search products based on their name 6. Remove product based on their code number 7. Sell product and deduct quantity from stock 8. Stock Class contains methods of search product, add product and check quantity of...
Grocery Store using Java Arraylist Requirements: 1. Need Product Class and Stock Class 2. Use arraylist...
Grocery Store using Java Arraylist Requirements: 1. Need Product Class and Stock Class 2. Use arraylist to store/ add product in stock 3. Product must have name, code number and quantity in stock 4. Print list of the products and their stock quantity 5. Search products based on their name 6. Remove product based on their code number 7. Sell product and deduct quantity from stock 8. Stock Class contains methods of search product, add product and check quantity of...
At a grocery store eggs come in cartons that hold a dozen eggs. Experience indicated that...
At a grocery store eggs come in cartons that hold a dozen eggs. Experience indicated that 66.5% of the cartons have no broken eggs, 27.2% have one broken egg, 6.1& have 2 broken eggs, and 0.2% have 3 broken eggs, and the percentage of cartons with 4 or more broken eggs is negligible. Using baye's rule or law of total probability: 1. an egg is selected at random from a carton. what is the probability that the egg is broken?...
A customer in a grocery store is purchasing three items. Write the pseudo code that will:...
A customer in a grocery store is purchasing three items. Write the pseudo code that will: • Ask the user to enter the name of the first item purchased. Then ask the user to enter the cost of the first item purchased. Make your program user friendly. If the user says the first item purchased is milk, then ask: “What is the cost of milk.” [This should work no matter what item is entered by the user. I might buy...
How have some grocery store product categories such as grab-and-go/convenience items benefitted from COVID-19?
How have some grocery store product categories such as grab-and-go/convenience items benefitted from COVID-19?
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT