Created
September 9, 2016 09:42
-
-
Save vslala/da0770e66f42245b9a8183867b343d1e 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.List; | |
| import java.util.Scanner; | |
| /** | |
| * | |
| * @author Varun Shrivastava (vslala) | |
| */ | |
| public class UI { | |
| Cart cart = new Cart(); | |
| private int ch = 0; | |
| public UI () { | |
| menu(); | |
| } | |
| public void startScreen () { | |
| System.out.println("1. Display Store Products"); | |
| System.out.println("2. Display Cart"); | |
| System.out.println("0. Exit"); | |
| } | |
| public void storeProductsMenu() { | |
| System.out.println("1. Add to Cart"); | |
| System.out.println("2. Remove From Cart"); | |
| System.out.println("0. Exit"); | |
| } | |
| public void menu () { | |
| do { | |
| startScreen(); | |
| getUserInput(); | |
| switch (ch) { | |
| case 1: | |
| displayStoreProducts(); | |
| storeProductsMenu(); | |
| getUserInput(); | |
| innerChoice1(); | |
| break; | |
| case 2: | |
| showCart(); | |
| break; | |
| case 0: | |
| System.exit(0); | |
| break; | |
| default: | |
| break; | |
| } | |
| } while (ch != 0); | |
| } | |
| private void innerChoice1() { | |
| switch (ch) { | |
| case 1: | |
| addProductToCart(); | |
| showCart(); | |
| break; | |
| case 2: | |
| removeProductFromCart(); | |
| break; | |
| default: | |
| break; | |
| } | |
| } | |
| private int getUserInput() throws NumberFormatException { | |
| Scanner in = new Scanner (System.in); | |
| ch = Integer.parseInt(in.nextLine()); | |
| return ch; | |
| } | |
| private void displayStoreProducts() { | |
| List<Product> products = new Products().getProducts(); | |
| for (Product prod: products) { | |
| System.out.println( | |
| prod.getPid() + "- " + | |
| prod.getName() + " " + | |
| prod.getPrice() + " " + | |
| prod.getStock() | |
| ); | |
| } | |
| } | |
| private void addProductToCart() { | |
| int pid = getUserInput(); | |
| cart.addProductToCartByPID(pid); | |
| } | |
| private void showCart() { | |
| cart.printCartItems(); | |
| } | |
| private void removeProductFromCart() { | |
| int pid = getUserInput(); | |
| cart.removeProductByPID(pid); | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment