2

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

Isabel Lopez
  • 101
  • 6
  • 1
    Do you have any more context-info about `declared as type List (static)` ? What is meant by or purpose of `static` ? Also, what is the benefit, use-case or requirement for the `getCart()` operation, especially its return type as array of the class itself ? Without this clarification the solution may not be practical or missing some important criteria (not given here) ️ – hc_dev Nov 23 '21 at 23:15
  • Thanks for your help! It turned out that the UML was too generic and need to be interpreted, the correct sentence was +getCart(): List which now makes sense – Isabel Lopez Nov 26 '21 at 16:47
  • 1
    Easier "thank you" and signaling to community is: [vote or accept](https://stackoverflow.com/help/someone-answers). When adding valuable interpretation or a _sentence_ to your question, do it by [edit] (not comment). – hc_dev Nov 27 '21 at 19:20
  • Thanks again, i didn't have any idea how to do that! – Isabel Lopez Nov 28 '21 at 20:29

2 Answers2

1

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);
}
Rhett Harrison
  • 430
  • 4
  • 19
  • Please don't post code-only answers, but instead explain why this code is a (step towards a) solution to OP's problem. And this doesn't compile, as `List` cannot be converted to `ShoppingCart[]`. – MC Emperor Nov 23 '21 at 22:27
1

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()).

hc_dev
  • 8,389
  • 1
  • 26
  • 38
  • 1
    Thank you for your help. It's really useful and i learned a lot from the links you gave me. It turned out the UML was too generic, it was changed into +getCart(): List which makes more sense – Isabel Lopez Nov 26 '21 at 16:48
  • @GabrielRomero, Thanks for your feedback, that was my intention by adding links for elaboration ️ I updated the changed UML operation. Now the member-name `cart` is only thing left to improve on sense ️ – hc_dev Nov 27 '21 at 20:09