Advertisement
Guest User

Inventory

a guest
Mar 1st, 2015
366
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 12.34 KB | None | 0 0
  1. Main Class
  2.  
  3. package inventory;
  4.  
  5. import java.util.ArrayList;
  6. import java.util.Scanner;
  7. import java.io.FileReader;
  8. import java.io.FileNotFoundException;
  9.  
  10. public class Inventory
  11. {
  12. private static Scanner scan = new Scanner(System.in);
  13.  
  14. public static void main(String[] args)
  15. {
  16. ArrayList<Product> InventoryList = new ArrayList<>();
  17. Inventory I = new Inventory();
  18. int option = 0;
  19.  
  20. Inventory.openFile(InventoryList);
  21.  
  22. while(option != 4)
  23. {
  24. System.out.println("1.) Add items to the inventory");
  25. System.out.println("2.) Show items in the inventory");
  26. System.out.println("3.) Show the total cost of the inventory.");
  27. System.out.println("4.) Quit the program.");
  28. option = scan.nextInt();
  29.  
  30. switch(option)
  31. {
  32. case 1:
  33. I.addItems(InventoryList);
  34. break;
  35. case 2:
  36. I.showInventory(InventoryList);
  37. break;
  38. case 3:
  39. I.showTotalCost(InventoryList);
  40. break;
  41. case 4:
  42. System.out.println("\nApplication Terminated.");
  43. break;
  44. default:
  45. System.out.println("This option does not exist in the menu. Try again");
  46. break;
  47. }
  48. }
  49. }
  50.  
  51. public static void openFile(ArrayList<Product> p)
  52. {
  53. Scanner scan = null;
  54. String firstLine = "";
  55. String line;
  56.  
  57. try
  58. {
  59. scan = new Scanner(new FileReader("product.csv"));
  60.  
  61. firstLine = scan.nextLine();
  62.  
  63. while(scan.hasNextLine())
  64. {
  65. line = scan.nextLine();
  66. String[] items = line.split(",");
  67.  
  68. Product product = new Product();
  69.  
  70. product.setProductName(items[0]);
  71. product.setQuantity(Integer.parseInt(items[1]));
  72. product.setPrice(Double.parseDouble(items[2]));
  73. product.setUPC(items[3]);
  74.  
  75. p.add(product);
  76. }
  77. scan.close();
  78. }
  79. catch(FileNotFoundException e)
  80. {
  81. System.err.println("File was not found");
  82. }
  83. }
  84.  
  85. public static void writeFile(ArrayList<Product> p)
  86. {
  87.  
  88. }
  89.  
  90. public void addItems(ArrayList<Product> p)
  91. {
  92. int option;
  93.  
  94. System.out.println("");
  95.  
  96. System.out.println("1.) Add a new product to the inventory.");
  97. System.out.println("2.) Increase the quantity of an existing product.");
  98. option = scan.nextInt();
  99.  
  100. if(option == 1)
  101. {
  102. String productName;
  103. int quantity;
  104. double price;
  105. String UPC;
  106.  
  107. scan.nextLine();
  108.  
  109. System.out.println("\nEnter the name of the product?");
  110. productName = scan.nextLine();
  111.  
  112. System.out.println("\nHow many of these items do you have?");
  113. quantity = scan.nextInt();
  114.  
  115. System.out.println("\nWhat is the price of this item?");
  116. price = scan.nextDouble();
  117.  
  118. scan.nextLine();
  119.  
  120. System.out.println("\nWhat is the UPC number for this item?");
  121. UPC = scan.nextLine();
  122.  
  123. if(p.size() > 0)
  124. {
  125. for (Product pro : p)
  126. {
  127. while(pro.getUPC().equals(UPC))
  128. {
  129. System.out.println("\nNo duplicates allowed, try again.");
  130. UPC = scan.nextLine();
  131. }
  132. }
  133. }
  134.  
  135. Product product = new Product(productName, quantity, price, UPC);
  136.  
  137. p.add(product);
  138. }
  139. else if(option == 2)
  140. {
  141. String oldProduct;
  142. String response;
  143. Inventory I = new Inventory();
  144.  
  145. scan.nextLine();
  146.  
  147. System.out.println("\nEnter the Name of the product.");
  148. oldProduct = scan.nextLine();
  149.  
  150. for(int i=0; ; i++)
  151. {
  152. if (p.get(i).getProductName().equalsIgnoreCase(oldProduct))
  153. {
  154.  
  155. }
  156. else if(i == p.size()-1)
  157. {
  158. System.out.println("\nThis product is not in our inventory.");
  159. break;
  160. }
  161. }
  162.  
  163. System.out.println("\nIs there another item that you would like to search by Name? ");
  164. response = scan.nextLine();
  165.  
  166. while(!(response.equalsIgnoreCase("Yes")) && !(response.equalsIgnoreCase("No")))
  167. {
  168. System.out.println("\nOnly, Yes or No are acceptable inputs. Try again.");
  169. response = scan.nextLine();
  170. }
  171.  
  172. if(response.equalsIgnoreCase("Yes"))
  173. {
  174. System.out.println("\nStarting a new search");
  175. I.showInventory(p);
  176. }
  177. else if(response.equalsIgnoreCase("No"))
  178. {
  179. System.out.println("\nReturning to the main menu\n");
  180. }
  181. }
  182.  
  183. System.out.println("");
  184. }
  185.  
  186. public void showInventory(ArrayList<Product> p)
  187. {
  188. int choice;
  189. String response;
  190. Inventory I = new Inventory();
  191.  
  192. System.out.println("");
  193.  
  194. System.out.println("1.) Search the inventory by row");
  195. System.out.println("2.) Search the entire inventory");
  196. System.out.println("3.) Search the inventory by UPC ID");
  197. System.out.println("4.) Search the inventory by name");
  198. choice = scan.nextInt();
  199. scan.nextLine();
  200.  
  201. if(choice == 1)
  202. {
  203. int arrayLimit = p.size();
  204. int row;
  205.  
  206. System.out.println("\nEnter the row that you would like to search");
  207. row = scan.nextInt();
  208.  
  209. while(row > arrayLimit || row <= 0)
  210. {
  211. System.out.println("\nSorry, that row doesn't exist. Try again");
  212. row = scan.nextInt();
  213. }
  214.  
  215. System.out.print("\nItem found: ");
  216.  
  217. System.out.println(p.get(row-1));
  218.  
  219. scan.nextLine();
  220.  
  221. System.out.println("\nIs there another item that you would like to seacrh by row? ");
  222. response = scan.nextLine();
  223.  
  224. while(!(response.equalsIgnoreCase("Yes")) && !(response.equalsIgnoreCase("No")))
  225. {
  226. System.out.println("\nOnly, Yes or No are acceptable inputs. Try again.");
  227. response = scan.nextLine();
  228. }
  229.  
  230. if(response.equalsIgnoreCase("Yes"))
  231. {
  232. System.out.println("\nStarting a new search");
  233. I.showInventory(p);
  234. }
  235. else if(response.equalsIgnoreCase("No"))
  236. {
  237. System.out.println("\nReturning to the main menu\n");
  238. }
  239. }
  240. else if(choice == 2)
  241. {
  242. System.out.println("");
  243.  
  244. for (Product p1 : p)
  245. {
  246. System.out.println(p1);
  247. }
  248.  
  249. System.out.println("");
  250. }
  251. else if(choice == 3)
  252. {
  253. String UPC;
  254.  
  255. System.out.println("\nEnter the UPC serial number.");
  256. UPC = scan.nextLine();
  257.  
  258. System.out.print("\nItem found: ");
  259.  
  260. for (Product p1 : p)
  261. {
  262. if (p1.getUPC().equals(UPC))
  263. {
  264. System.out.println(p1);
  265. }
  266. }
  267.  
  268. System.out.println("\nIs there another item that you would like to search by UPC? ");
  269. response = scan.nextLine();
  270.  
  271. while(!(response.equalsIgnoreCase("Yes")) && !(response.equalsIgnoreCase("No")))
  272. {
  273. System.out.println("\nOnly, Yes or No are acceptable inputs. Try again.");
  274. response = scan.nextLine();
  275. }
  276.  
  277. if(response.equalsIgnoreCase("Yes"))
  278. {
  279. System.out.println("\nStarting a new search");
  280. I.showInventory(p);
  281. }
  282. else if(response.equalsIgnoreCase("No"))
  283. {
  284. System.out.println("\nReturning to the main menu\n");
  285. }
  286. }
  287. else if(choice == 4)
  288. {
  289. String productName;
  290.  
  291. System.out.println("\nEnter the Name of the product.");
  292. productName= scan.nextLine();
  293.  
  294. for(int i=0; ; i++)
  295. {
  296. if (p.get(i).getProductName().equalsIgnoreCase(productName))
  297. {
  298. System.out.print("\nItem found: ");
  299. System.out.println(p.get(i));
  300. break;
  301. }
  302. else if(i == p.size()-1)
  303. {
  304. System.out.println("\nThis product is not in our inventory.");
  305. break;
  306. }
  307. }
  308.  
  309. System.out.println("\nIs there another item that you would like to search by Name? ");
  310. response = scan.nextLine();
  311.  
  312. while(!(response.equalsIgnoreCase("Yes")) && !(response.equalsIgnoreCase("No")))
  313. {
  314. System.out.println("\nOnly, Yes or No are acceptable inputs. Try again.");
  315. response = scan.nextLine();
  316. }
  317.  
  318. if(response.equalsIgnoreCase("Yes"))
  319. {
  320. System.out.println("\nStarting a new search");
  321. I.showInventory(p);
  322. }
  323. else if(response.equalsIgnoreCase("No"))
  324. {
  325. System.out.println("\nReturning to the main menu\n");
  326. }
  327. }
  328. }
  329.  
  330. public void showTotalCost(ArrayList<Product> p)
  331. {
  332. double totalCost = 0;
  333.  
  334. for(int i=0; i<p.size(); i++)
  335. {
  336. totalCost += p.get(i).getPrice() * p.get(i).getQuantity();
  337. }
  338.  
  339. System.out.println("");
  340. System.out.printf("The total cost of the entire inventory is %.2f",totalCost);
  341. System.out.println("\n");
  342. }
  343. }
  344.  
  345.  
  346.  
  347.  
  348. Product Class
  349.  
  350.  
  351.  
  352. package inventory;
  353.  
  354. public class Product
  355. {
  356. private String productName;
  357. private int quantity;
  358. private double price;
  359. private String UPC;
  360.  
  361. public Product(String productName, int quantity, double price, String UPC)
  362. {
  363. this.productName = productName;
  364. this.quantity = quantity;
  365. this.price = price;
  366. this.UPC = UPC;
  367. }
  368.  
  369. public Product()
  370. {
  371.  
  372. }
  373.  
  374. public String getProductName()
  375. {
  376. return productName;
  377. }
  378.  
  379. public int getQuantity()
  380. {
  381. return quantity;
  382. }
  383.  
  384. public double getPrice()
  385. {
  386. return price;
  387. }
  388.  
  389. public String getUPC()
  390. {
  391. return UPC;
  392. }
  393.  
  394. public void setProductName(String productName)
  395. {
  396. this.productName = productName;
  397. }
  398.  
  399. public void setQuantity(int quantity)
  400. {
  401. this.quantity = quantity;
  402. }
  403.  
  404. public void setPrice(double price)
  405. {
  406. this.price = price;
  407. }
  408.  
  409. public void setUPC(String UPC)
  410. {
  411. this.UPC = UPC;
  412. }
  413.  
  414. @Override
  415. public String toString()
  416. {
  417. return productName + ", " + quantity + ", " + price + ", " + UPC;
  418. }
  419. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement