0% found this document useful (0 votes)
21 views6 pages

Gift Java

This Java code defines a Gift class and methods to find the lowest priced gift by a given brand and the third highest priced gift. It takes gift details as input, stores them in an array, and finds the gifts matching the criteria.

Uploaded by

ROHAN CHOPDE
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)
21 views6 pages

Gift Java

This Java code defines a Gift class and methods to find the lowest priced gift by a given brand and the third highest priced gift. It takes gift details as input, stores them in an array, and finds the gifts matching the criteria.

Uploaded by

ROHAN CHOPDE
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/ 6

import java.util.

Scanner;

import java.util.Arrays;

public class Solution {

public static void main(String[] args) {

Scanner sc=new Scanner(System.in);

Gift[] gifts=new Gift[5];

for (int i = 0; i <5 ; i++) {

int a=sc.nextInt();sc.nextLine();

String b=sc.nextLine();

int c=sc.nextInt();sc.nextLine();

String d=sc.nextLine();

gifts[i]= new Gift(a,b,c,d);

String input=sc.nextLine();

Gift ans1=getLowestPricedGiftByBrand(gifts,input);

if(ans1==null)

System.out.println("No such gift found");

else {

System.out.println(ans1.getGiftId());

System.out.println(ans1.getGiftName());

System.out.println(ans1.getPrice());

System.out.println(ans1.getBrand());

Gift ans2= getThirdHighestPricedGift(gifts);


if(ans2==null)

System.out.println("No such gift found");

else {

System.out.println(ans2.getGiftId());

System.out.println(ans2.getGiftName());

System.out.println(ans2.getPrice());

System.out.println(ans2.getBrand());

public static Gift getLowestPricedGiftByBrand(Gift[] gifts,String input)

int low=Integer.MAX_VALUE;

for (int i = 1; i <gifts.length ; i++) {

if(gifts[i].getBrand().equalsIgnoreCase(input))

if(gifts[i].getPrice()<low)

low=gifts[i].getPrice();

}
for (int i = 0; i <gifts.length ; i++) {

if(gifts[i].getPrice()==low)

return gifts[i];

return null;

public static Gift getThirdHighestPricedGift(Gift[] gifts)

int[] help=new int[0];

for (int i = 0; i <gifts.length ; i++) {

if(gifts[i].getPrice()<1000 && gifts[i].getPrice()%2!=0 )

help=Arrays.copyOf(help,help.length+1);

help[help.length-1]=gifts[i].getPrice();

Arrays.sort(help);

if(help.length<=2)

return null;

int thp=help[help.length-2];
for (int i = 0; i <gifts.length ; i++) {

if(gifts[i].getPrice()==thp)

return gifts[i];

return null;

class Gift

private int giftId;

private String giftName;

private int price;

private String brand;

public Gift(int giftId, String giftName, int price, String brand) {

this.giftId = giftId;

this.giftName = giftName;

this.price = price;

this.brand = brand;

public int getGiftId() {


return giftId;

public void setGiftId(int giftId) {

this.giftId = giftId;

public String getGiftName() {

return giftName;

public void setGiftName(String giftName) {

this.giftName = giftName;

public int getPrice() {

return price;

public void setPrice(int price) {

this.price = price;

public String getBrand() {

return brand;

public void setBrand(String brand) {

this.brand = brand;

}
}

You might also like