Skip to content

Instantly share code, notes, and snippets.

@vslala
Created September 9, 2016 09:31
Show Gist options
  • Select an option

  • Save vslala/b7c54fc6bfe265a87e5e3a5778f97a88 to your computer and use it in GitHub Desktop.

Select an option

Save vslala/b7c54fc6bfe265a87e5e3a5778f97a88 to your computer and use it in GitHub Desktop.
package collections.shoppingcart;
import java.util.ArrayList;
import java.util.List;
/**
*
* @author Varun Shrivastava
*/
class Cart {
List<Product> cartItems = new ArrayList<Product>();
public void addProductToCartByPID(int pid) {
Product product = getProductByProductID(pid);
addToCart(product);
}
private Product getProductByProductID(int pid) {
Product product = null;
List<Product> products = new Products().getProducts();
for (Product prod: products) {
if (prod.getPid() == pid) {
product = prod;
break;
}
}
return product;
}
private void addToCart(Product product) {
cartItems.add(product);
}
public void removeProductByPID(int pid) {
Product prod = getProductByProductID(pid);
cartItems.remove(prod);
}
void printCartItems() {
for (Product prod: cartItems) {
System.out.println(prod.getName());
}
}
}
@vslala

vslala commented Sep 9, 2016

Copy link
Copy Markdown
Author

This class deals with all the operations which are performed by the class.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment