PDA

View Full Version : Simple Java


ranee
Nov 26, 2013, 02:56 PM
Can anyone help me in solving this problem please :


A fruits shop sells several types of fruits each day. Write a program that reads from user several lines of input. Each line includes a fruit's name, price per kilogram (as an integer), number of kilograms sold (as an integer). The program should calculate and print the earned money of all fruits sold and fruit that achieved the largest profit.


b. Give the exact output of your application. Provide 2 snapshots representing the exact output of 2 different inputs.
Hint:
 You could assume that user will insert valid data
 User could stop the program via entering the word "stop" as a fruits' name.

ma0641
Nov 26, 2013, 07:57 PM
You can hardly expect someone to write YOUR program. This is a HELP desk, not a DO desk. Read the sticky on posting homework.

ranee
Nov 26, 2013, 08:29 PM
OK I will put my code

but it's not giving the correct output I don't know where is the mistake



public static void main(String[] args) {
// TODO code application logic here
Scanner input = new Scanner (System.in);
String fruitname= " ";
String maxfruit = " ";
int price = 0,number=0;
int sum=0;
int max=0;


System.out.print("Fruit name, " + "price in killogram, number of killogram sold: ");

while (!fruitname.equals("stop")){

fruitname = input.next();
price = input.nextInt();
number = input.nextInt();
}
if (fruitname.equals("stop"))
{
sum = sum+(price*number);
}
if (max<(price*number))
{
max = price*number;
maxfruit = fruitname;
}


System.out.println("the earned money of all fruits is " + sum);
System.out.println("fruit that achieved the largest profit is "+ maxfruit);

}
}

ma0641
Nov 26, 2013, 09:04 PM
I am not a code writer but if you had have posted this before you would have a much better possibility that someone could help find the issue.

Scleros
Nov 30, 2013, 04:25 AM
One thing that jumps out at me is your summation is not cumulative.