Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

1 package Shopping; 2 3 import DataStructures.*; 4 import java.io.FileNotFoundException; 5 import java.util.ArrayList; 6 import java.util.Scanner; 7 8 /** 9 * @version Fall 2019 0

image text in transcribedimage text in transcribedimage text in transcribedimage text in transcribedimage text in transcribedimage text in transcribedimage text in transcribedimage text in transcribedimage text in transcribedimage text in transcribedimage text in transcribedimage text in transcribedimage text in transcribedimage text in transcribedimage text in transcribedimage text in transcribedimage text in transcribedimage text in transcribedimage text in transcribedimage text in transcribed

image text in transcribed

1 package Shopping; 2 3 import DataStructures.*; 4 import java.io.FileNotFoundException; 5 import java.util.ArrayList; 6 import java.util.Scanner; 7 8 /** 9 * @version Fall 2019 0 * @author ITCS 2214 1 */ 2 public class ShoppingListArrayList implements ShoppingListADT { private ArrayList shoppingList; L l /*** * Default constructor of ShoppingArray object. */ public ShoppingListArrayList() { this.shoppingList = new ArrayList(); } 6 7 8 9 0 1 2 3 p 4 5 6 7 8 /** * Constructor of ShoppingArray object that parses from a file. * 9 * @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); } 0 1 2 4 /** * Method to add a new entry. Only new entries can be added. Combines * quantities if entry already exists. 6 8 9 * @param entry the entry to be added */ 38 39 40 O 42 * @param entry the entry to be added */ @Override public void add (Grocery entry) { if (entry == null) { return; } 43 44 // Check if this item already exists if (this. contains (entry)) { //Merge the quantity of new entry into existing entry combineQuantity(entry); return; } 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 shoppingList.add(entry); } /** * Method to remove an entry. * @param ent to be removed * @return true when entry was removed * athrows DataStructures. ElementNotFoundException */ @Override public boolean remove (Grocery ent) { 62 63 65 66 67 // the boolean found describes whether or not we find the // entry to remove boolean found = false; 68 69 70 71 72 73 74 // TODO Search the parameter variable ent in the shoppingList; // if it is found, please remove it and set the value of 'found'. // It is okay to directly use methods from the Java Build-in // ArrayList class. // Return false if not found return found; 76 } return found; } 75 76 77 78 79 80 /** * Method to find an entry. * 81 82 84 85 * @param index to find * @return the entry if found * @throws Exceptions. EmptycollectionException */ @Override public Grocery find(int index) throws IndexOutOfBoundsException, Empty CollectionException { if (this.isEmpty()) { throw new EmptyCollectionException ("ECE - find"); } VA 88 89 90 91 92 93 94 95 // TODO check whether or not the given index is legal // for example, the given index is less than 0 or falls outside of // the size. If it is not legal, throw an IndexOutOfBounds Exception // return the corresponding entry in the shoppingList // need to change the return value // return null; return (Grocery) this.shoppingList.get(index); 96 97 98 99 } /** * Method to locate the index of an entry. * 100 01 L02 L03 104 L05 106 107 108 * @param ent to find the index * @return the index of the entry * @throws ElementNotFoundException if no entry was found 10 _11 _12 _13 @Override public int indexOf(Grocery ent) throws ElementNotFoundException { if (ent != null) { for (int i = 0; i > ShoppingListAD +void add(Grocery entry) + boolean remove(Grocery ent) Grocery find(int index) + int indexOf(Grocery ent) +boolean contains(Grocery ent) +inf size) boolean IsEmpty + String toStringo > 0.. +Grocery(String namo, String category) +Grocery(String name, String category, int aisle,float price, int quantity + String getName() +void setName(String name) String getCategory +void setCategory(String category) +int getAisle) +void setAisle(int aisle) float getPrice +void setPrice(float price) +int getQuantity +void setQuantity (int quantity) + String to String +Int compare To(Grocery t) ShoppingListArrayLis -ArrayList> 1.1 shoppingList ShoppingListArrayListo ShoppingListArrayList(String filename) +void add(Grocery entry) boolean remove(Grocery ent) Grocery find(int index) + int indexOf(Grocery ent) boolean contains(Grocery ent) Int size) + boolean isEmptyo String toString - void combine Quantity (Grocery entry) - void scanFile(String filename) 1..1 ShoppingListArra -finalint.DEFAULT_SIZE - Groceryl shoppingList - int size Shopping Simulatio + static void main(String[] args) + ShoppingListArray ShoppingListArray(String filename) Grocery find(int index) +vold add(Grocery entry) + boolean remove(Grocery ent) +int indexOf(Grocery ent) + boolean contains(Grocery ent) int size - boolean IsEmpty + String toStringo - void combine Quantity (Grocery entry - void scanFile(String filename) -vold expandCapacity What you must test Think of your shopping list as having "states. When your program starts and you create the shopping list, it is in its initial state. At this state, the shopping list has nothing in it, so size should be 0. It should also not allow you to remove any element (since it is empty). Once you start adding elements, then the shopping list is not empty anymore and with every unique element you add, the shopping list's size should reflect one more and you should be able to find the element within it. No null grocery reference is allowed in the shopping list, and there is no change to the shopping list if an attempt is made to add a null grocery item reference. As you remove elements, the shopping list size should decrease and you will no longer be able to find elements that have been removed since the whole entry is removed no matter how many quantities exist. In the function that you implement, make sure that you can handle special cases. For example, can you remove an entry that does not exist? How does your code handle an attempt to remove an item from an empty list? Does the find method detect and properly process an invalid index? A test file for the ArrayList implementation is included in the project. Some method tests are provided, but you must implement the tests that are empty. You can run your tests by right- clicking on either the class or test file in NetBeans and selecting 'Test File'. Code coverage note: Web-cat will run the JUnit tests that you submit and will count the percentage of lines of code in your classes that get executed as a result of your test cases. So, you want to try to write test cases that cause all lines of code in all branches of your code, to be executed. (So, if you have an if-else statement, write code in your test case that runs the branch of the if statement when the expression is true, and write other lines of code in your test case that will cause the else clause part of the if statement to be run). Shopping Simulation note: The project contains a class called Shopping Simulation.java. This simulation class also has the main method that executes when you run the project. You can run the simulation using the ShoppingListArray class or the ShoppingListArrayList class, depending on which line of code you leave uncommented in the main method. It is important for you to understand that this simulation class is there to give you a sense for how the other classes could be used in a real application. When you get everything working, this simulation will run. But just because this simulation runs, this does NOT mean your code is working perfectly. This simulation does not thoroughly test your code at all! You need to write good test cases to make sure your code is working. Also, note that you don't need to write a test file to test this simulation. Submission Submit your assignment to Web-Cat (more details to follow). Grading This assignment is worth about 9% of your final grade in this class. 1. TA Grade on quality of your JavaDoc and algorithmic clarity (20%) 2. Web-cat style testing* (20%) 3. Web-cat unit testing, code coverage, and instructor generated test cases (60%) 1 package Shopping; 2 3 import DataStructures.*; 4 import java.io.FileNotFoundException; 5 import java.util.ArrayList; 6 import java.util.Scanner; 7 8 /** 9 * @version Fall 2019 0 * @author ITCS 2214 1 */ 2 public class ShoppingListArrayList implements ShoppingListADT { private ArrayList shoppingList; L l /*** * Default constructor of ShoppingArray object. */ public ShoppingListArrayList() { this.shoppingList = new ArrayList(); } 6 7 8 9 0 1 2 3 p 4 5 6 7 8 /** * Constructor of ShoppingArray object that parses from a file. * 9 * @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); } 0 1 2 4 /** * Method to add a new entry. Only new entries can be added. Combines * quantities if entry already exists. 6 8 9 * @param entry the entry to be added */ 38 39 40 O 42 * @param entry the entry to be added */ @Override public void add (Grocery entry) { if (entry == null) { return; } 43 44 // Check if this item already exists if (this. contains (entry)) { //Merge the quantity of new entry into existing entry combineQuantity(entry); return; } 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 shoppingList.add(entry); } /** * Method to remove an entry. * @param ent to be removed * @return true when entry was removed * athrows DataStructures. ElementNotFoundException */ @Override public boolean remove (Grocery ent) { 62 63 65 66 67 // the boolean found describes whether or not we find the // entry to remove boolean found = false; 68 69 70 71 72 73 74 // TODO Search the parameter variable ent in the shoppingList; // if it is found, please remove it and set the value of 'found'. // It is okay to directly use methods from the Java Build-in // ArrayList class. // Return false if not found return found; 76 } return found; } 75 76 77 78 79 80 /** * Method to find an entry. * 81 82 84 85 * @param index to find * @return the entry if found * @throws Exceptions. EmptycollectionException */ @Override public Grocery find(int index) throws IndexOutOfBoundsException, Empty CollectionException { if (this.isEmpty()) { throw new EmptyCollectionException ("ECE - find"); } VA 88 89 90 91 92 93 94 95 // TODO check whether or not the given index is legal // for example, the given index is less than 0 or falls outside of // the size. If it is not legal, throw an IndexOutOfBounds Exception // return the corresponding entry in the shoppingList // need to change the return value // return null; return (Grocery) this.shoppingList.get(index); 96 97 98 99 } /** * Method to locate the index of an entry. * 100 01 L02 L03 104 L05 106 107 108 * @param ent to find the index * @return the index of the entry * @throws ElementNotFoundException if no entry was found 10 _11 _12 _13 @Override public int indexOf(Grocery ent) throws ElementNotFoundException { if (ent != null) { for (int i = 0; i > ShoppingListAD +void add(Grocery entry) + boolean remove(Grocery ent) Grocery find(int index) + int indexOf(Grocery ent) +boolean contains(Grocery ent) +inf size) boolean IsEmpty + String toStringo > 0.. +Grocery(String namo, String category) +Grocery(String name, String category, int aisle,float price, int quantity + String getName() +void setName(String name) String getCategory +void setCategory(String category) +int getAisle) +void setAisle(int aisle) float getPrice +void setPrice(float price) +int getQuantity +void setQuantity (int quantity) + String to String +Int compare To(Grocery t) ShoppingListArrayLis -ArrayList> 1.1 shoppingList ShoppingListArrayListo ShoppingListArrayList(String filename) +void add(Grocery entry) boolean remove(Grocery ent) Grocery find(int index) + int indexOf(Grocery ent) boolean contains(Grocery ent) Int size) + boolean isEmptyo String toString - void combine Quantity (Grocery entry) - void scanFile(String filename) 1..1 ShoppingListArra -finalint.DEFAULT_SIZE - Groceryl shoppingList - int size Shopping Simulatio + static void main(String[] args) + ShoppingListArray ShoppingListArray(String filename) Grocery find(int index) +vold add(Grocery entry) + boolean remove(Grocery ent) +int indexOf(Grocery ent) + boolean contains(Grocery ent) int size - boolean IsEmpty + String toStringo - void combine Quantity (Grocery entry - void scanFile(String filename) -vold expandCapacity What you must test Think of your shopping list as having "states. When your program starts and you create the shopping list, it is in its initial state. At this state, the shopping list has nothing in it, so size should be 0. It should also not allow you to remove any element (since it is empty). Once you start adding elements, then the shopping list is not empty anymore and with every unique element you add, the shopping list's size should reflect one more and you should be able to find the element within it. No null grocery reference is allowed in the shopping list, and there is no change to the shopping list if an attempt is made to add a null grocery item reference. As you remove elements, the shopping list size should decrease and you will no longer be able to find elements that have been removed since the whole entry is removed no matter how many quantities exist. In the function that you implement, make sure that you can handle special cases. For example, can you remove an entry that does not exist? How does your code handle an attempt to remove an item from an empty list? Does the find method detect and properly process an invalid index? A test file for the ArrayList implementation is included in the project. Some method tests are provided, but you must implement the tests that are empty. You can run your tests by right- clicking on either the class or test file in NetBeans and selecting 'Test File'. Code coverage note: Web-cat will run the JUnit tests that you submit and will count the percentage of lines of code in your classes that get executed as a result of your test cases. So, you want to try to write test cases that cause all lines of code in all branches of your code, to be executed. (So, if you have an if-else statement, write code in your test case that runs the branch of the if statement when the expression is true, and write other lines of code in your test case that will cause the else clause part of the if statement to be run). Shopping Simulation note: The project contains a class called Shopping Simulation.java. This simulation class also has the main method that executes when you run the project. You can run the simulation using the ShoppingListArray class or the ShoppingListArrayList class, depending on which line of code you leave uncommented in the main method. It is important for you to understand that this simulation class is there to give you a sense for how the other classes could be used in a real application. When you get everything working, this simulation will run. But just because this simulation runs, this does NOT mean your code is working perfectly. This simulation does not thoroughly test your code at all! You need to write good test cases to make sure your code is working. Also, note that you don't need to write a test file to test this simulation. Submission Submit your assignment to Web-Cat (more details to follow). Grading This assignment is worth about 9% of your final grade in this class. 1. TA Grade on quality of your JavaDoc and algorithmic clarity (20%) 2. Web-cat style testing* (20%) 3. Web-cat unit testing, code coverage, and instructor generated test cases (60%)

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

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

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

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

Get Started

Recommended Textbook for

Development Of Knowledge Framework For Affective Content Analysis

Authors: Swarnangini Sinha

1st Edition

B0CQJ13WZ1, 979-8223977490

More Books

Students also viewed these Databases questions

Question

Find the derivative. f(x) 8 3 4 mix X O 4 x32 4 x32 3 -4x - x2

Answered: 1 week ago

Question

Types of Interpersonal Relationships?

Answered: 1 week ago

Question

Self-Disclosure and Interpersonal Relationships?

Answered: 1 week ago