Advertisement
homeworkhelp111

Untitled

Apr 14th, 2023
24
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 6.34 KB | None | 0 0
  1. import java.util.ArrayList;
  2. import java.util.Scanner;
  3.  
  4. public class StockManagement {
  5. private static Product[] products;
  6. private static int maxProducts = 0;
  7. private static int index = 0;
  8. private static int selection = 0;
  9.  
  10. public static int getMaxNumProducts(Scanner sc) {
  11. System.out.println("Please enter maximum number of products:");
  12. maxProducts = sc.nextInt();
  13.  
  14. while(maxProducts < 0 ) {
  15. System.out.println("Please enter maximum number of products:");
  16. maxProducts = sc.nextInt();
  17. }
  18. System.out.println();
  19. if(maxProducts > 0) {
  20. products = new Product[maxProducts];
  21. }
  22. return maxProducts;
  23. }
  24.  
  25. public static int viewProduct(Product[] p, Scanner sc) {
  26. int choice;
  27. for(int i=0;i<maxProducts; i++) {
  28. System.out.println("Index: "+i+". \n"+products[i].toString());
  29. System.out.println();
  30. }
  31.  
  32. System.out.print("Please enter the index of product you want to select : ");
  33. choice = sc.nextInt();
  34. while(choice < 0 || choice >= maxProducts) {
  35. System.out.print("Please enter the product you want to select : ");
  36. choice = sc.nextInt();
  37. }
  38. System.out.println();
  39. return choice;
  40. }
  41.  
  42. public static int displayMenu(Scanner sc) {
  43. int choice;
  44. System.out.println("1. View products");
  45. System.out.println("2. Add stock");
  46. System.out.println("3. Deduct stock");
  47. System.out.println("4. Discontinue product");
  48. System.out.println("0. Exit");
  49. System.out.println("Please enter a menu option: ");
  50.  
  51. choice = sc.nextInt();
  52.  
  53. while(choice < 0 || choice >4) {
  54. System.out.println("Please enter a menu option: ");
  55. choice = sc.nextInt();
  56. }
  57. System.out.println();
  58. return choice;
  59. }
  60.  
  61. public static void addStock(Product[] p, Scanner sc) {
  62. int num=-1;
  63. System.out.println("How many values do you want to add: ");
  64. num = sc.nextInt();
  65.  
  66. while(num < 0) {
  67. System.out.println("Please enter only positive values!");
  68. System.out.println("How many values do you want to add: ");
  69. num = sc.nextInt();
  70. }
  71. System.out.println();
  72. p[selection].addQuantity(num);
  73. }
  74.  
  75. public static void deductStock(Product[] p, Scanner sc) {
  76. int num=-1;
  77. System.out.println("How many values do you want to deduct: ");
  78. num = sc.nextInt();
  79.  
  80. while(num < 0 || num > p[selection].getQuantity()) {
  81. System.out.println("Please enter value greater than equal to 0 and not more than current quantity of stock");
  82. System.out.println("How many values do you want to deduct: ");
  83. num = sc.nextInt();
  84. }
  85. System.out.println();
  86. p[selection].deductQuantity(num);
  87. }
  88.  
  89. public static void discontinueProduct(Product[] p, Scanner sc) {
  90. p[selection].setStatus(false);
  91. }
  92.  
  93. public static void executeMethod(Product[] p, Scanner sc, int choice) {
  94. switch(choice) {
  95. case 1:
  96. selection = viewProduct(p, sc);
  97. System.out.println("Product selected: "+p[selection].getName());
  98. break;
  99.  
  100. case 2:
  101. addStock(p, sc);
  102. break;
  103.  
  104. case 3:
  105. deductStock(p,sc);
  106. break;
  107.  
  108. case 4:
  109. discontinueProduct(p, sc);
  110. break;
  111.  
  112. default:
  113. System.out.println("Thank you for visting! Goodbye!");
  114. break;
  115. }
  116. }
  117.  
  118. public static void addProduct(Product[] p, Scanner sc) {
  119. int choice;
  120. System.out.println("Please select (1 or 2) depending upon product to add: ");
  121. System.out.println("1. TV");
  122. System.out.println("2. Refrigerator");
  123. choice = sc.nextInt();
  124.  
  125. while(choice <1 || choice>2) {
  126. System.out.println("Please select (1 or 2) depending upon product to add: ");
  127. System.out.println("1. TV");
  128. System.out.println("2. Refrigerator");
  129. choice = sc.nextInt();
  130. }
  131.  
  132. switch(choice) {
  133. case 1:
  134. addTV(p,sc);
  135. break;
  136.  
  137. case 2:
  138. addRefrigerator(p, sc);
  139. break;
  140. }
  141. }
  142.  
  143. public static void addRefrigerator(Product[] p, Scanner sc) {
  144. String name, design, color;
  145. int capacity, quantity, itemNum;
  146. double price;
  147. System.out.println();
  148. System.out.println("Please enter name of refrigerator: ");
  149. name = sc.next();
  150.  
  151. System.out.println("Please enter door design: ");
  152. design = sc.next();
  153.  
  154. System.out.println("Please enter color: ");
  155. color = sc.next();
  156.  
  157. System.out.println("Please enter capacity: ");
  158. capacity = sc.nextInt();
  159.  
  160. System.out.println("Please enter quantity in stock: ");
  161. quantity = sc.nextInt();
  162.  
  163. System.out.println("Please enter price: ");
  164. price = sc.nextDouble();
  165.  
  166. System.out.println("Please enter item number: ");
  167. itemNum = sc.nextInt();
  168.  
  169. p[index] = new Refrigerator(name, design, color, capacity, quantity, price, itemNum);
  170. index = index + 1;
  171. System.out.println();
  172. }
  173.  
  174. public static void addTV(Product[] p, Scanner sc) {
  175. String name, screenType;
  176. int resolution, displaySize, quantity, itemNum;
  177. double price;
  178.  
  179. System.out.println();
  180. System.out.println("Please enter name of TV: ");
  181. name = sc.next();
  182.  
  183. System.out.println("Please enter screen type: ");
  184. screenType = sc.next();
  185.  
  186. System.out.println("Please enter resolution: ");
  187. resolution = sc.nextInt();
  188.  
  189. System.out.println("Please enter display size: ");
  190. displaySize = sc.nextInt();
  191.  
  192. System.out.println("Please enter quantity in stock: ");
  193. quantity = sc.nextInt();
  194.  
  195. System.out.println("Please enter price: ");
  196. price = sc.nextDouble();
  197.  
  198. System.out.println("Please enter item number: ");
  199. itemNum = sc.nextInt();
  200.  
  201. p[index] = new TV(name, screenType, resolution, displaySize, quantity, price, itemNum);
  202. index = index + 1;
  203. System.out.println();
  204. }
  205.  
  206. public static void displayContent(Product[] p) {
  207. for(int i=0; i<maxProducts;i++) {
  208. System.out.println(p[i].toString());
  209. }
  210. }
  211.  
  212. public static void main(String args[]) {
  213. String name;
  214. int choice;
  215.  
  216. System.out.println("Please enter your name: ");
  217. Scanner sc = new Scanner(System.in);
  218.  
  219. name = sc.nextLine();
  220.  
  221. System.out.println("Welcome "+name+"! Please select menu option: ");
  222. System.out.println("1. Add product");
  223. System.out.println("0. Exit");
  224. choice = sc.nextInt();
  225. System.out.println();
  226. switch(choice) {
  227. case 0:
  228. System.out.println("Thank you for visting! Goodbye!");
  229. break;
  230.  
  231. case 1:
  232. maxProducts = getMaxNumProducts(sc);
  233. for(int i=0; i<maxProducts;i++) {
  234. addProduct(products, sc);
  235. }
  236. break;
  237. }
  238. choice = -1;
  239. System.out.println();
  240. while(choice!=0){
  241. choice = displayMenu(sc);
  242. System.out.println();
  243. executeMethod(products,sc,choice);
  244. }
  245. }
  246. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement