code:
import java.util.Arrays;
import java.util.Scanner;
public class cpa26june9am {
public static void main(String[] args) {
Scanner sc=new Scanner(System.in);
Goods[] goods=new Goods[4];
for (int i = 0; i <goods.length ; i++) {
int a=sc.nextInt();
sc.nextLine();
String b=sc.nextLine();
double c=sc.nextDouble();
sc.nextLine();
String d=sc.nextLine();
goods[i]=new Goods(a,b,c,d);
}
String input1=sc.nextLine();
int ans1= countGoodsByBrand(goods,input1);
if(ans1>0)
{
System.out.println(ans1);
}
else
{
System.out.println("No such goods for required brand available");
}
Goods ans2 =getSecondHighestPriceGoods(goods);
if(ans2==null)
{
System.out.println("No such goods available");
}
else
{
System.out.println(ans2.goodsId);
System.out.println(ans2.goodsName);
System.out.println(ans2.price);
System.out.println(ans2.brand);
}
}
public static int countGoodsByBrand(Goods[] goods,String input1)
{
int sum=0;
for (int i = 0; i < goods.length; i++) {
if(goods[i].brand.equalsIgnoreCase(input1))
{
sum=sum+1;
}
}
if(sum>0)
{
return sum;
}
return 0;
}
public static Goods getSecondHighestPriceGoods(Goods[] goods)
{
Goods[] help=new Goods[0];
for (int i = 0; i <goods.length ; i++) {
if(goods[i].price>500)
{
help= Arrays.copyOf(help,help.length+1);
help[help.length-1]=goods[i];
}
}
//sorting help array on the basis of price.
for (int i = 0; i <help.length-1 ; i++) {
for (int j = 0; j <help.length-i-1 ; j++) {
if(help[j].price>help[j+1].price)
{
Goods temp=help[j];
help[j]=help[j+1];
help[j+1]=temp;
}
}
}
if(help.length>1)
{
return help[help.length-2];
}
return null;
}
}
class Goods
{
int goodsId;
String goodsName;
double price;
String brand;
//parametrized constructor
public Goods(int goodsId, String goodsName, double price, String brand) {
this.goodsId = goodsId;
this.goodsName = goodsName;
this.price = price;
this.brand = brand;
}
}
Comments
Post a Comment