Problem Statement :
Create a class Resort and make attributes :
int resortID;
String resortName;
String category;
double rating;
double price;
Write constructor and their getter and setter of all the attributes and make the attributes private .
Write a Solution class and a main method inside it. Inside main method , create a method avgPriceOfGivenCategory .
in this method you have to pass array object and one String input that will return integer value . we needs to check every object and if any object which has the category matched to string value taken by user and rating should be more than 4 then find then find the average price of the resorts .
It returns the average price and print it and if returns 0 and print "No any resort" .
Input :
1001
abc resort
4 star
4.5
2000
1002
xyz resort
3 star
4.7
4000
1003
pqr resort
3 star
4.3
6000
1004
efg resort
3 star
4.9
5000
Code :
import java.util.*;
class Resort{
int resortId;
String resortName;
String category;
double rating;
int price;
public Resort(int resortId,String resortName,String category,double rating,int price){
this.resortId=resortId;
this.resortName=resortName;
this.category=category;
this.rating=rating;
this.price=price;
}
}
public class Solution{
public static void main(String[] args) {
Scanner sc=new Scanner(System.in);
Resort arr[]=new Resort[4];
for(int i=0;i<arr.length;i++){
int a =sc.nextInt();
sc.nextLine();
String b=sc.nextLine();
String c=sc.nextLine();
double d=sc.nextDouble();
int e=sc.nextInt();
sc.nextLine();
arr[i]=new Resort(a,b,c,d,e);
}
String str=sc.nextLine();
int avg=avgPriceOfGivenCategory(arr,str);
if(avg==0){
System.out.println("NO resort found");
}
else{
System.out.println(avg);
}
}
public static int avgPriceOfGivenCategory(Resort arr[],String str){
int sum=0,count=0;
for(int i=0;i<arr.length;i++){
if(arr[i].category.equalsIgnoreCase(str) && arr[i].rating>4){
sum+=arr[i].price;
count++;
}
}
if(count==0){
return 0;
}
else{
return sum/count;
}
}
}
Comments
Post a Comment