Created
September 9, 2016 09:31
-
-
Save vslala/b7c54fc6bfe265a87e5e3a5778f97a88 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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()); | |
| } | |
| } | |
| } |
Author
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
This class deals with all the operations which are performed by the class.