Shopping List

Shopping List

INTRODUCTION

The assignment will require you to write classes to represent a ShoppingList containing ShoppingItems. Each ShoppingItem will contain a String name and a double price. The ShoppingList will contain an array of ShoppingItems along with various methods for accessing, modifying and resizing the array.
You will find instructions for completing the assignment in the doc folder in JavaDoc format. Start with /doc/progfund/ShoppingItem.html. Once you have completed the ShoppingItem class, you can click Next Class to find instructions for writing ShoppingList. Run the provided TestSuite to see if your methods are working and to get a good estimation of your final grade. There may be errors that the TestSuite cannot help you with. You will need to write your own main method in the MyTestDriver class to debug your code.

ShoppingItem.java

package progfund;

import java.text.NumberFormat;

/**

* <p>This class represents a single Shopping Item.

* The ShoppingItem simply contains a name and a price and a few

* methods that allow us to compare two ShoppingItems in different

* ways.

* </p>

* <h3>How To Do The Assignment</h3>

* <p>In the Shopping List assignment, this is the class that you

* should write first. We suggest you write your methods in the

* following order:</p>

* <ul>

* <li>Write the constructors. Test.

* <li>Write the getters and setters. Test.

* <li>Write the toString method. Test.

* <li>Write the equals method. Test.

* <li>Write the comparePrice method. Test.

* <li>Write the compareName method. Test.

* </ul>

*  <h3>Additional Requirements</h3>

* <p>You must provide an academic integrity statement at the top of

* each class you write. This should include your name, id number,

* date and the statement, “This is my own work as defined by the

* SAIBT / EIBT Academic Misconduct policy.”</p>

* <p>You will also need to comment your code thoroughly while you are

* programming.</p>

* <p>Once you are confident these methods are working, you

* should start writing the ShoppingList class.</p>

*/

public class ShoppingItem {

/**The name of the shopping item.*/

private String name;

/**How much the shopping item costs in dollars.*/

private double price;

/**

* Constructor to create a ShoppingItem from the argument name

* and price.

* @param name The name of the shopping item.

* @param price The price of the shopping item in dollars.

*/

public ShoppingItem(String name, double price){

//TODO

}

/**

* A copy constructor for the item.

* This constructor should return a deep copy of the argument

* ShoppingItem.

* @param copy The ShoppingItem to copy

*/

public ShoppingItem(ShoppingItem copy){

//TODO

}

/**

* Sets the price to the argument newPrice. If the newPrice is

* less than or equal to 0.0, this method should do nothing.

* @param newPrice The price to change to.

*/

public void setPrice(double newPrice){

//TODO

}

/**

* Gets the current price in dollars.

* @return The ShoppingItem’s price.

*/

public double getPrice(){

//TODO

return -1;

}

/**

* Sets the name to the argument newName. If the newName is zero

* characters long or null, this method should do nothing.

* @param newName The name to change to.

*/

public void setName(String newName){

//TODO

}

/**

* Gets the current name of the ShoppingItem.

* @return The ShoppingItem’s name.

*/

public String getName(){

//TODO

return null;

}

/**

* The toString functions should return a meaningful String

* representation of the class. It will be called automatically

* when printing out objects of this class type.

* @return A String representation of the class in the form:

* “name: $price”. Price should be formatted to 2 decimal places.

*/

public String toString(){

//TODO

return null;

}

/**

* The equals method should return true if the argument object

* contains the same data as the calling object.

* They must be the same datatype and their name and price must

* be the same.

* @param other The Object to test against.

* @return True if the argument contains the same data.

* False in all other cases.

*/

public boolean equals(Object other){

//TODO

return false;

}

/**

* Compares the price of the argument ShoppingItem to the price

* of the calling object.

* @param other The ShoppingItem to compare.

* @return 1 if the calling object has a greater price than the

* argument. -1 if the calling object has a lesser price to the

* argument. 0 if the price of the argument and calling objects

* are the same.

*/

public int comparePrice(ShoppingItem other){

//TODO

return -9;

}

/**

* Compares the name of the argument ShoppingItem to the name of

* the calling object. The Strings will be compared

* lexicographically such that Bread < Eggs < Milk < Yoghurt.

*

* @param other The ShoppingItem to compare.

* @return 1 if the calling object has a greater name than the

* argument. -1 if the calling object has a lesser name to the

* argument. 0 if the name of the argument and calling objects

* are the same.

*/

public int compareName(ShoppingItem other){

//TODO

return -9;

}

}

ShoppingList.java

package progfund;

/**

* <p>This program represents a ShoppingList that contains a mutable

* array of ShoppingItem objects. It represents a list that you might

* take to the supermarket to keep track of which items you would

* like to buy.</p>

*

* <h3>Overview</h3>

* <p>In Java, most programs are a collection of objects working

* together to get something done. This assignment will require you

* to write two classes. One class will contain a mutable array. The

* other class will represent the items that should be stored into

* this array.</p>

*

* <p>To make your life easier you will be provided with an API

* specification (just like the JavaDocs you use for your

* programming). Your job is to follow this API to fill out the

* method stubs with appropriate code. You should also ensure that

* this code is as uncrashable as possible. If your code detects

* faulty/incorrect input it should handle it as instructed in the

* API.</p>

*

* <h3>Data Structure</h3>

* <p>We will be storing ShoppingItem objects into an array within the

* ShoppingList class. Something to note is that arrays are immutable

* (They cannot be changed once they are created.) A large part of

* this assignment is to add methods to the ShoppingList class to

* simulate an array that is mutable. These methods should facilitate

* adding and deleting items from the array and changing the array’s

* size.</p>

*

* <h3>Testing</h3>

* <p>To ensure your code is uncrashable you will have to filter out

* incorrect input with if statements. A test driver has been

* provided to help you test your classes. However, you should write

* your own main method to test your classes as well.

* Test the expected input, test the boundary cases, test outside the

* boundary cases and test the ridiculous cases. Test early and test

* thoroughly.</p>

*

* <h3>How To Do The Assignment</h3>

* <p>We suggest you start writing the ShoppingItem class first.

* Once the ShoppingItem class is written and tested, begin working

* on the ShoppingList class. We suggest you write your code in the

* following order:</p>

* <ul>

* <li>Write the constructors. Test.

* <li>Write the count method. Test.

* <li>Write the toString method. Test.

* <li>Write the itemAt method. Test.

* <li>Write the setSize method. Test.

* <li>Write the append and prepend methods. Test.

* <li>Write the insert method. Test.

* <li>Write the deleteItemAt method. Test.

* <li>Write the deleteItem method. Test.

* <li>Write the removeBlanks method. Test.

* <li>Write the getTotalCost method. Test.

* <li>Write the sortByCost methods. Test.

* </ul>

*

* <h3>Additional Requirements</h3>

* <p>You must provide an academic integrity statement at the top of

* each class you write. This should include your name, id number,

* date and the statement, “This is my own work as defined by the

* SAIBT / EIBT Academic Misconduct policy.”

* You will need to comment your code thoroughly while you are

* programming.</p>

*/

public class ShoppingList {

/**

* Storage for ShoppingItem objects.

*/

private ShoppingItem[] shoppingItems;

/**

* Constructor to create a shoppinglist with the initial contents

* of nothing. Set the size of shoppingItems to 10 and leave it empty.

*/

public ShoppingList() {

//TODO

}

/**

* Constructor to create a shoppinglist of the specified length.

* If length is less than or equal to zero, set shoppingItems to

* the default length of 10.

* @param length The size of the ShoppingItem array.

*/

public ShoppingList(int length) {

//TODO

}

/**

* Constructor to create a shopping list to store the array of

* ShoppingItems. The array should be created to fit only the

* specified items. Each item should be deep copied to avoid

* privacy leaks.

* If argument items is null, set shoppingItems to the default

* length of 10 and leave it empty.

* @param items The items to be copied.

*/

public ShoppingList(ShoppingItem[] items){

//TODO

}

/**

* Add the specified item to the position after the last non-null item.

* @param item The item to append to the list.

*/

public void append(ShoppingItem item){

//TODO

}

/**

* Returns the index of the first item matching the specified

* item name.

* @param name The name to search for from items in the

* ShoppingList.

* @return The index of the first occurrence of the specified

* name or -1 if the name is not found within the list.

*/

public int indexOf(String name){

//TODO

return -9;

}

/**

* Returns the number of items in the list.

* This is not the length of the array, it is the number of

* non-null items.

* @return The number of ShoppingItems in the array.

*/

public int count(){

//TODO

return -1;

}

/**

* Remove the first item with the specified name. If an item was

* successfully removed, the item should be returned. The

* following items should move down to fill in the empty space.

* Use removeBlanks(). If an item was successfully removed, the

* item should be returned.

* @param name The name of the item to be removed.

* @return The item that was removed from the array or null if

* the item was not found.

*/

public ShoppingItem deleteItem(String name){

//TODO

return null;

}

/**

* Remove the item at the specified position. The following items

* should move down to fill in the empty space.

* Use removeBlanks(). If an item was successfully removed, the

* item should be returned.

* @param index The index is a position between 0 and count()-1

* specifying an item to remove from the array.

* @return The item that was removed from the array or null if

* the index was outside the bounds of the array.

*/

public ShoppingItem deleteItemAt(int index){

//TODO

return null;

}

/**

* The equals method should return true if the argument object

* contains the same data as the calling object.

* They must be the same datatype and their name and price must

* be the same.

* @param other The Object to test against.

* @return True if the argument contains the same data.

* False in all other cases.

*/

public boolean equals(Object other){

//TODO

return false;

}

/**

* Returns the total cost of all ShoppingItems in the list.

* @return Total cost of items

*/

public double getTotalCost(){

//TODO

return -1;

}

/**

* Insert the item at the specified position. The position must

* be between 0 and count(), inclusive. An item can be inserted

* at the end of the list. The item currently at the specified

* position and all items after that position should be moved

* down a space.

* @param item The item to insert.

* @param index The position to insert the item at.

* @return true if item was inserted successfully, false otherwise

*/

public boolean insert(ShoppingItem item, int index){

//TODO

return false;

}

/**

* Return the item at the specified index.

* @param index The index is a position between 0 and count()-1

* specifying which item should be returned.

* @return The item at the specified position or null if the

* index does not contain an item.

*/

public ShoppingItem itemAt(int index){

//TODO

return null;

}

/**

* Add the specified item to the front of the array.

* All other items should be pushed down a space.

* @param item The item to prepend to the list.

*/

public void prepend(ShoppingItem item){

//TODO

}

/**

* The list may end up with null objects when deleting from the

* middle of the array. removeBlanks should move all non-null

* items together in their current order, pushing all null items

* to the end.

*

* For example, if the list contained

* [Milk, Eggs, null, Bread, null, Bananas, null, null],

* calling removeBlanks should result in

* [Milk, Eggs, Bread, Bananas, null, null, null, null].

*

* This private method should be called automatically when

* deleting items from the middle of the array.

*/

private void removeBlanks(){

//TODO

}

/**

* Sorts the list in ascending order by each item’s cost.

* The list should be sorted in place; that means that this

* method does not return a new list but changes the existing

* list. For example, if the list contained the dollar values

* [6.2, 3.1, 9.23, 4.80] the resulting list would be

* [3.1, 4.80, 6.2, 9.23]

*

* <p>For this method we suggest the selection sort algorithm.</p>

* <p>Pseudocode for the selection sort algorithm may be as follows:</p>

*  <p>FOR index in list:</p>

*     <p>Set minIndex to index</p>

*     <p>FOR each unsortedIndex in list:</p>

*         <p>IF item at unsortedIndex less than item at minIndex:</p>

*         <p>Set item at minIndex to item at unsortedIndex</p>

*          <p>END IF</p>

*     <p>END FOR</p>

*     <p>Swap item at index with item at minIndex</p>

*  <p>END FOR</p>

*/

public void sortByCost(){

//TODO

}

/**

* Changes the size of the array, retaining all current items in

* the current order. If the specified size is less than the

* number of items in the list, this method should do nothing and

* return false. If the size was successfully changed, the method

* should return true.

*

* This private method should be called automatically when adding

* or deleting items. You can either double the size of the array

* when the array runs out of space or call the method to change

* the size to count() whenever a change occurs.

*

* @param newSize a positive size between count() and infinite.

* @return true if successful, false in all other cases.

*/

private boolean setSize(int newSize){

//TODO

return false;

}

/**

* Returns a string representation of this object in the form:

* “Shopping List:

*    Milk: $2.00

*    Bread: $3.40

*    Eggs: $4.00”

* Each item must be on a new line. Try not to add extra spaces.

* @return A string containing the name and cost of each non null

* item in the array separated by new line characters.

*/

public String toString(){

//TODO

return null;

}

}

MyTestDriver.java

package testing;

import progfund.ShoppingItem;

import progfund.ShoppingList;

/**

* Use this space to do your own testing.

*/

public class MyTestDriver {

public static void main(String[] args){

// Here’s some code to start you off.

ShoppingItem item = new ShoppingItem(“Expensive Item”, 10000.999);

System.out.println(item);

ShoppingList list = new ShoppingList();

list.append(item);

System.out.println(list);

}

}

ShoppingItemTest.java

package testing;

import java.lang.reflect.Field;

import java.util.Random;

import org.junit.After;

import org.junit.AfterClass;

import org.junit.Before;

import org.junit.BeforeClass;

import org.junit.Test;

import static org.junit.Assert.*;

import progfund.ShoppingItem;

public class ShoppingItemTest {

@BeforeClass

public static void setUpBeforeClass() {

System.out.println(“Testing ShoppingItem Class”);

}

@Before

public void setUp() { }

@After

public void tearDown() {

TestSuite.finishTest();

}

@AfterClass

public static void tearDownAfterClass(){

System.out.println(“\nFinished Testing ShoppingItem Class”);

}

@Test

public void testItemConstructors() {

try{

TestSuite.startTest(“Test ShoppingItem Constructors”, 4);

System.out.println(“\tTesting ShoppingItem(\”Milk\”, 2.00)”);

ShoppingItem item = new ShoppingItem(“Milk”, 2.00);

assertEquals(“Milk”, getName(item));

assertEquals(2.00, getPrice(item), 0.00001);

TestSuite.pass(2);

System.out.println(“\tTesting copy constructor”);

ShoppingItem item2 = new ShoppingItem(item);

assertEquals(“Milk”, getName(item2));

assertEquals(2.00, getPrice(item2), 0.00001);

TestSuite.pass(2);

}

catch(AssertionError e){

TestSuite.fail(e.toString());

throw e;

}

catch(Exception e){

TestSuite.fail(e.toString());

throw e;

}

}

@Test

public void testItemGetters() {

try{

TestSuite.startTest(“Test ShoppingItem Getters”, 2);

System.out.println(“\tTesting (\”Beans\”, 2.99) getName()”);

ShoppingItem item = new ShoppingItem(“Beans”, 2.99);

assertEquals(“Beans”, item.getName());

TestSuite.pass(1);

System.out.println(“\tTesting (\”Beans\”, 2.99) getPrice()”);

item = new ShoppingItem(“Beans”, 2.99);

assertEquals(2.99, item.getPrice(), 0.00001);

TestSuite.pass(1);

}

catch(AssertionError e){

TestSuite.fail(e.toString());

throw e;

}

catch(Exception e){

TestSuite.fail(e.toString());

throw e;

}

}

@Test

public void testItemSetters() {

try{

TestSuite.startTest(“Test ShoppingItem Setters”, 6);

System.out.println(“\tTesting (\”Beans\”, 2.99) setName(\”Diapers\”)”);

ShoppingItem item = new ShoppingItem(“Beans”, 2.99);

item.setName(“Diapers”);

assertEquals(“Diapers”, getName(item));

TestSuite.pass(1);

System.out.println(“\tTesting (\”Beans\”, 2.99) setName(null)”);

item = new ShoppingItem(“Beans”, 2.99);

item.setName(null);

assertEquals(“Beans”, getName(item));

TestSuite.pass(1);

System.out.println(“\tTesting (\”Beans\”, 2.99) setName(\”\”)”);

item = new ShoppingItem(“Beans”, 2.99);

item.setName(“”);

assertEquals(“Beans”, getName(item));

TestSuite.pass(1);

System.out.println(“\tTesting (\”Beans\”, 2.99) setPrice(84.99)”);

item = new ShoppingItem(“Beans”, 2.99);

item.setPrice(84.99);

assertEquals(84.99, getPrice(item), 0.00001);

TestSuite.pass(1);

System.out.println(“\tTesting (\”Beans\”, 2.99) setPrice(0)”);

item = new ShoppingItem(“Beans”, 2.99);

item.setPrice(0);

assertEquals(2.99, getPrice(item), 0.00001);

TestSuite.pass(1);

System.out.println(“Testing (\”Beans\”, 2.99) setPrice(-1)”);

item = new ShoppingItem(“Beans”, 2.99);

item.setPrice(-1);

assertEquals(2.99, getPrice(item), 0.00001);

TestSuite.pass(1);

}

catch(AssertionError e){

TestSuite.fail(e.toString());

throw e;

}

catch(Exception e){

TestSuite.fail(e.toString());

throw e;

}

}

@Test

public void testItemToString() {

try{

TestSuite.startTest(“Testing ShoppingItem toString()”, 4);

System.out.println(“\tTesting (\”Bananas\”, 3.10)”);

ShoppingItem item = new ShoppingItem(“Bananas”, 3.10);

assertEquals(“Bananas: $3.10”, item.toString());

TestSuite.pass(2);

System.out.println(“\tTesting (\”Gold\”, 1,245,673.55555)”);

item = new ShoppingItem(“Gold”, 1245673.55555);

assertEquals(“Gold: $1,245,673.56”, item.toString());

TestSuite.pass(2);

}

catch(AssertionError e){

TestSuite.fail(e.toString());

throw e;

}

catch(Exception e){

TestSuite.fail(e.toString());

throw e;

}

}

@Test

public void testItemEquals() {

try{

TestSuite.startTest(“Testing ShoppingItem equals()”, 6);

System.out.println(“\tTesting (\”Eggs\”, 3.20) and (\”Eggs\”, 3.20)”);

ShoppingItem item1 = new ShoppingItem(“Eggs”, 3.20);

ShoppingItem item2 = new ShoppingItem(“Eggs”, 3.20);

assertTrue(item1.equals(item2));

TestSuite.pass(1);

System.out.println(“\tTesting (\”Eggs\”, 3.20) and (\”Milk\”, 3.20)”);

item1 = new ShoppingItem(“Eggs”, 3.20);

item2 = new ShoppingItem(“Milk”, 3.20);

assertFalse(item1.equals(item2));

TestSuite.pass(1);

System.out.println(“\tTesting (\”Milk\”, 3.20) and (\”Milk\”, 2.40)”);

item1 = new ShoppingItem(“Milk”, 3.20);

item2 = new ShoppingItem(“Milk”, 2.40);

assertFalse(item1.equals(item2));

TestSuite.pass(1);

System.out.println(“\tTesting (\”Eggs\”, 3.20) and null”);

item1 = new ShoppingItem(“Eggs”, 3.20);

item2 = null;

assertFalse(item1.equals(item2));

TestSuite.pass(1);

System.out.println(“\tTesting (\”Milk\”, 3.20) and (new Random())”);

item1 = new ShoppingItem(“Milk”, 3.20);

assertFalse(item1.equals(new Random()));

TestSuite.pass(1);

System.out.println(“\tTesting (null, -1) and (null, -1)”);

item1 = new ShoppingItem(null, -1);

item2 = new ShoppingItem(null, -1);

assertTrue(item1.equals(item2));

TestSuite.pass(1);

}

catch(AssertionError e){

TestSuite.fail(e.toString());

throw e;

}

catch(Exception e){

TestSuite.fail(e.toString());

throw e;

}

}

@Test

public void testItemComparePrice() {

try{

TestSuite.startTest(“Testing ShoppingItem comparePrice()”, 5);

System.out.println(“\tTesting Lobster $120.32 > Ham $3.40”);

ShoppingItem item1 = new ShoppingItem(“Lobster”, 120.32);

ShoppingItem item2 = new ShoppingItem(“Ham”, 3.40);

int result = item1.comparePrice(item2);

assertEquals(1, result);

TestSuite.pass(2);

System.out.println(“\tTesting Noodles $2.99 < Gold $1200”);

item1 = new ShoppingItem(“Noodles”, 2.99);

item2 = new ShoppingItem(“Gold”, 1200);

result = item1.comparePrice(item2);

assertEquals(-1, result);

TestSuite.pass(2);

System.out.println(“Testing Milk $2.00 == Biscuits $2.00”);

item1 = new ShoppingItem(“Milk”, 2.00);

item2 = new ShoppingItem(“Biscuits”, 2.00);

result = item1.comparePrice(item2);

assertEquals(0, result);

TestSuite.pass(1);

}

catch(AssertionError e){

TestSuite.fail(e.toString());

throw e;

}

catch(Exception e){

TestSuite.fail(e.toString());

throw e;

}

}

@Test

public void testItemCompareName() {

try{

TestSuite.startTest(“Testing ShoppingItem compareName()”, 5);

System.out.println(“\tTesting Juice $4.50 > Jam $3.40”);

ShoppingItem item1 = new ShoppingItem(“Juice”, 4.50);

ShoppingItem item2 = new ShoppingItem(“Jam”, 3.40);

int result = item1.compareName(item2);

assertEquals(1, result);

TestSuite.pass(2);

System.out.println(“\tTesting Gold $1200 < Noodles $2.99”);

item1 = new ShoppingItem(“Gold”, 1200);

item2 = new ShoppingItem(“Noodles”, 2.99);

result = item1.compareName(item2);

assertEquals(-1, result);

TestSuite.pass(2);

System.out.println(“\tTesting Milk $2.00 == Milk $2.99”);

item1 = new ShoppingItem(“Milk”, 2.00);

item2 = new ShoppingItem(“Milk”, 2.99);

result = item1.compareName(item2);

assertEquals(0, result);

TestSuite.pass(1);

}

catch(AssertionError e){

TestSuite.fail(e.toString());

throw e;

}

catch(Exception e){

TestSuite.fail(e.toString());

throw e;

}

}

/**

* Utility method that allows me to access the ShoppingItem private instance variable, name.

* @param item The ShoppingItem object to reflect into.

* @return the private name array from the argument.

*/

public static String getName(ShoppingItem item){

try{

Class<? extends ShoppingItem> itemReflect = item.getClass();

Field nameReflect = itemReflect.getDeclaredField(“name”);

nameReflect.setAccessible(true);

String nameGot = (String)nameReflect.get(item);

return nameGot;

}

catch(Exception e){

System.err.println(“ERROR: Could not access private name. Make sure the variable name is \”name\” and it is private”);

e.printStackTrace();

}

return null;

}

/**

* Utility method that allows me to access the ShoppingItem private instance variable, price.

* @param item The ShoppingItem object to reflect into.

* @return the private price array from the argument.

*/

public static double getPrice(ShoppingItem item){

try{

Class<? extends ShoppingItem> itemReflect = item.getClass();

Field priceReflect = itemReflect.getDeclaredField(“price”);

priceReflect.setAccessible(true);

double priceGot = (double)priceReflect.get(item);

return priceGot;

}

catch(Exception e){

System.err.println(“ERROR: Could not access private price. Make sure the variable name is \”price\” and it is private”);

e.printStackTrace();

}

return -1;

}

}

ShoppingListTest.java

package testing;

import java.lang.reflect.Field;

import java.lang.reflect.InvocationTargetException;

import java.lang.reflect.Method;

import java.util.Arrays;

import org.junit.After;

import org.junit.AfterClass;

import org.junit.Before;

import org.junit.BeforeClass;

import org.junit.Test;

import progfund.ShoppingItem;

import progfund.ShoppingList;

import static org.junit.Assert.*;

public class ShoppingListTest {

@BeforeClass

public static void setUpBeforeClass() {

System.out.println(“Testing ShoppingList Class”);

}

@Before

public void setUp() { }

@After

public void tearDown() {

TestSuite.finishTest();

}

@AfterClass

public static void tearDownAfterClass(){

System.out.println(“\nFinished Testing ShoppingList Class”);

}

@Test

public void testListConstructors() {

try{

TestSuite.startTest(“Testing ShoppingList Constructors”, 7);

System.out.println(“\tTesting ShoppingList()”);

ShoppingList list = new ShoppingList();

assertArrayEquals(new ShoppingItem[10], getShoppingItems(list));

TestSuite.pass(1);

System.out.println(“\tTesting ShoppingList(10)”);

list = new ShoppingList(10);

assertArrayEquals(new ShoppingItem[10], getShoppingItems(list));

TestSuite.pass(1);

System.out.println(“\tTesting ShoppingList(new ShoppingItem[]{Milk, Cookies, Sugar, Spice})”);

ShoppingItem[] items = new ShoppingItem[]{

new ShoppingItem(“Milk”, 2.00),

new ShoppingItem(“Cookies”, 2.49),

new ShoppingItem(“Sugar”, 3.99),

new ShoppingItem(“Spice”, 3.00)};

list = new ShoppingList(items);

assertArrayEquals(new ShoppingItem[]{

new ShoppingItem(“Milk”, 2.00),

new ShoppingItem(“Cookies”, 2.49),

new ShoppingItem(“Sugar”, 3.99),

new ShoppingItem(“Spice”, 3.00)},

getShoppingItems(list));

TestSuite.pass(1);

System.out.println(“\tTesting ShoppingList(new ShoppingItem[]{Milk, Cookies, Sugar, Spice}) Deep Copy”);

System.out.println(“\t\tCalling copyList[0] setName(\”Boogers\”)…”);

items = new ShoppingItem[]{

new ShoppingItem(“Milk”, 2.00),

new ShoppingItem(“Cookies”, 2.49),

new ShoppingItem(“Sugar”, 3.99),

new ShoppingItem(“Spice”, 3.00)};

list = new ShoppingList(items);

items[0].setName(“Boogers”);

assertArrayEquals(new ShoppingItem[]{

new ShoppingItem(“Milk”, 2.00),

new ShoppingItem(“Cookies”, 2.49),

new ShoppingItem(“Sugar”, 3.99),

new ShoppingItem(“Spice”, 3.00)},

getShoppingItems(list));

TestSuite.pass(2);

System.out.println(“\tTesting ShoppingList(null)”);

list = new ShoppingList(null);

assertArrayEquals(new ShoppingItem[10], getShoppingItems(list));

TestSuite.pass(1);

System.out.println(“\tTesting ShoppingList(-1)”);

list = new ShoppingList(-1);

assertArrayEquals(new ShoppingItem[10], getShoppingItems(list));

TestSuite.pass(1);

}

catch(AssertionError e){

TestSuite.fail(e.toString());

throw e;

}

catch(Exception e){

TestSuite.fail(e.toString());

throw e;

}

}

@Test

public void testListCount() {

try{

TestSuite.startTest(“Testing ShoppingList count()”, 4);

System.out.println(“\tTesting Empty list”);

ShoppingList list = new ShoppingList();

int result = list.count();

assertEquals(0, result);

TestSuite.pass(1);

System.out.println(“\tTesting Full list [Milk, Eggs, Bacon]”);

list = new ShoppingList(new ShoppingItem[]{

new ShoppingItem(“Milk”, 2.00),

new ShoppingItem(“Eggs”, 3.99),

new ShoppingItem(“Bacon”, 4.45)});

result = list.count();

assertEquals(3, result);

TestSuite.pass(2);

System.out.println(“\tTesting List With Nulls”);

list = new ShoppingList(new ShoppingItem[]{

new ShoppingItem(“Milk”, 2.00),

new ShoppingItem(“Juice”, 4.00),

new ShoppingItem(“Bread”, 3.20),

new ShoppingItem(“Eggs”, 3.99),

new ShoppingItem(“Bacon”, 4.45),

new ShoppingItem(“Toilet Paper”, 5.00)});

ShoppingItem[] items = getShoppingItems(list);

items[2] = null;

items[3] = null;

result = list.count();

assertEquals(4, result);

TestSuite.pass(1);

}

catch(AssertionError e){

TestSuite.fail(e.toString());

throw e;

}

catch(Exception e){

TestSuite.fail(e.toString());

throw e;

}

}

@Test

public void testListToString() {

try{

TestSuite.startTest(“Testing ShoppingList toString()”, 5);

System.out.println(“\tTesting toString On Empty List”);

ShoppingList list = new ShoppingList();

String result = list.toString();

assertEquals(“Shopping List:”, result);

TestSuite.pass(1);

System.out.println(“\tTesting toString On Full List”);

list = new ShoppingList(new ShoppingItem[]{new ShoppingItem(“Corn”, 3.30),

new ShoppingItem(“Jewels”, 800), new ShoppingItem(“Mazda”, 2800)});

result = list.toString();

assertEquals(“Shopping List:\nCorn: $3.30\nJewels: $800.00\nMazda: $2,800.00”, result);

TestSuite.pass(2);

System.out.println(“\tTesting toString On List With Nulls”);

list = new ShoppingList(new ShoppingItem[]{new ShoppingItem(“Corn”, 3.30),

new ShoppingItem(“Jewels”, 800), new ShoppingItem(“Mazda”, 2800)});

ShoppingItem[] items = getShoppingItems(list);

items[0] = null;

items[2] = null;

result = list.toString();

assertEquals(“Shopping List:\nJewels: $800.00”, result);

TestSuite.pass(2);

}

catch(AssertionError e){

TestSuite.fail(e.toString());

throw e;

}

catch(Exception e){

TestSuite.fail(e.toString());

throw e;

}

}

@Test

public void testListEquals(){

try{

TestSuite.startTest(“Testing ShoppingList equals()”, 4);

System.out.println(“\tTesting Equal Lists”);

ShoppingList list1 = new ShoppingList(new ShoppingItem[]{

new ShoppingItem(“Apples”, 2.90),

new ShoppingItem(“Bananas”, 3.70),

new ShoppingItem(“Pears”, 4.50)});

ShoppingList list2 = new ShoppingList(new ShoppingItem[]{

new ShoppingItem(“Apples”, 2.90),

new ShoppingItem(“Bananas”, 3.70),

new ShoppingItem(“Pears”, 4.50)});

boolean result = list1.equals(list2);

assertTrue(result);

TestSuite.pass(1);

System.out.println(“\tTesting Different Lists, Different Lengths”);

list1  = new ShoppingList(new ShoppingItem[]{

new ShoppingItem(“Bacon”, 4.50),

new ShoppingItem(“Ham”, 3.70),

new ShoppingItem(“Pork”, 6.90)});

list2 = new ShoppingList(new ShoppingItem[]{

new ShoppingItem(“Porterhouse Steak”, 4.50),

new ShoppingItem(“TBone Steak”, 3.70),

new ShoppingItem(“Ribeye Steak”, 6.90),

new ShoppingItem(“Veal”, 5.40)});

result = list1.equals(list2);

assertFalse(result);

TestSuite.pass(1);

System.out.println(“\tTesting Different Lists, Same Lengths”);

list1  = new ShoppingList(new ShoppingItem[]{

new ShoppingItem(“Bacon”, 4.50),

new ShoppingItem(“Ham”, 3.70),

new ShoppingItem(“Pork”, 6.90)});

list2 = new ShoppingList(new ShoppingItem[]{

new ShoppingItem(“Bacon”, 4.50),

new ShoppingItem(“TBone Steak”, 3.70),

new ShoppingItem(“Pork”, 6.90)});

result = list1.equals(list2);

assertFalse(result);

TestSuite.pass(1);

System.out.println(“\tTesting equals(null)”);

list1  = new ShoppingList(new ShoppingItem[]{

new ShoppingItem(“Bacon”, 4.50),

new ShoppingItem(“Ham”, 3.70),

new ShoppingItem(“Pork”, 6.90)});

list2 = null;

result = list1.equals(list2);

assertFalse(result);

TestSuite.pass(1);

}

catch(AssertionError e){

TestSuite.fail(e.toString());

throw e;

}

catch(Exception e){

TestSuite.fail(e.toString());

throw e;

}

}

@Test

public void testListItemAt() {

try{

TestSuite.startTest(“Testing ShoppingList itemAt()”, 4);

System.out.println(“\tTesting itemAt(0)”);

ShoppingList list = new ShoppingList(new ShoppingItem[]{

new ShoppingItem(“Milk”, 2.00),

new ShoppingItem(“Eggs”, 4.00),

new ShoppingItem(“Bacon”, 4.00)});

ShoppingItem item = new ShoppingItem(“Milk”, 2.00);

ShoppingItem result = list.itemAt(0);

assertEquals(item, result);

TestSuite.pass(1);

System.out.println(“\tTesting itemAt(length-1)”);

list = new ShoppingList(new ShoppingItem[]{

new ShoppingItem(“Milk”, 2.00),

new ShoppingItem(“Eggs”, 4.00),

new ShoppingItem(“Bacon”, 4.00)});

item = new ShoppingItem(“Bacon”, 4.00);

result = list.itemAt(2);

assertEquals(item, result);

TestSuite.pass(1);

System.out.println(“\tTesting itemAt(-x)”);

item = null;

list = new ShoppingList(new ShoppingItem[]{

new ShoppingItem(“Milk”, 2.00),

new ShoppingItem(“Eggs”, 4.00),

new ShoppingItem(“Bacon”, 4.00)});

item = null;

result = list.itemAt(-2);

assertEquals(item, result);

TestSuite.pass(1);

System.out.println(“\tTesting itemAt(length+2)”);

item = null;

result = list.itemAt(5);

assertEquals(item, result);

TestSuite.pass(1);

}

catch(AssertionError e){

TestSuite.fail(e.toString());

throw e;

}

catch(Exception e){

TestSuite.fail(e.toString());

throw e;

}

}

@Test

public void testListAppend() {

try{

TestSuite.startTest(“Testing ShoppingList append()”, 4);

 

System.out.println(“\tTesting append() Empty List”);

ShoppingList list = new ShoppingList();

ShoppingItem item = new ShoppingItem(“SSD”, 119.39);

list.append(item);

ShoppingItem[] test1 = new ShoppingItem[10];

test1[0] = new ShoppingItem(“SSD”, 119.39);

ShoppingItem[] test2 = new ShoppingItem[]{

new ShoppingItem(“SSD”, 119.39)};

assertTrue(arrayEquals(test1, getShoppingItems(list)) ||

arrayEquals(test2, getShoppingItems(list)));

TestSuite.pass(2);

System.out.println(“\tTesting append() To Full List”);

list = new ShoppingList(new ShoppingItem[]{

new ShoppingItem(“Milk”, 2.00),

new ShoppingItem(“Eggs”, 3.40),

new ShoppingItem(“Cookies”, 2.45)});

item = new ShoppingItem(“Sword”, 429.99);

list.append(item);

test1 = new ShoppingItem

[]{

new ShoppingItem(“Milk”, 2.00),

new ShoppingItem(“Eggs”, 3.40),

new ShoppingItem(“Cookies”, 2.45),

new ShoppingItem(“Sword”, 429.99)};

assertArrayEquals(test1, getShoppingItems(list));

TestSuite.pass(2);

}

catch(AssertionError e){

TestSuite.fail(e.toString());

throw e;

}

catch(Exception e){

TestSuite.fail(e.toString());

throw e;

}

}

@Test

public void testListPrepend() {

try{

TestSuite.startTest(“Testing ShoppingList prepend()”, 4);

System.out.println(“\tTesting prepend() Empty List”);

ShoppingList list = new ShoppingList();

ShoppingItem item = new ShoppingItem(“SSD”, 119.39);

list.prepend(item);

ShoppingItem[] test1 = new ShoppingItem[10];

test1[0] = new ShoppingItem(“SSD”, 119.39);

ShoppingItem[] test2 = new ShoppingItem[]{

new ShoppingItem(“SSD”, 119.39)};

assertTrue(arrayEquals(test1, getShoppingItems(list)) ||

arrayEquals(test2, getShoppingItems(list)));

TestSuite.pass(2);

System.out.println(“\tTesting prepend() To Full List”);

list = new ShoppingList(new ShoppingItem[]{

new ShoppingItem(“Milk”, 2.00),

new ShoppingItem(“Eggs”, 3.40),

new ShoppingItem(“Cookies”, 2.45)});

item = new ShoppingItem(“Sword”, 429.99);

list.prepend(item);

test1 = new ShoppingItem[]{

new ShoppingItem(“Sword”, 429.99),

new ShoppingItem(“Milk”, 2.00),

new ShoppingItem(“Eggs”, 3.40),

new ShoppingItem(“Cookies”, 2.45),

};

assertArrayEquals(test1, getShoppingItems(list));

TestSuite.pass(2);

}

catch(AssertionError e){

TestSuite.fail(e.toString());

throw e;

}

catch(Exception e){

TestSuite.fail(e.toString());

throw e;

}

}

@Test

public void testListInsert() {

try{

TestSuite.startTest(“Testing ShoppingList insert()”, 8);

System.out.println(“\tTesting insert() At Index 0”);

ShoppingList list = new ShoppingList(new ShoppingItem[]{

new ShoppingItem(“Milk”, 2.00),

new ShoppingItem(“Eggs”, 3.40),

new ShoppingItem(“Cookies”, 2.45)});

ShoppingItem item = new ShoppingItem(“Frisbee”, 0.99);

boolean result = list.insert(item, 0);

ShoppingItem[] test = new ShoppingItem[]{

new ShoppingItem(“Frisbee”, 0.99),

new ShoppingItem(“Milk”, 2.00),

new ShoppingItem(“Eggs”, 3.40),

new ShoppingItem(“Cookies”, 2.45)};

assertArrayEquals(test, getShoppingItems(list));

assertTrue(result);

TestSuite.pass(2);

System.out.println(“\tTesting insert() in Middle”);

list = new ShoppingList(new ShoppingItem[]{

new ShoppingItem(“Drill”, 89.00),

new ShoppingItem(“Saw”, 34.40),

new ShoppingItem(“Hammer”, 7.45),

new ShoppingItem(“Wrench”, 21.45),

new ShoppingItem(“Pipe”, 8.94)});

item = new ShoppingItem(“Screwdriver”, 4.55);

result = list.insert(item, 3);

test = new ShoppingItem[]{

new ShoppingItem(“Drill”, 89.00),

new ShoppingItem(“Saw”, 34.40),

new ShoppingItem(“Hammer”, 7.45),

new ShoppingItem(“Screwdriver”, 4.55),

new ShoppingItem(“Wrench”, 21.45),

new ShoppingItem(“Pipe”, 8.94)};

assertArrayEquals(test, getShoppingItems(list));

assertTrue(result);

TestSuite.pass(2);

System.out.println(“\tTesting insert() At End”);

list = new ShoppingList(new ShoppingItem[]{

new ShoppingItem(“Apples”, 2.49),

new ShoppingItem(“Bananas”, 4.30),

new ShoppingItem(“Oranges”, 3.99)});

item = new ShoppingItem(“Pears”, 4.50);

result = list.insert(item, 3);

test = new ShoppingItem[]{

new ShoppingItem(“Apples”, 2.49),

new ShoppingItem(“Bananas”, 4.30),

new ShoppingItem(“Oranges”, 3.99),

new ShoppingItem(“Pears”, 4.50)};

assertArrayEquals(test, getShoppingItems(list));

assertTrue(result);

TestSuite.pass(2);

System.out.println(“Testing insert() Out of Range”);

list = new ShoppingList(new ShoppingItem[]{

new ShoppingItem(“Shoes”, 102.49),

new ShoppingItem(“Shirt”, 42.30)});

item = new ShoppingItem(“Pants”, 120.50);

test = new ShoppingItem[]{

new ShoppingItem(“Shoes”, 102.49),

new ShoppingItem(“Shirt”, 42.30)};

result = list.insert(item, 15);

assertArrayEquals(test, getShoppingItems(list));

assertFalse(result);

TestSuite.pass(2);

}

catch(AssertionError e){

TestSuite.fail(e.toString());

throw e;

}

catch(Exception e){

TestSuite.fail(e.toString());

throw e;

}

}

@Test

public void testListIndexOf() {

try{

TestSuite.startTest(“Testing ShoppingList indexOf()”, 5);

System.out.println(“\tArray: ” + Arrays.toString(

new ShoppingItem[]{new ShoppingItem(“Milk”, 2.00),

new ShoppingItem(“Eggs”, 3.40),

new ShoppingItem(“Cookies”, 2.45)}));

System.out.println(“\tTesting Contains Eggs”);

ShoppingList list = new ShoppingList(new ShoppingItem[]{

new ShoppingItem(“Milk”, 2.00),

new ShoppingItem(“Eggs”, 3.40),

new ShoppingItem(“Cookies”, 2.45)});

int result = list.indexOf(“Eggs”);

assertEquals(1, result);

TestSuite.pass(2);

System.out.println(“\tTesting Does Not Contain Bork”);

list = new ShoppingList(new ShoppingItem[]{

new ShoppingItem(“Milk”, 2.00),

new ShoppingItem(“Eggs”, 3.40),

new ShoppingItem(“Cookies”, 2.45)});

result = list.indexOf(“Bork”);

assertEquals(-1, result);

TestSuite.pass(2);

System.out.println(“\tTesting indexOf(null)”);

list = new ShoppingList(new ShoppingItem[]{

new ShoppingItem(“Milk”, 2.00),

new ShoppingItem(“Eggs”, 3.40),

new ShoppingItem(“Cookies”, 2.45)});

result = list.indexOf(null);

assertEquals(-1, result);

TestSuite.pass(1);

}

catch(AssertionError e){

TestSuite.fail(e.toString());

throw e;

}

catch(Exception e){

TestSuite.fail(e.toString());

throw e;

}

}

@Test

public void testListDeleteItemAt() {

try{

TestSuite.startTest(“Testing ShoppingList deleteItemAt()”, 5);

System.out.println(“\tTesting DeleteItemAt(0)”);

ShoppingList list = new ShoppingList(new ShoppingItem[]{

new ShoppingItem(“Milk”, 2.00),

new ShoppingItem(“Bread”, 3.20),

new ShoppingItem(“Eggs”, 3.40),

new ShoppingItem(“Sausages”, 4.50),

new ShoppingItem(“Cookies”, 2.45),

new ShoppingItem(“Cereal”, 2.90)});

ShoppingItem[] test = new ShoppingItem[]{

new ShoppingItem(“Bread”, 3.20),

new ShoppingItem(“Eggs”, 3.40),

new ShoppingItem(“Sausages”, 4.50),

new ShoppingItem(“Cookies”, 2.45),

new ShoppingItem(“Cereal”, 2.90),

null};

ShoppingItem item = new ShoppingItem(“Milk”, 2.00);

ShoppingItem result = list.deleteItemAt(0);

assertEquals(item, result);

assertArrayEquals(test, getShoppingItems(list));

TestSuite.pass(1);

System.out.println(“\tTesting DeleteItemAt(1)”);

list = new ShoppingList(new ShoppingItem[]{

new ShoppingItem(“Milk”, 2.00),

new ShoppingItem(“Bread”, 3.20),

new ShoppingItem(“Eggs”, 3.40),

new ShoppingItem(“Sausages”, 4.50),

new ShoppingItem(“Cookies”, 2.45),

new ShoppingItem(“Cereal”, 2.90)});

test = new ShoppingItem[]{

new ShoppingItem(“Milk”, 2.00),

new ShoppingItem(“Eggs”, 3.40),

new ShoppingItem(“Sausages”, 4.50),

new ShoppingItem(“Cookies”, 2.45),

new ShoppingItem(“Cereal”, 2.90),

null};

item = new ShoppingItem(“Bread”, 3.20);

result = list.deleteItemAt(1);

assertEquals(item, result);

assertArrayEquals(test, getShoppingItems(list));

TestSuite.pass(1);

System.out.println(“\tTesting DeleteItemAt(length-1)”);

list = new ShoppingList(new ShoppingItem[]{

new ShoppingItem(“Milk”, 2.00),

new ShoppingItem(“Bread”, 3.20),

new ShoppingItem(“Eggs”, 3.40),

new ShoppingItem(“Sausages”, 4.50),

new ShoppingItem(“Cookies”, 2.45),

new ShoppingItem(“Cereal”, 2.90)});

test = new ShoppingItem[]{

new ShoppingItem(“Milk”, 2.00),

new ShoppingItem(“Bread”, 3.20),

new ShoppingItem(“Eggs”, 3.40),

new ShoppingItem(“Sausages”, 4.50),

new ShoppingItem(“Cookies”, 2.45),

null};

item = new ShoppingItem(“Cereal”, 2.90);

result = list.deleteItemAt(5);

assertEquals(item, result);

assertArrayEquals(test, getShoppingItems(list));

TestSuite.pass(1);

System.out.println(“\tTesting DeleteItemAt(15)”);

list = new ShoppingList(new ShoppingItem[]{

new ShoppingItem(“Milk”, 2.00),

new ShoppingItem(“Bread”, 3.20),

new ShoppingItem(“Eggs”, 3.40),

new ShoppingItem(“Sausages”, 4.50),

new ShoppingItem(“Cookies”, 2.45),

new ShoppingItem(“Cereal”, 2.90)});

test = new ShoppingItem[]{

new ShoppingItem(“Milk”, 2.00),

new ShoppingItem(“Bread”, 3.20),

new ShoppingItem(“Eggs”, 3.40),

new ShoppingItem(“Sausages”, 4.50),

new ShoppingItem(“Cookies”, 2.45),

new ShoppingItem(“Cereal”, 2.90)};

item = null;

result = list.deleteItemAt(15);

assertEquals(item, result);

assertArrayEquals(test, getShoppingItems(list));

TestSuite.pass(1);

System.out.println(“\tTesting DeleteItemAt -1”);

list = new ShoppingList(new ShoppingItem[]{

new ShoppingItem(“Milk”, 2.00),

new ShoppingItem(“Bread”, 3.20),

new ShoppingItem(“Eggs”, 3.40),

new ShoppingItem(“Sausages”, 4.50),

new ShoppingItem(“Cookies”, 2.45),

new ShoppingItem(“Cereal”, 2.90)});

test = new ShoppingItem[]{

new ShoppingItem(“Milk”, 2.00),

new ShoppingItem(“Bread”, 3.20),

new ShoppingItem(“Eggs”, 3.40),

new ShoppingItem(“Sausages”, 4.50),

new ShoppingItem(“Cookies”, 2.45),

new ShoppingItem(“Cereal”, 2.90)};

item = null;

result = list.deleteItemAt(-1);

assertEquals(item, result);

assertArrayEquals(test, getShoppingItems(list));

TestSuite.pass(1);

}

catch(AssertionError e){

TestSuite.fail(e.toString());

throw e;

}

catch(Exception e){

TestSuite.fail(e.toString());

throw e;

}

}

@Test

public void testListDeleteItem() {

try{

TestSuite.startTest(“Testing ShoppingList deleteItem()”, 4);

System.out.println(“Array: ” + Arrays.toString(

new ShoppingItem[]{

new ShoppingItem(“Milk”, 2.00),

new ShoppingItem(“Eggs”, 3.40),

new ShoppingItem(“Cookies”, 2.45)}));

System.out.println(“\tTesting Delete Milk”);

ShoppingList list = new ShoppingList(new ShoppingItem[]{

new ShoppingItem(“Milk”, 2.00),

new ShoppingItem(“Eggs”, 3.40),

new ShoppingItem(“Cookies”, 2.45)});

ShoppingItem[] test = new ShoppingItem[]{

new ShoppingItem(“Eggs”, 3.40),

new ShoppingItem(“Cookies”, 2.45),

null};

ShoppingItem item = new ShoppingItem(“Milk”, 2.00);

ShoppingItem result = list.deleteItem(“Milk”);

assertEquals(item, result);

assertArrayEquals(test, getShoppingItems(list));

TestSuite.pass(2);

System.out.println(“\tTesting Delete Bork”);

list = new ShoppingList(new ShoppingItem[]{

new ShoppingItem(“Milk”, 2.00),

new ShoppingItem(“Eggs”, 3.40),

new ShoppingItem(“Cookies”, 2.45)});

test = new ShoppingItem[]{

new ShoppingItem(“Milk”, 2.00),

new ShoppingItem(“Eggs”, 3.40),

new ShoppingItem(“Cookies”, 2.45)};

item = null;

result = list.deleteItem(“Bork”);

assertEquals(item, result);

assertArrayEquals(test, getShoppingItems(list));

TestSuite.pass(1);

System.out.println(“\tTesting Delete null”);

list = new ShoppingList(new ShoppingItem[]{

new ShoppingItem(“Milk”, 2.00),

new ShoppingItem(“Eggs”, 3.40),

new ShoppingItem(“Cookies”, 2.45)});

test = new ShoppingItem[]{

new ShoppingItem(“Milk”, 2.00),

new ShoppingItem(“Eggs”, 3.40),

new ShoppingItem(“Cookies”, 2.45)};

item = null;

result = list.deleteItem(null);

assertEquals(item, result);

assertArrayEquals(test, getShoppingItems(list));

TestSuite.pass(1);

}

catch(AssertionError e){

TestSuite.fail(e.toString());

throw e;

}

catch(Exception e){

TestSuite.fail(e.toString());

throw e;

}

}

@Test

public void testListRemoveBlanks() throws InvocationTargetException,

IllegalAccessException, NoSuchMethodException {

try{

TestSuite.startTest(“Testing ShoppingList removeBlanks()”, 3);

System.out.println(“\tYou might want to make use of a temporary array…”);

ShoppingList list = new ShoppingList(new ShoppingItem[]{

new ShoppingItem(“Milk”, 2.00),

new ShoppingItem(“Bread”, 3.20),

new ShoppingItem(“Eggs”, 3.40),

new ShoppingItem(“Sausages”, 4.50),

new ShoppingItem(“Cookies”, 2.45),

new ShoppingItem(“Cereal”, 2.90)});

ShoppingItem[] items = getShoppingItems(list);

items[0] = null;

items[2] = null;

items[3] = null;

items[5] = null;

ShoppingItem[] test = new ShoppingItem[]{

new ShoppingItem(“Bread”, 3.20),

new ShoppingItem(“Cookies”, 2.45),

null, null, null, null};

Method removeBlanks = ShoppingList.class.getDeclaredMethod(“removeBlanks”);

removeBlanks.setAccessible(true);

removeBlanks.invoke(list);

System.out.println(“\t\tExpected:\t” + Arrays.toString(test));

System.out.println(“\t\tGot:\t\t” + Arrays.toString(getShoppingItems(list)));

assertArrayEquals(test, getShoppingItems(list));

TestSuite.pass(3);

}

catch(AssertionError e){

TestSuite.fail(e.toString());

throw e;

}

catch(Exception e){

TestSuite.fail(e.toString());

throw e;

}

}

@Test

public void testListSetSize() throws InvocationTargetException,

IllegalAccessException, NoSuchMethodException {

try{

TestSuite.startTest(“Testing ShoppingList setSize()”, 6);

System.out.println(“\tTesting setSize Above Count()”);

ShoppingList list = new ShoppingList(new ShoppingItem[]{

new ShoppingItem(“Milk”, 2.00),

new ShoppingItem(“Eggs”, 4.00),

new ShoppingItem(“Bacon”, 4.00)});

ShoppingItem[] test = new ShoppingItem[]{

new ShoppingItem(“Milk”, 2.00),

new ShoppingItem(“Eggs”, 4.00),

new ShoppingItem(“Bacon”, 4.00),

null, null, null };

Method setSize = ShoppingList.class.getDeclaredMethod(“setSize”, int.class);

setSize.setAccessible(true);

boolean result = (boolean)setSize.invoke(list, 6);

assertArrayEquals(test, getShoppingItems(list));

assertTrue(result);

TestSuite.pass(2);

System.out.println(“\tTesting SetSize Below Count()”);

list = new ShoppingList(new ShoppingItem[]{

new ShoppingItem(“Milk”, 2.00),

new ShoppingItem(“Eggs”, 4.00),

new ShoppingItem(“Bacon”, 4.00)});

test = new ShoppingItem[]{

new ShoppingItem(“Milk”, 2.00),

new ShoppingItem(“Eggs”, 4.00),

new ShoppingItem(“Bacon”, 4.00)};

result = (boolean)setSize.invoke(list, 2);

assertArrayEquals(test, getShoppingItems(list));

assertFalse(result);

TestSuite.pass(2);

System.out.println(“\tTesting SetSize to Count()”);

list = new ShoppingList(new ShoppingItem[]{

new ShoppingItem(“Milk”, 2.00),

new ShoppingItem(“Eggs”, 4.00),

new ShoppingItem(“Bacon”, 4.00)});

test = new ShoppingItem[]{

new ShoppingItem(“Milk”, 2.00),

new ShoppingItem(“Eggs”, 4.00),

new ShoppingItem(“Bacon”, 4.00)};

result = (boolean)setSize.invoke(list, 3);

assertArrayEquals(test, getShoppingItems(list));

assertTrue(result);

TestSuite.pass(2);

}

catch(AssertionError e){

TestSuite.fail(e.toString());

throw e;

}

catch(Exception e){

TestSuite.fail(e.toString());

throw e;

}

}

@Test

public void testListSortByCost() {

try{

TestSuite.startTest(“Testing ShoppingList sortByCost()”, 3);

System.out.println(“\tTesting Sorts Correctly”);

ShoppingList list = new ShoppingList(new ShoppingItem[]{

new ShoppingItem(“Solar Powered Keyboard”, 100.10),

new ShoppingItem(“Solid State Drive”, 200.10),

new ShoppingItem(“Logitech Headset”, 89.89),

new ShoppingItem(“Ouroboros”, 150.10),

new ShoppingItem(“Paddle Pop”, 1.10)});

ShoppingItem[] test = new ShoppingItem[]{

new ShoppingItem(“Paddle Pop”, 1.10),

new ShoppingItem(“Logitech Headset”, 89.89),

new ShoppingItem(“Solar Powered Keyboard”, 100.10),

new ShoppingItem(“Ouroboros”, 150.10),

new ShoppingItem(“Solid State Drive”, 200.10)};

list.sortByCost();

System.out.println(“\t\tExpected:\t” + Arrays.toString(test));

System.out.println(“\t\tGot:\t\t” + Arrays.toString(getShoppingItems(list)));

assertArrayEquals(test, getShoppingItems(list));

TestSuite.pass(3);

}

catch(AssertionError e){

TestSuite.fail(e.toString());

throw e;

}

catch(Exception e){

TestSuite.fail(e.toString());

throw e;

}

}

@Test

public void testListGetTotalCost() {

try{

TestSuite.startTest(“Testing ShoppingList getTotalCost()”, 2);

System.out.println(“\tTesting $100.10 + $200.10 + $150.10 + $1.10”);

ShoppingList list = new ShoppingList(new ShoppingItem[]{

new ShoppingItem(“Solar Powered Keyboard”, 100.10),

new ShoppingItem(“Solid State Drive”, 200.10),

new ShoppingItem(“Ouroboros”, 150.10),

new ShoppingItem(“Paddle Pop”, 1.10)});

double result = list.getTotalCost();

assertEquals(451.40, result, 0.00001);

TestSuite.pass(2);

}

catch(AssertionError e){

TestSuite.fail(e.toString());

throw e;

}

catch(Exception e){

TestSuite.fail(e.toString());

throw e;

}

}

/**

* Utility method that allows me to access the ShoppingList private instance variable, shoppingItems.

* @param list The ShoppingList object to reflect into.

* @return the private shoppingItems array from the argument.

*/

public static ShoppingItem[] getShoppingItems(ShoppingList list){

try{

Class<? extends ShoppingList> listReflect = list.getClass();

Field itemsReflect = listReflect.getDeclaredField(“shoppingItems”);

itemsReflect.setAccessible(true);

ShoppingItem[] items = (ShoppingItem[]) itemsReflect.get(list);

return items;

}

catch(Exception e){

System.err.println(“ERROR: Could not access private shoppingItems. Make sure the variable name is \”shoppingItems\” and it is private”);

e.printStackTrace();

}

return null;

}

/**

* Utility method to compare two ShoppingItem[].

* Unfortunately, .equals does not work for array types.

* @param arr1

* @param arr2

* @return true if the same, false otherwise.

*/

public static boolean arrayEquals(ShoppingItem[] arr1, ShoppingItem[] arr2){

if(arr1.length == arr2.length){

for(int i=0; i<arr1.length; i++){

if(arr1[i] != null && arr2[i] != null){

if(!arr1[i].equals(arr2[i])){

return false;

}

}

else if(arr1[i] == null && arr2[i] != arr1[i]){

return false;

}

}

return true;

}

return false;

}

}

TestSuite.java

package testing;

import org.junit.AfterClass;

import org.junit.BeforeClass;

import org.junit.runner.RunWith;

import org.junit.runners.Suite;

/**

* <p>Run this class as a JUnit Test to get a good indication of how

* many marks you should receive. This is not a guarantee of your

* actual final marks. Marks may be deducted for no / poor

* commenting, poor variable names, poor style, use of the keywords

* break / continue, plagiarism, incorrect submission, etc. Your

* teacher will be checking your code thoroughly in addition to

* running this TestDriver.</p>

*

* <p>This TestSuite will automatically run all Test methods in both

* ShoppingItemTest and ShoppingListTest. JUnit does not guarantee

* the order of these tests. The order is effectively random, but

* that should not affect the output.</p>

*

* <p>Each test case has some dependencies – the ShoppingItem

* variables must be private and called “name” and “price”; The

* ShoppingList variable must be private and called “shoppingItems”.

* Each test case contains a number of sub-tests. Each sub-test is

* dependent on the previous sub-test. If a sub-test fails, JUnit

* will flag the test case as failed and it will not check the next

* sub-test.</p>

*

* @author Michael

*

*/

@RunWith(Suite.class)

@Suite.SuiteClasses({ShoppingItemTest.class, ShoppingListTest.class})

public class TestSuite {

private static int methodMarks = 0;

private static int subMarks = 0;

private static int totalMarks = 0;

private static int maxMarks = 100;

public static void startTest(String testName, int marks){

subMarks = 0;

methodMarks = marks;

System.out.println(“\n” + testName);

}

/**

* Called to notify the user of a passed test.

* @param marks the number of marks the test is worth.

*/

public static void pass(int marks){

subMarks += marks;

System.out.println(“\t\tPASSED (+”+ marks +”)”);

}

 

/**

* Called to notify the user of a failed test.

* @param message The error message.

*/

public static void fail(String message){

System.out.println(“\t\tFAILED: ” + message);

}

public static void finishTest(){

System.out.println(“\tSubtotal: ” + subMarks + ” / ” +methodMarks);

totalMarks += subMarks;

}

@BeforeClass

public static void setUpBeforeClass(){

System.out.println(“Start Testing”);

}

@AfterClass

public static void tearDownAfterClass(){

System.out.println(“\nFinished Testing”);

System.out.println(“Total: ” + totalMarks + ” / ” +maxMarks+”\n”);

if(totalMarks < 10){

System.out.println(“Looks like you have just started.”);

System.out.println(“You can use this Test Driver to check your progress.”);

System.out.println(“Start by writing the constructors, then your own main method to see if they work.”);

System.out.println(“There may be problems that this Test Driver cannot help you with.”);

System.out.println(“Good luck programmer.”);

}

else if(totalMarks < 30){

System.out.println(“Looks like you are making progress. Keep going.”);

System.out.println(“”);

}

else if(totalMarks < 60){

System.out.println(“Great work. Keep it up!”);

System.out.println(“Make sure you’re checking for nulls.”);

System.out.println(“Check your arguments are valid and handle them appropriately.”);

}

else if(totalMarks < 90){

System.out.println(“Almost there!”);

System.out.println(“It’s the little things that cause the biggest problems isn’t it?”);

System.out.println(“Sometimes its best to take a step back and think about the algorithm.”);

System.out.println(“Maybe writing pseudocode or drawing a diagram would help.”);

}

else if(totalMarks < 100){

System.out.println(“So close!”);

System.out.println(“I know you can do this.”);

System.out.println(“Sometimes the problem isn’t where you expect it to be.”);

System.out.println(“Make sure you have your own main method to test specific bugs.”);

System.out.println(“Print things out, do some debugging!”);

}

else if(totalMarks == 100){

System.out.println(“Great job!”);

System.out.println(“Your code has passed all the tests executed by this test driver.”);

System.out.println(“Remember, this is not a guarantee of your final marks.”);

System.out.println(“Your supervisor may test for more cases than are outlined here.”);

System.out.println(“You may also be marked on code style.”);

System.out.println(“Make sure you have meaningful variable names and that you have left\n”

+ “a comment to describe any complicated code.”);

System.out.println(“Once you’ve done that, take a break.”);

System.out.println(“You’ve earned it! :D”);

}

else{

System.out.println(“Looks like you got more that 100%”);

System.out.println(“That’s incredible!”);

System.out.println(“Maybe you should be teaching programming.”);

System.out.println(“…instead of cheating.”);

}

}

}

Solution

ShoppingItem.java

package progfund;

import java.text.DecimalFormat;

import java.text.NumberFormat;

import java.util.Locale;

/**

* <p>This class represents a single Shopping Item.

* The ShoppingItem simply contains a name and a price and a few

* methods that allow us to compare two ShoppingItems in different

* ways.

* </p>

* <h3>How To Do The Assignment</h3>

* <p>In the Shopping List assignment, this is the class that you

* should write first. We suggest you write your methods in the

* following order:</p>

* <ul>

* <li>Write the constructors. Test.

* <li>Write the getters and setters. Test.

* <li>Write the toString method. Test.

* <li>Write the equals method. Test.

* <li>Write the comparePrice method. Test.

* <li>Write the compareName method. Test.

* </ul>

*  <h3>Additional Requirements</h3>

* <p>You must provide an academic integrity statement at the top of

* each class you write. This should include your name, id number,

* date and the statement, “This is my own work as defined by the

* SAIBT / EIBT Academic Misconduct policy.”</p>

* <p>You will also need to comment your code thoroughly while you are

* programming.</p>

* <p>Once you are confident these methods are working, you

* should start writing the ShoppingList class.</p>

*/

public class ShoppingItem {

/**The name of the shopping item.*/

private String name;

/**How much the shopping item costs in dollars.*/

private double price;

/**

* Constructor to create a ShoppingItem from the argument name

* and price.

* @param name The name of the shopping item.

* @param price The price of the shopping item in dollars.

*/

public ShoppingItem(String name, double price){

//TODO

//changing this.name to name variable described initially

this.name=name;

//changing this.price to price variable described initially

this.price=price;

}

/**

* A copy constructor for the item.

* This constructor should return a deep copy of the argument

* ShoppingItem.

* @param copy The ShoppingItem to copy

*/

public ShoppingItem(ShoppingItem copy){

//TODO

//setting name property to the name property of given parameter

this.name=copy.name;

//setting price property to the price property of given parameter

this.price=copy.price;

}

/**

* Sets the price to the argument newPrice. If the newPrice is

* less than or equal to 0.0, this method should do nothing.

* @param newPrice The price to change to.

*/

public void setPrice(double newPrice){

//TODO

//setting the price property to given parameter if it is greater than zero

if(newPrice>0.0) {

this.price=newPrice;

}

}

/**

* Gets the current price in dollars.

* @return The ShoppingItem’s price.

*/

public double getPrice(){

//TODO

//returning the price property

return this.price;

}

/**

* Sets the name to the argument newName. If the newName is zero

* characters long or null, this method should do nothing.

* @param newName The name to change to.

*/

public void setName(String newName){

//TODO

//setting the name property to given parameter if it is not null

if(newName!=null) {

//setting the name property to given parameter if it is not empty

if(newName.length()>0) {

this.name=newName;}

}

}

/**

* Gets the current name of the ShoppingItem.

* @return The ShoppingItem’s name.

*/

public String getName(){

//TODO

//returning the name property

return this.name;

}

/**

* The toString functions should return a meaningful String

* representation of the class. It will be called automatically

* when printing out objects of this class type.

* @return A String representation of the class in the form:

* “name: $price”. Price should be formatted to 2 decimal places.

*/

public String toString(){

//TODO

//using Decimal format(“#,###.00”) to convert the price to locale.US format and upto 2 decimal places

NumberFormat numberFormat = new DecimalFormat(“#,###.00″);

//returning name and price as string

return this.name+”: $”+numberFormat.format(this.price);

}

/**

* The equals method should return true if the argument object

* contains the same data as the calling object.

* They must be the same datatype and their name and price must

* be the same.

* @param other The Object to test against.

* @return True if the argument contains the same data.

* False in all other cases.

*/

public boolean equals(Object other){

//TODO

boolean k=false;

//checking whether fiven parameter is of ShoppingItem() class or not

if ((other instanceof ShoppingItem)) {

ShoppingItem h=(ShoppingItem) other;

//Checking whether name and price properties are matching

if(h.getName()==this.getName()&&h.getPrice()==this.getPrice())

k= true;

}

return k;

}

/**

* Compares the price of the argument ShoppingItem to the price

* of the calling object.

* @param other The ShoppingItem to compare.

* @return 1 if the calling object has a greater price than the

* argument. -1 if the calling object has a lesser price to the

* argument. 0 if the price of the argument and calling objects

* are the same.

*/

public int comparePrice(ShoppingItem other){

//TODO

//initializing a variable k which will be returned after certain operations

int k=0;

//checking whether price of parameter is less than the price property if yes,change k to 1

if(other.getPrice()<this.getPrice()) {

k=1;

}

//checking whether price of parameter is greater than the price property if yes,change k to -1

else if(other.getPrice()>this.getPrice()) {

k=-1;

}

//else k remains zero

return k;

}

/**

* Compares the name of the argument ShoppingItem to the name of

* the calling object. The Strings will be compared

* lexicographically such that Bread < Eggs < Milk < Yoghurt.

*

* @param other The ShoppingItem to compare.

* @return 1 if the calling object has a greater name than the

* argument. -1 if the calling object has a lesser name to the

* argument. 0 if the name of the argument and calling objects

* are the same.

*/

public int compareName(ShoppingItem other){

//TODO

//checking whether name of parameter to our name property lexographically using compareTo function

int k= other.getName().compareTo(this.getName());

//sets k to 1 if the calling object has a greater name than the argument

if(k>0) {

k=-1;

}

//sets k to 1 if the calling object has a greater name than the argument.

else if(k<0) {

k=1;

}

//else k is zero

//returns k

return k;

}

}

ShoppingList.java

package progfund;

import java.util.Arrays;

/**

* <p>This program represents a ShoppingList that contains a mutable

* array of ShoppingItem objects. It represents a list that you might

* take to the supermarket to keep track of which items you would

* like to buy.</p>

*

* <h3>Overview</h3>

* <p>In Java, most programs are a collection of objects working

* together to get something done. This assignment will require you

* to write two classes. One class will contain a mutable array. The

* other class will represent the items that should be stored into

* this array.</p>

*

* <p>To make your life easier you will be provided with an API

* specification (just like the JavaDocs you use for your

* programming). Your job is to follow this API to fill out the

* method stubs with appropriate code. You should also ensure that

* this code is as uncrashable as possible. If your code detects

* faulty/incorrect input it should handle it as instructed in the

* API.</p>

*

* <h3>Data Structure</h3>

* <p>We will be storing ShoppingItem objects into an array within the

* ShoppingList class. Something to note is that arrays are immutable

* (They cannot be changed once they are created.) A large part of

* this assignment is to add methods to the ShoppingList class to

* simulate an array that is mutable. These methods should facilitate

* adding and deleting items from the array and changing the array’s

* size.</p>

*

* <h3>Testing</h3>

* <p>To ensure your code is uncrashable you will have to filter out

* incorrect input with if statements. A test driver has been

* provided to help you test your classes. However, you should write

* your own main method to test your classes as well.

* Test the expected input, test the boundary cases, test outside the

* boundary cases and test the ridiculous cases. Test early and test

* thoroughly.</p>

*

* <h3>How To Do The Assignment</h3>

* <p>We suggest you start writing the ShoppingItem class first.

* Once the ShoppingItem class is written and tested, begin working

* on the ShoppingList class. We suggest you write your code in the

* following order:</p>

* <ul>

* <li>Write the constructors. Test.

* <li>Write the count method. Test.

* <li>Write the toString method. Test.

* <li>Write the itemAt method. Test.

* <li>Write the setSize method. Test.

* <li>Write the append and prepend methods. Test.

* <li>Write the insert method. Test.

* <li>Write the deleteItemAt method. Test.

* <li>Write the deleteItem method. Test.

* <li>Write the removeBlanks method. Test.

* <li>Write the getTotalCost method. Test.

* <li>Write the sortByCost methods. Test.

* </ul>

*

* <h3>Additional Requirements</h3>

* <p>You must provide an academic integrity statement at the top of

* each class you write. This should include your name, id number,

* date and the statement, “This is my own work as defined by the

* SAIBT / EIBT Academic Misconduct policy.”

* You will need to comment your code thoroughly while you are

* programming.</p>

*/

public class ShoppingList {

/**

* Storage for ShoppingItem objects.

*/

private ShoppingItem[] shoppingItems;

 

/**

* Constructor to create a shoppinglist with the initial contents

* of nothing. Set the size of shoppingItems to 10 and leave it empty.

*/

public ShoppingList() {

//TODO

//initializing an empty ShoppingItem[] array

ShoppingItem[] a= {};

//setting size of the initialized array to 10 and setting our shippingItems array to the obtained array

//Arrays.copyOf copies the references of the objects to new array

this.shoppingItems=Arrays.copyOf(a,10);

}

/**

* Constructor to create a shoppinglist of the specified length.

* If length is less than or equal to zero, set shoppingItems to

* the default length of 10.

* @param length The size of the ShoppingItem array.

*/

public ShoppingList(int length) {

//TODO

//initializing an empty ShoppingItem[] array

ShoppingItem[] a= {};

//checking whether given length is less  than zero

if(length<=0) {

this.shoppingItems=Arrays.copyOf(a,10);}

else {

//setting size of the initialized array to the given argument and setting our shippingItems array to the obtained array

this.shoppingItems=Arrays.copyOf(a,length);

}

}

/**

* Constructor to create a shopping list to store the array of

* ShoppingItems. The array should be created to fit only the

* specified items. Each item should be deep copied to avoid

* privacy leaks.

* If argument items is null, set shoppingItems to the default

* length of 10 and leave it empty.

* @param items The items to be copied.

*/

public ShoppingList(ShoppingItem[] items){

//TODO

//got some error in this code.Couldnot complete

ShoppingItem[] shoppingItems=new ShoppingItem[items.length];

if(items.length==0) {

shoppingItems=Arrays.copyOf(shoppingItems,10);

}

else {

for(int i=0;i<items.length;i++) {

shoppingItems[i]=items[i];

shoppingItems[i].setName(items[i].getName());

shoppingItems[i].setPrice(items[i].getPrice());

}

this.shoppingItems=shoppingItems;

}

}

/**

* Add the specified item to the position after the last non-null item.

* @param item The item to append to the list.

*/

public void append(ShoppingItem item){

//TODO

//initializing a boolean varaiable k

boolean k=true;

//runing a ffor loop to traverse through the array

for(int i=this.shoppingItems.length-1;i>=0;i–) {

//checking whether we have reached 1st element and whether 1st element is null

if(this.shoppingItems[i]!=null && i==this.shoppingItems.length-1){

//copying out shoppingItems array to new array with length 1 more than the initial array

this.shoppingItems=Arrays.copyOf(this.shoppingItems, this.shoppingItems.length+1);

//setting (i+1)th element to ith element value

this.shoppingItems[i+1]=item;

//if this occurs k is changed to false

k=false;

//breaking the loop

break;

}

//checking whether shoppingItems[i] is null

else if(this.shoppingItems[i]!=null) {

//if shoppingItems[i] is not null then setting its next element to the given parameter

this.shoppingItems[i+1]=item;

//if this occurs k is changed to false

k=false;

//breaking the loop

break;

}

}

//if k is still true,that is none of above cases occured,then set shoppingItems[0] to the given parameter

if(k) {

this.shoppingItems[0]=item;

}

}

/**

* Returns the index of the first item matching the specified

* item name.

* @param name The name to search for from items in the

* ShoppingList.

* @return The index of the first occurrence of the specified

* name or -1 if the name is not found within the list.

*/

public int indexOf(String name){

//TODO

//initiaizing an integer k

int k=0;

//initializing a boolean variable p

boolean p=true;

//loop for traversing through array

while(p) {

//if the traversion is complete by exceeding the length of the array,then element is not found,set p to false to exit loop

if(k==this.shoppingItems.length) {

k=-1;

p=false;

}

//if element is found ,set p to fale to exit loop

else if(this.shoppingItems[k].getName()==name) {

p=false;

}

//else increment k by 1

else {

k++;

}

}

//return k which is the index of found element or equal to -1 if element is not found

return k;

}

/**

* Returns the number of items in the list.

* This is not the length of the array, it is the number of

* non-null items.

* @return The number of ShoppingItems in the array.

*/

public int count(){

//TODO

//initializing integer k to keep track of count()

int k=0;

//traversing through array using for loop

for(int i=0;i<this.shoppingItems.length;i++) {

//increment k if element is not null

if(this.shoppingItems[i]!=null) {

k++;

}

}

//returning k which is the number of non null elements

return k;

}

/**

* Remove the first item with the specified name. If an item was

* successfully removed, the item should be returned. The

* following items should move down to fill in the empty space.

* Use removeBlanks(). If an item was successfully removed, the

* item should be returned.

* @param name The name of the item to be removed.

* @return The item that was removed from the array or null if

* the item was not found.

*/

public ShoppingItem deleteItem(String name){

//TODO

//initializing a ShippingItem variable

ShoppingItem k=null;

//checking whether given argument is null

if(name!=null) {

//traversing through array using for loop to find the matching name

for(int i=0;i<this.shoppingItems.length;i++) {

//comparing current element’s name to given argument

if(this.shoppingItems[i].getName().compareTo(name)==0) {

//storing the matched element value in k

k=this.shoppingItems[i];

//replacing found element by null

this.shoppingItems[i]=null;

//breaking the loop

break;

}

}

}

//using removeBlanks() function to remove null elements

removeBlanks();

//returns k which is the deleted element if element is deleted or null otherwise

return k;

}

/**

* Remove the item at the specified position. The following items

* should move down to fill in the empty space.

* Use removeBlanks(). If an item was successfully removed, the

* item should be returned.

* @param index The index is a position between 0 and count()-1

* specifying an item to remove from the array.

* @return The item that was removed from the array or null if

* the index was outside the bounds of the array.

*/

public ShoppingItem deleteItemAt(int index){

//TODO

//initializing a ShippingItem variable

ShoppingItem k=null;

//checking whether given index is within the range of the array

if(index>=0&&index<this.shoppingItems.length) {

//storing the matched element value in k

k=this.shoppingItems[index];

//replacing found element by null

this.shoppingItems[index]=null;

//using removeBlanks() function to remove null elements

removeBlanks();

}

//returns k which is the deleted element if element is deleted or null otherwise

return k;

}

/**

* The equals method should return true if the argument object

* contains the same data as the calling object.

* They must be the same datatype and their name and price must

* be the same.

* @param other The Object to test against.

* @return True if the argument contains the same data.

* False in all other cases.

*/

public boolean equals(Object other){

//TODO

//initializing a boolean variable k with default true

boolean k=true;

//checking whether given argument is null

if(other!=null) {

//initializing a Shoppinglist element which is nothing but the type cated version of the argument object

ShoppingList p=(ShoppingList) other;

//traversing through the array using for loop

for(int i=0;i<this.shoppingItems.length;i++) {

//checking whether any element in p is different from the shoppingItems array

if(!(p.itemAt(i).equals(this.shoppingItems[i]))) {

//setting k to false if any different element is found

k=false;

//breaking the loop

break;

}

}

}

else {

//checking whether ShoppingItems is a null array

if(this.shoppingItems!=null) {

//setting k to false if shoppingItems is not a null array

k=false;

}

}

//returning the boolean k

return k;

}

/**

* Returns the total cost of all ShoppingItems in the list.

* @return Total cost of items

*/

public double getTotalCost(){

//TODO

//initializing a double variable k

double k=0.0;

//traversing through the array using for loop

for(int i=0;i<this.shoppingItems.length;i++) {

//checking whether the element is null

if(this.shoppingItems[i]!=null) {

//adding the price of element to k if it is not null

k=k+this.shoppingItems[i].getPrice();

}

}

//returning the total cost k

return k;

}

/**

* Insert the item at the specified position. The position must

* be between 0 and count(), inclusive. An item can be inserted

* at the end of the list. The item currently at the specified

* position and all items after that position should be moved

* down a space.

* @param item The item to insert.

* @param index The position to insert the item at.

* @return true if item was inserted successfully, false otherwise

*/

public boolean insert(ShoppingItem item, int index){

//TODO

//checking whether given index is within the range if the array

if(index>=0&&index<=this.shoppingItems.length) {

//updating shoppingItems array by increaing its size by 1

this.shoppingItems=Arrays.copyOf(this.shoppingItems,this.shoppingItems.length+1);

//traversing through the array using for loop starting from the end of the array

for(int i=this.shoppingItems.length-1;i>=0;i–) {

//if i is index,then replaceing it by the given argument item

if(i==index) {

this.shoppingItems[i]=item;

//returning true after successful insertion thus breaking the loop

return true;

}

//else update i th element with the element in i-1 th position

else {

this.shoppingItems[i]=this.shoppingItems[i-1];

}

}

}

//returns false if insertion is not successful

return false;

}

/**

* Return the item at the specified index.

* @param index The index is a position between 0 and count()-1

* specifying which item should be returned.

* @return The item at the specified position or null if the

* index does not contain an item.

*/

public ShoppingItem itemAt(int index){

//TODO

//initializing a ShoppingItem variable k with null value

ShoppingItem k=null;

//checking whether given index is in the range of the array

if(index>=0&&index<this.shoppingItems.length) {

//checking whether element at given index is null

if(this.shoppingItems[index]!=null) {

//storing the element at given index in k if it is not null

k=this.shoppingItems[index];

}

}

//returning k

return k;

}

/**

* Add the specified item to the front of the array.

* All other items should be pushed down a space.

* @param item The item to prepend to the list.

*/

public void prepend(ShoppingItem item){

//TODO

//checking whether the shoppingItems array is empty

if(this.count()==0) {

//if shoppingItems array is empty then replacing 1st element with the given argument

this.shoppingItems[0]=item;

}

else {

//updating shopping items array by increasing its size by 1

this.shoppingItems=Arrays.copyOf(this.shoppingItems,this.shoppingItems.length+1);

//traversing through the array using for loop starting from the end

for(int i=this.shoppingItems.length-1;i>=0;i–) {

//at the index i=0,which occurs at last step in the loop,replace given element with the given argument

if(i==0) {

this.shoppingItems[i]=item;

}

//else update the element at i th index by replacing it with the element at i-1 th index

else {

this.shoppingItems[i]=this.shoppingItems[i-1];

}

}

}

}

/**

* The list may end up with null objects when deleting from the

* middle of the array. removeBlanks should move all non-null

* items together in their current order, pushing all null items

* to the end.

*

* For example, if the list contained

* [Milk, Eggs, null, Bread, null, Bananas, null, null],

* calling removeBlanks should result in

* [Milk, Eggs, Bread, Bananas, null, null, null, null].

*

* This private method should be called automatically when

* deleting items from the middle of the array.

*/

private void removeBlanks(){

//TODO

//initializing an integer variable k with value zero

int k=0;

//initializing a ShoppingItem array with length equal to our shoppingItems array

ShoppingItem[] p=new ShoppingItem[this.shoppingItems.length];

//traversing through the array using for loop

for(int i=0;i<this.shoppingItems.length;i++) {

//checking whether the element at index i is null

if(this.shoppingItems[i]!=null) {

//if the element at index i is not null,replace k th element of array p by this element

p[k]=this.shoppingItems[i];

//increment k by one

k++;

}

}

//replace shoppingItems array with array p

this.shoppingItems=Arrays.copyOf(p, p.length);

}

/**

* Sorts the list in ascending order by each item’s cost.

* The list should be sorted in place; that means that this

* method does not return a new list but changes the existing

* list. For example, if the list contained the dollar values

* [6.2, 3.1, 9.23, 4.80] the resulting list would be

* [3.1, 4.80, 6.2, 9.23]

*

* <p>For this method we suggest the selection sort algorithm.</p>

* <p>Pseudocode for the selection sort algorithm may be as follows:</p>

*  <p>FOR index in list:</p>

*     <p>Set minIndex to index</p>

*     <p>FOR each unsortedIndex in list:</p>

*         <p>IF item at unsortedIndex less than item at minIndex:</p>

*         <p>Set item at minIndex to item at unsortedIndex</p>

*          <p>END IF</p>

*     <p>END FOR</p>

*     <p>Swap item at index with item at minIndex</p>

*  <p>END FOR</p>

*/

public void sortByCost(){

//TODO

//initilizing 3 integer variables i,j and min_idx

int i, j, min_idx;

//initializing a ShoppingItem variable with value null

ShoppingItem p=null;

//traversing through the loop using a for loop

for (i = 0; i < this.shoppingItems.length; i++)

{

//putting value of i in min_idx

min_idx = i;

//traversing through the loop using a for loop starting from i+1 th index

for (j = i+1; j < this.shoppingItems.length; j++)

//checking whether the price of element at j th index is less than price of eement at min_idx price

if (this.shoppingItems[j].getPrice() < this.shoppingItems[min_idx].getPrice())

//replacing min_idx with j if the price of element at j th index is less than price of eement at min_idx price

min_idx = j;

//replacing p with the element at index min_idx

p=this.shoppingItems[min_idx];

//replacing element at min_idx with element at i th index

this.shoppingItems[min_idx]=this.shoppingItems[i];

//replacinf element at ith index with p

this.shoppingItems[i]=p;

//the above 3 steps are for swapping the elements

}

}

/**

* Changes the size of the array, retaining all current items in

* the current order. If the specified size is less than the

* number of items in the list, this method should do nothing and

* return false. If the size was successfully changed, the method

* should return true.

*

* This private method should be called automatically when adding

* or deleting items. You can either double the size of the array

* when the array runs out of space or call the method to change

* the size to count() whenever a change occurs.

*

* @param newSize a positive size between count() and infinite.

* @return true if successful, false in all other cases.

*/

private boolean setSize(int newSize){

//TODO

//initializing a boolean variable set to false

boolean k=false;

//checking whether the given argument newSize is greater than or equal to size of surrent array

if(newSize>=this.shoppingItems.length) {

//updating shoppingItems with size=newSize if argument newSize is greater than or equal to size of surrent array

this.shoppingItems=Arrays.copyOf(this.shoppingItems, newSize);

//setting k to true

k=true;

}

//returning k which is true if the size is changed, false otherwise

return k;

}

/**

* Returns a string representation of this object in the form:

* “Shopping List:

*    Milk: $2.00

*    Bread: $3.40

*    Eggs: $4.00”

* Each item must be on a new line. Try not to add extra spaces.

* @return A string containing the name and cost of each non null

* item in the array separated by new line characters.

*/

public String toString(){

//TODO

//initializing a string k with text “Shopping List:”which is the 1st line of the output string

String k=”Shopping List:”;

//traversing through the array using for loop

for(int i=0;i<this.shoppingItems.length;i++) {

if(this.shoppingItems[i]!=null) {

//updating k by adding the elements using toString() finction in shoppingItem class

k=k+”\n”+this.shoppingItems[i].toString();

}

}

//returning string k

return k;

}

}

MyTestDriver.java

package testing;

import progfund.ShoppingItem;

import progfund.ShoppingList;

/**

* Use this space to do your own testing.

*/

public class MyTestDriver {

public static void main(String[] args){

// Here’s some code to start you off.

ShoppingItem item = new ShoppingItem(“Expensive Item”, 10000.999);

System.out.println(item);

ShoppingList list = new ShoppingList();

list.append(item);

System.out.println(list);

}

}

ShoppingItemTest.java

package testing;

import java.lang.reflect.Field;

import java.util.Random;

import org.junit.After;

import org.junit.AfterClass;

import org.junit.Before;

import org.junit.BeforeClass;

import org.junit.Test;

import static org.junit.Assert.*;

import progfund.ShoppingItem;

public class ShoppingItemTest {

@BeforeClass

public static void setUpBeforeClass() {

System.out.println(“Testing ShoppingItem Class”);

}

@Before

public void setUp() { }

@After

public void tearDown() {

TestSuite.finishTest();

}

@AfterClass

public static void tearDownAfterClass(){

System.out.println(“\nFinished Testing ShoppingItem Class”);

}

@Test

public void testItemConstructors() {

try{

TestSuite.startTest(“Test ShoppingItem Constructors”, 4);

System.out.println(“\tTesting ShoppingItem(\”Milk\”, 2.00)”);

ShoppingItem item = new ShoppingItem(“Milk”, 2.00);

assertEquals(“Milk”, getName(item));

assertEquals(2.00, getPrice(item), 0.00001);

TestSuite.pass(2);

System.out.println(“\tTesting copy constructor”);

ShoppingItem item2 = new ShoppingItem(item);

assertEquals(“Milk”, getName(item2));

assertEquals(2.00, getPrice(item2), 0.00001);

TestSuite.pass(2);

}

catch(AssertionError e){

TestSuite.fail(e.toString());

throw e;

}

catch(Exception e){

TestSuite.fail(e.toString());

throw e;

}

}

@Test

public void testItemGetters() {

try{

TestSuite.startTest(“Test ShoppingItem Getters”, 2);

System.out.println(“\tTesting (\”Beans\”, 2.99) getName()”);

ShoppingItem item = new ShoppingItem(“Beans”, 2.99);

assertEquals(“Beans”, item.getName());

TestSuite.pass(1);

System.out.println(“\tTesting (\”Beans\”, 2.99) getPrice()”);

item = new ShoppingItem(“Beans”, 2.99);

assertEquals(2.99, item.getPrice(), 0.00001);

TestSuite.pass(1);

}

catch(AssertionError e){

TestSuite.fail(e.toString());

throw e;

}

catch(Exception e){

TestSuite.fail(e.toString());

throw e;

}

}

@Test

public void testItemSetters() {

try{

TestSuite.startTest(“Test ShoppingItem Setters”, 6);

System.out.println(“\tTesting (\”Beans\”, 2.99) setName(\”Diapers\”)”);

ShoppingItem item = new ShoppingItem(“Beans”, 2.99);

item.setName(“Diapers”);

assertEquals(“Diapers”, getName(item));

TestSuite.pass(1);

System.out.println(“\tTesting (\”Beans\”, 2.99) setName(null)”);

item = new ShoppingItem(“Beans”, 2.99);

item.setName(null);

assertEquals(“Beans”, getName(item));

TestSuite.pass(1);

System.out.println(“\tTesting (\”Beans\”, 2.99) setName(\”\”)”);

item = new ShoppingItem(“Beans”, 2.99);

item.setName(“”);

assertEquals(“Beans”, getName(item));

TestSuite.pass(1);

System.out.println(“\tTesting (\”Beans\”, 2.99) setPrice(84.99)”);

item = new ShoppingItem(“Beans”, 2.99);

item.setPrice(84.99);

assertEquals(84.99, getPrice(item), 0.00001);

TestSuite.pass(1);

System.out.println(“\tTesting (\”Beans\”, 2.99) setPrice(0)”);

item = new ShoppingItem(“Beans”, 2.99);

item.setPrice(0);

assertEquals(2.99, getPrice(item), 0.00001);

TestSuite.pass(1);

System.out.println(“Testing (\”Beans\”, 2.99) setPrice(-1)”);

item = new ShoppingItem(“Beans”, 2.99);

item.setPrice(-1);

assertEquals(2.99, getPrice(item), 0.00001);

TestSuite.pass(1);

}

catch(AssertionError e){

TestSuite.fail(e.toString());

throw e;

}

catch(Exception e){

TestSuite.fail(e.toString());

throw e;

}

}

@Test

public void testItemToString() {

try{

TestSuite.startTest(“Testing ShoppingItem toString()”, 4);

System.out.println(“\tTesting (\”Bananas\”, 3.10)”);

ShoppingItem item = new ShoppingItem(“Bananas”, 3.10);

assertEquals(“Bananas: $3.10”, item.toString());

TestSuite.pass(2);

System.out.println(“\tTesting (\”Gold\”, 1,245,673.55555)”);

item = new ShoppingItem(“Gold”, 1245673.55555);

assertEquals(“Gold: $1,245,673.56”, item.toString());

TestSuite.pass(2);

}

catch(AssertionError e){

TestSuite.fail(e.toString());

throw e;

}

catch(Exception e){

TestSuite.fail(e.toString());

throw e;

}

}

@Test

public void testItemEquals() {

try{

TestSuite.startTest(“Testing ShoppingItem equals()”, 6);

System.out.println(“\tTesting (\”Eggs\”, 3.20) and (\”Eggs\”, 3.20)”);

ShoppingItem item1 = new ShoppingItem(“Eggs”, 3.20);

ShoppingItem item2 = new ShoppingItem(“Eggs”, 3.20);

assertTrue(item1.equals(item2));

TestSuite.pass(1);

System.out.println(“\tTesting (\”Eggs\”, 3.20) and (\”Milk\”, 3.20)”);

item1 = new ShoppingItem(“Eggs”, 3.20);

item2 = new ShoppingItem(“Milk”, 3.20);

assertFalse(item1.equals(item2));

TestSuite.pass(1);

System.out.println(“\tTesting (\”Milk\”, 3.20) and (\”Milk\”, 2.40)”);

item1 = new ShoppingItem(“Milk”, 3.20);

item2 = new ShoppingItem(“Milk”, 2.40);

assertFalse(item1.equals(item2));

TestSuite.pass(1);

System.out.println(“\tTesting (\”Eggs\”, 3.20) and null”);

item1 = new ShoppingItem(“Eggs”, 3.20);

item2 = null;

assertFalse(item1.equals(item2));

TestSuite.pass(1);

System.out.println(“\tTesting (\”Milk\”, 3.20) and (new Random())”);

item1 = new ShoppingItem(“Milk”, 3.20);

assertFalse(item1.equals(new Random()));

TestSuite.pass(1);

System.out.println(“\tTesting (null, -1) and (null, -1)”);

item1 = new ShoppingItem(null, -1);

item2 = new ShoppingItem(null, -1);

assertTrue(item1.equals(item2));

TestSuite.pass(1);

}

catch(AssertionError e){

TestSuite.fail(e.toString());

throw e;

}

catch(Exception e){

TestSuite.fail(e.toString());

throw e;

}

}

@Test

public void testItemComparePrice() {

try{

TestSuite.startTest(“Testing ShoppingItem comparePrice()”, 5);

System.out.println(“\tTesting Lobster $120.32 > Ham $3.40”);

ShoppingItem item1 = new ShoppingItem(“Lobster”, 120.32);

ShoppingItem item2 = new ShoppingItem(“Ham”, 3.40);

int result = item1.comparePrice(item2);

assertEquals(1, result);

TestSuite.pass(2);

System.out.println(“\tTesting Noodles $2.99 < Gold $1200”);

item1 = new ShoppingItem(“Noodles”, 2.99);

item2 = new ShoppingItem(“Gold”, 1200);

result = item1.comparePrice(item2);

assertEquals(-1, result);

TestSuite.pass(2);

System.out.println(“Testing Milk $2.00 == Biscuits $2.00”);

item1 = new ShoppingItem(“Milk”, 2.00);

item2 = new ShoppingItem(“Biscuits”, 2.00);

result = item1.comparePrice(item2);

assertEquals(0, result);

TestSuite.pass(1);

}

catch(AssertionError e){

TestSuite.fail(e.toString());

throw e;

}

catch(Exception e){

TestSuite.fail(e.toString());

throw e;

}

}

@Test

public void testItemCompareName() {

try{

TestSuite.startTest(“Testing ShoppingItem compareName()”, 5);

System.out.println(“\tTesting Juice $4.50 > Jam $3.40”);

ShoppingItem item1 = new ShoppingItem(“Juice”, 4.50);

ShoppingItem item2 = new ShoppingItem(“Jam”, 3.40);

int result = item1.compareName(item2);

assertEquals(1, result);

TestSuite.pass(2);

System.out.println(“\tTesting Gold $1200 < Noodles $2.99”);

item1 = new ShoppingItem(“Gold”, 1200);

item2 = new ShoppingItem(“Noodles”, 2.99);

result = item1.compareName(item2);

assertEquals(-1, result);

TestSuite.pass(2);

System.out.println(“\tTesting Milk $2.00 == Milk $2.99”);

item1 = new ShoppingItem(“Milk”, 2.00);

item2 = new ShoppingItem(“Milk”, 2.99);

result = item1.compareName(item2);

assertEquals(0, result);

TestSuite.pass(1);

}

catch(AssertionError e){

TestSuite.fail(e.toString());

throw e;

}

catch(Exception e){

TestSuite.fail(e.toString());

throw e;

}

}

/**

* Utility method that allows me to access the ShoppingItem private instance variable, name.

* @param item The ShoppingItem object to reflect into.

* @return the private name array from the argument.

*/

public static String getName(ShoppingItem item){

try{

Class<? extends ShoppingItem> itemReflect = item.getClass();

Field nameReflect = itemReflect.getDeclaredField(“name”);

nameReflect.setAccessible(true);

String nameGot = (String)nameReflect.get(item);

return nameGot;

}

catch(Exception e){

System.err.println(“ERROR: Could not access private name. Make sure the variable name is \”name\” and it is private”);

e.printStackTrace();

}

return null;

}

/**

* Utility method that allows me to access the ShoppingItem private instance variable, price.

* @param item The ShoppingItem object to reflect into.

* @return the private price array from the argument.

*/

public static double getPrice(ShoppingItem item){

try{

Class<? extends ShoppingItem> itemReflect = item.getClass();

Field priceReflect = itemReflect.getDeclaredField(“price”);

priceReflect.setAccessible(true);

double priceGot = (double)priceReflect.get(item);

return priceGot;

}

catch(Exception e){

System.err.println(“ERROR: Could not access private price. Make sure the variable name is \”price\” and it is private”);

e.printStackTrace();

}

return -1;

}

}

ShoppingListTest.java

package testing;

import java.lang.reflect.Field;

import java.lang.reflect.InvocationTargetException;

import java.lang.reflect.Method;

import java.util.Arrays;

import org.junit.After;

import org.junit.AfterClass;

import org.junit.Before;

import org.junit.BeforeClass;

import org.junit.Test;

import progfund.ShoppingItem;

import progfund.ShoppingList;

import static org.junit.Assert.*;

public class ShoppingListTest {

@BeforeClass

public static void setUpBeforeClass() {

System.out.println(“Testing ShoppingList Class”);

}

@Before

public void setUp() { }

@After

public void tearDown() {

TestSuite.finishTest();

}

@AfterClass

public static void tearDownAfterClass(){

System.out.println(“\nFinished Testing ShoppingList Class”);

}

@Test

public void testListConstructors() {

try{

TestSuite.startTest(“Testing ShoppingList Constructors”, 7);

System.out.println(“\tTesting ShoppingList()”);

ShoppingList list = new ShoppingList();

assertArrayEquals(new ShoppingItem[10], getShoppingItems(list));

TestSuite.pass(1);

System.out.println(“\tTesting ShoppingList(10)”);

list = new ShoppingList(10);

assertArrayEquals(new ShoppingItem[10], getShoppingItems(list));

TestSuite.pass(1);

System.out.println(“\tTesting ShoppingList(new ShoppingItem[]{Milk, Cookies, Sugar, Spice})”);

ShoppingItem[] items = new ShoppingItem[]{

new ShoppingItem(“Milk”, 2.00),

new ShoppingItem(“Cookies”, 2.49),

new ShoppingItem(“Sugar”, 3.99),

new ShoppingItem(“Spice”, 3.00)};

list = new ShoppingList(items);

assertArrayEquals(new ShoppingItem[]{

new ShoppingItem(“Milk”, 2.00),

new ShoppingItem(“Cookies”, 2.49),

new ShoppingItem(“Sugar”, 3.99),

new ShoppingItem(“Spice”, 3.00)},

getShoppingItems(list));

TestSuite.pass(1);

System.out.println(“\tTesting ShoppingList(new ShoppingItem[]{Milk, Cookies, Sugar, Spice}) Deep Copy”);

System.out.println(“\t\tCalling copyList[0] setName(\”Boogers\”)…”);

items = new ShoppingItem[]{

new ShoppingItem(“Milk”, 2.00),

new ShoppingItem(“Cookies”, 2.49),

new ShoppingItem(“Sugar”, 3.99),

new ShoppingItem(“Spice”, 3.00)};

list = new ShoppingList(items);

items[0].setName(“Boogers”);

assertArrayEquals(new ShoppingItem[]{

new ShoppingItem(“Milk”, 2.00),

new ShoppingItem(“Cookies”, 2.49),

new ShoppingItem(“Sugar”, 3.99),

new ShoppingItem(“Spice”, 3.00)},

getShoppingItems(list));

TestSuite.pass(2);

System.out.println(“\tTesting ShoppingList(null)”);

list = new ShoppingList(null);

assertArrayEquals(new ShoppingItem[10], getShoppingItems(list));

TestSuite.pass(1);

System.out.println(“\tTesting ShoppingList(-1)”);

list = new ShoppingList(-1);

assertArrayEquals(new ShoppingItem[10], getShoppingItems(list));

TestSuite.pass(1);

}

catch(AssertionError e){

TestSuite.fail(e.toString());

throw e;

}

catch(Exception e){

TestSuite.fail(e.toString());

throw e;

}

}

@Test

public void testListCount() {

try{

TestSuite.startTest(“Testing ShoppingList count()”, 4);

System.out.println(“\tTesting Empty list”);

ShoppingList list = new ShoppingList();

int result = list.count();

assertEquals(0, result);

TestSuite.pass(1);

System.out.println(“\tTesting Full list [Milk, Eggs, Bacon]”);

list = new ShoppingList(new ShoppingItem[]{

new ShoppingItem(“Milk”, 2.00),

new ShoppingItem(“Eggs”, 3.99),

new ShoppingItem(“Bacon”, 4.45)});

result = list.count();

assertEquals(3, result);

TestSuite.pass(2);

System.out.println(“\tTesting List With Nulls”);

list = new ShoppingList(new ShoppingItem[]{

new ShoppingItem(“Milk”, 2.00),

new ShoppingItem(“Juice”, 4.00),

new ShoppingItem(“Bread”, 3.20),

new ShoppingItem(“Eggs”, 3.99),

new ShoppingItem(“Bacon”, 4.45),

new ShoppingItem(“Toilet Paper”, 5.00)});

ShoppingItem[] items = getShoppingItems(list);

items[2] = null;

items[3] = null;

result = list.count();

assertEquals(4, result);

TestSuite.pass(1);

}

catch(AssertionError e){

TestSuite.fail(e.toString());

throw e;

}

catch(Exception e){

TestSuite.fail(e.toString());

throw e;

}

}

@Test

public void testListToString() {

try{

TestSuite.startTest(“Testing ShoppingList toString()”, 5);

System.out.println(“\tTesting toString On Empty List”);

ShoppingList list = new ShoppingList();

String result = list.toString();

assertEquals(“Shopping List:”, result);

TestSuite.pass(1);

System.out.println(“\tTesting toString On Full List”);

list = new ShoppingList(new ShoppingItem[]{new ShoppingItem(“Corn”, 3.30),

new ShoppingItem(“Jewels”, 800), new ShoppingItem(“Mazda”, 2800)});

result = list.toString();

assertEquals(“Shopping List:\nCorn: $3.30\nJewels: $800.00\nMazda: $2,800.00”, result);

TestSuite.pass(2);

System.out.println(“\tTesting toString On List With Nulls”);

list = new ShoppingList(new ShoppingItem[]{new ShoppingItem(“Corn”, 3.30),

new ShoppingItem(“Jewels”, 800), new ShoppingItem(“Mazda”, 2800)});

ShoppingItem[] items = getShoppingItems(list);

items[0] = null;

items[2] = null;

result = list.toString();

assertEquals(“Shopping List:\nJewels: $800.00”, result);

TestSuite.pass(2);

}

catch(AssertionError e){

TestSuite.fail(e.toString());

throw e;

}

catch(Exception e){

TestSuite.fail(e.toString());

throw e;

}

}

@Test

public void testListEquals(){

try{

TestSuite.startTest(“Testing ShoppingList equals()”, 4);

System.out.println(“\tTesting Equal Lists”);

ShoppingList list1 = new ShoppingList(new ShoppingItem[]{

new ShoppingItem(“Apples”, 2.90),

new ShoppingItem(“Bananas”, 3.70),

new ShoppingItem(“Pears”, 4.50)});

ShoppingList list2 = new ShoppingList(new ShoppingItem[]{

new ShoppingItem(“Apples”, 2.90),

new ShoppingItem(“Bananas”, 3.70),

new ShoppingItem(“Pears”, 4.50)});

boolean result = list1.equals(list2);

assertTrue(result);

TestSuite.pass(1);

System.out.println(“\tTesting Different Lists, Different Lengths”);

list1  = new ShoppingList(new ShoppingItem[]{

new ShoppingItem(“Bacon”, 4.50),

new ShoppingItem(“Ham”, 3.70),

new ShoppingItem(“Pork”, 6.90)});

list2 = new ShoppingList(new ShoppingItem[]{

new ShoppingItem(“Porterhouse Steak”, 4.50),

new ShoppingItem(“TBone Steak”, 3.70),

new ShoppingItem(“Ribeye Steak”, 6.90),

new ShoppingItem(“Veal”, 5.40)});

result = list1.equals(list2);

assertFalse(result);

TestSuite.pass(1);

System.out.println(“\tTesting Different Lists, Same Lengths”);

list1  = new ShoppingList(new ShoppingItem[]{

new ShoppingItem(“Bacon”, 4.50),

new ShoppingItem(“Ham”, 3.70),

new ShoppingItem(“Pork”, 6.90)});

list2 = new ShoppingList(new ShoppingItem[]{

new ShoppingItem(“Bacon”, 4.50),

new ShoppingItem(“TBone Steak”, 3.70),

new ShoppingItem(“Pork”, 6.90)});

result = list1.equals(list2);

assertFalse(result);

TestSuite.pass(1);

System.out.println(“\tTesting equals(null)”);

list1  = new ShoppingList(new ShoppingItem[]{

new ShoppingItem(“Bacon”, 4.50),

new ShoppingItem(“Ham”, 3.70),

new ShoppingItem(“Pork”, 6.90)});

list2 = null;

result = list1.equals(list2);

assertFalse(result);

TestSuite.pass(1);

}

catch(AssertionError e){

TestSuite.fail(e.toString());

throw e;

}

catch(Exception e){

TestSuite.fail(e.toString());

throw e;

}

}

@Test

public void testListItemAt() {

try{

TestSuite.startTest(“Testing ShoppingList itemAt()”, 4);

System.out.println(“\tTesting itemAt(0)”);

ShoppingList list = new ShoppingList(new ShoppingItem[]{

new ShoppingItem(“Milk”, 2.00),

new ShoppingItem(“Eggs”, 4.00),

new ShoppingItem(“Bacon”, 4.00)});

ShoppingItem item = new ShoppingItem(“Milk”, 2.00);

ShoppingItem result = list.itemAt(0);

assertEquals(item, result);

TestSuite.pass(1);

System.out.println(“\tTesting itemAt(length-1)”);

list = new ShoppingList(new ShoppingItem[]{

new ShoppingItem(“Milk”, 2.00),

new ShoppingItem(“Eggs”, 4.00),

new ShoppingItem(“Bacon”, 4.00)});

item = new ShoppingItem(“Bacon”, 4.00);

result = list.itemAt(2);

assertEquals(item, result);

TestSuite.pass(1);

System.out.println(“\tTesting itemAt(-x)”);

item = null;

list = new ShoppingList(new ShoppingItem[]{

new ShoppingItem(“Milk”, 2.00),

new ShoppingItem(“Eggs”, 4.00),

new ShoppingItem(“Bacon”, 4.00)});

item = null;

result = list.itemAt(-2);

assertEquals(item, result);

TestSuite.pass(1);

System.out.println(“\tTesting itemAt(length+2)”);

item = null;

result = list.itemAt(5);

assertEquals(item, result);

TestSuite.pass(1);

}

catch(AssertionError e){

TestSuite.fail(e.toString());

throw e;

}

catch(Exception e){

TestSuite.fail(e.toString());

throw e;

}

}

@Test

public void testListAppend() {

try{

TestSuite.startTest(“Testing ShoppingList append()”, 4);

System.out.println(“\tTesting append() Empty List”);

ShoppingList list = new ShoppingList();

ShoppingItem item = new ShoppingItem(“SSD”, 119.39);

list.append(item);

ShoppingItem[] test1 = new ShoppingItem[10];

test1[0] = new ShoppingItem(“SSD”, 119.39);

ShoppingItem[] test2 = new ShoppingItem[]{

new ShoppingItem(“SSD”, 119.39)};

assertTrue(arrayEquals(test1, getShoppingItems(list)) ||

arrayEquals(test2, getShoppingItems(list)));

TestSuite.pass(2);

System.out.println(“\tTesting append() To Full List”);

list = new ShoppingList(new ShoppingItem[]{

new ShoppingItem(“Milk”, 2.00),

new ShoppingItem(“Eggs”, 3.40),

new ShoppingItem(“Cookies”, 2.45)});

item = new ShoppingItem(“Sword”, 429.99);

list.append(item);

test1 = new ShoppingItem

[]{

new ShoppingItem(“Milk”, 2.00),

new ShoppingItem(“Eggs”, 3.40),

new ShoppingItem(“Cookies”, 2.45),

new ShoppingItem(“Sword”, 429.99)};

assertArrayEquals(test1, getShoppingItems(list));

TestSuite.pass(2);

}

catch(AssertionError e){

TestSuite.fail(e.toString());

throw e;

}

catch(Exception e){

TestSuite.fail(e.toString());

throw e;

}

}

@Test

public void testListPrepend() {

try{

TestSuite.startTest(“Testing ShoppingList prepend()”, 4);

System.out.println(“\tTesting prepend() Empty List”);

ShoppingList list = new ShoppingList();

ShoppingItem item = new ShoppingItem(“SSD”, 119.39);

list.prepend(item);

ShoppingItem[] test1 = new ShoppingItem[10];

test1[0] = new ShoppingItem(“SSD”, 119.39);

ShoppingItem[] test2 = new ShoppingItem[]{

new ShoppingItem(“SSD”, 119.39)};

assertTrue(arrayEquals(test1, getShoppingItems(list)) ||

arrayEquals(test2, getShoppingItems(list)));

TestSuite.pass(2);

System.out.println(“\tTesting prepend() To Full List”);

list = new ShoppingList(new ShoppingItem[]{

new ShoppingItem(“Milk”, 2.00),

new ShoppingItem(“Eggs”, 3.40),

new ShoppingItem(“Cookies”, 2.45)});

item = new ShoppingItem(“Sword”, 429.99);

list.prepend(item);

test1 = new ShoppingItem[]{

new ShoppingItem(“Sword”, 429.99),

new ShoppingItem(“Milk”, 2.00),

new ShoppingItem(“Eggs”, 3.40),

new ShoppingItem(“Cookies”, 2.45),

};

assertArrayEquals(test1, getShoppingItems(list));

TestSuite.pass(2);

}

catch(AssertionError e){

TestSuite.fail(e.toString());

throw e;

}

catch(Exception e){

TestSuite.fail(e.toString());

throw e;

}

}

@Test

public void testListInsert() {

try{

TestSuite.startTest(“Testing ShoppingList insert()”, 8);

System.out.println(“\tTesting insert() At Index 0”);

ShoppingList list = new ShoppingList(new ShoppingItem[]{

new ShoppingItem(“Milk”, 2.00),

new ShoppingItem(“Eggs”, 3.40),

new ShoppingItem(“Cookies”, 2.45)});

ShoppingItem item = new ShoppingItem(“Frisbee”, 0.99);

boolean result = list.insert(item, 0);

ShoppingItem[] test = new ShoppingItem[]{

new ShoppingItem(“Frisbee”, 0.99),

new ShoppingItem(“Milk”, 2.00),

new ShoppingItem(“Eggs”, 3.40),

new ShoppingItem(“Cookies”, 2.45)};

assertArrayEquals(test, getShoppingItems(list));

assertTrue(result);

TestSuite.pass(2);

System.out.println(“\tTesting insert() in Middle”);

list = new ShoppingList(new ShoppingItem[]{

new ShoppingItem(“Drill”, 89.00),

new ShoppingItem(“Saw”, 34.40),

new ShoppingItem(“Hammer”, 7.45),

new ShoppingItem(“Wrench”, 21.45),

new ShoppingItem(“Pipe”, 8.94)});

item = new ShoppingItem(“Screwdriver”, 4.55);

result = list.insert(item, 3);

test = new ShoppingItem[]{

new ShoppingItem(“Drill”, 89.00),

new ShoppingItem(“Saw”, 34.40),

new ShoppingItem(“Hammer”, 7.45),

new ShoppingItem(“Screwdriver”, 4.55),

new ShoppingItem(“Wrench”, 21.45),

new ShoppingItem(“Pipe”, 8.94)};

assertArrayEquals(test, getShoppingItems(list));

assertTrue(result);

TestSuite.pass(2);

System.out.println(“\tTesting insert() At End”);

list = new ShoppingList(new ShoppingItem[]{

new ShoppingItem(“Apples”, 2.49),

new ShoppingItem(“Bananas”, 4.30),

new ShoppingItem(“Oranges”, 3.99)});

item = new ShoppingItem(“Pears”, 4.50);

result = list.insert(item, 3);

test = new ShoppingItem[]{

new ShoppingItem(“Apples”, 2.49),

new ShoppingItem(“Bananas”, 4.30),

new ShoppingItem(“Oranges”, 3.99),

new ShoppingItem(“Pears”, 4.50)};

assertArrayEquals(test, getShoppingItems(list));

assertTrue(result);

TestSuite.pass(2);

System.out.println(“Testing insert() Out of Range”);

list = new ShoppingList(new ShoppingItem[]{

new ShoppingItem(“Shoes”, 102.49),

new ShoppingItem(“Shirt”, 42.30)});

item = new ShoppingItem(“Pants”, 120.50);

test = new ShoppingItem[]{

new ShoppingItem(“Shoes”, 102.49),

new ShoppingItem(“Shirt”, 42.30)};

result = list.insert(item, 15);

assertArrayEquals(test, getShoppingItems(list));

assertFalse(result);

TestSuite.pass(2);

}

catch(AssertionError e){

TestSuite.fail(e.toString());

throw e;

}

catch(Exception e){

TestSuite.fail(e.toString());

throw e;

}

}

@Test

public void testListIndexOf() {

try{

TestSuite.startTest(“Testing ShoppingList indexOf()”, 5);

System.out.println(“\tArray: ” + Arrays.toString(

new ShoppingItem[]{new ShoppingItem(“Milk”, 2.00),

new ShoppingItem(“Eggs”, 3.40),

new ShoppingItem(“Cookies”, 2.45)}));

System.out.println(“\tTesting Contains Eggs”);

ShoppingList list = new ShoppingList(new ShoppingItem[]{

new ShoppingItem(“Milk”, 2.00),

new ShoppingItem(“Eggs”, 3.40),

new ShoppingItem(“Cookies”, 2.45)});

int result = list.indexOf(“Eggs”);

assertEquals(1, result);

TestSuite.pass(2);

System.out.println(“\tTesting Does Not Contain Bork”);

list = new ShoppingList(new ShoppingItem[]{

new ShoppingItem(“Milk”, 2.00),

new ShoppingItem(“Eggs”, 3.40),

new ShoppingItem(“Cookies”, 2.45)});

result = list.indexOf(“Bork”);

assertEquals(-1, result);

TestSuite.pass(2);

System.out.println(“\tTesting indexOf(null)”);

list = new ShoppingList(new ShoppingItem[]{

new ShoppingItem(“Milk”, 2.00),

new ShoppingItem(“Eggs”, 3.40),

new ShoppingItem(“Cookies”, 2.45)});

result = list.indexOf(null);

assertEquals(-1, result);

TestSuite.pass(1);

}

catch(AssertionError e){

TestSuite.fail(e.toString());

throw e;

}

catch(Exception e){

TestSuite.fail(e.toString());

throw e;

}

}

@Test

public void testListDeleteItemAt() {

try{

TestSuite.startTest(“Testing ShoppingList deleteItemAt()”, 5);

System.out.println(“\tTesting DeleteItemAt(0)”);

ShoppingList list = new ShoppingList(new ShoppingItem[]{

new ShoppingItem(“Milk”, 2.00),

new ShoppingItem(“Bread”, 3.20),

new ShoppingItem(“Eggs”, 3.40),

new ShoppingItem(“Sausages”, 4.50),

new ShoppingItem(“Cookies”, 2.45),

new ShoppingItem(“Cereal”, 2.90)});

ShoppingItem[] test = new ShoppingItem[]{

new ShoppingItem(“Bread”, 3.20),

new ShoppingItem(“Eggs”, 3.40),

new ShoppingItem(“Sausages”, 4.50),

new ShoppingItem(“Cookies”, 2.45),

new ShoppingItem(“Cereal”, 2.90),

null};

ShoppingItem item = new ShoppingItem(“Milk”, 2.00);

ShoppingItem result = list.deleteItemAt(0);

assertEquals(item, result);

assertArrayEquals(test, getShoppingItems(list));

TestSuite.pass(1);

System.out.println(“\tTesting DeleteItemAt(1)”);

list = new ShoppingList(new ShoppingItem[]{

new ShoppingItem(“Milk”, 2.00),

new ShoppingItem(“Bread”, 3.20),

new ShoppingItem(“Eggs”, 3.40),

new ShoppingItem(“Sausages”, 4.50),

new ShoppingItem(“Cookies”, 2.45),

new ShoppingItem(“Cereal”, 2.90)});

test = new ShoppingItem[]{

new ShoppingItem(“Milk”, 2.00),

new ShoppingItem(“Eggs”, 3.40),

new ShoppingItem(“Sausages”, 4.50),

new ShoppingItem(“Cookies”, 2.45),

new ShoppingItem(“Cereal”, 2.90),

null};

item = new ShoppingItem(“Bread”, 3.20);

result = list.deleteItemAt(1);

assertEquals(item, result);

assertArrayEquals(test, getShoppingItems(list));

TestSuite.pass(1);

System.out.println(“\tTesting DeleteItemAt(length-1)”);

list = new ShoppingList(new ShoppingItem[]{

new ShoppingItem(“Milk”, 2.00),

new ShoppingItem(“Bread”, 3.20),

new ShoppingItem(“Eggs”, 3.40),

new ShoppingItem(“Sausages”, 4.50),

new ShoppingItem(“Cookies”, 2.45),

new ShoppingItem(“Cereal”, 2.90)});

test = new ShoppingItem[]{

new ShoppingItem(“Milk”, 2.00),

new ShoppingItem(“Bread”, 3.20),

new ShoppingItem(“Eggs”, 3.40),

new ShoppingItem(“Sausages”, 4.50),

new ShoppingItem(“Cookies”, 2.45),

null};

item = new ShoppingItem(“Cereal”, 2.90);

result = list.deleteItemAt(5);

assertEquals(item, result);

assertArrayEquals(test, getShoppingItems(list));

TestSuite.pass(1);

System.out.println(“\tTesting DeleteItemAt(15)”);

list = new ShoppingList(new ShoppingItem[]{

new ShoppingItem(“Milk”, 2.00),

new ShoppingItem(“Bread”, 3.20),

new ShoppingItem(“Eggs”, 3.40),

new ShoppingItem(“Sausages”, 4.50),

new ShoppingItem(“Cookies”, 2.45),

new ShoppingItem(“Cereal”, 2.90)});

test = new ShoppingItem[]{

new ShoppingItem(“Milk”, 2.00),

new ShoppingItem(“Bread”, 3.20),

new ShoppingItem(“Eggs”, 3.40),

new ShoppingItem(“Sausages”, 4.50),

new ShoppingItem(“Cookies”, 2.45),

new ShoppingItem(“Cereal”, 2.90)};

item = null;

result = list.deleteItemAt(15);

assertEquals(item, result);

assertArrayEquals(test, getShoppingItems(list));

TestSuite.pass(1);

System.out.println(“\tTesting DeleteItemAt -1”);

list = new ShoppingList(new ShoppingItem[]{

new ShoppingItem(“Milk”, 2.00),

new ShoppingItem(“Bread”, 3.20),

new ShoppingItem(“Eggs”, 3.40),

new ShoppingItem(“Sausages”, 4.50),

new ShoppingItem(“Cookies”, 2.45),

new ShoppingItem(“Cereal”, 2.90)});

test = new ShoppingItem[]{

new ShoppingItem(“Milk”, 2.00),

new ShoppingItem(“Bread”, 3.20),

new ShoppingItem(“Eggs”, 3.40),

new ShoppingItem(“Sausages”, 4.50),

new ShoppingItem(“Cookies”, 2.45),

new ShoppingItem(“Cereal”, 2.90)};

item = null;

result = list.deleteItemAt(-1);

assertEquals(item, result);

assertArrayEquals(test, getShoppingItems(list));

TestSuite.pass(1);

}

catch(AssertionError e){

TestSuite.fail(e.toString());

throw e;

}

catch(Exception e){

TestSuite.fail(e.toString());

throw e;

}

}

@Test

public void testListDeleteItem() {

try{

TestSuite.startTest(“Testing ShoppingList deleteItem()”, 4);

System.out.println(“Array: ” + Arrays.toString(

new ShoppingItem[]{

new ShoppingItem(“Milk”, 2.00),

new ShoppingItem(“Eggs”, 3.40),

new ShoppingItem(“Cookies”, 2.45)}));

System.out.println(“\tTesting Delete Milk”);

ShoppingList list = new ShoppingList(new ShoppingItem[]{

new ShoppingItem(“Milk”, 2.00),

new ShoppingItem(“Eggs”, 3.40),

new ShoppingItem(“Cookies”, 2.45)});

ShoppingItem[] test = new ShoppingItem[]{

new ShoppingItem(“Eggs”, 3.40),

new ShoppingItem(“Cookies”, 2.45),

null};

ShoppingItem item = new ShoppingItem(“Milk”, 2.00);

ShoppingItem result = list.deleteItem(“Milk”);

assertEquals(item, result);

assertArrayEquals(test, getShoppingItems(list));

TestSuite.pass(2);

System.out.println(“\tTesting Delete Bork”);

list = new ShoppingList(new ShoppingItem[]{

new ShoppingItem(“Milk”, 2.00),

new ShoppingItem(“Eggs”, 3.40),

new ShoppingItem(“Cookies”, 2.45)});

test = new ShoppingItem[]{

new ShoppingItem(“Milk”, 2.00),

new ShoppingItem(“Eggs”, 3.40),

new ShoppingItem(“Cookies”, 2.45)};

item = null;

result = list.deleteItem(“Bork”);

assertEquals(item, result);

assertArrayEquals(test, getShoppingItems(list));

TestSuite.pass(1);

System.out.println(“\tTesting Delete null”);

list = new ShoppingList(new ShoppingItem[]{

new ShoppingItem(“Milk”, 2.00),

new ShoppingItem(“Eggs”, 3.40),

new ShoppingItem(“Cookies”, 2.45)});

test = new ShoppingItem[]{

new ShoppingItem(“Milk”, 2.00),

new ShoppingItem(“Eggs”, 3.40),

new ShoppingItem(“Cookies”, 2.45)};

item = null;

result = list.deleteItem(null);

assertEquals(item, result);

assertArrayEquals(test, getShoppingItems(list));

TestSuite.pass(1);

}

catch(AssertionError e){

TestSuite.fail(e.toString());

throw e;

}

catch(Exception e){

TestSuite.fail(e.toString());

throw e;

}

}

@Test

public void testListRemoveBlanks() throws InvocationTargetException,

IllegalAccessException, NoSuchMethodException {

try{

TestSuite.startTest(“Testing ShoppingList removeBlanks()”, 3);

System.out.println(“\tYou might want to make use of a temporary array…”);

ShoppingList list = new ShoppingList(new ShoppingItem[]{

new ShoppingItem(“Milk”, 2.00),

new ShoppingItem(“Bread”, 3.20),

new ShoppingItem(“Eggs”, 3.40),

new ShoppingItem(“Sausages”, 4.50),

new ShoppingItem(“Cookies”, 2.45),

new ShoppingItem(“Cereal”, 2.90)});

ShoppingItem[] items = getShoppingItems(list);

items[0] = null;

items[2] = null;

items[3] = null;

items[5] = null;

ShoppingItem[] test = new ShoppingItem[]{

new ShoppingItem(“Bread”, 3.20),

new ShoppingItem(“Cookies”, 2.45),

null, null, null, null};

Method removeBlanks = ShoppingList.class.getDeclaredMethod(“removeBlanks”);

removeBlanks.setAccessible(true);

removeBlanks.invoke(list);

System.out.println(“\t\tExpected:\t” + Arrays.toString(test));

System.out.println(“\t\tGot:\t\t” + Arrays.toString(getShoppingItems(list)));

assertArrayEquals(test, getShoppingItems(list));

TestSuite.pass(3);

}

catch(AssertionError e){

TestSuite.fail(e.toString());

throw e;

}

catch(Exception e){

TestSuite.fail(e.toString());

throw e;

}

}

@Test

public void testListSetSize() throws InvocationTargetException,

IllegalAccessException, NoSuchMethodException {

try{

TestSuite.startTest(“Testing ShoppingList setSize()”, 6);

System.out.println(“\tTesting setSize Above Count()”);

ShoppingList list = new ShoppingList(new ShoppingItem[]{

new ShoppingItem(“Milk”, 2.00),

new ShoppingItem(“Eggs”, 4.00),

new ShoppingItem(“Bacon”, 4.00)});

ShoppingItem[] test = new ShoppingItem[]{

new ShoppingItem(“Milk”, 2.00),

new ShoppingItem(“Eggs”, 4.00),

new ShoppingItem(“Bacon”, 4.00),

null, null, null };

Method setSize = ShoppingList.class.getDeclaredMethod(“setSize”, int.class);

setSize.setAccessible(true);

boolean result = (boolean)setSize.invoke(list, 6);

assertArrayEquals(test, getShoppingItems(list));

assertTrue(result);

TestSuite.pass(2);

System.out.println(“\tTesting SetSize Below Count()”);

list = new ShoppingList(new ShoppingItem[]{

new ShoppingItem(“Milk”, 2.00),

new ShoppingItem(“Eggs”, 4.00),

new ShoppingItem(“Bacon”, 4.00)});

test = new ShoppingItem[]{

new ShoppingItem(“Milk”, 2.00),

new ShoppingItem(“Eggs”, 4.00),

new ShoppingItem(“Bacon”, 4.00)};

result = (boolean)setSize.invoke(list, 2);

assertArrayEquals(test, getShoppingItems(list));

assertFalse(result);

TestSuite.pass(2);

System.out.println(“\tTesting SetSize to Count()”);

list = new ShoppingList(new ShoppingItem[]{

new ShoppingItem(“Milk”, 2.00),

new ShoppingItem(“Eggs”, 4.00),

new ShoppingItem(“Bacon”, 4.00)});

test = new ShoppingItem[]{

new ShoppingItem(“Milk”, 2.00),

new ShoppingItem(“Eggs”, 4.00),

new ShoppingItem(“Bacon”, 4.00)};

result = (boolean)setSize.invoke(list, 3);

assertArrayEquals(test, getShoppingItems(list));

assertTrue(result);

TestSuite.pass(2);

}

catch(AssertionError e){

TestSuite.fail(e.toString());

throw e;

}

catch(Exception e){

TestSuite.fail(e.toString());

throw e;

}

}

@Test

public void testListSortByCost() {

try{

TestSuite.startTest(“Testing ShoppingList sortByCost()”, 3);

System.out.println(“\tTesting Sorts Correctly”);

ShoppingList list = new ShoppingList(new ShoppingItem[]{

new ShoppingItem(“Solar Powered Keyboard”, 100.10),

new ShoppingItem(“Solid State Drive”, 200.10),

new ShoppingItem(“Logitech Headset”, 89.89),

new ShoppingItem(“Ouroboros”, 150.10),

new ShoppingItem(“Paddle Pop”, 1.10)});

ShoppingItem[] test = new ShoppingItem[]{

new ShoppingItem(“Paddle Pop”, 1.10),

new ShoppingItem(“Logitech Headset”, 89.89),

new ShoppingItem(“Solar Powered Keyboard”, 100.10),

new ShoppingItem(“Ouroboros”, 150.10),

new ShoppingItem(“Solid State Drive”, 200.10)};

list.sortByCost();

System.out.println(“\t\tExpected:\t” + Arrays.toString(test));

System.out.println(“\t\tGot:\t\t” + Arrays.toString(getShoppingItems(list)));

assertArrayEquals(test, getShoppingItems(list));

TestSuite.pass(3);

}

catch(AssertionError e){

TestSuite.fail(e.toString());

throw e;

}

catch(Exception e){

TestSuite.fail(e.toString());

throw e;

}

}

@Test

public void testListGetTotalCost() {

try{

TestSuite.startTest(“Testing ShoppingList getTotalCost()”, 2);

System.out.println(“\tTesting $100.10 + $200.10 + $150.10 + $1.10”);

ShoppingList list = new ShoppingList(new ShoppingItem[]{

new ShoppingItem(“Solar Powered Keyboard”, 100.10),

new ShoppingItem(“Solid State Drive”, 200.10),

new ShoppingItem(“Ouroboros”, 150.10),

new ShoppingItem(“Paddle Pop”, 1.10)});

double result = list.getTotalCost();

assertEquals(451.40, result, 0.00001);

TestSuite.pass(2);

}

catch(AssertionError e){

TestSuite.fail(e.toString());

throw e;

}

catch(Exception e){

TestSuite.fail(e.toString());

throw e;

}

}

/**

* Utility method that allows me to access the ShoppingList private instance variable, shoppingItems.

* @param list The ShoppingList object to reflect into.

* @return the private shoppingItems array from the argument.

*/

public static ShoppingItem[] getShoppingItems(ShoppingList list){

try{

Class<? extends ShoppingList> listReflect = list.getClass();

Field itemsReflect = listReflect.getDeclaredField(“shoppingItems”);

itemsReflect.setAccessible(true);

ShoppingItem[] items = (ShoppingItem[]) itemsReflect.get(list);

return items;

}

catch(Exception e){

System.err.println(“ERROR: Could not access private shoppingItems. Make sure the variable name is \”shoppingItems\” and it is private”);

e.printStackTrace();

}

return null;

}

/**

* Utility method to compare two ShoppingItem[].

* Unfortunately, .equals does not work for array types.

* @param arr1

* @param arr2

* @return true if the same, false otherwise.

*/

public static boolean arrayEquals(ShoppingItem[] arr1, ShoppingItem[] arr2){

if(arr1.length == arr2.length){

for(int i=0; i<arr1.length; i++){

if(arr1[i] != null && arr2[i] != null){

if(!arr1[i].equals(arr2[i])){

return false;

}

}

else if(arr1[i] == null && arr2[i] != arr1[i]){

return false;

}

}

return true;

}

return false;

}

}

TestSuite.java

package testing;

import org.junit.AfterClass;

import org.junit.BeforeClass;

import org.junit.runner.RunWith;

import org.junit.runners.Suite;

/**

* <p>Run this class as a JUnit Test to get a good indication of how

* many marks you should receive. This is not a guarantee of your

* actual final marks. Marks may be deducted for no / poor

* commenting, poor variable names, poor style, use of the keywords

* break / continue, plagiarism, incorrect submission, etc. Your

* teacher will be checking your code thoroughly in addition to

* running this TestDriver.</p>

*

* <p>This TestSuite will automatically run all Test methods in both

* ShoppingItemTest and ShoppingListTest. JUnit does not guarantee

* the order of these tests. The order is effectively random, but

* that should not affect the output.</p>

*

* <p>Each test case has some dependencies – the ShoppingItem

* variables must be private and called “name” and “price”; The

* ShoppingList variable must be private and called “shoppingItems”.

* Each test case contains a number of sub-tests. Each sub-test is

* dependent on the previous sub-test. If a sub-test fails, JUnit

* will flag the test case as failed and it will not check the next

* sub-test.</p>

*

* @author Michael

*

*/

@RunWith(Suite.class)

@Suite.SuiteClasses({ShoppingItemTest.class, ShoppingListTest.class})

public class TestSuite {

private static int methodMarks = 0;

private static int subMarks = 0;

private static int totalMarks = 0;

private static int maxMarks = 100;

public static void startTest(String testName, int marks){

subMarks = 0;

methodMarks = marks;

System.out.println(“\n” + testName);

}

/**

* Called to notify the user of a passed test.

* @param marks the number of marks the test is worth.

*/

public static void pass(int marks){

subMarks += marks;

System.out.println(“\t\tPASSED (+”+ marks +”)”);

}

/**

* Called to notify the user of a failed test.

* @param message The error message.

*/

public static void fail(String message){

System.out.println(“\t\tFAILED: ” + message);

}

public static void finishTest(){

System.out.println(“\tSubtotal: ” + subMarks + ” / ” +methodMarks);

totalMarks += subMarks;

}

@BeforeClass

public static void setUpBeforeClass(){

System.out.println(“Start Testing”);

}

@AfterClass

public static void tearDownAfterClass(){

System.out.println(“\nFinished Testing”);

System.out.println(“Total: ” + totalMarks + ” / ” +maxMarks+”\n”);

if(totalMarks < 10){

System.out.println(“Looks like you have just started.”);

System.out.println(“You can use this Test Driver to check your progress.”);

System.out.println(“Start by writing the constructors, then your own main method to see if they work.”);

System.out.println(“There may be problems that this Test Driver cannot help you with.”);

System.out.println(“Good luck programmer.”);

}

else if(totalMarks < 30){

System.out.println(“Looks like you are making progress. Keep going.”);

System.out.println(“”);

}

else if(totalMarks < 60){

System.out.println(“Great work. Keep it up!”);

System.out.println(“Make sure you’re checking for nulls.”);

System.out.println(“Check your arguments are valid and handle them appropriately.”);

}

else if(totalMarks < 90){

System.out.println(“Almost there!”);

System.out.println(“It’s the little things that cause the biggest problems isn’t it?”);

System.out.println(“Sometimes its best to take a step back and think about the algorithm.”);

System.out.println(“Maybe writing pseudocode or drawing a diagram would help.”);

}

else if(totalMarks < 100){

System.out.println(“So close!”);

System.out.println(“I know you can do this.”);

System.out.println(“Sometimes the problem isn’t where you expect it to be.”);

System.out.println(“Make sure you have your own main method to test specific bugs.”);

System.out.println(“Print things out, do some debugging!”);

}

else if(totalMarks == 100){

System.out.println(“Great job!”);

System.out.println(“Your code has passed all the tests executed by this test driver.”);

System.out.println(“Remember, this is not a guarantee of your final marks.”);

System.out.println(“Your supervisor may test for more cases than are outlined here.”);

System.out.println(“You may also be marked on code style.”);

System.out.println(“Make sure you have meaningful variable names and that you have left\n”

+ “a comment to describe any complicated code.”);

System.out.println(“Once you’ve done that, take a break.”);

System.out.println(“You’ve earned it! :D”);

}

else{

System.out.println(“Looks like you got more that 100%”);

System.out.println(“That’s incredible!”);

System.out.println(“Maybe you should be teaching programming.”);

System.out.println(“…instead of cheating.”);

}

}

}