I need solution for this :


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

Please help me

Recommended Answers

All 15 Replies

I'll do it for you. $50/hour, OK?

You had better get started then, saturday is tomorrow! James, if you were to charge $50/hour you'd only get about 3 bucks for that lol

I want solution for code before saturday

Then you'd better get cracking. We are not a homework (cheating) service.

Maybe next time you'll do your homework rather than go partying.

I'll do it for you. $50/hour, OK?

No , Thank you..

Then you'd better get cracking. We are not a homework (cheating) service.

Maybe next time you'll do your homework rather than go partying.

that not true

I can write this code but I can't Complete it

I don't know How to calculate the highest value I entered ?

if you can tell me how to calculate ? or thank you for that

note I'm use loop !

We will be more than happy to help you. Post your code with a complete listing of all compiler message and/or exception messages and complete, but brief description of what, exactly you are haviong a problem with and we will help you correct it. But, when you dump an assignment text and say "do this by saturday" what are you expecting? I'll tell you what you're expecting. You're expecting someone to do it for you.

I can write this code but I can't Complete it

As masijade said post what you have done so far and we will see what we can do for you.

import java.util.*;
public class  Products 
{ // start class
public static void main(String[] args)
{ // start main
Scanner console = new Scanner (System.in);

int id,i;
double p=0;
String name ;
name = console.next();
id=0;

for (i=1 ;  ; i++ )

{ 
System.out.println("Enter product id:");
id = console.nextInt();
     if (id != -1 )
        {
System.out.println("Enter product name: ");
name = console.next();
 
System.out.println("Enter product price:  ");
p = console.nextDouble();

         }
			
System.out.println("The most expensive product is:"); 
System.out.println("Id :"+id+" "+"name : "+ name +"Price: "+p); 
System.out.println("Average price = "+);
}

}
}

Hint
Create class Product that is able to store following data
Enter product id
Enter product name
Enter product price

Make the Product class available to your application.
Use a Collection like ArrayList, List, or Vector to store data of every product.
Provide logic that will do discovery of most expensive product and calculate average.

So please start working on this and when you get problems post

^^

thank you so much

What do I use?

ex : if , for , while , swicth

For the Product class you need some constructor method and some getter and setter methods.
As for the logic of finding most expensive item use what ever you most comfortable...

I don't know how to Create class Product that is able to store data :s

please help me , or give me website for that

I don't know how to Create class Product that is able to store data :s

please help me , or give me website for that

Similar example here, you need to just adapt to your needs

You can use ArrayList from java.util.*. Its a collection class.
It has various functions for add, remove etc. You can read in book about it in detail. There's other like TreeSet which have sorted set. And as they are generic class you can even declare to your data Structure need. like declare a class with elements product_id, Name, Price. and now in main declareArrayList<your_class> variable= new ArrayList<your_class>()
with the add method add each class which hold your different product to ArrayList. ArrayList have iterator method which allow you to traverse thru data. in while loop you can traverse thru it and with simple logic find the Max price of product.
Hint *Simple compare and exchange to get Max, Hold the complete data in a temp variable latter display it you can get the avg same way add all while traversing..........

----------------------------------------------------------------------------------Hope I didn't break the rule as there's no code just Hints to get it done

thank you to all

you have hellped me REALY

(L)

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.