Skip to content

❗ Incorrect Sample Output & Description – getSecondHighestPriceByBrand #16

Description

@limitless-insanity

Issues

  1. Wrong Output Format

The description says:

"The main method should print price from the returned footwear object"

But the sample output shows:

99  
reebok  
5666

✅ Expected Output:
Just the price:


5666
  1. Incorrect Wording in Description

Current:

"The method will return the second highest footwear objects..."

❌ “objects” is wrong — only one object is returned.

✅ Fix:

"The method will return the footwear object with the second highest price..."

  1. Sample Output Does Not Match Logic
Input brand: reebok
Matching prices: 5667, 5666, 5656

✅ Second highest = 5666

But current output includes extra info. Only price should be printed.

✅ Suggested Fixes
Fix output to only show the price

Replace "objects" with "object" in method description

Ensure sample matches the logic

This is my code, if it helps:

import java.util.*;
class Footwear{
    int footwearId;
    String footwearName;
    String footwareType;
    int price;

    public Footwear(int footwearId, String footwearName, String footwareType, int price) {
        this.footwearId = footwearId;
        this.footwearName = footwearName;
        this.footwareType = footwareType;
        this.price = price;
    }
}

public class second
{
    public static void main(String[] args) {
        Scanner sc=new Scanner(System.in);
        Footwear[] arr=new Footwear[5];
        for(int i=0;i<5;i++){
            int a=sc.nextInt();sc.nextLine();
            String b=sc.nextLine();
            String c=sc.nextLine();
            int d=sc.nextInt();sc.nextLine();
            arr[i]=new Footwear(a,b,c,d);

        }
        String footwareType=sc.nextLine();
        String footwearName=sc.nextLine();

        int res=getCountByType(arr,footwareType);
        if(res>0){
            System.out.println(res);
        }
        else{
            System.out.println("Footwear not available");
        }
        Footwear[] objs=getSecondHighestPriceByBrand(arr,footwearName);
        if(null==objs){
            System.out.println("Brand not available");
        }else{

            System.out.println(objs[0].price);
        }
    }

    public static int getCountByType(Footwear[] arr, String footwearType){
        int res=0;
        for(Footwear ft:arr){
            if(ft.footwareType.equalsIgnoreCase(footwearType))res++;
        }
        return res;
    }

    public static Footwear[] getSecondHighestPriceByBrand(Footwear[] arr, String footwearName){
        int first=-1, second=-1,count=0;
        Arrays.sort(arr,(a,b)->Integer.compare(a.price,b.price));
        Footwear[] ff=new Footwear[0];
        for(Footwear f:arr){
            if(footwearName.equalsIgnoreCase(f.footwearName)  ){
                if(f.price>first){
                    second=first;first=f.price;
                }
            }
        }
        for(Footwear f:arr){
            if(footwearName.equalsIgnoreCase(f.footwearName)){
                if(f.price==second){
                    ff=Arrays.copyOf(ff,(ff.length+1));
                    ff[ff.length-1]=f;
                }
            }
        }
        if(ff.length==0)return null;
        return ff;
    }

}

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions