code:
import java.util.Arrays;
import java.util.Scanner;
public class cpa26june230pm {
public static void main(String[] args) {
Scanner sc=new Scanner(System.in);
Painting[] paintings=new Painting[4];
for (int i = 0; i <paintings.length ; i++) {
int a=sc.nextInt();
sc.nextLine();
String b=sc.nextLine();
String c=sc.nextLine();
int d=sc.nextInt();
sc.nextLine();
String e=sc.nextLine();
paintings[i]=new Painting(a,b,c,d,e);
}
String inputType=sc.nextLine();
String inputName=sc.nextLine();
double ans1=findAvgpriceForGivenType(paintings,inputType);
if(ans1==0.0)
{
System.out.println("No such painting for the given type");
}
else
{
System.out.println(ans1);
}
Painting[] ans2 =findByGivenName(paintings,inputName);
if(ans2==null)
{
System.out.println("No such painting made by the given painter");
}
else
{
for (int i = 0; i <ans2.length ; i++) {
System.out.println(ans2[i].paintingId);
}
}
}
public static double findAvgpriceForGivenType(Painting[] paintings,String inputType)
{
int count=0;
double sum=0;
for (int i = 0; i <paintings.length ; i++) {
if(paintings[i].type.equalsIgnoreCase(inputType))
{
sum=sum+paintings[i].price;
count=count+1;
}
}
if(count>0)
{
return sum/count;
}
return 0.0;
}
public static Painting[] findByGivenName(Painting[] paintings,String inputName)
{
Painting[] help=new Painting[0];
for (int i = 0; i <paintings.length ; i++) {
if(paintings[i].painterName.equalsIgnoreCase(inputName))
{
help= Arrays.copyOf(help,help.length+1);
help[help.length-1]=paintings[i];
}
}
// sorting help array in descending order on basis of paintingId.
for (int i = 0; i <help.length-1 ; i++) {
for (int j = 0; j <help.length-i-1 ; j++) {
if(help[j].paintingId<help[j+1].paintingId)
{
Painting temp=help[j];
help[j]=help[j+1];
help[j+1]=temp;
}
}
}
if(help.length>0)
{
return help;
}
return null;
}
}
class Painting
{
int paintingId;
String paintingName;
String type;
int price;
String painterName;
//constructor
public Painting(int paintingId, String paintingName, String type, int price, String painterName) {
this.paintingId = paintingId;
this.paintingName = paintingName;
this.type = type;
this.price = price;
this.painterName = painterName;
}
}
Comments
Post a Comment