python-checkio/IceBase/microwave-ovens.py
2019-12-20 17:23:34 +01:00

102 lines
2.7 KiB
Python

class MicrowaveBase:
def __init__(self):
self.time = "00:00"
def set_time(self, time: str):
self.time = time
def add_time(self, time: str):
minute, second = self.time.split(":")
minute = int(minute)
second = int(second)
number_to_add = int(time[0:-1])
unit_to_add = time[-1:]
if unit_to_add == "s":
second += number_to_add
while second >= 60:
minute += 1
second = second - 60
else:
minute += number_to_add
if minute > 90:
self.time = "90:00"
else:
self.time = "{}:{}".format(str(minute).rjust(2, "0"), str(second).rjust(2, "0"))
def del_time(self, time: str):
minute, second = self.time.split(":")
minute = int(minute)
second = int(second)
number_to_add = int(time[0:-1])
unit_to_add = time[-1:]
if unit_to_add == "s":
second -= number_to_add
while second <= 0:
minute -= 1
second *= -1
else:
minute -= number_to_add
if minute < 0:
self.time = "00:00"
else:
self.time = "{}:{}".format(str(minute).rjust(2, "0"), str(second).rjust(2, "0"))
class Microwave1(MicrowaveBase):
def show_time(self) -> str:
print("_"+self.time[1:])
return "_"+self.time[1:]
class Microwave2(MicrowaveBase):
def show_time(self):
print(self.time[0:-1] + "_")
return self.time[0:-1] + "_"
class Microwave3(MicrowaveBase):
def show_time(self):
print(self.time)
return self.time
class RemoteControl:
def __init__(self, microwave: MicrowaveBase):
self.microwave = microwave
def set_time(self, time: str):
self.microwave.set_time(time)
def add_time(self, time: str):
self.microwave.add_time(time)
def del_time(self, time: str):
self.microwave.del_time(time)
def show_time(self):
return self.microwave.show_time()
if __name__ == '__main__':
# These "asserts" using only for self-checking and not necessary for auto-testing
microwave_1 = Microwave1()
microwave_2 = Microwave2()
microwave_3 = Microwave3()
remote_control_1 = RemoteControl(microwave_1)
remote_control_1.set_time("01:00")
remote_control_2 = RemoteControl(microwave_2)
remote_control_2.add_time("90s")
remote_control_3 = RemoteControl(microwave_3)
remote_control_3.del_time("300s")
remote_control_3.add_time("100s")
assert remote_control_1.show_time() == "_1:00"
assert remote_control_2.show_time() == "01:3_"
assert remote_control_3.show_time() == "01:40"
print("Coding complete? Let's try tests!")