I need in in java by not comlicated code and not handwriting please Thank you Wr
ID: 3827991 • Letter: I
Question
I need in in java by not comlicated code and not handwriting please
Thank you
Write a Java program that: Reads from Mr. Nader the amount of money with him Reads from Mr. Nader many lines; each line consists of 2 values: the name of product, and its price. At the end, the program should print a message indicating if Mr. Nader has enough money or not. Only if he has enough money, the program should also print the name of product with the minimum price in addition to this price (rounded to 3 decimal places)Explanation / Answer
Program:-
import java.util.*;
import java.text.DecimalFormat;
public class Invoice{
private static DecimalFormat df2 = new DecimalFormat(".##");
public static void main(String []args){
System.out.println("How much money do you have?");
Scanner sc = new Scanner(System.in);
int num = sc.nextInt();
System.out.println("please, insert the item in the invoice (the name of product and its price): ");
System.out.println("insert "stop" as name of product to finish your input");
String str = sc.nextLine();
int i=0;
Double price[]=new Double[10];
String product[]=new String[10];
while(!(str.equalsIgnoreCase("stop"))){
String input[]=str.split(" ");
price[i]=Double.parseDouble(input[1]);
product[i]=input[0];
i++;
str = sc.nextLine();
}
double sum=0,min=price[0];
int pos=0;
for(int j=0;j<price.length;j++){
sum=sum+price[j];
if(price[j]<min){
pos=j;
min=price[j];
}
}
if(sum<=num){
System.out.println("You have enough money");
System.out.println(product[pos]+" is the item with the minimum price (which is INR"+df2.format(min)+")");
}
}
}
Output:-
How much money do you have?
100
please, insert the item in the invoice (the name of product and its price):
insert "stop" as name of product to finish your input
Banana 40.123
biscuit 10.045
juice 20.145
stop
you have enough money
biscuit is the item with the minimum price (which is INR10.045)