write program have product - using loops

write program have product - using loops

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

View Answers

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);
        }
        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


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 !









Related Tutorials/Questions & Answers:
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
write a program
write a program  write a program add 2 no.s without use arithmetic,unary operaters in java
Advertisements
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 java program
write a java program  write a program to print '*' in a circular form
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
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 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  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 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
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
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
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
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?ADS_TO_REPLACE_1 Thanks
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
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
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 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
write a program in C to print following triangles on the screen
write a program in C to print following triangles on the screen  write a program in C to print following triangles on the screen
write a program to demonstrate wrapper class and its methods......
write a program to demonstrate wrapper class and its methods......  write a program to demonstrate wrapper class and its methods
write a program to use for loop print **********========== in c language
write a program to use for loop print **********========== in c language  write a program to use for loop print **========== in c language
Write a program to get student details and store in a database
Write a program to get student details and store in a database  Write a program to get student details and store in a database
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
write a program for complete JDBC based application
write a program for complete JDBC based application   thank you sir giving answer for my question. my another question is: Write a complete JDBC... and the program should have the facilities of Adding, Modifying, Viewing and Deleting
how do i write a java program for this??
how do i write a java program for this??  â??Ask the user if they have a dog. If â??yesâ??, ask the user how old is and compute the dogâ??s age... as the source for your calculations. You only have to account for input from this table
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
Help me to write this simple java frame program
Help me to write this simple java frame program   I want to write a Java program for.... To create a frame with 4 text fields;name,street,city and pin;with suitable labels. Also add a button "OK". After enter values
ModuleNotFoundError: No module named 'loops'
ModuleNotFoundError: No module named 'loops'  Hi, My Python program is throwing following error: ModuleNotFoundError: No module named 'loops' How to remove the ModuleNotFoundError: No module named 'loops'
Write a program named InternetCharge_YourLastName that do the following:
Write a program named InternetCharge_YourLastName that do the following: ... per month unlimited access is provided. Write a program named InternetCharge.... Instruction: We should have two classes class named InternetService
write a java program that implements the following classes:
write a java program that implements the following classes:  write a java program that implements the following classes: A) a) Point in the Cartesian coordinate system. b) Circle with a given center and radius c) Cylinder
How to write a program count total prices
How to write a program count total prices  Hi. all I've got a assignment to write a program that count total prices of 8 costumers. class PriceCalculator{ public void method(double quantity) { double result
Please write code for this program in java and Explain it ? pl'sssssssss
Please write code for this program in java and Explain it ? pl'sssssssss ... that were ever alive at one time. Write a method to compute this, given the above array of 2n integers. Write one of the following methods: C/C++: int
Write a program to design a page.use radio button?
Write a program to design a page.use radio button?  i would lik to know about well designed page   In the given code, we have used swing components to create a form on frame. import java.sql.*; import javax.swing.
How to write a program count total prices
How to write a program count total prices  Hi. all I've got a assignment to write a program that count total prices of 8 costumers. First class class PriceCalculator {print("code sample"); public void method(double quantity
please help me to write a code for this program
please help me to write a code for this program   1 1 1 1 2 2 1 1 3 4 3 1 1 4 7 7 4 1
please help me to write a code for this program
please help me to write a code for this program   1 1 1 1 2 1 1 3 3 1 1 4 6 4 1
Write a program to display grade message according to the marks
Write a program to display grade message according to the marks  pls give d answer how to Write a program to display grade message according to the marks     Write a program to display grade message according
write a program in java to demonstrate the complete life cycle of servlet:
write a program in java to demonstrate the complete life cycle of servlet:  l have an assignment question. i don't know how to write the code for this program. my question is :Write a program in Java to demonstrate the complete
Write a program in Java to convert Decimal number into Binary number
Write a program in Java to convert Decimal number into Binary number  Hi, I have decimal number and I want to convert it to Binary. How to Write a program in Java to convert Decimal number into Binary number? Thanks
Loops
3.10. Loops Loops are the essential part of the program that have.... More than one loops can be used several times in a script. Loops makes easy to write the program, saves times and shorten the scripting. It instruct the codes

Ads