You are on page 1of 5

Create a class Spud with below attributes

 int marketId
 String spudType
 String producingState
 int price(per kg)
Write getters, setters and parameterized constructor in the above mentioned
attribute sequence as required.

Create class Solution with main method


Implement two static methods in Solution class-
 findTypeByState
 sortByPrice

1. findTypeByState in the solution class. This method will take array of Spud
objects, String state and returns spud type if found. If the state of any object
matches with String state, if found else return null if not found.

2. sortByPrice in the Solution class. This method will take array of Spud objects
and int pirce and returns the array of Spud object is ascending order of their
price which are less than Spud price input. If found else return null if not
found.

This method should be called from main method.


Write code to perform following tasks:
1. Take necessary input variable and call findTypeByState . For this method- The
main method should print the spudType from return object. As if the returned value is
not null, or it should print "State not found."

2. Take necessary input variable and call sortByPrice. For this method- The main
method should print the spudType and price from return object. As it is if the returned
value is not null, or it should print "No Spud found.”

Before calling these static methods in main, use Scanner object to read the
values of four spuds objects referring attributes in the above mentioned
Attribute sequence.
Next read the value for string parameter and int parameter.
Consider below sample input and output to test your code.

Input
121
Kufri Jyoti
Bengal
70
321
Bhura Aloo
Uttar Pradesh
55
621
Rosetta
Karnataka
40
921
Kufri Chipsona
Maharastra
90
Karnataka
60

Output

Rosetta

Rosetta

40

Bhura Aloo

55
Program-
import java.util.*;
class Spud{
int id;
String type;
String state;
int price;

Spud(int id, String type, String state, int price)


{
this.id=id;
this.type=type;
this.state=state;
this.price=price;
}

public int getId(){ return this.id; }

public String getType(){ return this.type; }

public String getState(){ return this.state; }

public int getPrice(){ return this.price; }


}

class Solution{
public static void main(String []args)
{
Scanner sc=new Scanner(System.in);
Spud[] s=new Spud[4];
for(int i=0;i<s.length;i++)
{
int id=sc.nextInt();
sc.nextLine();
String type=sc.nextLine();
String state=sc.nextLine();
int price=sc.nextInt();
sc.nextLine();

s[i]=new Spud(id,type,state,price);
}
String state=sc.nextLine();
int price=sc.nextInt();
sc.close();

Spud m=findTypeByState(s,state);
if(m!=null)
{
System.out.println(m.getType());
}
else
{
System.out.println("State not found.");
}

Spud[] m2=sortByPrice(s,price);
if(m2!=null)
{
for(int i=0;i<m2.length;i++)
{
System.out.println(m2[i].getType());
System.out.println(m2[i].getPrice());
}
}
else
{
System.out.println("No Spud found.");
}
}

public static Spud findTypeByState(Spud []s, String state)


{
Spud m=null;

if(s!=null && state.length()>0)


{
for(int i=0;i<s.length;i++)
{
if(s[i].getState().equalsIgnoreCase(state))
{
m=s[i];
}
}
}

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

public static Spud [] sortByPrice(Spud []s, int price)


{
PriorityQueue<Integer> pq=new PriorityQueue<>();
Spud []s2=null;
int size=0;
if(s!=null && price>0)
{
for(int i=0;i<s.length;i++)
{
if(s[i].getPrice()<price)
{
pq.add(s[i].getPrice());
size++;//2
}
}

s2=new Spud[size];

for(int i=0;i<s2.length;i++)
{
for(int j=0;j<s.length;j++)
{
if(pq.peek()==s[j].getPrice())
{
s2[i]=s[j];
pq.poll();
break;
}
}

if(pq.size()==0)
{
break;
}
}

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

You might also like