app_customer.py 2.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182
  1. from consumer.connect import DataConsumer
  2. from consumer.credentials import getCredentials
  3. from basic_display import BasicDisplay
  4. from cli.select import Selecter
  5. from cli.pass_asker import PassAsker
  6. from cli.customer_action_asker import CustomerActionAsker
  7. from cli.book_asker import BookAsker
  8. from cli.product_select import ProductSelecter
  9. from cli.house_asker import HouseAsker
  10. from cli.confirm import Confirmer
  11. from datetime import date, timedelta
  12. display = BasicDisplay()
  13. display.title('Customer application')
  14. credentials = getCredentials()
  15. consumer = DataConsumer(credentials)
  16. companyAsker = Selecter('Choose your company')
  17. passAsker = PassAsker()
  18. actionAsker = CustomerActionAsker()
  19. bookAsker = BookAsker()
  20. productAsker = ProductSelecter()
  21. houseAsker = HouseAsker()
  22. confirmAsker = Confirmer()
  23. # Login phase
  24. compList = consumer.getCompanyNames()
  25. compList.append('dev')
  26. login = companyAsker.selectInto(compList)
  27. pwd = passAsker.ask()
  28. # Dummy password check
  29. if pwd != "":
  30. print('Successfully logged as', login)
  31. else:
  32. print('Name', login, 'not recognized.')
  33. print('(dummy password check : enter an non-empty password)')
  34. exit()
  35. # Action phase
  36. running = True
  37. while running:
  38. ans = actionAsker.ask()
  39. action = ans['action']
  40. if action == 'quit':
  41. running = False
  42. elif action == 'list orders':
  43. bookList = consumer.getOrdersOfCompany(login)
  44. if len(bookList) == 0:
  45. display.error('No book order for', login, 'yet.')
  46. else:
  47. bookId = bookAsker.select(bookList)
  48. itemList = consumer.getItemsOfOrder(bookId)
  49. if len(itemList) == 0:
  50. display.error('No item found for order', bookId)
  51. else:
  52. display.orderedItemList(itemList)
  53. elif action == 'list warehouses':
  54. houseList = consumer.getWarehousesOfCompany(login)
  55. if len(houseList) == 0:
  56. display.error('No warehouse for', login, 'yet.')
  57. else:
  58. display.warehouseList(houseList)
  59. elif action == 'new order':
  60. houseList = consumer.getWarehousesOfCompany(login)
  61. targetHouseId = houseAsker.select(houseList)
  62. proList = consumer.getProductSet()
  63. proSelectNameList = productAsker.selectInto(proList)
  64. productQuantityList = [100] * len(proSelectNameList)
  65. dueDate = date.today() + timedelta(15)
  66. if (confirmAsker.confirm()):
  67. consumer.addOrder(login, targetHouseId, proSelectNameList, productQuantityList, dueDate)
  68. consumer.commit()
  69. else:
  70. display.error('Order creation aborted')
  71. else:
  72. display.notImplemented(action)
  73. display.end()