Please solve this problem in JAVA
ASAP without modifying given
code. Thanks in advance!
Problem Statement:
Write a Java method to calculate the totalFruitPrice (int)
for the customers based on array of purchased fruits
inStrArr1 (String[]), array of
quantityInKg of fruits inIntArr1 (int[]),
array of available fruits inStrArr2 (String[])and
array of pricePerKg of each fruit
inIntArr2 (int[]) and return the
outStrArr (String[]) as mentioned in the below
logic:
For each fruit name in the inStrArr1.
1. Check if the fruitName is present as one of the elements in
InStrArr2.
2. If it is present, add such fruitName to validFruits(String) and
then multiply the corresponding pricePerkg of
inIntArr2 with quantitylnKg of
inIntArr1 to obtain
totalFruitPrice.
3. Otherwise, add the fruitNarne to
invalidFruits(String)
4. Repeat the above steps for all the fruitName in
the inStrArr1
Add validFruits and above identified
totalFruitPrice in the below format to the
outStrArr
<< "validFruit1:validFruit2: ...totalFruitPrice">>
Add invalidFruits to the outStrArr in the below
format-
<<"invalidFruit1: invalidFruit2: ....X">>
Note: "X" would be in
uppercase
Return the outStrArr
Note:
->
Perform case-insensitive comparision
-> Perform case-sensitive operations when
adding elements to outStrArr
-> Add validFruits and invalidFruits string to
outStrArr only if it contains at least one fruit
in it
Assumptions:
-> Both inIntArr1,
inIntArr2 and inStrArr1,
inStrArr2 would not be empty
-> inStrArr1 and
inStrArr2 would contain only alphabets and would
not contain any spaces and special characters and numbers.
-> Elements in inIntArr1 and
inIntArr2 contains only non-zero positive
numbers
-> outStrArr would always contain two
elements
Note: No need to validate the assumptions
Input format:
-> First line will contain the elements of
inStrArr1 with the elements, separated by ","
(comma)
-> Second line will contain the élements of
inIntArr1 with the elements separated by ","
(comma)
-> Third line will contain the elements of
inStrArr2 with the elements separated by ","
(comma)
-> Fourth line will contain elements
of inIntArr2 with the elements seperated
by "," (comma)
Read the inputs from the standard input stream
Output format:
Print the elements of outStrArr, seperated
by "," (comma), to the standard output stream.
Example:
inStrArr1: {"APPle", "kiwi", "Guava",
"StRAWBerry"}
inIntArr1: {2, 1, 5, 10}
inStrArr2: ("Apple", "MANGo", "PiNEApple",
"Guava". "GRAPES'")
inIntArr2: {60, 50, 55, 40,
65}
outStrArr: {"APPle:
Guava:320", "kwi:StRAWBerry:X"}
Example Explanation:
-> The first fruitName of inStrArr1 is
"APPle" which is present in
inStrArr2. Therefore add "APPle" to
validFruits and multiply corresponding pricePerKg i.e. 60 with
quantitylnKg i.e. 2 and add the result i.e. 120 to the
totalFruitPrice.
Now the totalFruitPrice would be 120 and
validFruits would be "APPle"
-> The second fruitName
of inStrArr1 is "kiwi",
which is not present
in inStrArr2. Therefore add
"kiwi" to invalidFruits.
Now the invalidFruits would be "kiwi"
-> The third fruitName of inStrArr1 is
"Guava" which is present in inStrArr2. Therefore
add "Guava" to validFruits and multiply corresponding pricePerKg
i.e 40 with quantitylnKg i.e. 5 and add the result i.e. 200 to the
totalFruitPrice
Now the totalFruitPrice would be 320 and
validFruits would be "APPle:Guava"
-> The fourth fruitName of inStrArr1 is
"StRAWBerry", which is not present in inStrArr2.
Therefore add "StRAWBerry" to invalidFruits.
Now the invalidFruits would be
"kiwi:StRAWBerry"
• Thus, validFruits contain "APPle:Guava"
and invalidFruits contain "kiwi:StRAWBerry".
Hence add validFruits with totalFruitPrice and invalidFruits with
"X" in the specified format to
outStrArr.
Now the outStrArr would be {
"APPle:Guava: 320",
"kiwi:StRAWBerry:X"}
mango
2
mango
50
jackfruit,banana,strawberry
2,10,3
banana,strawberry,jackfruit
3,60,30
// DO NOT MODIFY THE CODE PROVIDED TO YOU
import java.util.*;
class Solution {
public String[] fruitInfo(string[] inStrArr1, int[] inIntArr1,
string[] inStrArr2, int[] inIntArr2)
{
String outStrArr[] = new String[2];
//Implement your logic here
return outStrArr;
}
public static void main(String args[])
{
// DO NOT MODIFY BELOW CODE. "PROVIDE CUSTOM
INPUT" AS MENTIONED IN THE INPUT FORMAT OF THE PROBLEM
DESCRIPTION
Scanner sc = new Scanner (System.in);
//Accepting elements of instrArr1
String[] inStrArr1 = sc.nextLine().split(",");
//Accepting elements of inIntArr1
String[] input1 = sc.nextLine().split(",");
// Accepting elements of inStrArr2
String[] inStrArr2 = sc.nextLine().split(",");
//Accepting elements of inIntArr1
String[] input2 = sc.nextLine().split(",");
// Converting string[] to int[] for
inIntArr1
int[] inIntArr1 = new int[input1.length];
for(int index=0; index<input1.length; index++)
{
intIntArr1[index] = Integer.parseInt(input1[index]);
}
//Converting string[] to int[] for
inIntArr2
int[] inIntArr2 = new int[input2.length];
for(int index=0; index<input2.length; index++)
{
intIntArr2[index] = Integer.parseInt(input2[index]);
}
Solution fruitDetailobj = new Solution();
string [] outStrArr = fruitDetailobj.fruitInfo(inStrArr1,
inIntArr1, inStrArr2, inIntArr2);
//printing elements of outStrArr
if(outStrArr[0] == null && outStrArr[1] != null)
{
System.out.println(outStrArr[1]);
}
else if(outStrArr[1]==null && outStrArr[0] !=null)
{
System.out.println(outStrArr[0]);
}
else if(outStrArr[0]!=null && outStrArr[1]!=null)
{
System.out.println(outStrArr[0]+","+outStrArr[1]);
}
else{
System.out.println("outStrArr is empty");
}
}
}
Please solve this problem in JAVA ASAP without modifying given code. Thanks in advance! Problem Statement: Write a Java
-
answerhappygod
- Site Admin
- Posts: 899604
- Joined: Mon Aug 02, 2021 8:13 am
Please solve this problem in JAVA ASAP without modifying given code. Thanks in advance! Problem Statement: Write a Java
Join a community of subject matter experts. Register for FREE to view solutions, replies, and use search function. Request answer by replying!