Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>As others have said, the use of <code>==</code> for comparing strings is almost certainly inappropriate. However, it shouldn't actually have caused a problem in your test case, as you're using the same constant string twice, so <code>==</code> should have worked. Of course, you should still fix the code to use <code>equals</code>.</p> <p>It's also not clear what the <code>Patron</code> constructor or <code>getName</code> methods do - either of those could cause a problem (e.g. if they create a <em>new</em> copy of the string - that would cause your test to fail, but would also be unnecessary usually).</p> <p>What's slightly more worrying to me is this comment:</p> <pre><code>// 3. If the name is unique, we want to get the largest key value (customer number) // already in the hash, an increment by one. </code></pre> <p>This comment is <em>within</em> the main loop. So by that point we don't know that the name is unique - we only know that it doesn't match the name <em>of the patron in this iteration</em>.</p> <p>Even more worrying - and I've only just noticed this - you perform the add <em>within the iteration block</em> too. It seems to me that you should have something more like this:</p> <pre><code>public int addPatron(String name) throws PatronException { int maxKey = -1; for (Map.Entry&lt;Integer, Patron&gt; entry : patrons.entrySet()) { if (entry.getValue().getName().equals(name)) { // TODO: Consider using IllegalArgumentException throw new PatronException("This patron already exists"); } maxKey = Math.max(maxKey, entry.getKey()); } int newKey = maxKey + 1; Patron newPatron = new Patron(name, newKey); patrons.put(newKey, newPatron); return newKey; } </code></pre> <p>Additionally, it sounds like <em>really</em> you want a map from name to patron, possibly as well as the id to patron map.</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload