Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Update getSecondHighestPriceByBrand.java #10

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
31 changes: 21 additions & 10 deletions IPA2/footwearProgram.java
Original file line number Diff line number Diff line change
Expand Up @@ -62,17 +62,28 @@ public static int getCountByType(Footwear[] ft, String t)
}
}

public static Footwear getSecondHighestPriceByBrand(Footwear[] ft, String name)
public static Footwear getSecondHighestPriceByBrand(Footwear[] ft, String name)
{
for(int i =0; i<ft.length; i++)
{
if(ft[i].getName().equalsIgnoreCase(name))
{
return ft[i];
}
}
return null;
}
List<Footwear> matchingFootwear = new ArrayList<>();

// Collect footwears matching the brand name
for (Footwear footwear : ft) {
if (footwear.getName().equalsIgnoreCase(name)) {
matchingFootwear.add(footwear);
}
}

// If there are less than two matching items, return null
if (matchingFootwear.size() < 2) {
return null;
}

// Sort by price in descending order
matchingFootwear.sort((f1, f2) -> Integer.compare(f2.getPrice(), f1.getPrice()));

// Return the second highest priced footwear
return matchingFootwear.get(1);
}
}

class Footwear
Expand Down