0% found this document useful (0 votes)
9 views

Code1

Uploaded by

Aayush Agrawal
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
9 views

Code1

Uploaded by

Aayush Agrawal
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
You are on page 1/ 3

// Online Java Compiler

// Use this editor to write, compile and run your Java code online
import java.util.*;

class Medicine {
private String MedicineName;
private String batch;
private String disease;
private int price;

public Medicine(String MedicineName, String batch, String disease, int price)


{
this.MedicineName = MedicineName;
this.batch = batch;
this.disease = disease;
this.price = price;
}

public String getName() {


return MedicineName;
}
public String getBatch() {
return batch;
}
public String getDisease() {
return disease;
}
public int getPrice() {
return price;
}

public void setName(String MedicineName) {


this.MedicineName = MedicineName;
}
public void setBatch(String batch) {
this.batch = batch;
}
public void setDisease(String disease) {
this.disease = disease;
}
public void setPrice(int price) {
this.price = price;
}
}
class Main {
public static Medicine[] getPriceByDisease(Medicine []med, String
target_disease)
{
boolean found = false;
ArrayList<Medicine> list = new ArrayList<Medicine>();
for(int i=0;i<med.length;i++)
{
if(med[i].getDisease().equalsIgnoreCase(target_disease))
{
found = true;
list.add(med[i]);
}
}
for(int i=0;i<list.size();i++)
{
for(int j=0;j<list.size()-i-1;j++)
{
if(list.get(j).getPrice() > list.get(j+1).getPrice())
{
Medicine temp = list.get(j+1);
list.set(j+1, list.get(j));
list.set(j, temp);
}
}
}

Medicine []result = new Medicine[list.size()];


result = list.toArray(result);

if(found)
{
return result;
}
else
{
return null;
}
}

public static void main(String[] args) {


Scanner sc = new Scanner(System.in);

int n = sc.nextInt();
sc.nextLine();
Medicine []med = new Medicine[n];

for(int i=0;i<n;i++)
{
String MedicineName = sc.nextLine();
String batch = sc.nextLine();
String disease = sc.nextLine();
int price = sc.nextInt();
sc.nextLine();

med[i] = new Medicine(MedicineName, batch, disease, price);


}

String target_disease = sc.nextLine();

Medicine []ans2 = getPriceByDisease(med, target_disease);


if(ans2 != null)
{
for(int i=0;i<ans2.length;i++)
{
System.out.println(ans2[i].getPrice());
}
}
else
{
System.out.println("No player is available with specified level, skill
and eligibility points.");
}
}
}

You might also like