OnlinePurchaseManager is #2 from the from the 2020 AP Computer Science A Course Description sample problems.

2020 AP CS A Course Description

The sample free response start on PDF page 206 (labeled page 199 at the bottom right). If this link has been redirected to the 2025 Course Description, please let me know as a comment on this page.

Part (a) countElectronicsByMaker method

public int countElectronicsByMaker(String maker)
{
    int matches = 0;

    for(Gizmo g : purchases)
        if(g.isElectronic() && g.getMaker().equals(maker))
            matches++;

    return matches;
}

Part (b) hasAdjacentEqualPair method

public boolean hasAdjacentEqualPair()
{
    for(int i = 1; i < purchases.size(); i++)
        if(purchases.get(i - 1).equals(purchases.get(i)))
            return true;

    return false;
}

Zero or 1 elements are not special cases. The loop will only run if purchases contains at least 2 elements.

Java files with test code

OnlinePurchaseManagerTest.java includes JUnit 5 test code with the examples from the problem. See Running JUnit 5 tests. Gizmo.java includes a working implementation of equals.

Gizmo.java
OnlinePurchaseManager.java
OnlinePurchaseManagerTest.java

2020 AP CS A Course Description Free Response Solutions

Help & comments

Get help from AP CS Tutor Brandon Horn

See an error? Question? Please comment below.

Comment on OnlinePurchaseManager free response answer