0% found this document useful (0 votes)
12 views1 page

Bundle

The document defines a Java class named 'Bundle' that implements the 'Product' interface. It allows for the addition of multiple products and calculates the total price of the bundle. The class also includes a method to return a string representation of the bundle and its total price.

Uploaded by

yoshiyashasganil
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
12 views1 page

Bundle

The document defines a Java class named 'Bundle' that implements the 'Product' interface. It allows for the addition of multiple products and calculates the total price of the bundle. The class also includes a method to return a string representation of the bundle and its total price.

Uploaded by

yoshiyashasganil
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
You are on page 1/ 1

import java.util.

ArrayList;
import java.util.List;

public class Bundle implements Product {


private List<Product> products;

public Bundle() {
products = new ArrayList<>();
}

// Adds a product to this bundle.


public void add(Product product) {
products.add(product);
}

public double getPrice() {


double total = 0;
for (Product p : products) {
total += p.getPrice();
}
return total;
}

@Override
public String toString() {
StringBuilder sb = new StringBuilder("Bundle: ");
for (Product p : products) {
sb.append(p.toString()).append(", ");
}
// Remove the last comma and space if any product exists.
if (!products.isEmpty()) {
sb.setLength(sb.length() - 2);
}
sb.append(String.format(" (Total Price: %.2f)", getPrice()));
return sb.toString();
}
}

You might also like