
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
The most expensive product is: Id: 1021 Name: Kit Kat Price: 2 S.R. Average price = 1.167

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

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);
}
list1.add(id);
System.out.print("Name: ");
String name=input.next();
list2.add(name);
System.out.print("Price:");
double price=input.nextDouble();
list3.add(price);
System.out.println();
}
while(id!=-1);
}
}
Thanks

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 ?
If you are facing any programming issue, such as compilation errors or not able to find the code you are looking for.
Ask your questions, our development team will try to give answers to your questions.