-
Notifications
You must be signed in to change notification settings - Fork 0
/
quiz2_sda.py
162 lines (121 loc) · 3.82 KB
/
quiz2_sda.py
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
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
class Queue:
def __init__(self):
self.data = []
def __repr__(self):
return str(self.data)
def enqueue(self, element):
self.data.append(element)
def dequeue(self):
return self.data.pop(0)
def front(self):
return self.data[0]
def rear(self):
return self.data[-1]
def isEmpty(self):
return len(self.data) == 0
def size(self):
return len(self.data)
def printQueue(self):
for i in range(len(self.data)):
print(f'{i+1}. {self.data[i]}')
def clear(self):
self.data = []
def printMenu():
print('1. Add new vehicle to queue')
print('2. Remove vehicle from queue')
print('3. Vehicle terdepan')
print('4. Check is queue empty')
print('5. Length of queue')
print('6. Exit program')
if __name__ == "__main__":
print('>>> Welcome to Parking System ')
qq = Queue()
while True:
printMenu()
menu = int(input())
if menu == 1:
a = input("Input to queue: ")
qq.enqueue(a)
elif menu == 2:
print(qq.dequeue(), 'Removed from queue')
elif menu == 3:
print('kendaraan terdepan adalah:', qq.front() )
elif menu == 4:
if qq.isEmpty() == True:
print('Queue is empty')
elif menu == 5:
print('length of queue:', qq.size())
elif menu == 6:
print('>>> Thank you for using our program')
print('>>> See You')
break
else:
print('>>> Invalid Menu!')
##################################################################
class Nasabah:
def __init__(self, name, prio):
self.name = name
self.prio = prio
class PriorityQueue:
def __init__(self):
self.queue = []
def isEmpty(self):
return len(self.queue) == 0
def insert(self, data):
self.queue.append(data)
def front(self):
if self.isEmpty():
return None
else:
maxVal = 0
for i in range(len(self.queue)):
if self.queue[i].prio > self.queue[maxVal].prio:
maxVal = i
item = self.queue[maxVal]
return item
def size(self):
return len(self.queue)
def delete(self):
if self.isEmpty():
return None
else:
maxVal = 0
for i in range(len(self.queue)):
if self.queue[i].prio > self.queue[maxVal].prio:
maxVal = i
item = self.queue[maxVal]
del self.queue[maxVal]
return item
def printMenu():
print('1. Add new Nasabah to queue')
print('2. Remove Nasabah from queue')
print('3. Nasabah prioritas tertinggi')
print('4. Check is queue empty')
print('5. Length of queue')
print('6. Exit program')
if __name__ == "__main__":
print('>>> Welcome to Bank System ')
qq = PriorityQueue()
while True:
printMenu()
menu = int(input())
if menu == 1:
a = input("Input name: ")
b = int(input("Input priority: "))
c = Nasabah(a, b)
qq.insert(c)
elif menu == 2:
print(qq.delete(), 'Removed from queue')
elif menu == 3:
print('Nasabah prioritas tertinggi adalah:', qq.front().name )
elif menu == 4:
if qq.isEmpty() == True:
print('Queue is empty')
elif menu == 5:
print('Banyaknya nasabah:', qq.size())
elif menu == 6:
print('>>> Thank you for using our program')
print('>>> See You')
break
else:
print('>>> Invalid Menu!')