Question

In: Computer Science

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” keyword.

The data will look like this:

Juice 10 7

Apples 3 3.5

Cereal 5 3

Gum 1 15 Pears

3.5 5

end

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

/******************************************************************************

                            Online Java Compiler.
                Code, Compile, Run and Debug java program online.
Write your code in this editor and press "Run" button to execute it.

*******************************************************************************/
import java.util.*;

class Grocery{
    public String name;
    public double price;
    public double quantity;
    public Grocery(String name,double price,double quantity){
        this.name = name;
        this.price = price;
        this.quantity = quantity;
    }
    
    public String getName(){
        return this.name;
    }
    
    public double getPrice(){
        return this.price;
    }
    
    public double getQuantity(){
        return this.quantity;
    }
}


public class Main{

     public static void main(String []args){
        double totalPrice = 0.0;
        Scanner myObj = new Scanner(System.in);
        System.out.println("Enter No of items purchased");
        int n = Integer.parseInt(myObj.nextLine());
        String name;
        double price;
        double quantity;
        for (int i=0;i<n;i++){
            System.out.println("Enter Name of item purchased");
            name = myObj.nextLine();
            System.out.println("Enter Price of item purchased");
            price = Double.parseDouble(myObj.nextLine());
            System.out.println("Enter quantity of item purchased");
            quantity = Double.parseDouble(myObj.nextLine());
            Grocery g1 = new Grocery(name,price,quantity);
            totalPrice += g1.getPrice()*g1.getQuantity();
        }
        System.out.println(totalPrice);
     }
}

Sample Output:

Please comment in case of doubts, Please upvote the solution


Related Solutions

C++ program: Define a structure to hold the contact's information including: name, phone number, and a...
C++ program: Define a structure to hold the contact's information including: name, phone number, and a pointer to the next node on the list. Each node on the list will be a contact instead of a number (like the example in the book). struct ContactNode { string name; string phoneNumber; ContactNode *next; } Define a class containing the structure, private member variable head (that will point to the beginning of the list) and the following member functions: A constructor that...
The Person Class Uses encapsulation Attributes private String name private Address address Constructors one constructor with...
The Person Class Uses encapsulation Attributes private String name private Address address Constructors one constructor with no input parameters since it doesn't receive any input values, you need to use the default values below: name - "John Doe" address - use the default constructor of Address one constructor with all (two) parameters one input parameter for each attribute Methods public String toString() returns this object as a String, i.e., make each attribute a String, concatenate all strings and return as...
Define a JAVA class ISP. The class consists of the name of the subscription plan and...
Define a JAVA class ISP. The class consists of the name of the subscription plan and a method that display the plan. Derive a class DPlan from ISP. The DPlan will charge RM10 per Mbps subscribe and RM0.20 per GB used. Derive a class MPlan from ISP. The MPlan will charge RM5 per Mbps subscribe and RM0.80 per GB used. Display the plan and select the best plan.
Complete the table by dragging each structure name or description into the appropriate place
Complete the table by dragging each structure name or description into the appropriate place 
C++ Define a base class called Person. The class should have two data members to hold...
C++ Define a base class called Person. The class should have two data members to hold the first name and last name of a person, both of type string. The Person class will have a default constructor to initialize both data members to empty strings, a constructor to accept two string parameters and use them to initialize the first and last name, and a copy constructor. Also include appropriate accessor and mutator member functions. Overload the operators == and !=...
Define a class Fraction3YourName as follows, /** * Program Name: Fraction3YourName.java * Discussion: Fraction3Yourname class *...
Define a class Fraction3YourName as follows, /** * Program Name: Fraction3YourName.java * Discussion: Fraction3Yourname class * written By: * Date: 2019/09/19 */ public class Fraction3YourName { private int sign; private int num; private int denom; public Fraction3YourName() { //sign = ; //denom = ; } public Fraction3YourName(int n) { //sign = ; //num = n; //denom = ; } public Fraction3YourName(int s, int n, int d) { //sign = s; //num = n; //denom = d; } } You are...
4) Define an abstract class Name Java class that implements interface Comparable   
4) Define an abstract class Name Java class that implements interface Comparable   
In object C Define a class called XYPoint that will hold a Cartesian coordinate (x, y),...
In object C Define a class called XYPoint that will hold a Cartesian coordinate (x, y), where x and y are integers. Define methods to individually set the x and y coordinates of your new point and retrieve their values. Write an Objective-C program to implement your new class and test it (main section of the file). Your test program needs to create two instances of your class. Make sure your program test prints out the values that you have...
Create a class named Lease with fields that hold an apartment tenant’s name, apartment number, monthly...
Create a class named Lease with fields that hold an apartment tenant’s name, apartment number, monthly rent amount, and term of the lease in months. Include a constructor that initializes the name to “XXX”, the apartment number to 0, the rent to 1000, and the term to 12. Also include methods to get and set each of the fields. Include a nonstatic method named addPetFee() that adds $10 to the monthly rent value and calls a static method named explainPetPolicy()...
Implement an Employee class that includes data fields to hold the Employee’s ID number, first name,...
Implement an Employee class that includes data fields to hold the Employee’s ID number, first name, last name, and hourly pay rate (use of string datatype is not allowed). Create an array of five Employee objects. Input the data in the employee array. Write a searchById() function, that ask the user to enter an employee id, if its present, your program should display the employee record, if it isn’t there, simply print the message ”Employee with this ID doesn’t exist”....
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT