You are on page 1of 5

Create a class Fruits with below attributes as private

 int fruitId
 String fruitName
 int price
 int rating

Write getters, setters and parameterized constructor in the above


mentioned attribute sequence as required.
Create class Solution with main method

Implement One static method-


 findMaximumPriceByRating

findMaximumPriceByRating in the solution class. This method will


take array of Fruit objects, int paramater and returns the maximum
priced fruit object from array of Fruit objects whichever has rating
greater than the rating (rating parameter passed)

This method should be called from main method.


Write code to perform following tasks:
1. Take necessary input variable and call findMaximumPriceByRating.
For this method- The main method should print the fruitId from returned
object. As if the returned value is not null, or it should print "No such
Fruit".

Note:- Before calling this static method, use Scanner object to read the
values of four Fruits objects. No two Fruits will have same price. All the
searches should be case insensitive.

Consider below sample input and output to test your code.


Input
555
Apple
200
5
777
Orange
150
4
333
Banana
100
4
888
Avocado
250
4
3

Output
888
Program
import java.util.*;

class Fruit
{
int fruitId;
String fruitName;
int price;
int rating;

Fruit(int fruitId, String fruitName, int price, int rating)


{
this.fruitId=fruitId;
this.fruitName=fruitName;
this.price=price;
this.rating=rating;
}

public int getFruitId()


{
return this.fruitId;
}

public String getFruitName()


{
return this.fruitName;
}

public int getPrice()


{
return this.price;
}

public int getRating()


{
return this.rating;
}

public class Solution{


public static void main(String[] args)
{
Scanner sc=new Scanner(System.in);
Fruit[] f=new Fruit[4];

for(int i=0;i<f.length;i++)
{
int fruitId=sc.nextInt();
sc.nextLine();
String fruitName=sc.nextLine();
int price=sc.nextInt();
sc.nextLine();
int rating=sc.nextInt();
sc.nextLine();
f[i]=new Fruit(fruitId, fruitName, price,rating);
}

int rating1=sc.nextInt();
sc.close();
Fruit m=findMaximumPriceByRating(f,rating1);

if(m!=null)
{
System.out.println(m.getFruitId());
}
else
{
System.out.println("No such Fruit");
}
}

public static Fruit findMaximumPriceByRating(Fruit []f, int rating)


{
Fruit m=null;
int price=0;

if(f!=null && rating>0)


{
ArrayList<Integer> al=new ArrayList<Integer>();
for(int i=0;i<f.length;i++)
{
if(f[i].getRating()>rating)
{
al.add(f[i].getPrice());
}
}

for(int i=0;i<al.size();i++)
{
if(al.get(i)>price)
{
price=al.get(i);
}
}

for(int i=0;i<f.length;i++)
{
if(f[i].getPrice()==price)
{
m=f[i];
}
}
}

if(m==null)
{
return null;
}
else
return m;
}
}

You might also like