0 like 0 dislike
553 views
Full Code is below
in Service-based-companies by Expert (17,730 points) | 553 views

1 Answer

0 like 0 dislike
package practice;

//code by Harshit Agarwal
import java.util.*;
public class MyClass4
{
 public static void main(String[] args) throws java.lang.Exception
 {
  Allium[] v=new Allium[4];
  Scanner inp=new Scanner(System.in);
  for(int i=0;i<v.length;i++)
  {
   int Alliumid=inp.nextInt(); inp.nextLine();
   String Alliumtype=inp.nextLine();
   String state=inp.nextLine();
   int price =inp.nextInt(); inp.nextLine();
   v[i]=new Allium(Alliumid,Alliumtype,state,price);
   
  }
 String i1=inp.nextLine();
 int i2=inp.nextInt();
 inp.close();
 Allium value1=getstate(v,i1);
 if(value1==null)
  System.out.println("State not found.");
 else

  System.out.println(value1.getAlliumType());
 
 
 Allium[] value2=sortAllium(v,i2);
 if(value2==null)
  System.out.println("No Allium found.");
 else
  for(int i=0;i<value2.length-2;i++)
   System.out.println(value2[i].getAlliumType()+"\n"+value2[i].getPrice());
 
 }
 public static Allium getstate(Allium[] arr1,String s1)
 {
  Allium[] count=new Allium[4];
  int j=0;
  for(int i=0;i<arr1.length;i++)
  {
   if(arr1[i].getState().equalsIgnoreCase(s1))
   {
    count[j]=arr1[i];
    j++;
   }
  }
  if(j==0)
  {
   return null;
  }
  else
   return count[0];
 }
 
 public static Allium[] sortAllium(Allium[] arr2,int s2)
 {
  int j=0;
  Allium[] arr = new Allium[4];
  for(int i=0;i<arr2.length;i++)
  {
   if(arr2[i].getPrice()<s2)
   {
    arr[j]=arr2[i];
    j++;
   }
  }
  
  for(int i=0;i<j;i++)
  {
   for(int k=i+1;k<j;k++)
   {
    if(arr[k].getPrice()<arr[i].getPrice())
    {
     if(arr[k]!=null && arr[i]!=null)
     {
      Allium temp=arr[k];
      arr[k]=arr[i];
      arr[i]=temp;
     }
     
    }
   }
  }
  if(j==0) return null;
  
     return arr;
   }
  
 }

class Allium
{
 private int Alliumid,price;
 private String Alliumtype, state;
 Allium(int Alliumid,String Alliumtype,String state,int price)
 {
  this.Alliumid=Alliumid;
  this.Alliumtype=Alliumtype;
  this.state=state;
  this.price=price;
  
 }
 public int getAlliumId()
 {
  return Alliumid;
 }
 public String getAlliumType()
 {
  return Alliumtype;
 }
 public String getState()
 {
  return state;
 }
 public int getPrice()
 {
  return price;
 }
}
by Expert (17,730 points)

Get best answers to any doubt/query/question related to programming , jobs, gate, internships and tech-companies. Feel free to ask a question and you will receive the best advice/suggestion related to anything you ask about software-engineering , development and programming problems .