forked from IrvKalb/Object-Oriented-Python-Code
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathTV.py
More file actions
103 lines (84 loc) · 2.79 KB
/
Copy pathTV.py
File metadata and controls
103 lines (84 loc) · 2.79 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
# TV class
class TV():
def __init__(self):
self.isOn = False
self.isMuted = False
# Some default list of channels
self.channelList = [2, 4, 5, 7, 9, 11, 20, 36, 44, 54, 65]
self.nChannels = len(self.channelList)
self.channelIndex = 0
self.VOLUME_MINIMUM = 0 # constant
self.VOLUME_MAXIMUM = 10 # constant
self.volume = self.VOLUME_MAXIMUM // 2 # integer divide
def power(self):
self.isOn = not self.isOn # toggle
def volumeUp(self):
if not self.isOn:
return
if self.isMuted:
self.isMuted = False # changing the volume while muted unmutes the sound
if self.volume < self.VOLUME_MAXIMUM:
self.volume = self.volume + 1
def volumeDown(self):
if not self.isOn:
return
if self.isMuted:
self.isMuted = False # changing the volume while muted unmutes the sound
if self.volume > self.VOLUME_MINIMUM:
self.volume = self.volume - 1
def channelUp(self):
if not self.isOn:
return
self.channelIndex = self.channelIndex + 1
if self.channelIndex == self.nChannels:
self.channelIndex = 0 # wrap around to the first channel
def channelDown(self):
if not self.isOn:
return
self.channelIndex = self.channelIndex - 1
if self.channelIndex < 0:
self.channelIndex = self.nChannels - 1 # wrap around to the top channel
def mute(self):
if not self.isOn:
return
self.isMuted = not self.isMuted
def setChannel(self, newChannel):
if newChannel in self.channelList:
self.channelIndex = self.channelList.index(newChannel)
# if the newChannel is not in our list of channels, don't do anything
def showInfo(self):
print()
print('TV Status:')
if self.isOn:
print(' TV is: On')
print(' Channel is:', self.channelList[self.channelIndex])
if self.isMuted:
print(' Volume is:', self.volume, '(sound is muted)')
else:
print(' Volume is:', self.volume)
else:
print(' TV is: Off')
# Test code
oTV = TV() # create the TV object
# Turn the TV on and show the status
oTV.power()
oTV.showInfo()
# Change the channel up twice, and raise the volume twice, show status
oTV.channelUp()
oTV.channelUp()
oTV.volumeUp()
oTV.volumeUp()
oTV.showInfo()
# Turn the TV off, show status, turn TV on, show status
oTV.power()
oTV.showInfo()
oTV.power()
oTV.showInfo()
# Lower the volume, mute the sound, show status
oTV.volumeDown()
oTV.mute()
oTV.showInfo()
# Change the Channel to 11
oTV.setChannel(11)
oTV.mute()
oTV.showInfo()