Advertisement
Guest User

Untitled

a guest
May 20th, 2019
112
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.18 KB | None | 0 0
  1. package test;
  2.  
  3. import java.util.Scanner;
  4.  
  5. import javax.swing.JOptionPane;
  6.  
  7. import java.util.ArrayList;
  8.  
  9. public class UserInterface {
  10.  
  11. int choice;
  12.  
  13. //Create an object of the Scanner class to get input
  14.  
  15. Scanner input = new Scanner(System.in);
  16.  
  17. //Create an ArrayList too store Products
  18.  
  19. ArrayList<Product> inventory = populateArrayList();
  20.  
  21. //begin() method
  22.  
  23. public void begin() {
  24.  
  25.  
  26. //User a loop too repeat the menu being printed
  27.  
  28. do {
  29.  
  30. choice = displayMenu();
  31. System.out.println("Your choice is "+choice+".");
  32.  
  33. switch (choice) {
  34. case 0:
  35. System.out.println("You have chosen too QUIT!");
  36. break;
  37. case 1:
  38. System.out.println("You have chosen too search.");
  39. break;
  40. case 2:
  41. System.out.println("You have chosen too add a new product.");
  42. addnewProduct();
  43. break;
  44. case 3:
  45. System.out.println("You have chosen too delete a product.");
  46. break;
  47. case 4:
  48. System.out.println("You have chosen too list inventory.");
  49. listInventory();
  50. break;
  51. default:
  52. System.out.println("Invalid choice.");
  53. break;
  54. }
  55.  
  56. } while (choice != 0);
  57. }
  58.  
  59. //A method too display menu
  60.  
  61. public int displayMenu() {
  62. //System.out.println("ABC Sports Company");
  63. //System.out.println("0. Quit");
  64. //System.out.println("1. Search Product");
  65. //System.out.println("2. Add New Product");
  66. //System.out.println("3. Delete Product");
  67. //System.out.println("4. List Inventory");
  68. //System.out.print("Enter your choice: ");
  69. //choice = input.nextInt();
  70. //return choice;
  71.  
  72. String stringSelection = JOptionPane.showInputDialog(
  73. "******* MENU *******\n\n" +
  74. "0. Quit\n" +
  75. "1. Search product\n" +
  76. "2. Add New Product\n" +
  77. "3. Delete product\n" +
  78. "4. List Inventory\n\n" +
  79. "Enter your choice and click OK: ");
  80. choice = Integer.parseInt(stringSelection.trim());
  81. return choice;
  82.  
  83. }
  84. //Method too populate the ArrayList
  85. public ArrayList<Product> populateArrayList() {
  86. inventory = new ArrayList<Product>();
  87.  
  88. //add all the existing Products
  89.  
  90. inventory.add(new Product("110","Hi-Bounce Balls",200,19.95));
  91. inventory.add(new Product("120","Destruct Racquets",20,190.00));
  92. inventory.add(new Product("130","Slogger Tennis Shoes",20,45.00));
  93. inventory.add(new Product("140","Swich Caps",100,10.50));
  94.  
  95. return inventory;
  96. }
  97.  
  98. //Method too list the inventory
  99.  
  100. public void listInventory() {
  101.  
  102. //A loop too print all the Products from the inventory ArrayList
  103.  
  104. for (int i=0; i<inventory.size(); i++) {
  105. System.out.println(inventory.get(i).toString());
  106. }
  107. }
  108.  
  109. //Method add new Product
  110.  
  111. public void addnewProduct() {
  112. System.out.print("Enter a new code:");
  113. String newCode = input.next();
  114. System.out.print("Enter a new description:");
  115. String newDesc = input.next();
  116. System.out.print("Enter a new quantity:");
  117. int newQuan = input.nextInt();
  118. System.out.print("Enter a new price:");
  119. double newPrice = input.nextDouble();
  120.  
  121. //Create an object of this new Product
  122.  
  123. Product newProduct = new Product(newCode,newDesc,newQuan,newPrice);
  124. inventory.add(newProduct);
  125.  
  126. }
  127. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement