Isabel Lopez
Isabel Lopez

Reputation: 111

How to build this association in Java

I have to code this UML that is linking 2 classes: Product (not visible) and ShoppingCart

enter image description here

I've declared the 2 variables clientName and total and have started to create the constructor. In the formulation of the exercise it's mentioned that "cart" has to be declared as type List (static) and will be built as ArrayList (dynamic) using the default's constructor.

I am not sure how to declare it, and I'm also not sure how to create the getCart() method. I don't know where the array ShoppingCart[] comes from as I have not declared it.

This is the code I've created so far:

import java.util.ArrayList; import java.util.List;

public class ShoppingCart {

private String clientName;
private double total;
private List<String> cart;

public ShoppingCart(String clientName){
    setClientName(clientName);
    List<String> cart = new ArrayList<String>();

}

public ShoppingCart[] getCart(){
    return cart;
}

Thanks in advance

Upvotes: 2

Views: 297

Answers (2)

hc_dev
hc_dev

Reputation: 9418

UML symbols explained

To explain the visual notation (UML) and common symbols shown in the given diagram:

  1. the concrete class Product (box clipped at top)
  2. the concrete class ShoppingCart (box at bottom)
  3. the aggregation association between both: a directed connection starting with white-filled diamond from ShoppingCart ending with open arrow at Product.

The UML diagram reads: a ShoppingCart is composed of 1 Product. The association has a role denoted by label -cart. A single Product (denoted by cardinality next to arrow: 1) may be contained in 0 or many (denoted by cardinality next to diamond: *) ShoppingCarts. But a ShoppingCart has access (denoted by direction of arrow) to a Product, not vice-versa.

See also:

Implemented in Java

class Product {
}

class ShoppingCart {
    private List<Product> cart;  // the role interpreted as private member because minus before

    public ShoppingCart(String clientName){
        // omitted initializations
        this.cart = new ArrayList<>();
    }

    public ShoppingCart[] getCart(){
        final ShoppingCart[] self = new ShoppingCart[1]; // with one element
        self[0] = this; // only a self-reference, i.e. this object
        return self; // purpose unclear, but as in UML: an array of type ShoppingCart
    }

    public boolean add(Product product) {
        return this.cart.add(product);
    }

    public boolean remove(Product product) {
        return this.cart.remove(product);
    }
}

I don't understand the sense, usefulness or purpose of getter getCart and its return type (array of ShoppingCarts), but at least we could return a single-element array containing a self-reference.

See also: Java array of constants


Update

From the author's comment the question's UML changed:

UML was too generic, it was changed into +getCart(): List <Product> which makes more sense

So the accessor operation (getter) should be adapted to reflect this:

    public List<Product> getCart() {  // more intent-revealing to name it: getProducts()
        return this.cart; // consider a more scoped/expressive name: this.products
    }

Note on Naming: My comments to improve the property name from cart to products does not only express the type List<Products> but conveys the contents (collection of products) and avoids duplicating the context (e.g. ShopingCart.prodcuts instead ShoppingCart.cart). For callers from outside the cart is represented as products, but from inside the ShoppingCart contains/has products (aggregation drawn in UML). Members should be interpreted and named to reflect their purpose from within their scope.

On getter naming convention: In Java (especially for beans) it is conventional to name getters either after the property they return (pattern get<property>()) , or for their return-value (e.g. predicate pattern is<Attribute>() like isCheckedOut()).

Upvotes: 1

Rhett Harrison
Rhett Harrison

Reputation: 432

Here is a general implementation of the UML diagram. The ShoppingCart[] array is not actually instantiated. In the UML diagram that is stating what type that method should return. I changed it in the code to be a List because that is what was in your code originally.

private String clientName;
private double total;
private List<String> cart;

public ShoppingCart(String clientName){
    this.clientName = clientName;
    this.cart = new ArrayList<>();

}

public String getClientName(){
return this.clientName;
}

public void setClientName(String clientName){
this.clientName = clientName;
}

public double getTotal(){
return this.total;
}

public void setTotal(Double total){
this.total = total;
}


public List<String> getCart(){
    return this.cart;
}

public boolean add(Product product){
this.cart.add(product);
return this.cart.contains(product);
}

public boolean removeProduct(Product product){
return this.cart.remove(product);
}

Upvotes: 1

Related Questions