Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package test;
- import java.util.Scanner;
- import javax.swing.JOptionPane;
- import java.util.ArrayList;
- public class UserInterface {
- int choice;
- //Create an object of the Scanner class to get input
- Scanner input = new Scanner(System.in);
- //Create an ArrayList too store Products
- ArrayList<Product> inventory = populateArrayList();
- //begin() method
- public void begin() {
- //User a loop too repeat the menu being printed
- do {
- choice = displayMenu();
- System.out.println("Your choice is "+choice+".");
- switch (choice) {
- case 0:
- System.out.println("You have chosen too QUIT!");
- break;
- case 1:
- System.out.println("You have chosen too search.");
- break;
- case 2:
- System.out.println("You have chosen too add a new product.");
- addnewProduct();
- break;
- case 3:
- System.out.println("You have chosen too delete a product.");
- break;
- case 4:
- System.out.println("You have chosen too list inventory.");
- listInventory();
- break;
- default:
- System.out.println("Invalid choice.");
- break;
- }
- } while (choice != 0);
- }
- //A method too display menu
- public int displayMenu() {
- //System.out.println("ABC Sports Company");
- //System.out.println("0. Quit");
- //System.out.println("1. Search Product");
- //System.out.println("2. Add New Product");
- //System.out.println("3. Delete Product");
- //System.out.println("4. List Inventory");
- //System.out.print("Enter your choice: ");
- //choice = input.nextInt();
- //return choice;
- String stringSelection = JOptionPane.showInputDialog(
- "******* MENU *******\n\n" +
- "0. Quit\n" +
- "1. Search product\n" +
- "2. Add New Product\n" +
- "3. Delete product\n" +
- "4. List Inventory\n\n" +
- "Enter your choice and click OK: ");
- choice = Integer.parseInt(stringSelection.trim());
- return choice;
- }
- //Method too populate the ArrayList
- public ArrayList<Product> populateArrayList() {
- inventory = new ArrayList<Product>();
- //add all the existing Products
- inventory.add(new Product("110","Hi-Bounce Balls",200,19.95));
- inventory.add(new Product("120","Destruct Racquets",20,190.00));
- inventory.add(new Product("130","Slogger Tennis Shoes",20,45.00));
- inventory.add(new Product("140","Swich Caps",100,10.50));
- return inventory;
- }
- //Method too list the inventory
- public void listInventory() {
- //A loop too print all the Products from the inventory ArrayList
- for (int i=0; i<inventory.size(); i++) {
- System.out.println(inventory.get(i).toString());
- }
- }
- //Method add new Product
- public void addnewProduct() {
- System.out.print("Enter a new code:");
- String newCode = input.next();
- System.out.print("Enter a new description:");
- String newDesc = input.next();
- System.out.print("Enter a new quantity:");
- int newQuan = input.nextInt();
- System.out.print("Enter a new price:");
- double newPrice = input.nextDouble();
- //Create an object of this new Product
- Product newProduct = new Product(newCode,newDesc,newQuan,newPrice);
- inventory.add(newProduct);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement