Question

In: Computer Science

Complete the three empty methods (remove(), find(), and contains()) in the ShoppingListArrayList.java file. These methods are...

  1. Complete the three empty methods (remove(), find(), and contains()) in the ShoppingListArrayList.java file. These methods are already implemented in the ShoppingListArray class.
  2. Complete the ShoppingListArrayListTest.java (For many tests, we now just throw a false statement which will cause the test cases to fail. You need to complete them.)

ShoppingListArrayList

package Shopping;

import DataStructures.*;
import java.io.FileNotFoundException;
import java.util.ArrayList;
import java.util.Scanner;

/**
* @version Spring 2019
* @author Paul Franklin, Kyle Kiefer
*/
public class ShoppingListArrayList implements ShoppingListADT {

private ArrayList<Grocery> shoppingList;

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

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

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

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

shoppingList.add(entry);
}

/**
* Method to remove an entry.
*
* @param ent to be removed
* @return true when entry was removed
* @throws DataStructures.ElementNotFoundException
*/
@Override
public boolean remove(Grocery ent) {
  
// the boolean found describes whether or not we find the
// entry to remove
  
boolean found = false;

// search in the shoppingList, if find ent in the
// list remove it, set the value of `found'

// Return false if not found
return found;
}

/**
* Method to find an entry.
*
* @param index to find
* @return the entry if found
* @throws Exceptions.EmptyCollectionException
*/
@Override
public Grocery find(int index) throws IndexOutOfBoundsException,
EmptyCollectionException {
if (this.isEmpty()) {
throw new EmptyCollectionException("ECE - find");
}
  
throw new IndexOutOfBoundsException("ArrayList - find");
  
// check whether or not the input index number is legal
// for example, < 0 or falls outside of the size
  
// return the corresponding entry in the shoppingList
// need to change the return value
// return null;
}

/**
* Method to locate the index of an entry.
*
* @param ent to find the index
* @return the index of the entry
* @throws ElementNotFoundException if no entry was found
*/
@Override
public int indexOf(Grocery ent) throws ElementNotFoundException {
for (int i = 0; i < shoppingList.size(); i++) {
if (shoppingList.get(i).compareTo(ent) == 0) {
return i;
}
}

throw new ElementNotFoundException("indexOf");
}

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

// go through the shoppingList and try to find the
// item in the list. If found, return true.

return hasItem;
}

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

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

/**
* Returns a string representing this object.
*
* @return a string representation of this object
*/
@Override
public String toString() {
StringBuilder s = new StringBuilder();
s.append(String.format("%-25s", "NAME"));
s.append(String.format("%-18s", "CATEGORY"));
s.append(String.format("%-10s", "AISLE"));
s.append(String.format("%-10s", "QUANTITY"));
s.append(String.format("%-10s", "PRICE"));
s.append('\n');
s.append("------------------------------------------------------------"
+ "-------------");
s.append('\n');
for (int i = 0; i < shoppingList.size(); i++) {
s.append(String.format("%-25s", shoppingList.get(i).getName()));
s.append(String.format("%-18s", shoppingList.get(i).getCategory()));
s.append(String.format("%-10s", shoppingList.get(i).getAisle()));
s.append(String.format("%-10s", shoppingList.get(i).getQuantity()));
s.append(String.format("%-10s", shoppingList.get(i).getPrice()));
s.append('\n');
s.append("--------------------------------------------------------"
+ "-----------------");
s.append('\n');
}

return s.toString();
}

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

}

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

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

}

ShoppingListArrayListTest

package Shopping;

import DataStructures.*;
import org.junit.Test;
import static org.junit.Assert.*;
import org.junit.Before;

/**
* @version Spring 2019
* @author Paul Franklin, Kyle Kiefer
*/
public class ShoppingListArrayListTest {

private ShoppingListArrayList instance;

/**
* Initialize instance and entries
*/
@Before
public void setupTestCases() {
instance = new ShoppingListArrayList();
}

/**
* Test of add method, of class ShoppingArray.
*/
@Test
public void testAdd() {
  
assertTrue(0==1);
}

/**
* Test of remove method, of class ShoppingArrayList.
*/
@Test
public void testRemove() {
assertTrue(0==1);
}

/**
* Test of find method, of class ShoppingArrayList.
*/
@Test
public void testFind() {
assertTrue(0==1);
}

/**
* Test of indexOf method, of class ShoppingArrayList.
*/
@Test
public void testIndexOf() {
assertTrue(0==1);
}

/**
* Test of contains method, of class ShoppingArrayList.
*/
@Test
public void testContains() {
assertTrue(0==1);
}

/**
* Test of size method, of class ShoppingArrayList.
*/
@Test
public void testSize() {
Grocery entry1 = new Grocery("Mayo", "Dressing / Mayo", 1, 2.99f, 1);

assertEquals(0, instance.size());

instance.add(entry1);

// Test increment
assertEquals(1, instance.size());

assertTrue(instance.remove(entry1));

// Test decrement
assertEquals(0, instance.size());
}

/**
* Test of isEmpty method, of class ShoppingArrayList.
*/
@Test
public void testIsEmpty() {
Grocery entry1 = new Grocery("Mayo", "Dressing / Mayo", 1, 2.99f, 1);

// Test empty
assertTrue(instance.isEmpty());

instance.add(entry1);

// Test not empty
assertFalse(instance.isEmpty());
}

}

package Shopping;

import DataStructures.*;

/**
* @version Spring 2019
* @author Paul Franklin, Kyle Kiefer
*/
public interface ShoppingListADT {
  
/**
* Method to add a new entry. Only new entries can be added. Combines
* quantities if entry already exists.
*
* @param entry the entry to be added
*/
public void add(Grocery entry);
  
/**
* Method to remove an entry.
*
* @param ent to be removed
* @return true when entry was removed
*/
public boolean remove(Grocery ent);
  
/**
* Method to find an entry.
*
* @param index to find
* @return the entry if found
* @throws Exceptions.EmptyCollectionException
*/
public Grocery find(int index) throws IndexOutOfBoundsException,
EmptyCollectionException;
  
/**
* Method to locate the index of an entry.
*
* @param ent to find the index
* @return the index of the entry
* @throws ElementNotFoundException if no entry was found
*/
public int indexOf(Grocery ent) throws ElementNotFoundException;
  
/**
* Method to determine whether the object contains an entry.
*
* @param ent to find
* @return true if and only if the entry is found
*/
public boolean contains(Grocery ent);
  
/**
* Gets the size of the collection.
*
* @return the size of the collection
*/
public int size();
  
/**
* Gets whether the collection is empty.
*
* @return true if and only if the collection is empty
*/
public boolean isEmpty();
  
/**
* Returns a string representing this object.
*
* @return a string representation of this object
*/
@Override
public String toString();
}

Solutions

Expert Solution


// Method to remove an entry.

@Override
public boolean remove(Grocery ent) {
    
boolean found = shoppingList.contains(ent);

if(found == true){
shoppingList.remove(ent);
}
else{
System.out.println("Specified item to be removed is not in the shopping list.");
}
return found;
}

//Method to find an entry.

@Override
public Grocery find(int index) throws IndexOutOfBoundsException,
EmptyCollectionException {
if (this.isEmpty()) {
throw new EmptyCollectionException("ECE - find");
}
if (index < shoppingList.size(){
return shoppingList.get(index);
}
else{
return null;
}
}


// Method to determine whether the object contains an entry.

@Override
public boolean contains(Grocery ent) {
boolean hasItem = false;
int item_index = shoppingList.indexOf(ent);
if(item_index == -1){
System.out.println("Item not in list.");
hasitem = false;
}
else{
hasitem = true;
}
return hasItem;
}


Related Solutions

Write methods contains and remove for the BinarySearchTree class. Use methods find and delete to do...
Write methods contains and remove for the BinarySearchTree class. Use methods find and delete to do the work
please complete the header file that contains a class template for ADT Queue and complete all...
please complete the header file that contains a class template for ADT Queue and complete all the member functions in the class template. Submit the header file only, but please write a source file that tests all the member functions to make sure they are working correctly. queue.h #ifndef _QUEUE #define _QUEUE #include"Node.h" template<class ItemType> class Queue { private:    Node<ItemType> *backPtr;    Node<ItemType> *frontPtr; public:    Queue(); //Default constructor    Queue(const Queue<ItemType> &aQueue);    bool isEmpty() const;    bool...
Task 1: Remove Number Complete the function remove number such that given a list of integers...
Task 1: Remove Number Complete the function remove number such that given a list of integers and an integer n, the function removes every instance of n from the list. Remember that this function needs to modify the list, not return a new list. Task 2: Logged List The log2() function is one of an algorithm designer’s favourite functions. You’ll learn more about this later, but briefly – if your input size is 1048576 elements, but you only look at...
Get the avehighs.txt file from BB. The file contains monthly average temperatures from three different locations
  Get the avehighs.txt file from BB. The file contains monthly average temperatures from three different locations. The location ID is in the first column and monhtly average temperatures are in the next 12 columns. Write a script that will: data from file avehighs 432 33 37 42 45 53 72 82 79 66 55 46 41 777 29 33 41 46 52 66 77 88 68 55 48 39 567 55 62 68 72 75 79 83 89 85...
On Moodle, you will find a file labelled “Data for Question 4 Assignment 1”. It contains...
On Moodle, you will find a file labelled “Data for Question 4 Assignment 1”. It contains data on past students in this course. Under Midterm is information on whether past studentsgot an A grade (A−, A, A+) an F or D grade (D is a passing grade but most students need aC− for it to count towards their program) or Other (any grade in between). Under FinalExam is information on whether students got a D or F grade or anything...
Your primary task for this exercise is to complete header file by writing three functions with...
Your primary task for this exercise is to complete header file by writing three functions with its description below: removeAt function – to remove the item from the list at the position specified by location. Because the list elements are in no particular order (unsorted list), you could simple remove the element by swapping the last element of the list with the item to be removed and reducing the length of the list. insertAt function - to insert an item...
Inside the skeletal file you'll find a complete main function, as well as the function headers...
Inside the skeletal file you'll find a complete main function, as well as the function headers for the DispMatrix and InitMatrix functions. A 15x5 array of ints is allocated in the main function, without being initialized. Just to highlight the fact that the array will contain undefined values, a call is made to the DispMatrix function, which will display the 2D array's contents (you should just see a bunch of garbage values). The DispMatrix will receive as arguments the base...
Inside the skeletal file you'll find a complete main function, as well as the function headers...
Inside the skeletal file you'll find a complete main function, as well as the function headers for the DispMatrix and InitMatrix functions. A 15x5 array of ints is allocated in the main function, without being initialized. Just to highlight the fact that the array will contain undefined values, a call is made to the DispMatrix function, which will display the 2D array's contents (you should just see a bunch of garbage values). The DispMatrix will receive as arguments the base...
C programing language A file "data.txt" contains only integers. Write a code to find average of...
C programing language A file "data.txt" contains only integers. Write a code to find average of all values and print the average How would you use execlp function to execute "ps –e –a –l" command char *dt = "The five boxing wizards jump quickly"; write a program to count frequency of each letter, ignore case. Print the letter and frequency of each letter. // 1A: . Ask the user to enter a password string, store it in pass. Password should...
An HTML file is a text file that contains text to be displayed in a browser...
An HTML file is a text file that contains text to be displayed in a browser and __________ to be interpreted (read) by the browser formatting and styling the document Both C++ and javascript are computer programing languages; what are their differences? What HTML tag can let you insert javascript in to document Which attributes of the <script> tag tells the brorwser what kind of scripting language is insterted? (give an example) in the javascript section of the HTML file,...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT