Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

NEED HELP, I need help on test class not on shopping class package Shopping; import Shopping.ElementNotFoundException; import Shopping.EmptyCollectionException; import java.io.FileNotFoundException; import java.util.ArrayList; import java.util.Scanner; /**

NEED HELP, I need help on test class not on shopping class

package Shopping;

import Shopping.ElementNotFoundException; import Shopping.EmptyCollectionException; import java.io.FileNotFoundException; import java.util.ArrayList; import java.util.Scanner;

/** * @version Fall 2019 * @author ITCS 2214 */ public class ShoppingListArrayList implements ShoppingListADT {

private ArrayList shoppingList;

/** * Default constructor of ShoppingArray object. */ public ShoppingListArrayList() { this.shoppingList = new ArrayList(); }

/** * Constructor of ShoppingArray object that parses from a file. * * @param filename the name of the file to parse * @throws FileNotFoundException if an error occurs when parsing the file */ public ShoppingListArrayList(String filename) throws FileNotFoundException { this.shoppingList = new ArrayList(); scanFile(filename); }

/** * Method to add a new entry. Only new entries can be added. Combines * quantities if entry already exists. * * @param entry the entry to be added */ @Override public void add(Grocery entry) { if (entry == null) { return; }

// Check if this item already exists if (this.contains(entry)) { //Merge the quantity of new entry into existing entry combineQuantity(entry); return; }

shoppingList.add(entry); }

/** * Method to remove an entry. * (Should throw ElementNotFound exception is parameter doesn't exist.) * * @param ent to be removed * @return true when entry was removed */ @Override public boolean remove(Grocery ent) {

// the boolean found describes whether or not we find the // entry to remove Grocery item = null; int i = 0; boolean found; for (; i

/** * Method to find an entry. * * @param index to find * @return the entry if found * @throws Exceptions.EmptyCollectionException */ @Override public Grocery find(int index) throws IndexOutOfBoundsException, EmptyCollectionException { if (this.isEmpty()) { throw new EmptyCollectionException("ECE - find"); } if ((index shoppingList.size()) ) { throw new IndexOutOfBoundsException("ArrayList - find"); } // TODO check whether or not the given index is legal // for example, the given index is less than 0 or falls outside of // the size. If it is not legal, throw an IndexOutOfBoundsException // return the corresponding entry in the shoppingList return (Grocery) this.shoppingList.get(index); }

/** * Method to locate the index of an entry. * * @param ent to find the index * @return the index of the entry * @throws ElementNotFoundException if no entry was found */ @Override public int indexOf(Grocery ent) throws ElementNotFoundException { if (ent != null) { for (int i = 0; i

/** * Method to determine whether the object contains an entry. * * @param ent to find * @return true if and only if the entry is found */ @Override public boolean contains(Grocery ent) { boolean hasItem = false;

if (ent != null) { // TODO go through the shoppingList and try to find the // given item, named ent, in the list. If found, return true. // Either using methods from Java build-in ArrayList method or // writing your own loop is fine. return this.shoppingList.contains(ent);

} return hasItem; }

/** * Gets the size of the collection. * * @return the size of the collection */ @Override public int size() { return shoppingList.size(); }

/** * Gets whether the collection is empty. * * @return true if and only if the collection is empty */ @Override public boolean isEmpty() { return shoppingList.isEmpty(); }

/** * Returns a string representing this object. * * @return a string representation of this object */ @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

return s.toString(); }

/** * Add the quantity of a duplicate entry into the existing * * @param entry duplicate */ private void combineQuantity(Grocery entry) { try { int index = this.indexOf(entry); shoppingList.get(index).setQuantity( shoppingList.get(index).getQuantity() + entry.getQuantity()); } catch (ElementNotFoundException e) { System.out.println("combineQuantity - ECE"); }

}

/** * Scans the specified file to add items to the collection. * * @param filename the name of the file to scan * @throws FileNotFoundException if the file is not found */ private void scanFile(String filename) throws FileNotFoundException { Scanner scanner = new Scanner(getClass().getResourceAsStream(filename)) .useDelimiter("(,| )");

while (scanner.hasNext()) { Grocery temp = new Grocery(scanner.next(), scanner.next(), Integer.parseInt(scanner.next()), Float.parseFloat(scanner.next()), Integer.parseInt(scanner.next()));

add(temp); } }

}

1.Complete ShoppingListArrayListTest.java by completing the testAdd(), testRemove(), and testFind() methods. Feel free to refer to the testIndexOf(), testContains(), testSize(), and testIsEmpty() methods for examples of how to test elements of this project.

image text in transcribed

image text in transcribed

image text in transcribed

image text in transcribed

image text in transcribed

aTest public void testRemove() \{ //Create grocery objects Grocery iteml = new Grocery("Harry Potter", "book", 3,15.5f,2); Grocery item2 = new Grocery ("Hunger Game", "book", 3,10.5f,3); // Construct a shopping list instance = new Shoppingtistarraytist(); instance.add(iteml); instance.add(item2); assertequals(2, instance.size()); boolean isRemoved; // Todo test the remove method for an existing entry, iteml // Be sure that // 1 ) the returned value from the remove method is true // 2) the shopping list is decreased by 1 // 3) the item being removed cannot be found in the shopping list // TODO test the remove method for a non-existing entry // Be sure that // 1) the returned value from the remove method is false // 2) the shopping list is not changed // Construct a case that the shopping list becomes empty isRemoved = instance. remove (item2); assertTrue (isRemoved); assertEquals (0, instance.size ()); // Test the remove method when the shopping list is empty isRemoved = instance.remove (item2); assertFalse (isRemoved); assertEquals (0, instance.size ()); \} @Test public void testIndexOf() \{ //Create grocery objects Grocery iteml = new Grocery ("Harry Potter", "book", 3, 15.5f, 2); Grocery item2 = new Grocery("Hunger Game", "book", 3, 10.5f, 3); // Construct an empty shopping list instance = new ShoppingListarrayList () ; // Check the indexof method when the shopping list is empty try \{ int index = instance. indexof (iteml); \} catch (Exception e) \{ assertTrue (e instanceof ElementNotFoundException); \} // Add grocery items into the shopping list instance. add (iteml); instance. add (item2); // Check the indexof method when the grocery item appears in the list try \{ assertEquals (1, instance.indexOf (item2)); assertEquals (0, instance.indexof (item1)); \} catch (Exception el) \{ fail("Shouldn't get here in testindexOf" + el.getmessage ()); \} // Check the indexof method when the grocery item does not appear in // the list try \{ Grocery item3 = new Grocery("Aladin", "book", 3, 15.5f, 2); int index = instance. indexof (item3); \} catch (Exception e2) \{ assertTrue(e2 instanceof ElementNotFoundException); \} // Check the indexof method when the grocery item is null try \{ Grocery obj = null; int index = instance.indexOf (obj); \} catch (Exception e3) \{ assertTrue (e3 instanceof ElementNotFoundException); \} \}

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Professional SQL Server 2012 Internals And Troubleshooting

Authors: Christian Bolton, Justin Langford

1st Edition

1118177657, 9781118177655

More Books

Students also viewed these Databases questions