Maha abdulla
write program have product - using loops
3 Answer(s)      6 years and 5 months ago
Posted in : Java Beginners

Write a program for a grocery that reads products data and determine and display the product that has the highest price and the average price. A product has three pieces of data: id (int), name (String) and price (double). Your program should continue reading new products until user enters -1 as id of a product.

Example: Enter product id: 1001 Enter product name: Pepsi Enter product price: 1 Enter product id: 1021 Enter product name: Kit Kat Enter product price: 2

Enter product id: -1

The most expensive product is: Id: 1021 Name: Kit Kat Price: 2 S.R. Average price = 1.167

November 28, 2010 at 9:14 PM

for highest price use static method max( double, double) of java.lang.Math class

November 29, 2010 at 12:31 PM

Hi Friend,

Try the following code:

```import java.util.*;
class Grocery{
public static void main(String[] args){
ArrayList list1=new ArrayList();
ArrayList list2=new ArrayList();
ArrayList list3=new ArrayList();
Scanner input=new Scanner(System.in);
int id=0;
do{
System.out.print("Enter Product ID: ");
id=input.nextInt();
if(id==-1){
Collections.sort(list1);
Collections.sort(list2);
Collections.sort(list3);
double sum=0;
for(int i=0;i<list3.size();i++){
sum+=Double.parseDouble(list3.get(i).toString());
}
System.out.println("The most expensive product is: \n ID is: "+list1.get(list1.size()-1)+"\nName: "+list2.get(list2.size()-1)+"\nPrice: "+list3.get(list3.size()-1));
System.out.println("Average Price is: "+sum/list3.size());
System.exit(0);
}
System.out.print("Name: ");
String name=input.next();
System.out.print("Price:");
double price=input.nextDouble();
System.out.println();
}
while(id!=-1);
}
}
```

Thanks

November 29, 2010 at 7:44 PM

thanks ..

but why you didn't put break ? becase the while is not stop when I run it !! and I think it's will be smaller when we use console ?

• I use JGrasp program !

Write a program that (algorithmically) shows a times table.(convert integers to strings, format text, nested loops)
Write a program that (algorithmically) shows a times table.(convert integers to strings, format text, nested loops)   Write a java program..., nested loops) 0 1 2 3 4 5 6 7 8 9 0 0 0 0 0 0 0
PRODUCT
PRODUCT  WHAT IS THE DIFFERENCE BETWEEN PRODUCT AND PROJECT
program that uses while loops to perform the following steps :
program that uses while loops to perform the following steps :  Write a program that uses while loops to perform the following steps : a. Prompt the use to input two integers : FirstNum and secondNum.( FirstNum must be less
write a program
write a program  write a program add 2 no.s without use arithmetic,unary operaters in java
Loops
Loops  Write code that uses nested loops to print the following patterns: Pattern1: 1 1 2 1 2 3 1 2 3 4 1 2 3 4 5 1 2 3 4 5 6 Pattern 2: 1 2 3 4 5 6 1 2 3 4 5 1 2 3 4 1 2 3 1 2 1
Loops
Loops  Write code that uses nested loops to print the following patterns: Pattern1: 1 1 2 1 2 3 1 2 3 4 1 2 3 4 5 1 2 3 4 5 6 Pattern 2: 1 2 3 4 5 6 1 2 3 4 5 1 2 3 4 1 2 3 1 2 1
Loops
Loops  Write a program that repeatedly reads an integer from the console. If the integer is odd, outputs ΓΆβ?¬Ε?Odd Day!ΓΆβ?¬Β? if the integer... program
Loops
Loops  Write code that ask user to insert an integer (n) and prints out the factorial (n!) of this number. n! = n (n-1)(n-2)(n-3)...1 , n > 0
Write a java application program........?
Write a java application program........?  Welcome every One : I have Q in Java? Q : Write a java application program that reads 20 integer numbers input by the user and prints how many numbers > 100 and how many numbers
write a java program
write a java program  write a program to print '*' in a circular form
How to write Java Program
How to write Java Program  how to write a program to find average of 5 student marks
WRITE A PROGRAM IN JAVA
WRITE A PROGRAM IN JAVA  Write program which accepts five students id,name,age,department,courses, taken and its grade. a. calculate its GPA . display all student information in ascending order
WRITE A PROGRAM IN JAVA
WRITE A PROGRAM IN JAVA  How do I write a program in Java, have the program display a message with your name in it and a number (Hello john Smith!1) The number must increment from zero to nine, or decrement fron nine to zero. You
write the java program?
write the java program?  1 11 121 1331 14641 15101051
write the java program?
write the java program?  1 11 121 1331 14641 15101051
Write java program?
Write java program?  1 11 121 1331 14641 15101051
Write java program?
Write java program?  1 11 121 1331 14641 15101051
write a program to print
write a program to print   write a program to print following 1 2 3 4 5 4 3 2 1 1 2 3 4 4 3 2 1 1 2 3 3 2 1 1 2 2 1 1 1
Write a program in java...
Write a program in java...  Hi, friends Please, can you help me? Q1: Write a program in java to simulate a calculator. Your program should take two... to enter an integer number. Write a program in java to find the factorial
write a program - Java Beginners
write a program  1. write a program that reads a string composed of 6 words then generate and print a word composed of the first letters of the 6 words.  Hi friend, Code to solve the problem : class FirstLetter
write program - Java Beginners
write program  write a java program that will read the values of matrix A and B give its multiplication in matrix C  Hi friend, Code for Matrix Multiplication : class MatrixMultiplication{ public static void
write a program in java.
write a program in java.  arrange the natural n umber in 5x5 matrix as 21 22 23 24 25 20 7 8 9 10 19 6 1 2 11 18 5 4 3 12 17 16 15 14 13 i at centerd position and remaining arrange in anticlockwise direction.
write following program
write following program  Q1 What is the difference between single...-threading. Explain with examples Q2 Write a java applet that takes your name through a parameter from the HTML file and displays it Q3 Write a short note
software or i have to write a code for this suff
convert windows picture or fax viewer file into ms word or pdf   sir i want to convert windows picture or fax viewer file into ms word or pdf so for this is i have to take help from the java code or any software is present
How to write calculator in J2ME program?
How to write calculator in J2ME program?  How to write calculator in J2ME program
how to write servlet program in eclipse?
how to write servlet program in eclipse?  how to write servlet program in eclipse
how to write this program? - Java Beginners
how to write this program?  (game:scissor,rock ,paper) A program that plays the sciccor-rock-paper game.Revise the program to let the user continuously play until either the user or the computer wins more than 4 times
write a program to create a user defined
write a program to create a user defined   plz send me code   Hi Friend, Please specify what do you want to create? Thanks
Write a program that takes the marks of user as input
Write a program that takes the marks of user as input  Write... the program should display "Great, you have passed the course", else the program should... not understand how to write a program like this and thank you
how to write the program - Java Beginners
how to write the program  WAP to create the report card user input Name, Class, Division, Roll no., Marks obtained in following subjects Lang Hindi History Geography Math Phy Chem. Bio Eve CSTA Also has a back up
i have problem in that program sir - JavaMail
i have problem in that program sir  1.Develop multi-threaded echo server and a corresponding GUI client in Java
Tutorials