In: Computer Science
Problem 01: GroceryCalculator
Write a program to calculate the cost of a list of grocery items. It includes one attribute: totalCost. It includes five user-defined methods:
Problem 02: Dog
“Hi, my name is Lucky. I am a Chiwawa. I am 3 years old, and my color is white.”
PROGRAM -
import java.util.*;
import java.io.*;
public class store{
double TotalCost;
//asks the user to type an item name and
returns the name of the item. It returns a string.
public String getItemName(){
System.out.println("Enter the item name: ");
Scanner sc = new
Scanner(System.in);
String name =
sc.next();
return name;
}
//asks the user to type the price for an item
and returns the price, which return a double.
public double getItemPrice(){
System.out.println("Enter the item price: ");
Scanner sc = new
Scanner(System.in);
double price =
sc.nextDouble();
return price;
}
//asks the user to type how many of the item
they want to buy and returns an integer variable.
public int getItemQuantity(){
System.out.println("Enter the item quantity: ");
Scanner sc = new
Scanner(System.in);
int Quantity =
sc.nextInt();
return Quantity;
}
//calls the above three methods. Inside this
method, use a while loop to allow the user to enter more items. The
user may type "q" to quit typing more items. In this method,
public void getTotalGrocery()
{
double cost = 0;
while(true)
{
String name= getItemName();
double price = getItemPrice();
int Quantity = getItemQuantity();
cost+=price*Quantity;
Scanner sc = new Scanner(System.in);
System.out.println("Enter anything to continue, press q to exit....
");
String s = sc.next();
if(s.equals("q"))
{
break;
}
}
TotalCost = cost;
}
//returns the total cost
public double getTotalCost(){
return TotalCost;
}
public static void main(String[] args) {
store s = new
store();//creating a new object
s.getTotalGrocery();//calling to get all items
System.out.println("The
total cost is: "+s.getTotalCost());//print result
}
}
INTENDATION-
OUTPUT TESTED-
ASK ANY DOUBTS IN THE COMMENTS.