def modifyShoppingCart(self): while True: choice = UserInterface.displayConfirm("Continue? ", "") if choice.upper().strip() == 'Y': idPrice = UserInterface.displayForm( "Please input the product ", [("id", "nstring"), ("price", "money")]) sc = self.store.getCustomer(self.loginDetail).getShoppingCart() # print(idPrice[0]) acStock = sc.getListByIdPrice(idPrice[0], float(idPrice[1])) print(type(idPrice[0])) if acStock is not None: newQuan = UserInterface.displayForm( "New quantity", [("", "money")])[0] print(type(newQuan), float(newQuan)) if float(newQuan) == 0.0: sc.deleteFromShoppingCart(idPrice[0], float(idPrice[1])) UserInterface.writeLine( "Item deleted from shopping Cart. :\'(") else: acStock[2] = float(newQuan) break else: UserInterface.writeLine( "Try again. Id and price does not match.") else: break
def manageAccount(self): customer = self.store.getCustomer(self.loginDetail) # option_String = 'Edit Options: 1- Name 2- Password 3- Phone Number 4- Address 5- Balance' # keyboardInput = UserInterface.displayList('Account Info.', [('Name', name), ('Password', pwd), ('Phone', phoneNumber) # , ('Address', address), ('Balance', balance)], option_String) while True: option_String = 'Edit Options: N- Name P- Password C- Phone Number A- Address B- Top Up Balance X- Exit' keyboardInput = UserInterface.displayList( 'Account Info.', [('Name', customer.getName()), ('Password', customer.getPassword()), ('Phone', customer.getPhoneNumber()), ('Address', customer.getAddress()), ('Balance', customer.getBalance())], option_String).strip().upper() if keyboardInput == 'N': name = UserInterface.displayForm( 'Please give a new value for -', [('Name', 'nstring')]) name = name[0] customer.setName(name) UserInterface.writeLine('Name updated.') elif keyboardInput == 'P': pwd = UserInterface.displayForm( 'Please give a new value for -', [('Password', 'nstring')]) pwd = pwd[0] customer.setPassword(pwd) UserInterface.writeLine('Password updated.') elif keyboardInput == 'C': phoneNumber = UserInterface.displayForm( 'Please give a new value for -', [('Phone Number', 'nstring')]) phoneNumber = phoneNumber[0] customer.setPhoneNumber(phoneNumber) UserInterface.writeLine('Phone number updated.') elif keyboardInput == 'A': address = UserInterface.displayForm( 'Please give a new value for -', [('Address', 'nstring')]) address = address[0] customer.setAddress(address) UserInterface.writeLine('Address updated.') elif keyboardInput == 'B': updated = False while not updated: balance = UserInterface.displayForm( 'Please give a new value for -', [('Balance', 'money')])[0] if float(balance) >= 0: customer.topUp(balance) UserInterface.writeLine('Balance updated.') updated = True else: print( 'Please enter a value greater or equal than zero.') elif keyboardInput == 'X': self.store.writeStore() break
def viewProductByBatch(self, productId): product = self.store.getProduct(productId) batches = product.getBatches() tuples = [("BatchId", "Price, Quantity, Expiry Date")] batchIds = [] for batch in batches: batchId = batch.getBatchID() batchIds.append(batchId) theRest = str(batch.getActualPrice()) + " " + str( batch.getQuantity()) + " " + str(batch.getExpiryDate()) tuples.append((batchId, theRest)) UserInterface.displayList("Batch details: ", tuples, "", False) confirm = UserInterface.displayConfirm("Edit batch ", "Do you wish to edit Batch?") if confirm.lower() == 'y' and len(batchIds) > 0: while True: batchId = UserInterface.displayForm( "Batch Id", [("Please input the batchID you would like to edit.", "number")])[0] #print(batchIds) if batchId in batchIds: self.editBatch(productId, batchId) break else: UserInterface.writeLine("Batch Id incorrect. Try again.") else: # ML added else condition so user doesn't get stuck when no batch exists. UserInterface.writeLine("No batches to edit.")
def addProduct(self): inputs = UserInterface.displayForm( "Please give the details of the new product", [('Name', 'nstring'), ('Unit of Measure', 'nstring'), ('Price per unit', 'money'), ('Source/Origin', 'nstring'), ('shelfLife', 'int')]) self.store.addProduct(inputs[0], inputs[1], float(inputs[2]), inputs[3], int(inputs[4])) self.store.writeStore() UserInterface.writeLine("Product added.")
def register(self): inputs = UserInterface.displayForm( "Please enter your user details to register.\nYour userID will be generated automatically.", [('Type your password', 'nstring'), ('Type your password again', 'nstring'), ('Enter your name', 'nstring'), ('Enter your phone number', 'nstring'), ('Enter your address', 'nstring')]) while inputs[0] != inputs[1]: inputs[0:2] = UserInterface.displayForm( "Passwords don't match! Please try again", [('Type your password', 'nstring'), ('Type your password again', 'nstring')]) newCustomer = self.store.addCustomer(inputs[0], inputs[2], inputs[3], inputs[4]) self.store.writeStore() UserInterface.writeLine( "Successfully registered! Your ID is {}. You will be automatically logged in." .format(newCustomer.getId())) self.loginDetail = newCustomer.getId()
def addToCart(self, productId, priceGroup): toAdd = UserInterface.displayConfirm( "", "Do you wish to add this Product into shopping cart?") if toAdd in ['y', 'Y']: # check input, add to cart acPrice, pQuantity = None, None UserInterface.writeLine( "input the price you want. enter 9999 to quit.") acPrice = UserInterface.displayForm("select price:", [('', 'money')])[0] acPrice = float(acPrice) while acPrice not in priceGroup and acPrice != 9999: acPrice = UserInterface.displayForm( "price not found. select price:", [('', 'money')])[0] acPrice = float(acPrice) if acPrice != 9999: pQuantity = UserInterface.displayForm("input quantity:", [('', 'number')])[0] pQuantity = float(pQuantity) while pQuantity > priceGroup[acPrice] and pQuantity != 9999: UserInterface.writeLine( "quantity larger than stock. input new or 9999 to quit" ) pQuantity = UserInterface.displayForm( "input new:", [('', 'number')])[0] pQuantity = float(pQuantity) if acPrice != 9999 and pQuantity != 9999: UserInterface.writeLine("selected price: " + str(acPrice) + " Quantity: " + str(pQuantity)) confirmed = UserInterface.displayConfirm( "", "confirm to add to shopping cart.") if confirmed == 'y': sc = self.store.getCustomer( self.loginDetail).getShoppingCart() sc.addToShoppingCart(productId, acPrice, pQuantity) else: pass
def editBatchPrice(self, productId, batchId): currentPrice = self.store.getProduct(productId).getBatch( batchId).getActualPrice() UserInterface.writeLine("The current quantity is " + str(currentPrice)) results = UserInterface.displayForm("Please enter the new price", [('Price', 'money')]) #input newPrice = float(results[0]) confirmMsg = UserInterface.displayConfirm( "Your new price is " + results[0], "Are you sure?") if confirmMsg == 'y' or confirmMsg == 'Y': self.store.getProduct(productId).getBatch(batchId).setActualPrice( newPrice) UserInterface.writeLine("The new price is: " + str(newPrice)) else: UserInterface.writeLine("The action is abandoned.")
def login(self): inputs = UserInterface.displayForm("Please enter your login details", [('Customer ID', 'nstring'), ('Password', 'nstring')]) try: if inputs[0] == 'owner': self.store.owner.logIn(inputs[1]) self.loginDetail = self.store.owner.getId() else: customer = self.store.getCustomer(inputs[0]) customer.logIn(inputs[1]) self.loginDetail = customer.getId() UserInterface.writeLine("Successfully logged in!") except Exception: UserInterface.writeLine( "Invalid Customer ID and password combination")
def editBatchQuantity(self, productId, batchId): currentQuantity = self.store.getProduct(productId).getBatch( batchId).getQuantity() UserInterface.writeLine("The current quantity is " + str(currentQuantity)) results = UserInterface.displayForm("Please enter the new quantity", [('Quantity', 'number')]) #input newQuantity = float(results[0]) confirmMsg = UserInterface.displayConfirm( "Your new quantity is " + results[0], "Are you sure?") if confirmMsg == 'y' or confirmMsg == 'Y': self.store.getProduct(productId).getBatch(batchId).setQuantity( newQuantity) UserInterface.writeLine("The new quantity is: " + str(newQuantity)) else: UserInterface.writeLine("The action is abandoned.")
def reduceProductQuantityByPrice(self, productId, actualPrice): totalQ = self.store.getProduct(productId).calculateStock(actualPrice) UserInterface.writeLine("Current quantity at $" + str(actualPrice) + " is "\ + str(totalQ)) valid = False while not valid: results = UserInterface.displayForm( "Deduct the quantity BY this amount: ", [('Quantity', 'number')]) newQuantity = float(results[0]) if newQuantity <= totalQ: valid = True self.store.getProduct(productId).deductStock( actualPrice, newQuantity) else: UserInterface.writeLine("The quantity is too much.") UserInterface.writeLine( "The new quantity is: " + str(self.store.getProduct(productId).calculateStock(actualPrice)))
def searchProduct(self): keyword = UserInterface.displayForm( "Search Product: Please input the product you would like to search: ", [('name', 'string')]) # here keyword is a list, so the "real" keyword is keyword[0] matchingList = self.store.searchProductByName(keyword[0]) toBeDisplayed = [] displayNumber = 0 for matchingProduct in matchingList: tuple0 = displayNumber theRest = matchingProduct.getName( ) + " " + matchingProduct.getUnit( ) + " " + matchingProduct.getSource() newTuple = (tuple0, theRest) toBeDisplayed.append(newTuple) displayNumber += 1 choice = UserInterface.displayList( "The matching products are: ", toBeDisplayed, "Which product to choose? X to go back.") validInput = False while not validInput: if choice == "x" or choice == "X": validInput = True return None # The end of this method else: matchIndex = 0 stop = False while not stop and matchIndex < len(matchingList): if choice == str(matchIndex): stop = True else: matchIndex += 1 if stop is False: choice = input("Please enter a valid input.") else: validInput = True return matchingList[matchIndex].getId()
def editProduct(self, productId): attToEdit = True while attToEdit is not None: UserInterface.writeLine( "A. Name B. Unit C. Source D. Original Price E. Restock F. Deduct Stock Q. Quit " ) select = UserInterface.displayForm("", [('select:', 'string')]) if select[0].upper().strip() == 'Q': attToEdit = None else: if select[0].upper().strip() == 'A': newData = UserInterface.displayForm( 'input new name:', [('', 'nstring')]) self.store.editProductName(productId, newData[0]) elif select[0].upper().strip() == 'B': newData = UserInterface.displayForm( 'input new unit:', [('', 'nstring')]) self.store.editProductUnit(productId, newData[0]) elif select[0].upper().strip() == 'C': newData = UserInterface.displayForm( 'input new source:', [('', 'nstring')]) self.store.editProductSource(productId, newData[0]) elif select[0].upper().strip() == 'D': newData = UserInterface.displayForm( 'input new price:', [('', 'money')]) self.store.editProductOriginalPrice( productId, float(newData[0])) elif select[0].upper().strip() == 'E': self.restockProduct(productId) elif select[0].upper().strip() == 'F': ap = UserInterface.displayForm("Input the product price to deduct quantity at that price", \ [("price point: ", "money")]) self.reduceProductQuantityByPrice(productId, float(ap[0])) attToEdit = True # view product after quit the edition self.viewProductByPrice(productId)
def restockProduct(self, productId): quantity = UserInterface.displayForm("Input how much you wanna restock: ", \ [("restock quantity","number")]) self.store.getProduct(productId).addBatch(float(quantity[0]))