In: Computer Science
I have to complete all //to do comments for the following code:
| /** | |
| * A ShoppingBasket holds zero or more Products and can provide information | |
| * about the Products. One can add Products to a ShoppingBasket during its | |
| * lifetime, reset the ShoppingBasket, create a copy which contains Products of | |
| * at least a certain value, and make various queries to the ShoppingBasket. | |
| * (Thus, the number of Products that will be stored by a ShoppingBasket object | |
| * is not yet known when the new object is created, and it may grow and shrink | |
| * over the lifetime of a ShoppingBasket object.) | |
| * | |
| * @author Carsten Fuhs | |
| */ | |
| public class ShoppingBasket { | |
| // TO DO instance variables | |
| /* Constructors */ | |
| /** | |
| * Constructs a new ShoppingBasket without any Products. | |
| */ | |
| public ShoppingBasket() { | |
| // TO DO | |
| } | |
| /** | |
| * Constructs a new ShoppingBasket containing the non-null Products in | |
| * products. The products array may be modified by the caller afterwards | |
| * without affecting this ShoppingBasket, and it will not be modified by | |
| * this constructor. | |
| * | |
| * @param products must not be null; non-null elements are added to the | |
| * constructed ShoppingBasket | |
| */ | |
| public ShoppingBasket(Product[] products) { | |
| // TO DO | |
| } | |
| /* Modifiers */ | |
| /** | |
| * Adds a Product e to this ShoppingBasket if e is not null; does not | |
| * modify this ShoppingBasket otherwise. Returns true if e is not null, | |
| * false otherwise. | |
| * | |
| * @param e an product to be added to this ShoppingBasket | |
| * @return true if e is not null, false otherwise | |
| */ | |
| public boolean add(Product e) { | |
| // TO DO | |
| return false; | |
| } | |
| /** | |
| * Adds all non-null Products in products to this ShoppingBasket. | |
| * | |
| * @param products contains the Product objects to be added to | |
| * this ShoppingBasket; must not be null (but may contain null) | |
| * @return true if at least one element of products is non-null; | |
| * false otherwise | |
| */ | |
| public boolean addAll(Product[] products) { | |
| // TO DO | |
| return false; | |
| } | |
| /** | |
| * Removes certain Products from this ShoppingBasket. Exactly those | |
| * Products are kept whose price in pence is greater than or equal to the | |
| * specified minimum price in pence. | |
| * | |
| * @param minProductPriceInPence the minimum price in pence for the | |
| * Products that are kept | |
| */ | |
| public void keepOnlyProductsWith(long minProductPriceInPence) { | |
| // TO DO | |
| } | |
| /* Accessors */ | |
| /** | |
| * Returns the number of non-null Products in this ShoppingBasket. | |
| * | |
| * @return the number of non-null Products in this ShoppingBasket | |
| */ |
***Please upvote/thumbsup if you liked the answer***
N.B I do not have the description of the Product class so cannot run the code
Screenshot of the Java code:-

Java code to copy:-
public class ShoppingBasket {
private int index = 0;
private Product[] products;
/* Constructors */
/**
* Constructs a new ShoppingBasket without any Products.
*/
public ShoppingBasket() {
//Initialising the instance variable to hold 100 products
//but initially all positions are empty
this.products = new Product[100];
}
/**
* Constructs a new ShoppingBasket containing the non-null Products in
* products. The products array may be modified by the caller afterwards
* without affecting this ShoppingBasket, and it will not be modified by
* this constructor.
*
* @param products must not be null; non-null elements are added to the
* constructed ShoppingBasket
*/
public ShoppingBasket(Product[] products) {
for (Product e:products)
{
if(e != null) {
this.products[index] = e;
index++;
}
}
}
}
/* Modifiers */
/**
* Adds a Product e to this ShoppingBasket if e is not null; does not
* modify this ShoppingBasket otherwise. Returns true if e is not null,
* false otherwise.
*
* @param e an product to be added to this ShoppingBasket
* @return true if e is not null, false otherwise
*/
public boolean add(Product e) {
if (e != null)
{
products[index] = e;
index++;
return true;
}
return false;
}
/**
* Adds all non-null Products in products to this ShoppingBasket.
*
* @param products contains the Product objects to be added to
* this ShoppingBasket; must not be null (but may contain null)
* @return true if at least one element of products is non-null;
* false otherwise
*/
public boolean addAll(Product[] products) {
boolean flag = false;
for (Product e:products) {
{
if (e != null)
{
this.products[index] = e;
index++;
flag = true;
}
}
}
return flag;
}
/**
* Removes certain Products from this ShoppingBasket. Exactly those
* Products are kept whose price in pence is greater than or equal to the
* specified minimum price in pence.
*
* @param minProductPriceInPence the minimum price in pence for the
* Products that are kept
*/
public void keepOnlyProductsWith(long minProductPriceInPence) {
for (Product e:this.products) {
{
if (e.getProductPriceInPence() >= minProductPriceInPence)
{
products[index] = e;
index++;
flag = true;
}
}
}
}
/* Accessors */
/**
* Returns the number of non-null Products in this ShoppingBasket.
*
* @return the number of non-null Products in this ShoppingBasket
*/
public Product[] getProducts() {
return products;
}
}