class Inventory: option = 0 def selectOptions(self): print("press 1 to purchase:") print("press 2 to return:") print("press 3 to display Inventory:") return int(raw_input()) def __init__(self): self.manager = Manager() self.cashier = Cashier() flag = True while flag: self.option = self.selectOptions() if self.option == 1: self.client_id = random.randint(1, 1000) self.manager.itemList() orderDetails = self.manager.takeOrder(self.client_id) if orderDetails[0] == "checkout": self.cashier.checkout(orderDetails[1]) self.manager.displayInventory() elif orderDetails[0] == "don't checkout": print("we have ordered the requested items right away! please wait for us to get back to you.") elif self.option ==2: self.manager.close() self.cashier.close() flag = False sys.exit(0) elif self.option == 3: self.manager.displayInventory()
def checkout(self, cartID, aCardOwner, aCardNumber, aCardExpirationDate): session = self.getSession(cartID) with session: cart = session.cart aCreditCard = CreditCard(aCardOwner, aCardNumber, aCardExpirationDate) cashier = Cashier(self.merchantProcessor, cart, aCreditCard, session.clientId, self.salesBook) cashier.checkout()
def testCashierCantCheckoutEmptyCart(self): aCart = Cart(self.defaultCatalog()) try: salesBook = [] aCashier = Cashier(MerchantProccesorAdapterSpy(), aCart, self.validCard(), self.aClient(), salesBook) aCashier.checkout() self.fail() except Exception as e: self.assertTrue(e.message, Cashier.CAN_NOT_CHECKOUT_EMPTY_CART)
def testCashierCalculatesSalesTotalCorectly(self): aCart = self.createCartWithSomeBooks() expectedTotal = self.productPrice() + self.otherProductPrice() merchantProccesor = MerchantProccesorAdapterSpy() card = self.validCard() salesBook = [] aCashier = Cashier(merchantProccesor, aCart, card, self.aClient(), salesBook) sale = aCashier.checkout() self.assertEquals(sale.total, expectedTotal)
def testCashierCantCheckOutDatedCard(self): aCart = self.createCartWithSomeBooks() merchantProccesor = MerchantProccesorAdapterSpy() try: salesBook = [] aCreditCard = self.alwaysDatedCard() aCashier = Cashier(merchantProccesor, aCart, aCreditCard, self.aClient(), salesBook) aCashier.checkout() self.fail() except Exception as e: self.assertEquals(e.message, Cashier.CREDIT_CARD_EXPIRED) self.assertFalse(merchantProccesor.hasDebitCard(aCreditCard))
class TheaterSimulator: def __init__(self, length, odds_customer_arrives): self._length = length self._odds_customer_arrives = odds_customer_arrives self._cashier = Cashier() self._cashier2 = Cashier() def run_simulation(self): """ The following loop controls our entire simulation Each iteration represents a single clock tick (i.e. a minute) """ for i in range(self._length): # See if a new customer is created this clock tick new_customer = Customer.generate_customer( self._odds_customer_arrives, i) # If it is, add it to the Cashier's queue if new_customer is not None: if i % 2 == 0: self._cashier.add_customer(new_customer) else: self._cashier2.add_customer(new_customer) # Tell the Cashier to give front Customer one clock tick of help self._cashier.serve_customer(i) self._cashier2.serve_customer(i) print(self._cashier) print(self._cashier2)
def testCashierAddesSaleToSalesBook(self): aCart = self.createCartWithSomeBooks() expectedTotal = self.productPrice() + self.otherProductPrice() merchantProccesor = MerchantProccesorAdapterSpy() card = self.validCard() salesBook = [] aCashier = Cashier(merchantProccesor, aCart, card, self.aClient(), salesBook) sale = aCashier.checkout() self.assertEquals(len(salesBook), 1) self.assertTrue(sale in salesBook)
def testMerchantProccesorReceivesTherRightCreditCardAndAmount(self): aCart = self.createCartWithSomeBooks() expectedTotal = self.productPrice() + self.otherProductPrice() merchantProccesor = MerchantProccesorAdapterSpy() card = self.validCard() salesBook = [] aCashier = Cashier(merchantProccesor, aCart, card, self.aClient(), salesBook) sale = aCashier.checkout() self.assertEquals(sale.total, expectedTotal) self.assertTrue(merchantProccesor.hasDebitCard(card)) self.assertEquals(merchantProccesor.debitAmount(card), expectedTotal)
class TheaterSimulator: def __init__(self, length, odds_customer_arrives): self._length = length self._odds_customer_arrives = odds_customer_arrives self._cashier = Cashier() def run_simulation(self): for i in range(self._length): #see if a new customer is created this clock tick new_customer = Customer.generate_customer( self._odds_customer_arrives, i) if new_customer is not None: self._cashier.add_customer(new_customer) self._cashier.serve_customer(i) print(self._cashier)
def testCashierCantCheckoutWithStolenCard(self): def merchantProccesorStolenCardException(**kwargs): raise Exception(MerchantProccesorAdapter.CREDIT_CARD_STOLEN) aCart = self.createCartWithSomeBooks() merchantProccesor = MerchantProccesorAdapterStub( merchantProccesorStolenCardException) card = self.validCard() salesBook = [] aCashier = Cashier(merchantProccesor, aCart, card, self.aClient(), salesBook) try: aCashier.checkout() self.fail() except Exception as e: self.assertEquals(e.message, MerchantProccesorAdapter.CREDIT_CARD_STOLEN) self.assertEquals(salesBook, [])
def testCashierCanCheckoutOnlyOnce(self): aCart = self.createCartWithSomeBooks() expectedTotal = self.productPrice() + self.otherProductPrice() merchantProccesor = MerchantProccesorAdapterSpy() card = self.validCard() salesBook = [] aCashier = Cashier(merchantProccesor, aCart, card, self.aClient(), salesBook) sale = aCashier.checkout() try: aCashier.checkout() self.fail() except Exception as e: self.assertEquals(e.message, Cashier.CAN_CHECKOUT_ONLY_ONCE) self.assertEquals(len(salesBook), 1) self.assertTrue(sale in salesBook)
def main(): queue_inside = [] queue_outside = [] main_queue = {'inside': queue_inside, 'outside': queue_outside} saloon = [] client_generator = Generator(main_queue, saloon, EvenDistribution(1, 3), max_inside_len=15, max_outside_len=4, saloon_capacity=50) queue_cook = [] cashiers = [ Cashier(queue_inside, queue_cook, EvenDistribution(2, 6)), Cashier(queue_outside, queue_cook, EvenDistribution(2, 6)) ] cooks = [ Cook(queue_cook, NormalDistribution(7, 1)), Cook(queue_cook, NormalDistribution(7, 1)) ] total_requests = 10000 t_start = time() res = modeling(saloon, client_generator, cashiers, cooks, total_requests) print('time seconds', time() - t_start) for key in res.keys(): print(key, res[key]) print('lost', (res['lost inside'] + res['lost outside'] + res['lost saloon is full']) / total_requests)
def start(self): ''' 启动业务线程 ''' #初始化商品 self.init_goods() #记录销售开始时间 self.sale_begin_time = time.time() print(u"商品销售开始时间: %s" % time.asctime(time.localtime(self.sale_begin_time))) #初始化消费者线程 self.init_customer_threads() #初始化收银员线程 self.all_cashiers = [Cashier(name, self) for name in ALL_CASHIERS] for cashier in self.all_cashiers: self.init_cashier_threads(cashier) #初始销售监控线程 self.init_sale_goods_monitor_threads() for thd in self.all_threades: thd.join()
def runCashier(buy_queue, bought_queue, cashier_queue, task_number, lock): cashier = Cashier() cashier.sellGoods(buy_queue, bought_queue, cashier_queue, task_number, lock)
def __init__(self, length, odds_customer_arrives): self._length = length self._odds_customer_arrives = odds_customer_arrives self._cashier = Cashier() self._cashier2 = Cashier()
from Window import Window from Cashier import Cashier controller = Cashier() view = Window(controller) controller.setView(view) view.start()
from Cashier import Cashier Cashier.showProducts()