Advertisement
Guest User

Untitled

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