Cafeteria with distinct menu items

Hey everyone I’m working on a project with a Cafeteria class and a MenuItem class. The Cafeteria has a bunch of menu items. A menu item is supposed to be unique based on its name serving date and cafeteria ID combined. I wrote a test that should throw an error if I try to add a non-unique item to a cafeteria. But it’s not working like I thought it would. The test (addDuplicateMenuItemToCafeteriaTest) doesn’t throw any errors. I think I messed up my Cafeteria and MenuItem classes. Can anyone spot what I’m doing wrong?

I’m also new to TDD so any tips on that would be great too.

Here’s a simplified version of my code:

public class Cafeteria {
    private Set<MenuItem> menu = new HashSet<>();

    public boolean addMenuItem(MenuItem item) {
        return menu.add(item);
    }
}

public class MenuItem {
    private String name;
    private Date serveDate;
    private Cafeteria cafeteria;

    // getters setters and equals/hashCode methods
}

@Test
public void addDuplicateMenuItemToCafeteriaTest() {
    Cafeteria cafe = new Cafeteria();
    MenuItem item = new MenuItem();
    cafe.addMenuItem(item);
    cafe.addMenuItem(item);  // This should throw an exception but doesn't
}

What am I missing here? Thanks for any help!

Hey Liam39, interesting project you’ve got there! :plate_with_cutlery:

I think I might see what’s tripping you up. Your HashSet isn’t throwing an error when you add a duplicate - it’s just quietly failing to add it. That’s actually how HashSet is designed to work.

Have you considered using a Map instead? Something like this could work:

public class Cafeteria {
    private Map<String, MenuItem> menu = new HashMap<>();

    public void addMenuItem(MenuItem item) {
        String key = item.getName() + item.getServeDate() + item.getCafeteria().getId();
        if (menu.containsKey(key)) {
            throw new IllegalArgumentException("Duplicate menu item!");
        }
        menu.put(key, item);
    }
}

This way, you’re explicitly checking for duplicates and throwing an exception when you find one. It might be more in line with what you’re trying to achieve.

BTW, how are you liking TDD so far? It can be tricky to get into at first, but it’s super rewarding once you get the hang of it. Keep at it!

yo liam, ur code looks good but the problem’s in how hashset works. it won’t throw errors for dupes, just won’t add em. try this:

in cafeteria class:

public void addMenuItem(MenuItem item) {
    if (!menu.add(item)) {
        throw new IllegalArgumentException("dupe item!");
    }
}

this’ll throw an error when u try addin a dupe. good luck with ur project!

I see the issue in your code. The problem lies in how you’re using HashSet and implementing equals() and hashCode() for MenuItem.

HashSet relies on these methods to determine uniqueness. If they’re not properly overridden, duplicates can slip through. Make sure your MenuItem class correctly implements equals() and hashCode(), considering name, serveDate, and cafeteria.

Also, HashSet’s add() method returns a boolean, not throws an exception. If you want an exception, you’ll need to implement that logic yourself in the Cafeteria class.

For TDD, start with a failing test, then write just enough code to make it pass. Refactor as needed. It’s an iterative process that helps ensure your code meets requirements and remains testable.

Hope this helps point you in the right direction!