0

I have multiple Client objects. Each client object has an ArrayList called shoppingCart. These ArrayLists are populated by objects of the class Product, made by me. These products can be of class Shirt, Jeans or Skirt (all inherit Product). I want to print what each Client has on his shoppingCart as Strings. For example, if a Client has a Shirt and a skirt object in his shoppingCart, the console would print: "Contents of cart: Shirt, Skirt" How can i accomplish this?

BenBoozle
  • 5
  • 1
  • Does this answer your question? [Best way to convert an ArrayList to a string](https://stackoverflow.com/questions/599161/best-way-to-convert-an-arraylist-to-a-string) – user Sep 30 '20 at 17:34
  • Please show us the code you're having problems with. This isn't a homework-writing service, but we're here to help with specific programming problems. – NomadMaker Sep 30 '20 at 18:33

3 Answers3

1

Sample Code:

public enum ProductType {
    PANT,
    SHIRT,
    SKIRT,
    TSHIRT,
}

public class Product {
    private ProductType productType;

    public Product( ProductType productType) {
        this.productType = productType;
    }

    public ProductType getProductType() {
        return productType;
    }
}

public class Pant extends Product {
    private int size;

    public Pant(ProductType productType, int size) {
        super(productType);
        this.size = size;
    }

}

public class Shirt extends Product {
    private int size;

    public Shirt(ProductType productType, int size) {
        super(productType);
        this.size = size;
    }

}

public class App {
    public static void main(String[] args) {
        List<Product> cart = List.of(new Pant(ProductType.PANT, 100),
                new Pant(ProductType.PANT, 101),
                new Shirt(ProductType.SHIRT, 42));

        System.out.println("Contents of cart:  " +
                cart.stream()
                .map(Product::getProductType)
                .collect(Collectors.toList()));

    }


}

Output:

Contents of cart:  [PANT, PANT, SHIRT]
soumya-kole
  • 1,111
  • 7
  • 18
0

You could do something like this. An example of Polymorphism:

abstract class Product{
  abstract String getType();
}

class Shirt extends Product {
  String getType() {
    return "Shirt";
  }
}

class Skirt extends Product {
  String getType() {
    return "Skirt";
  }
}

When you iterate over your shoppingCart and print the type you get the corresponding type.

for(Product p : shoppingCart) {
  System.out.println(p.getType);
}
Kuldeep Jain
  • 8,409
  • 8
  • 48
  • 73
0

I think this can achieved by using instanceof operator. You can try doing something like this:

public List<String> getContentsOfCart(List<Product> products) {
  List<String> result = new ArrayList<>();
  for (Product p : products) {
    if (p instanceof Skirt) {
      result.add("Skirt");
    } else if (p instanceof Shirt) {
      result.add("Shirt");
    } else if (p instancef Jeans) {
      result.add("Jeans");
    }
  }
  return result;
}

You can then print this list like this:

System.out.println("Contents of cart: " + Strings.join(result, ","));
Rohan Kumar
  • 5,427
  • 8
  • 25
  • 40