# Notes from Monday, November 13

# An example of inheritance

#-----------------------------------------------------------------
# A Clock keeps track of the current time.

class Clock:

    def __init__(self):
        self.time = 0

    def __str__(self):
        return 'Clock: Time is %d' % self.time

    def reset(self):
        self.time = 0

    def tick(self):
        self.time = self.time + 1
        return self.time

#-------------------------------------------------------------------
# An AlarmClock is a special kind of Clock that also keeps track of
# the alarm time and whether the alarm is set, in addition to the
# current time.

class AlarmClock(Clock):

    def __init__(self):
        # call the constructor of the base class to initialize time
        Clock.__init__(self)
        self.alarmTime = 0
        self.alarmSet = False

    def __str__(self):
        if self.alarmSet:
            return "AlarmClock: Time is %d, Alarm set for %d" % \
                   (self.time, self.alarmTime)
        else:
            return "AlarmClock: Time is %d, Alarm not set" % self.time

    def setAlarm(self, newTime):
        self.alarmTime = newTime
        self.alarmSet = True
        print 'Alarm is set for %d' % self.alarmTime

    def alarmOff(self):
        if not self.alarmSet:
            print 'Alarm is already off'
        else:
            self.alarmSet = False
            print 'Alarm is now off'

    def tick(self):
        # call the tick method of the base class to update time
        Clock.tick(self)
        if self.alarmSet and self.time >= self.alarmTime:
            print 'Riiiiiing!!!!'
        return self.time
    
#-------------------------------------------------------------------
# A ClockRadio is a special kind of AlarmClock that keeps track of
# radio stations, in addition to the current time and the alarm.

class ClockRadio(AlarmClock):

    def __init__(self):
        # call the constructor of the base class
        AlarmClock.__init__(self)
        self.band = 'AM'
        self.AMfreq = 530
        self.FMfreq = 88.1

    def __str__(self):
        if self.band == 'FM':
            return "ClockRadio: Time is %d, Tuned to FM %.1f" % \
                   (self.time, self.FMfreq)
        else:
            return "ClockRadio: Time is %d, Tuned to AM %d" % \
                   (self.time, self.AMfreq)

    def toggle(self):
        if self.band == 'FM':
            self.band = 'AM'
        else:
            self.band = 'FM'
        print self

    def setFreq(self, newFreq):
        if self.band == 'FM':
            assert 88.1 <= newFreq <= 107.9, "invalid FM frequency"
            self.FMfreq = newFreq
        else:
            assert 530 <= newFreq <= 1620, "invalid AM frequency"
            self.AMfreq = newFreq
        print self

#-------------------------------------------------------------------
# Example:
#
# >>> c = ClockRadio()
# >>> c.setAlarm(3)
# Alarm is set for 3
# >>> print c
# ClockRadio: Time is 0, Tuned to AM 530
# >>> c.toggle()
# ClockRadio: Time is 0, Tuned to FM 88.1
# >>> c.tick()
# 1
# >>> c.tick()
# 2
# >>> c.tick()
# Riiiiiing!!!!
# 3
# >>> c.tick()
# Riiiiiing!!!!
# 4
# >>> c.alarmOff()
# Alarm is now off
# >>> c.tick()
# 5
# >>> c.setFreq(101.1)
# ClockRadio: Time is 5, Tuned to FM 101.1
# >>>

