Question: Would someone be able to explain what I'm doing wrong here? I am not entirely sure why I keep getting the error below when I
Would someone be able to explain what I'm doing wrong here? I am not entirely sure why I keep getting the error below when I test the file:
Compiling 1 source file to ShoppingListArrayListTest.java:134: error: incompatible types: void cannot be converted to Grocery Assert.assertEquals(first, new Grocery("Mayo", "Dressing / Mayo"));
1 error : The following error occurred while executing this line: : Compile failed; see the compiler error output for details. BUILD FAILED (total time: 1 second)
And here is the original source code and part of the test file where the error keeps coming from.
package Shopping;
import java.util.*;
public class ShoppingListArrayList implements ShoppingListADT {
private ArrayList
public ShoppingListArrayList() { this.shoppingList = new ArrayList<>(); }
@Override public void add(Grocery entry) { if (entry == null) { return; }
if (this.contains(entry)) { }
this.shoppingList.add(entry); }
@Override public boolean remove(Grocery entry) { boolean found = false;
if (shoppingList.contains(entry) == true){ found = true; } else{ System.out.println("Item does not exist in Shopping List."); found = false; }
}
@Override public Grocery find(int index) throws IndexOutOfBoundsException, EmptyCollectionException { if (this.isEmpty()){ throw new EmptyCollectionException("EmptyCollExcept_Find"); } if (index < shoppingList.size()){ return shoppingList.get(index); } else { return null; }
}
@Override public int indexOf(Grocery entry) throws ElementNotFoundException { if (entry != null) { for (int i = 0; i < shoppingList.size(); i++) { if (shoppingList.get(i).compareTo(entry) == 0) { return i; } } } throw new ElementNotFoundException("indexOf"); }
@Override public boolean contains(Grocery entry) { //Come back to fix!!! boolean hasItem = false; int item_index = shoppingList.indexOf(entry); if (entry != null || item_index == -1) { System.out.print("Item N/A."); } else { hasItem = true; } return hasItem; }
@Override public int size() { return shoppingList.size(); }
@Override public boolean isEmpty() { return shoppingList.isEmpty(); }
@Override public String toString() { StringBuilder s = new StringBuilder(); s.append(String.format("%-25s", "NAME")); s.append(String.format("%-18s", "CATEGORY")); s.append(String.format("%-10s", "AISLE")); s.append(String.format("%-10s", "QUANTITY")); s.append(String.format("%-10s", "PRICE")); s.append(' '); s.append("------------------------------------------------------------" + "-------------"); s.append(' '); for (int i = 0; i < shoppingList.size(); i++) { s.append(String.format("%-25s", this.shoppingList.get(i).getName())); s.append(String.format("%-18s", this.shoppingList.get(i).getCategory())); s.append(String.format("%-10s", this.shoppingList.get(i).getAisle())); s.append(String.format("%-10s", this.shoppingList.get(i).getQuantity())); s.append(String.format("%-10s", this.shoppingList.get(i).getPrice())); s.append(' '); s.append("--------------------------------------------------------" + "-----------------"); s.append(' '); }
return s.toString(); }
private void combineQuantity(Grocery entry) { try { int index = this.indexOf(entry); this.shoppingList.get(index).setQuantity(this.shoppingList.get(index).getQuantity() + entry.getQuantity()); } catch (ElementNotFoundException e) { System.out.println("combineQuantity - ECE"); }
}
}
-----------------------------------> Test File:
@Test public void testFind() { // Test Index out of Bounds errors try { // TODO Test that a negative index throws an exception
Assert.fail("find does not fail with negative index"); } catch (IndexOutOfBoundsException e) { } catch (Exception e) { Assert.fail("Unexpected exception testing negative index"); } try { // Test that an index that is too big throws an exception instance.find(20); Assert.fail("find does not fail with large index"); } catch (IndexOutOfBoundsException e) { } catch (Exception e) { Assert.fail("Unexpected exception testing large index"); }
// Test normal find behavior try { Grocery first = // TODO Find the first item in the list Assert.assertEquals(first, new Grocery("Mayo", "Dressing / Mayo")); // <---------- Assert.assertEquals(11, instance.size()); } catch (Exception e) { Assert.fail("Unexpected exception testing normal index"); }
// Prepare for Empty Collection Exception test try { for (int i = instance.size() - 1; i >= 0; i--) { instance.remove(instance.find(i)); } Assert.assertTrue(instance.isEmpty()); } catch (Exception e) { Assert.fail("Unexpected exception clearing list"); }
// Test Empty Collection Exception errors try { instance.find(0); Assert.fail("find does not fail with empty collection"); } catch (EmptyCollectionException e) { } catch (Exception e) { Assert.fail("Unexpected exception testing empty collection"); } }
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
