这是代码
package products;
import Money.*;
import java.util.Arrays;
import java.util.*;
public class ProductList
{ /** Array, which contains the products in the quantity
* The array fulfills the following invariant, i.e. the following
* Condition applies after each completed constructor or
* Method call:
*
* forall p in 0..products.length - 1: products [p]! = null
*/
private ArrayList<Product> ProductList;
public ProductList() {
// Erzeuge Array der Laenge Null
ProductList = new ArrayList<>();
}
/**
* Copy constructor that creates a deep copy of the other product list.
* The created ProductList instance contains copies of the same products and
* with the same number as contained in other. One uses for this
* the copy constructor from the Product class. This constructor is only for testing
* required.
*/
public ProductList(ProductList other) {
ProductList = new ArrayList<Product>();
for(Product p:ProductList){
ProductList.add(new Product(p));
}
}
/**
* Search for a product identified by the product name name
* is already in the list. If so, give the reference to the product
* back, otherwise zero.
*/
public Product findProduct(String name) {
for (Product i:ProductList) {
ProductList.add(new Product(i));
if ( i.getName().equals(name) ) {
return i; }
}
return null;
}
/**
* Add a product to the list in the required number.
* A distinction must be made between the following cases:
* 1. If the product is already in the list (use the method
* findProduct for testing), only the number of pieces of the product is increased.
* 2. Otherwise the array is copied using the Arrays.copyOf method
* and extended by 1,
* and the product is inserted as the last element of the array.
* The method must also cover the case where the product to be inserted
* prod.getNumItems ()> 1 is available.
* Prod must be registered in full in the product list.
*/
public void addProd(Product prod) {
Product prodInList = findProduct(prod.getName());
if ( prodInList != null ) {
prodInList.add(prod.getNumItems());
}
else {
ArrayList<Product> ProductList= new ArrayList(this.ProductList.size()+1);
ProductList.add(ProductList.size()-1,prod);
}
}
/**
* Add all products from a given ProductList instance prods
* in your own product list. For this the method add (Product prod)
* used.
*/
public void add(ProductList prods) {
for (Product p: ProductList) {
new ProductList(prods);
}
}
/**
public Product getProducts() {
return product;
}
*/
@Override
public String toString() {
String prodString = new String();
for (Product p:ProductList) {
prodString += p.toString() + System.lineSeparator();
}
return prodString;
}
public boolean isEmpty() {
return (ProductList.size() == 0);
}
public int size() {
return ProductList.size();
}
public boolean isEqual(ProductList other) {
// Gleich, wenn die Listen dieselben Objekte sind
if ( this == other ) return true;
// Ungleich, wenn other == null ist
if ( other == null ) return false;
// Ungleich, wenn die Arrays unterschiedlich lang sind
if ( other.size() != size() ) return false;
// Gleich, wenn beide Arrays die Laenge 0 haben (gleich lang sind
// sie ja schon).
if ( size() == 0 ) return true;
// Jetzt sind beide Arrays gleich lang und nicht leer.
// Dann sind sie gleich, wenn alle Produkte unseres Arrays
// mit derselben Stueckzahl im anderen Array enthalten sind.
for (Product p : ProductList){
Product otherProd = other.findProduct(p.getName());
// Ungleich, wenn Produkt nicht im anderen Array vorhanden
if ( otherProd == null) return false;
// Ungleich, wenn Stueckzahl fuer das geleiche Produkt unterschiedlich ist
if ( otherProd.getNumItems() != p.getNumItems() ) return false;
}
// Gleich: Alle Pruefungen wurden bestanden
return true;
}
/**
* Calculate the gross total value of the products,
* which are included in the product list.
* Also take into account the number of pieces included
* Products.
*/
public Euro getGrossValue() {
Euro total = new Euro(0,0);
if ( isEmpty() ) return total;
for ( Product p: ProductList) {
Product prod = p;
total = total.add(prod.getPrice().getFullPrice().mult(prod.getNumItems()));
}
return total;
}
public ArrayList<Product> getProducts(){
return ProductList;
}
}