You are on page 1of 2

## Problem Statement

Create class Medicine with below attributes:


MedicineName - String
batch - String
disease - String
price - int

Create class Solution and implement static method "getPriceByDisease" in the


Solution class.
This method will take array of Medicine objects and a disease String as parameters.
And will return another sorted array of Integer objects where the disease String
matches with the original
array of Medicine object's disease attribute (case insensitive search).

Write necessary getters and setters.

Before calling "getPriceByDisease" method in the main method, read values for four
Medicine objects referring
the attributes in above sequence along with a String disease.
Then call the "getPriceByDisease" method and print the result.

## Input

dolo650
FAC124W
fever
100
paracetamol
PAC545B
bodypain
150
almox
ALM747S
fever
200
aspirin
ASP849Q
flu
250
fever

## Output

100
200

## Solution:-
import java.util.Arrays;
import java.util.Scanner;
public class Solution {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
Medicine[] medicines = new Medicine[4];
for(int i =0;i<medicines.length;i++) {
medicines[i] = new
Medicine(sc.nextLine(),sc.nextLine(),sc.nextLine(),sc.nextInt());
sc.nextLine();
}
String disease = sc.nextLine();
sc.close();
Integer[] result = getPriceByDisease(medicines,disease);
for(int i=0;i<result.length;i++) {
System.out.println(result[i]);
}
}
public static Integer[] getPriceByDisease(Medicine[] medicines,String disease)
{
Integer[] result = new Integer[0];
for(int i=0;i<medicines.length;i++) {
if(medicines[i].getDisease().equalsIgnoreCase(disease)) {
result = Arrays.copyOf(result, result.length+1);
result[result.length-1] = medicines[i].getPrice();
}
Arrays.sort(result);
}
return result;
}
}
class Medicine{
String MedicineName;
String batch;
String disease;
int price;

public String getDisease() {


return disease;
}
public int getPrice() {
return price;
}

public Medicine(String medicineName, String batch, String disease, int price) {


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

You might also like