[go: up one dir, main page]

0% found this document useful (0 votes)
2 views4 pages

Define Cart and Product Classes

Uploaded by

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

Define Cart and Product Classes

Uploaded by

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

Define Cart and Product Classes: Make sure your Product

class has the necessary details and Cart class can return these
details.
java
Copy code
import java.util.ArrayList;
import java.util.List;

public class Cart {


private List<Product> products;

public Cart() {
this.products = new ArrayList<>();
}

public void addProduct(Product product) {


products.add(product);
}

public void removeProduct(Product product) {


products.remove(product);
}

public List<Product> getProducts() {


return products;
}

public boolean isEmpty() {


return products.isEmpty();
}
}

public class Product {


private int id;
private String name;
private double price;

public Product(int id, String name, double price) {


this.id = id;
this.name = name;
this.price = price;
}

@Override
public String toString() {
return "Product ID: " + id + ", Name: " + name
+ ", Price: $" + price;
}
}

Update CartService to Handle Empty and Non-Empty


Carts: Modify the CartService class to include product details
in the message when the cart is not empty.
java
Copy code
public class CartService {

public String getCartStatus(Cart cart) {


if (cart.isEmpty()) {
return "Cart is empty.";
} else {
StringBuilder details = new
StringBuilder("You have the following item(s) in your
cart:\n");
for (Product product : cart.getProducts())
{
details.append(product.toString()).append("\n");
}
return details.toString();
}
}
}

Here’s how you would use these classes in your application:


java
Copy code
public class ECommerceApp {
public static void main(String[] args) {
// Create a new cart
Cart cart = new Cart();

// Add products to the cart


cart.addProduct(new Product(1, "Laptop",
999.99));
cart.addProduct(new Product(2, "Smartphone",
499.99));

// Create a CartService to get the cart status


CartService cartService = new CartService();

// Check and print cart status


String cartStatus =
cartService.getCartStatus(cart);
System.out.println(cartStatus);

// Remove all products to test the empty case


cart.getProducts().clear();
cartStatus = cartService.getCartStatus(cart);
System.out.println(cartStatus);
}
}
 Product Details: The Product class has a toString method
to provide a string representation of product details.
 Cart Management: The Cart class manages product
additions and removals and has an isEmpty method to check if
the cart is empty.
 Cart Status: The CartService class provides a method to
generate a message based on whether the cart is empty or not,
including product details if it is not empty.

You might also like