def repair(self): self.count = 0 def serve(self, arg): try: if isinstance(arg, beverages.HotBeverage) == False: raise Exception ("Type Error") except Exception as e: print (e) arg = self.EmptyCup() self.count += 1 if self.count > 10: raise self.BrokenMachineException() return random.choice([arg, self.EmptyCup()]) if __name__ == '__main__': var = CoffeeMachine() try: for i in range(20): bev = var.serve(arg = random.choice([beverages.Coffee(), beverages.Tea(), beverages.Chocolate(), beverages.Capuccino()])) print(bev.des) except Exception as e: print(e.msg) var.repair() try: for i in range(20): bev = var.serve(beverages.Coffee()) print(bev.des) except Exception as e: print(e.msg)
class BrokenMachineException(Exception): def __init__(self, e="This coffee machine has to be repaired."): super().__init__(self, e) def repair(self): print("Machine repaired") def serve(self, beverage): if self.nbUsage > 9: self.nbUsage = 0 raise self.BrokenMachineException self.nbUsage += 1 r = random.randint(0, 1) ret = beverage if r == 1: ret = self.EmptyCup() return (ret) if __name__ == '__main__': m = CoffeeMachine() for i in range(1, 15): try: print(m.serve(beverages.Cappuccino())) print(m.serve(beverages.Coffee())) print(m.serve(beverages.Tea())) print(m.serve(beverages.Chocolate())) except CoffeeMachine.BrokenMachineException as e: print(e) m.repair()
@classmethod def reset_counter(cls): cls.counter = 0 # --------------------------------------------------------------------------- if __name__ == '__main__': """ tests """ coffee_machine_instance = CoffeeMachine() try: for i in range(42): b = coffee_machine_instance.serve(beverage=random.choice([ beverages.Coffee(), beverages.Tea(), beverages.Chocolate(), beverages.Cappuccino() ])) print(b.name) except Exception as e: print(e) coffee_machine_instance.repair() try: for i in range(42): b = coffee_machine_instance.serve(beverages.Coffee()) print(b.name) except Exception as e: print(e)
def repair(self): self.serve_count = 0 def serve(self, obj: beverages.HotBeverage): if (self.serve_count > 9): raise self.BrokenMachineException() if random.randint(0, 1): self.serve_count = self.serve_count + 1 # print (self.serve_count) return obj else: return self.EmptyCup() if __name__ == '__main__': machine = CoffeeMachine() for i in range(0, 8): try: print(machine.serve(beverages.HotBeverage())) print(machine.serve(beverages.Coffee())) print(machine.serve(beverages.Tea())) print(machine.serve(beverages.Chocolate())) print(machine.serve(beverages.Cappuccino())) except Exception as a: print("***************** Warning ********************") print(a) print("==============================================\n") machine.repair()
raise Exception ("Type Error") except Exception as e: print (e) type = self.EmptyCup() self.count += 1 if self.count > 10: raise self.BrokenMachineException() return random.choice([type, self.EmptyCup()]) if __name__ == '__main__' : coco = CoffeeMachine() i = 0; try : while i < 15 : gg = (coco.serve(beverages.Coffee())) print(gg.desc) i+=1 except Exception as e : print(e.txt) coco.repair() i = 0 try: while i < 15: bev = coco.serve(type = random.choice([beverages.Coffee(), beverages.Tea(), beverages.Chocolate(), beverages.Capuccino()])) print(bev.desc) except Exception as e: print(e.txt) coco.repair()