Advertisement
Guest User

Untitled

a guest
Oct 10th, 2018
100
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 41.16 KB | None | 0 0
  1. package view;
  2.  
  3. import java.util.ArrayList;
  4. import java.util.HashMap;
  5. import java.util.Scanner;
  6.  
  7. import model.pay.Sale;
  8. import model.pay.SalesLineItem;
  9. import model.people.Customer;
  10. import model.people.Manager;
  11. import model.people.SalesStaff;
  12. import model.people.Supplier;
  13. import model.people.User;
  14. import model.people.WarehouseStaff;
  15. import model.system.AccountManager;
  16. import model.system.Product;
  17. import model.system.ProductManager;
  18. import model.system.SalesManager;
  19.  
  20. public class Menu {
  21.  
  22. private AccountManager am;
  23. private ProductManager pm;
  24. private SalesManager sm;
  25.  
  26. public Menu(AccountManager am, ProductManager pm, SalesManager sm) {
  27. this.am = am;
  28. this.pm = pm;
  29. this.sm = sm;
  30. }
  31.  
  32. public void displayMainMenu() {
  33. System.out.println("\nWelcome to Kostko!");
  34. System.out.println("------------------");
  35.  
  36. System.out.println("Please login to continue or type \"quit\" to quit:");
  37. System.out.println("\nUsername:");
  38. Scanner sc = new Scanner(System.in);
  39. String userName = sc.nextLine();
  40. if (userName.equalsIgnoreCase("quit")) {
  41. System.out.println("Good bye!\n");
  42. return;
  43. }
  44. System.out.println("\nPassword:");
  45. String pin = sc.nextLine();
  46. if (pin.equalsIgnoreCase("quit")) {
  47. System.out.println("Good bye!\n");
  48. return;
  49. }
  50.  
  51. User user = am.verify(userName, pin);
  52. if (user == null || user instanceof SalesStaff) // as Supplier doesnt have to login!
  53. // and i've done salesStaff requirements dont worry
  54. {
  55. System.out.println("Login failed! Please try again.");
  56. sc.nextLine();
  57. displayMainMenu();
  58. return;
  59. }
  60. if (user instanceof Manager) {
  61. System.out.println("\nWelcome Manager " + user.getUserID());
  62. managerView((Manager) user);
  63. } else if (user instanceof Customer) {
  64. System.out.println("\nWelcome Customer " + user.getUserID());
  65. Sale sale = new Sale((Customer) user);
  66. customerView((Customer) user, sale);
  67. } else if (user instanceof WarehouseStaff) {
  68. System.out.println("\nWelcome Warehouse Staff " + user.getUserID());
  69. warehouseView((WarehouseStaff) user);
  70. displayMainMenu();
  71. return;
  72. }
  73. }
  74.  
  75. private void managerView(Manager manager) {
  76. // README:
  77.  
  78. // consult customerView() for flow control and stuff please.
  79.  
  80. // *****btw to list products, can use the following code*****
  81.  
  82. // System.out.println("Please select one of the following items:");
  83. // HashMap<String, Product> products = pm.getProductsMap();
  84. // int counter = 1;
  85. // System.out.println("#\tID\tName\tWeightable\tPrice");
  86. // for (Product tempProduct : products.values()) {
  87. // if (tempProduct.getWeightable() == true)
  88. // System.out.println(counter + "\t" + tempProduct.getProductId() + '\t' + tempProduct.getProductName()
  89. // + "\t \t" + tempProduct.getWeightable() + "\t$" + tempProduct.getPricePerGram() + " /g");
  90. // else
  91. // System.out.println(counter + "\t" + tempProduct.getProductId() + '\t' + tempProduct.getProductName()
  92. // + "\t \t$" + tempProduct.getProductPrice());
  93. // counter++;
  94.  
  95. // ******and to select a product from list*****
  96.  
  97. // System.out.println("Please enter the product #:");
  98. // int prodNum = sc.nextInt();
  99. // sc.nextLine();
  100. //
  101. // if (!(prodNum > 0 && prodNum <= counter)) {
  102. // System.out.println("Invalid input! Would you like to try again? (Y/N)");
  103. // String yes = sc.nextLine();
  104. // if (yes.equalsIgnoreCase("n")) {
  105. // return;
  106. // } else {
  107. // continue;
  108. // }
  109. // }
  110. //
  111. // counter = 1;
  112. //
  113. // for (Product tempProduct : products.values()) {
  114. // if (prodNum == counter) {
  115. // product = tempProduct; // this is where it stores the selected product
  116. // }
  117. // counter++;
  118. // }
  119.  
  120. // ... see selectItemFromList() down at line 580
  121.  
  122. // ****** TODO / implement ******:
  123. // - a constructor that adds weighable products (ask in the UI first, "Is
  124. // product weighable?" as the first question. then only asks for input
  125.  
  126. // - (verify) if getPrice function in Product class works for weighable products
  127.  
  128. // - function that uses productManager (pm) to maintain unit-price, stock level,
  129. // replenish level and reorder quantity for all items.
  130.  
  131. // - Maintains supplier details for all products
  132.  
  133. // - Allows me to override the standard price for a specific product when there is a
  134. // promotion (use setDiscount in Product class)
  135.  
  136. // - Allows me to offer special discounts for bulk sales (for example, 10% discount for 5
  137. // items, 20% discount for 10 items etc.) on specific products (use setBulkDiscount in Product class, please read how the class works)
  138.  
  139. // - Allows me to modify these percentages and item numbers through the interface.
  140.  
  141. // - That automatically places a purchase order for all items below replenishment level (ALREADY DONE (auto restocks when stock < level) , just make sure to have an UI to input replenishment level
  142.  
  143. // - Allows me to generate sales report for the specified period (probably just use amtSold in Product class)
  144.  
  145. // - Generate supply report (Payments for supplies are out of scope) (huh?)
  146.  
  147. // - List products generating the most revenue. (use revenueGenerated in Product class! then sort.)
  148.  
  149. Scanner scanner = new Scanner(System.in);
  150. boolean quit = false;
  151. double newPrice = 0;
  152. String productID = null;
  153. Product product = null;
  154. int userInput;
  155.  
  156. while (quit != true) {
  157.  
  158. System.out.println("Select one of the following options (1-7):");
  159. System.out.println("\n1. Override standard price for a specific product");
  160. System.out.println("2. Apply discount on item");
  161. System.out.println("3. Check stock levels");
  162. System.out.println("4. Generate sales report");
  163. System.out.println("5. List products generating the most revenue");
  164. System.out.println("6. Display existing products");
  165. System.out.println("7. Exit");
  166. System.out.print("\nPlease enter your choice: ");
  167. userInput = scanner.nextInt();
  168. scanner.nextLine();
  169. // Change price of product
  170. if (userInput == 1) {
  171. System.out.print("Please enter the product ID: ");
  172. productID = scanner.nextLine();
  173. Product temp = pm.getProduct(productID);
  174.  
  175. if (temp == null) {
  176. return;
  177. } else {
  178. System.out.print("Please enter the new price for the product: ");
  179. newPrice = scanner.nextDouble();
  180.  
  181. // product = new Product(productID, newPrice);
  182.  
  183. temp.setProductPrice(newPrice);
  184.  
  185. System.out.println("The new price for product ID: " + temp.getProductId() + " is now $"
  186. + temp.getProductPrice());
  187.  
  188. System.out.println("\nWould you like to override a different product price? (Y/N)");
  189. String userChoice = scanner.nextLine();
  190. scanner.nextLine();
  191.  
  192. if (userChoice.equalsIgnoreCase("n")) {
  193. System.out.println("\nThanks for using the System, you've been logged out....");
  194. return;
  195. } else {
  196. continue;
  197. }
  198. }
  199. }
  200.  
  201. // Apply discount on item
  202. if (userInput == 2) {
  203. System.out.print("Please enter the product ID: ");
  204. productID = scanner.nextLine();
  205. Product temp = pm.getProduct(productID);
  206.  
  207.  
  208. if (temp == null) {
  209. return;
  210. } else {
  211. System.out.print("How much discount would you like to apply on this item? ");
  212. double discountPercentage = scanner.nextDouble();
  213.  
  214. temp.setDiscountedPrice(discountPercentage);
  215.  
  216.  
  217. System.out.println("Product ID: " + temp.getProductId() + " Old Price: $" + temp.getProductPrice());
  218.  
  219. // System.out.println(product.getDiscountRate() + "% " + "has been applied to " + product.getProductName());
  220. System.out.println(temp.getDiscountRate() + "% " + "has been applied to " + temp.getProductName());
  221. System.out
  222. .println("\nProduct ID: " + temp.getProductId() + " is now $" + temp.getDiscountedPrice());
  223.  
  224. }
  225.  
  226. }
  227. //**********STILL NEED TO FIX UP RESTOCK **********
  228. if (userInput == 3) {
  229.  
  230. System.out.print("Please enter the ID of the product you would like to restock: ");
  231. productID = scanner.nextLine();
  232.  
  233. Product temp = pm.getProduct(productID);
  234.  
  235. if (temp == null) {
  236. continue;
  237. } else {
  238.  
  239. System.out.println("The Stock Quantity for this product: " + temp.getStockQty());
  240. System.out.println("Would you like to do a restock? (Y/N)");
  241. String yes = scanner.nextLine();
  242.  
  243. if (yes.equalsIgnoreCase("n")) {
  244. return;
  245. }
  246.  
  247. System.out.print("Enter restock quantity: ");
  248. int restockQty = scanner.nextInt(); // please do input verification
  249. scanner.nextLine();
  250. temp.addStockQty(restockQty);
  251. System.out.println("Successfully restocked items");
  252. System.out.println("New stock quantity: " + temp.getStockQty() + "\n");
  253.  
  254.  
  255. if (temp.getRestockLvl() == 0 || temp.getBulkSalesQty() == 0)
  256. {
  257. System.out.println("Auto restock is not enabled for this product!");
  258. System.out.println("Would you like to setup auto restock for this product? (Y/N)");
  259. }
  260. else
  261. {
  262. System.out.println("Current auto restock level: " + temp.getRestockLvl());
  263. System.out.println("Current auto restock quantity: " + temp.getReorderQty());
  264. System.out.println("Would you like to modify auto restock for this product? (Y/N)");
  265. }
  266.  
  267. yes = scanner.nextLine();
  268.  
  269. if (yes.equalsIgnoreCase("n")) {
  270. return;
  271. }
  272.  
  273. System.out.print("Enter auto restock level: ");
  274. int autoRestockLevel = scanner.nextInt();
  275. scanner.nextLine();
  276. temp.setRestockLvl(autoRestockLevel);
  277.  
  278. System.out.print("Enter auto restock quantity: ");
  279. int autoRestockQty = scanner.nextInt();
  280. scanner.nextLine();
  281. temp.setReorderQty(autoRestockQty);
  282.  
  283.  
  284. System.out.println("Current auto restock level: " + temp.getRestockLvl());
  285. System.out.println("Current auto restock quantity: " + temp.getReorderQty());
  286.  
  287. }
  288.  
  289. }
  290.  
  291. if (userInput == 7) {
  292. quit = true;
  293. }
  294. }
  295. }
  296.  
  297. // **** ONE QUESTION THOUGH, for the warehouse staff requirement should we
  298. // complete it by implementing
  299. // another User class and function to replenish stock levels?
  300. // then warehouse staff will have to key in supplier details. manager can then
  301. // view the stuff
  302.  
  303. // "I want to be able to replenish stock levels before placing items received on
  304. // the shelves."
  305.  
  306. private void customerView(Customer user, Sale sale) {
  307. System.out.println("You have " + sale.getItemsInCart() + " items in your cart.");
  308. if (sale.getItemsInCart() > 0) {
  309. displayCart(sale);
  310. System.out.println("Total Price: $" + sale.getTotalPrice());
  311. }
  312. System.out.println("-------------------------------------");
  313. System.out.println("Select one of the following options (1-5):");
  314. System.out.println("1. Enter Item ID");
  315. System.out.println("2. Enter Item Name");
  316. System.out.println("3. Select Item from List");
  317. System.out.println("4. Finish and Pay");
  318. System.out.println("5. Cancel Order");
  319. System.out.println("6. I Need Assistance");
  320. Scanner sc = new Scanner(System.in);
  321. int menuIndex = sc.nextInt();
  322. sc.nextLine();
  323. if (menuIndex == 1) {
  324. addProductByID(sale);
  325. customerView(user, sale);
  326. return;
  327. } else if (menuIndex == 2) {
  328. addProductByName(sale);
  329. customerView(user, sale);
  330. return;
  331. } else if (menuIndex == 3) {
  332. selectItemFromList(sale);
  333. customerView(user, sale);
  334. return;
  335. } else if (menuIndex == 4) {
  336. if (!(sale.getItemsInCart() > 0)) {
  337. System.out.println("No items in cart! Please add item!");
  338. customerView(user, sale);
  339. return;
  340. }
  341. boolean success = finishAndPay(sale, user);
  342. if (success == false) {
  343. customerView(user, sale);
  344. return;
  345. } else {
  346. System.out.println("Thanks for shopping with us! See you soon!");
  347. displayMainMenu();
  348. return;
  349. }
  350. } else if (menuIndex == 5) {
  351. System.out.println("Are you sure you would like to cancel the order? (Y/N)");
  352. String yes = sc.nextLine();
  353. if (yes.equalsIgnoreCase("n")) {
  354. customerView(user, sale);
  355. return;
  356. } else {
  357. System.out.println("You've been logged out! See you soon!");
  358. displayMainMenu();
  359. return;
  360. }
  361. } else if (menuIndex == 6) {
  362. System.out.println("1. Top Up Credit Card");
  363. if (sale.getItemsInCart() > 0)
  364. System.out.println("2. Modify Cart");
  365. int helpIndex = sc.nextInt();
  366. sc.nextLine();
  367. if (helpIndex == 1) {
  368. topUpCard(user);
  369. customerView(user, sale);
  370. return;
  371. } else if (helpIndex == 2 && sale.getItemsInCart() > 0) {
  372. modifyCart(sale);
  373. customerView(user, sale);
  374. return;
  375. } else {
  376. System.out.println("Invalid Input!");
  377. customerView(user, sale);
  378. return;
  379. }
  380. } else {
  381. // prints invalid input then rerun?
  382. }
  383. }
  384.  
  385. private void warehouseView(WarehouseStaff user) {
  386. Scanner sc = new Scanner(System.in);
  387. boolean idOK = false;
  388. boolean nameOK = false;
  389. boolean inputA = false;
  390. boolean inputB = false;
  391. String productName = null;
  392. String productID = null;
  393. Product product = null;
  394.  
  395. System.out.println("Would you like to add an item received? (Y/N)");
  396. String yes = sc.nextLine();
  397. if (yes.equalsIgnoreCase("n")) {
  398. System.out.println("\nYou've been logged out!");
  399. return;
  400. }
  401.  
  402. while (idOK == false) {
  403. System.out.println("Enter product ID:");
  404. productID = sc.nextLine();
  405. if (productID == null) {
  406. System.out.println("Input error! Please try again.");
  407. continue;
  408. }
  409. Product temp = pm.getProduct(productID);
  410. if (temp == null) {
  411. System.out.println("Proceeding to add product...");
  412. idOK = true;
  413. } else {
  414. System.out.println("Product with the same ID exists in database!");
  415. }
  416. }
  417.  
  418. while (nameOK == false) {
  419. System.out.println("Enter product name:");
  420. productName = sc.nextLine();
  421. if (productName == null) {
  422. System.out.println("Input error! Please try again.");
  423. continue;
  424. } else
  425. nameOK = true;
  426. }
  427.  
  428. System.out.println("Is product weighable? (Y/N)");
  429. yes = sc.nextLine();
  430. if (yes.equalsIgnoreCase("n")) {
  431. double price = 0;
  432. int quantity = 0;
  433. while (inputA == false) {
  434. try {
  435. System.out.println("Enter unit price ($):");
  436. price = sc.nextDouble();
  437. sc.nextLine();
  438. if (price <= 0) {
  439. System.out.println("Invalid price! Please try again.");
  440. continue;
  441. }
  442. inputA = true;
  443. } catch (Exception e) {
  444. System.out.println("Invalid input! Please try again.");
  445. }
  446. }
  447. while (inputB == false) {
  448. try {
  449. System.out.println("Enter stock quantity:");
  450. quantity = sc.nextInt();
  451. sc.nextLine();
  452. if (quantity <= 0) {
  453. System.out.println("Invalid quantity! Please try again.");
  454. continue;
  455. }
  456. inputB = true;
  457.  
  458. } catch (Exception e) {
  459. System.out.println("Invalid input! Please try again.");
  460. }
  461. }
  462. product = new Product(productID, productName, price, quantity);
  463.  
  464. System.out.println("Product ID: " + productID);
  465. System.out.println("Product Name: " + productName);
  466. System.out.println("Unit price: " + price);
  467. System.out.println("Stock quantity: " + quantity);
  468. System.out.println("Product added!");
  469. } else {
  470. double pricePerGram = 0;
  471. double stockWeight = 0;
  472. while (inputA == false) {
  473. try {
  474. System.out.println("Enter price per 100g:");
  475. pricePerGram = sc.nextDouble();
  476. sc.nextLine();
  477. if (pricePerGram <= 0) {
  478. System.out.println("Invalid price! Please try again.");
  479. continue;
  480. }
  481. inputA = true;
  482. } catch (Exception e) {
  483. System.out.println("Invalid input! Please try again.");
  484. }
  485. }
  486. while (inputB == false) {
  487. try {
  488. System.out.println("Enter stock weight in g:");
  489. stockWeight = sc.nextInt();
  490. sc.nextLine();
  491. if (stockWeight <= 0) {
  492. System.out.println("Invalid weight! Please try again.");
  493. continue;
  494. }
  495. inputB = true;
  496.  
  497. } catch (Exception e) {
  498. System.out.println("Invalid input! Please try again.");
  499. }
  500. }
  501. product = new Product(productID, productName, pricePerGram, stockWeight);
  502.  
  503. System.out.println("Product ID: " + productID);
  504. System.out.println("Product Name: " + productName);
  505. System.out.println("Price per 100g: " + pricePerGram);
  506. System.out.println("Stock weight: " + stockWeight);
  507. System.out.println("Product added!");
  508. }
  509.  
  510. System.out.println("\nNext up, we'll input supplier details for product " + productID + "...");
  511.  
  512. boolean supplierCompanyNameOK = false, supplierContactNoOK = false, supplierEmailOK = false,
  513. supplierLocationOK = false;
  514. String supplierCompanyName = null, supplierContactNo = null, supplierEmail = null, supplierLocation = null;
  515.  
  516. while (supplierCompanyNameOK == false) {
  517. System.out.println("\nEnter Supplier Company Name:");
  518. supplierCompanyName = sc.nextLine();
  519. if (supplierCompanyName == null) {
  520. System.out.println("Input error! Please try again.");
  521. continue;
  522. } else
  523. supplierCompanyNameOK = true;
  524. }
  525.  
  526. while (supplierContactNoOK == false) {
  527. System.out.println("\nEnter Supplier Contact Number:");
  528. supplierContactNo = sc.nextLine();
  529. if (supplierContactNo == null) {
  530. System.out.println("Input error! Please try again.");
  531. continue;
  532. } else
  533. supplierContactNoOK = true;
  534. }
  535.  
  536. while (supplierEmailOK == false) {
  537. System.out.println("\nEnter Supplier Email:");
  538. supplierEmail = sc.nextLine();
  539. if (supplierEmail == null) {
  540. System.out.println("Input error! Please try again.");
  541. continue;
  542. } else
  543. supplierEmailOK = true;
  544. }
  545.  
  546. while (supplierLocationOK == false) {
  547. System.out.println("\nEnter Supplier Location:");
  548. supplierLocation = sc.nextLine();
  549. if (supplierLocation == null) {
  550. System.out.println("Input error! Please try again.");
  551. continue;
  552. } else
  553. supplierLocationOK = true;
  554. }
  555.  
  556. Supplier supplier = new Supplier(supplierCompanyName, supplierContactNo, supplierEmail, supplierLocation);
  557. System.out.println("Supplier details added to product " + productID + "!");
  558. System.out.println("Supplier Company Name:" + supplierCompanyName);
  559. System.out.println("Supplier Contact No:" + supplierContactNo);
  560. System.out.println("Supplier Email:" + supplierEmail);
  561. System.out.println("Supplier Location:" + supplierLocation);
  562. product.setSupplier(supplier);
  563. pm.addProduct(product);
  564. System.out.println("\nThanks for using the system. You've been logged out!");
  565. }
  566.  
  567. private void addProductByID(Sale sale) {
  568. Scanner sc = new Scanner(System.in);
  569.  
  570. boolean quit;
  571. Product product = null;
  572. int quantity = 0;
  573. double weight = 0;
  574. quit = false;
  575. while (quit == false) {
  576. System.out.println("Enter Product ID:");
  577. String id = sc.nextLine();
  578. product = pm.getProduct(id);
  579. if (product == null) {
  580. System.out.println("Would you like to try again? (Y/N)");
  581. String yes = sc.nextLine();
  582. if (yes.equalsIgnoreCase("n")) {
  583. return;
  584. } else {
  585. continue;
  586. }
  587. }
  588. // check if product is in cart
  589. ArrayList<SalesLineItem> lineItems = sale.getSalesLineItems();
  590. SalesLineItem lineItem = null;
  591. for (int i = 0; i < lineItems.size(); i++) {
  592. if (lineItems.get(i).getProduct().equals(product)) {
  593. lineItem = lineItems.get(i);
  594. }
  595. }
  596.  
  597. if (lineItem == null) {
  598. if (product.getWeightable() == false) {
  599. System.out.println("Product isn't weightable!");
  600. boolean quantityOK = false;
  601. while (quantityOK == false) {
  602. System.out.println("Enter Quantity:");
  603. quantity = sc.nextInt();
  604. sc.nextLine();
  605. // verfiication?
  606. if (quantity > 0 && quantity <= product.getStockQty()) {
  607. quantityOK = true;
  608. break;
  609. } else if (quantity > product.getStockQty()) {
  610. System.out.println(
  611. "Quantity inputted exceeded what we have in stock! Would you like to try again? (Y/N)");
  612. } else if (quantity <= 0) {
  613. System.out.println("Quantity error! Would you like to try again? (Y/N)");
  614. }
  615. String yes = sc.nextLine();
  616. if (yes.equalsIgnoreCase("n")) {
  617. return;
  618. }
  619. }
  620. lineItem = new SalesLineItem(quantity, product);
  621. sale.addLineItem(lineItem);
  622. System.out.println(
  623. "Added " + lineItem.getProductQuantity() + ' ' + product.getProductName() + " to cart!");
  624. quit = true;
  625. } else {
  626. System.out.println("Product weightable!");
  627. boolean weightOK = false;
  628. while (weightOK == false) {
  629. System.out.println(
  630. "Price per gram for " + product.getProductName() + ":" + product.getPricePerGram());
  631. System.out.println("Enter Weight (in g):");
  632. weight = sc.nextDouble();
  633. sc.nextLine();
  634. // verfiication?
  635. if (weight > 0 && weight <= product.getStockWeight()) {
  636. weightOK = true;
  637. break;
  638. } else if (weight > product.getStockWeight()) {
  639. System.out.println(
  640. "Weight inputted exceeded what we have in stock! Would you like to try again? (Y/N)");
  641. } else if (weight <= 0) {
  642. System.out.println("Weight error! Would you like to try again? (Y/N)");
  643. }
  644. String yes = sc.nextLine();
  645. if (yes.equalsIgnoreCase("n")) {
  646. return;
  647. }
  648. }
  649. quit = true;
  650. lineItem = new SalesLineItem(weight, product, true);
  651. System.out.println(
  652. "Added " + lineItem.getWeight() + "g of " + product.getProductName() + " to cart!");
  653. }
  654.  
  655. } else {
  656. System.out.println("Product already in cart! Would you like to add more? (Y/N)");
  657. String yes = sc.nextLine();
  658. if (yes.equalsIgnoreCase("n")) {
  659. return;
  660. }
  661. if (product.getWeightable() == false) {
  662. System.out.println("Product isn't weightable!");
  663.  
  664. boolean quantityOK = false;
  665. while (quantityOK == false) {
  666. System.out.println("Enter Quantity (" + lineItem.getProductQuantity() + " in cart) :");
  667. quantity = sc.nextInt();
  668. sc.nextLine();
  669. // verfiication?
  670. if (quantity > 0 && quantity <= product.getStockQty()) {
  671. quantityOK = true;
  672. break;
  673. } else if (quantity > product.getStockQty()) {
  674. System.out.println(
  675. "Quantity inputted is greater than what we have in stock! Would you like to try again? (Y/N)");
  676. } else if (quantity <= 0) {
  677. System.out.println("Quantity error! Would you like to try again? (Y/N)");
  678. }
  679. yes = sc.nextLine();
  680. if (yes.equalsIgnoreCase("n")) {
  681. return;
  682. }
  683. }
  684. int prodQuantity = lineItem.getProductQuantity();
  685. prodQuantity += quantity;
  686. lineItem.setProductQuantity(prodQuantity);
  687. System.out.println("Added " + quantity + "more " + product.getProductName() + " to cart!");
  688. System.out.println("Total quantity: " + lineItem.getProductQuantity());
  689. quit = true;
  690. } else {
  691. boolean weightOK = false;
  692. while (weightOK == false) {
  693. System.out.println(
  694. "Price per gram for " + product.getProductName() + ":" + product.getPricePerGram());
  695. System.out.println("Enter Weight (in g, " + lineItem.getWeight() + "g in cart):");
  696. weight = sc.nextDouble();
  697. sc.nextLine();
  698. // verfiication?
  699. if (weight > 0 && weight <= product.getStockWeight()) {
  700. weightOK = true;
  701. break;
  702. } else if (weight > product.getStockWeight()) {
  703. System.out.println(
  704. "Weight inputted exceeded what we have in stock! Would you like to try again? (Y/N)");
  705. } else if (weight <= 0) {
  706. System.out.println("Weight error! Would you like to try again? (Y/N)");
  707. }
  708. yes = sc.nextLine();
  709. if (yes.equalsIgnoreCase("n")) {
  710. return;
  711. }
  712. }
  713. double prodWeight = lineItem.getWeight();
  714. weight += prodWeight;
  715. lineItem.setWeight(weight);
  716. System.out.println("Added " + weight + "g of more " + product.getProductName() + " to cart!");
  717. System.out.println("Total weight: " + lineItem.getWeight() + "g");
  718. quit = true;
  719. }
  720. }
  721. }
  722.  
  723. }
  724.  
  725. private void addProductByName(Sale sale) {
  726. Scanner sc = new Scanner(System.in);
  727.  
  728. boolean quit;
  729. Product product = null;
  730. int quantity = 0;
  731. double weight = 0;
  732. quit = false;
  733. while (quit == false) {
  734. System.out.println("Enter Product Name:");
  735. String name = sc.nextLine();
  736. product = pm.getProductByName(name);
  737.  
  738. if (product == null) {
  739. System.out.println("Can't find selected product. Would you like to try again? (Y/N)");
  740. String yes = sc.nextLine();
  741. if (yes.equalsIgnoreCase("n")) {
  742. return;
  743. } else {
  744. continue;
  745. }
  746. }
  747. // check if product is in cart
  748. ArrayList<SalesLineItem> lineItems = sale.getSalesLineItems();
  749. SalesLineItem lineItem = null;
  750. for (int i = 0; i < lineItems.size(); i++) {
  751. if (lineItems.get(i).getProduct().equals(product)) {
  752. lineItem = lineItems.get(i);
  753. }
  754. }
  755.  
  756. if (lineItem == null) {
  757. if (product.getWeightable() == false) {
  758. System.out.println("Product isn't weightable!");
  759. boolean quantityOK = false;
  760. while (quantityOK == false) {
  761. System.out.println("Enter Quantity:");
  762. quantity = sc.nextInt();
  763. sc.nextLine();
  764. // verfiication?
  765. if (quantity > 0 && quantity <= product.getStockQty()) {
  766. quantityOK = true;
  767. break;
  768. } else if (quantity > product.getStockQty()) {
  769. System.out.println(
  770. "Quantity inputted is greater than what we have in stock! Would you like to try again? (Y/N)");
  771. } else if (quantity <= 0) {
  772. System.out.println("Quantity error! Would you like to try again? (Y/N)");
  773. }
  774. String yes = sc.nextLine();
  775. if (yes.equalsIgnoreCase("n")) {
  776. return;
  777. }
  778. }
  779. lineItem = new SalesLineItem(quantity, product);
  780. sale.addLineItem(lineItem);
  781. System.out.println(
  782. "Added " + lineItem.getProductQuantity() + ' ' + product.getProductName() + " to cart!");
  783. quit = true;
  784. } else {
  785. System.out.println("Product weightable!");
  786. boolean weightOK = false;
  787. while (weightOK == false) {
  788. System.out.println(
  789. "Price per gram for " + product.getProductName() + ":" + product.getPricePerGram());
  790. System.out.println("Enter Weight (in g):");
  791. weight = sc.nextDouble();
  792. sc.nextLine();
  793. // verfiication?
  794. if (weight > 0 && weight <= product.getStockWeight()) {
  795. weightOK = true;
  796. break;
  797. } else if (weight > product.getStockWeight()) {
  798. System.out.println(
  799. "Weight inputted exceeded what we have in stock! Would you like to try again? (Y/N)");
  800. } else if (weight <= 0) {
  801. System.out.println("Weight error! Would you like to try again? (Y/N)");
  802. }
  803. String yes = sc.nextLine();
  804. if (yes.equalsIgnoreCase("n")) {
  805. return;
  806. }
  807. }
  808. quit = true;
  809. lineItem = new SalesLineItem(weight, product, true);
  810. System.out.println(
  811. "Added " + lineItem.getWeight() + "g of " + product.getProductName() + " to cart!");
  812. }
  813.  
  814. } else {
  815. System.out.println("Product already in cart! Would you like to add more? (Y/N)");
  816. String yes = sc.nextLine();
  817. if (yes.equalsIgnoreCase("n")) {
  818. return;
  819. }
  820. if (product.getWeightable() == false) {
  821. System.out.println("Product isn't weightable!");
  822. boolean quantityOK = false;
  823. while (quantityOK == false) {
  824. System.out.println("Enter Quantity (" + lineItem.getProductQuantity() + " in cart) :");
  825. quantity = sc.nextInt();
  826. sc.nextLine();
  827. // verfiication?
  828. if (quantity > 0 && quantity <= product.getStockQty()) {
  829. quantityOK = true;
  830. break;
  831. } else if (quantity > product.getStockQty()) {
  832. System.out.println(
  833. "Quantity inputted is greater than what we have in stock! Would you like to try again? (Y/N)");
  834. } else if (quantity <= 0) {
  835. System.out.println("Quantity error! Would you like to try again? (Y/N)");
  836. }
  837. yes = sc.nextLine();
  838. if (yes.equalsIgnoreCase("n")) {
  839. return;
  840. }
  841. }
  842. int prodQuantity = lineItem.getProductQuantity();
  843. prodQuantity += quantity;
  844. lineItem.setProductQuantity(prodQuantity);
  845. System.out.println("Added " + quantity + "more " + product.getProductName() + " to cart!");
  846. System.out.println("Total quantity: " + lineItem.getProductQuantity());
  847. quit = true;
  848. } else {
  849. boolean weightOK = false;
  850. while (weightOK == false) {
  851. System.out.println(
  852. "Price per gram for " + product.getProductName() + ":" + product.getPricePerGram());
  853. System.out.println("Enter Weight (in g, " + lineItem.getWeight() + "g in cart):");
  854. weight = sc.nextDouble();
  855. sc.nextLine();
  856. // verfiication?
  857. if (weight > 0 && weight <= product.getStockWeight()) {
  858. weightOK = true;
  859. break;
  860. } else if (weight > product.getStockWeight()) {
  861. System.out.println(
  862. "Weight inputted exceeded what we have in stock! Would you like to try again? (Y/N)");
  863. } else if (weight <= 0) {
  864. System.out.println("Weight error! Would you like to try again? (Y/N)");
  865. }
  866. yes = sc.nextLine();
  867. if (yes.equalsIgnoreCase("n")) {
  868. return;
  869. }
  870. }
  871. double prodWeight = lineItem.getWeight();
  872. weight += prodWeight;
  873. lineItem.setWeight(weight);
  874. System.out.println("Added " + weight + "g of more " + product.getProductName() + " to cart!");
  875. System.out.println("Total weight: " + lineItem.getWeight() + "g");
  876. quit = true;
  877. }
  878. }
  879. }
  880. }
  881.  
  882. private void selectItemFromList(Sale sale) {
  883. Scanner sc = new Scanner(System.in);
  884. boolean quit;
  885. Product product = null;
  886. int quantity = 0;
  887. double weight = 0;
  888. quit = false;
  889. while (quit == false) {
  890. System.out.println("Please select one of the following items:");
  891. HashMap<String, Product> products = pm.getProductsMap();
  892. int counter = 1;
  893. System.out.println("#\tID\tName\tWeightable\tPrice");
  894. for (Product tempProduct : products.values()) {
  895. if (tempProduct.getWeightable() == true)
  896. System.out.println(counter + "\t" + tempProduct.getProductId() + '\t' + tempProduct.getProductName()
  897. + "\t \t" + tempProduct.getWeightable() + "\t$" + tempProduct.getPricePerGram() + " /g");
  898. else
  899. System.out.println(counter + "\t" + tempProduct.getProductId() + '\t' + tempProduct.getProductName()
  900. + "\t \t$" + tempProduct.getProductPrice());
  901. counter++;
  902. }
  903. System.out.println("Please enter the product #:");
  904. int prodNum = sc.nextInt();
  905. sc.nextLine();
  906.  
  907. if (!(prodNum > 0 && prodNum <= counter)) {
  908. System.out.println("Invalid input! Would you like to try again? (Y/N)");
  909. String yes = sc.nextLine();
  910. if (yes.equalsIgnoreCase("n")) {
  911. return;
  912. } else {
  913. continue;
  914. }
  915. }
  916.  
  917. counter = 1;
  918.  
  919. for (Product tempProduct : products.values()) {
  920. if (prodNum == counter) {
  921. product = tempProduct;
  922. }
  923. counter++;
  924. }
  925. ArrayList<SalesLineItem> lineItems = sale.getSalesLineItems();
  926. SalesLineItem lineItem = null;
  927. for (int i = 0; i < lineItems.size(); i++) {
  928. if (lineItems.get(i).getProduct().equals(product)) {
  929. lineItem = lineItems.get(i);
  930. }
  931. }
  932.  
  933. if (lineItem == null) {
  934. if (product.getWeightable() == false) {
  935. System.out.println("Product isn't weightable!");
  936. boolean quantityOK = false;
  937. while (quantityOK == false) {
  938. System.out.println("Enter Quantity:");
  939. quantity = sc.nextInt();
  940. sc.nextLine();
  941. // verfiication?
  942. if (quantity > 0 && quantity <= product.getStockQty()) {
  943. quantityOK = true;
  944. break;
  945. } else if (quantity > product.getStockQty()) {
  946. System.out.println(
  947. "Quantity inputted is greater than what we have in stock! Would you like to try again? (Y/N)");
  948. } else if (quantity <= 0) {
  949. System.out.println("Quantity error! Would you like to try again? (Y/N)");
  950. }
  951. String yes = sc.nextLine();
  952. if (yes.equalsIgnoreCase("n")) {
  953. return;
  954. }
  955. }
  956. lineItem = new SalesLineItem(quantity, product);
  957. sale.addLineItem(lineItem);
  958. System.out.println(
  959. "Added " + lineItem.getProductQuantity() + ' ' + product.getProductName() + " to cart!");
  960. quit = true;
  961. } else {
  962. System.out.println("Product weightable!");
  963. boolean weightOK = false;
  964. while (weightOK == false) {
  965. System.out.println(
  966. "Price per gram for " + product.getProductName() + ":" + product.getPricePerGram());
  967. System.out.println("Enter Weight (in g):");
  968. weight = sc.nextDouble();
  969. sc.nextLine();
  970. // verfiication?
  971. if (weight > 0 && weight <= product.getStockWeight()) {
  972. weightOK = true;
  973. break;
  974. } else if (weight > product.getStockWeight()) {
  975. System.out.println(
  976. "Weight inputted exceeded what we have in stock! Would you like to try again? (Y/N)");
  977. } else if (weight <= 0) {
  978. System.out.println("Weight error! Would you like to try again? (Y/N)");
  979. }
  980. String yes = sc.nextLine();
  981. if (yes.equalsIgnoreCase("n")) {
  982. return;
  983. }
  984. }
  985. quit = true;
  986. lineItem = new SalesLineItem(weight, product, true);
  987. System.out.println(
  988. "Added " + lineItem.getWeight() + "g of " + product.getProductName() + " to cart!");
  989. }
  990.  
  991. } else {
  992. System.out.println("Product already in cart! Would you like to add more? (Y/N)");
  993. String yes = sc.nextLine();
  994. if (yes.equalsIgnoreCase("n")) {
  995. return;
  996. }
  997. if (product.getWeightable() == false) {
  998. System.out.println("Product isn't weightable!");
  999. boolean quantityOK = false;
  1000. while (quantityOK == false) {
  1001. System.out.println("Enter Quantity (" + lineItem.getProductQuantity() + " in cart) :");
  1002. quantity = sc.nextInt();
  1003. sc.nextLine();
  1004. // verfiication?
  1005. if (quantity > 0 && quantity <= product.getStockQty()) {
  1006. quantityOK = true;
  1007. break;
  1008. } else if (quantity > product.getStockQty()) {
  1009. System.out.println(
  1010. "Quantity inputted is greater than what we have in stock! Would you like to try again? (Y/N)");
  1011. } else if (quantity <= 0) {
  1012. System.out.println("Quantity error! Would you like to try again? (Y/N)");
  1013. }
  1014. yes = sc.nextLine();
  1015. if (yes.equalsIgnoreCase("n")) {
  1016. return;
  1017. }
  1018. }
  1019. int prodQuantity = lineItem.getProductQuantity();
  1020. prodQuantity += quantity;
  1021. lineItem.setProductQuantity(prodQuantity);
  1022. System.out.println("Added " + quantity + "more " + product.getProductName() + " to cart!");
  1023. System.out.println("Total quantity: " + lineItem.getProductQuantity());
  1024. quit = true;
  1025. } else {
  1026. boolean weightOK = false;
  1027. while (weightOK == false) {
  1028. System.out.println(
  1029. "Price per gram for " + product.getProductName() + ":" + product.getPricePerGram());
  1030. System.out.println("Enter Weight (in g, " + lineItem.getWeight() + "g in cart):");
  1031. weight = sc.nextDouble();
  1032. sc.nextLine();
  1033. // verfiication?
  1034. if (weight > 0 && weight <= product.getStockWeight()) {
  1035. weightOK = true;
  1036. break;
  1037. } else if (weight > product.getStockWeight()) {
  1038. System.out.println(
  1039. "Weight inputted exceeded what we have in stock! Would you like to try again? (Y/N)");
  1040. } else if (weight <= 0) {
  1041. System.out.println("Weight error! Would you like to try again? (Y/N)");
  1042. }
  1043. yes = sc.nextLine();
  1044. if (yes.equalsIgnoreCase("n")) {
  1045. return;
  1046. }
  1047. }
  1048. double prodWeight = lineItem.getWeight();
  1049. weight += prodWeight;
  1050. lineItem.setWeight(weight);
  1051. System.out.println("Added " + weight + "g of more " + product.getProductName() + " to cart!");
  1052. System.out.println("Total weight: " + lineItem.getWeight() + "g");
  1053. quit = true;
  1054. }
  1055. }
  1056. }
  1057. }
  1058.  
  1059. public boolean finishAndPay(Sale sale, Customer customer) {
  1060.  
  1061. Scanner sc = new Scanner(System.in);
  1062. boolean quit = false;
  1063. System.out.println("This is your cart:");
  1064. displayCart(sale);
  1065. System.out.println("Total Price: $" + sale.getTotalPrice());
  1066. System.out.println("Total Loyalty Points Earned: " + sale.getLoyaltyPtsEarned());
  1067. System.out.println("Total Loyalty Points Used: " + sale.getLoyaltyPtsUsed());
  1068. System.out.println("Total Discounted Price: $" + sale.getTotalDiscountedPrice());
  1069. System.out.println("Would you like to finish and pay? (Y/N)");
  1070. String yes = sc.nextLine();
  1071. if (yes.equalsIgnoreCase("n")) {
  1072. return false;
  1073. }
  1074. while (quit == false) {
  1075. System.out.println("Amount Payable: $" + sale.getTotalDiscountedPrice());
  1076. System.out.println("Please enter credit card ID:");
  1077. String credID = sc.nextLine();
  1078. System.out.println("Please enter credit card PIN:");
  1079. String pin = sc.nextLine();
  1080. if (credID.equals(customer.getCreditCard().getCreditCardID()) == false
  1081. || pin.equals(customer.getCreditCard().getPin()) == false) {
  1082. System.out.println("Credit Card ID / PIN Error! Would you like to try again? (Y/N)");
  1083. yes = sc.nextLine();
  1084. if (yes.equalsIgnoreCase("n")) {
  1085. return false;
  1086. } else
  1087. continue;
  1088. }
  1089. System.out.println("Credit card balance: $" + customer.getCreditCard().getBalance());
  1090. if (customer.getCreditCard().getBalance() < sale.getTotalDiscountedPrice()) {
  1091. System.out.println(
  1092. "Insufficient balance! Would you like one of our friendly staffs to top up for you? (Y/N)");
  1093. yes = sc.nextLine();
  1094. if (yes.equalsIgnoreCase("n")) {
  1095. return false;
  1096. } else {
  1097. boolean topUpSuccessful = topUpCard(customer);
  1098. if (topUpSuccessful == false) {
  1099. System.out.println("Top up failed! Please try again later.");
  1100. return false;
  1101. } else {
  1102. System.out.println("New balance: $" + customer.getCreditCard().getBalance());
  1103. }
  1104. }
  1105. }
  1106. sale.pay(sm);
  1107. customer.getCreditCard().deductBalance(sale.getTotalDiscountedPrice());
  1108. System.out.println("Payment successful! Amount paid: $" + sale.getTotalDiscountedPrice());
  1109. System.out.println("New Credit card balance: $" + customer.getCreditCard().getBalance());
  1110. System.out.println("Loyalty points: " + customer.getLoyaltyPts());
  1111. quit = true;
  1112. }
  1113. return true;
  1114. }
  1115.  
  1116. private boolean topUpCard(Customer customer) {
  1117. boolean quit = false;
  1118. Scanner sc = new Scanner(System.in);
  1119. while (quit == false) {
  1120. System.out.println("Please login to continue or type \"quit\" to quit:");
  1121. System.out.print("\nStaff Username:");
  1122. String userName = sc.nextLine();
  1123. if (userName.equalsIgnoreCase("quit")) {
  1124. return false;
  1125. }
  1126. System.out.print("\nPassword:");
  1127. String pin = sc.nextLine();
  1128.  
  1129. User user = am.verify(userName, pin);
  1130. if (user == null || !(user instanceof SalesStaff)) {
  1131. System.out.println("Login failed! Would you like to try again? (Y/N)");
  1132. String yes = sc.nextLine();
  1133. if (yes.equalsIgnoreCase("n")) {
  1134. return false;
  1135. } else
  1136. continue;
  1137. } else {
  1138. System.out.println("Enter amount to be topped up:");
  1139. double topupAmt = sc.nextDouble();
  1140. sc.nextLine();
  1141. if (topupAmt <= 0) {
  1142. System.out.println("Input error! You've been logged out, please try again!");
  1143. continue;
  1144. }
  1145. customer.getCreditCard().addBalance(topupAmt);
  1146. System.out.println(
  1147. "Amount topped up: " + topupAmt + " for Customer" + customer.getUserID() + "\'s credit card");
  1148. return true;
  1149. }
  1150. }
  1151. return false;
  1152. }
  1153.  
  1154. private void modifyCart(Sale sale) {
  1155. Scanner sc = new Scanner(System.in);
  1156. boolean quit = false;
  1157. boolean innerQuit = false;
  1158. while (quit == false) {
  1159. System.out.println("Please login to continue or type \"quit\" to quit:");
  1160. System.out.print("\nStaff Username:");
  1161. String userName = sc.nextLine();
  1162. if (userName.equalsIgnoreCase("quit")) {
  1163. return;
  1164. }
  1165. System.out.print("\nPassword:");
  1166. String pin = sc.nextLine();
  1167.  
  1168. User user = am.verify(userName, pin);
  1169. if (user == null || !(user instanceof SalesStaff)) {
  1170. System.out.println("Login failed! Would you like to try again? (Y/N)");
  1171. String yes = sc.nextLine();
  1172. if (yes.equalsIgnoreCase("n")) {
  1173. return;
  1174. } else
  1175. continue;
  1176. } else {
  1177. while (innerQuit == false) {
  1178. displayCart(sale);
  1179. System.out.println("Remove an item by its #:");
  1180. int itemNum = sc.nextInt();
  1181. sc.nextLine();
  1182. if (!(itemNum >= 0 && itemNum <= sale.getItemsInCart())) {
  1183. System.out.println("Invalid input! Would you like to try again? (Y/N)");
  1184. String yes = sc.nextLine();
  1185. if (yes.equalsIgnoreCase("n")) {
  1186. return;
  1187. } else {
  1188. continue;
  1189. }
  1190. } else {
  1191. sale.getSalesLineItems().remove(itemNum);
  1192. System.out.println("Item removed!!");
  1193. }
  1194. innerQuit = true;
  1195. }
  1196. }
  1197. quit = true;
  1198. }
  1199. }
  1200.  
  1201. private void displayCart(Sale sale) {
  1202. ArrayList<SalesLineItem> lineItems = sale.getSalesLineItems();
  1203. System.out.println("#\tID\tName\tQuantity\tWeight\tPrice");
  1204. for (int i = 0; i < sale.getItemsInCart(); i++) {
  1205. Product tempProduct = lineItems.get(i).getProduct();
  1206. SalesLineItem lineItem = lineItems.get(i);
  1207. if (lineItem.getWeightable() == true)
  1208. System.out.println(i + "\t" + tempProduct.getProductId() + '\t' + tempProduct.getProductName() + "\t \t"
  1209. + lineItem.getWeight() + "g\t$" + lineItem.getTotalPrice());
  1210. else
  1211. System.out.println(i + "\t" + tempProduct.getProductId() + '\t' + tempProduct.getProductName() + '\t'
  1212. + lineItem.getProductQuantity() + "\t \t$" + lineItem.getTotalPrice());
  1213. }
  1214. }
  1215.  
  1216. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement