-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathchallenge_printer_my_solution.py
77 lines (59 loc) · 2.18 KB
/
challenge_printer_my_solution.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
#!usr/bin/env python3
import random
from data_structures.custom_queue import CustomQueue
# create three classes that, together, model how a printer could take print
# jobs out of a print queue. There are a few requirements.
# 1. The first is a class called "PrintQueue" that follows the queue data
# structure implementation.
# 2. The second requirement would be a class called "Job", that has a "pages"
# attribute and each job can have one to 10 pages. You can assign this number
# randomly in your program. The Job class should also have a "print_page"
# method that decrements "pages" and a "check_complete" method, which will
# check whether or not all the pages have been printed.
# 3. The third requirement is a "Printer" class. The "Printer" class should
# have a "get_job" method that makes use of the queues built-in dequeue
# method to take the first job in the PrintQueue off of the queue.
class PrintQueue(CustomQueue):
def __init__(self):
super().__init__()
class Job:
def __init__(self, job_name):
self._name = job_name
self._pages = random.randint(1, 10)
def print_page(self):
if self._pages > 0:
print('.', end=' ')
self._pages -= 1
def check_complete(self):
return self._pages == 0
def __repr__(self):
return f'Job <name: {self._name} pages:{self._pages}>'
class Printer:
def __init__(self):
self._queue = PrintQueue()
def add_job(self, job):
if not isinstance(job, Job):
raise TypeError('the job should be of type Job')
self._queue.enqueue(job)
def get_job(self):
while not self._queue.is_empty():
job: Job = self._queue.dequeue()
print(f'Printing pages of: {str(job)}')
while job.check_complete() is False:
job.print_page()
print('\n')
return None
printer = Printer()
printer.add_job(Job('job1'))
printer.add_job(Job('job2'))
printer.add_job(Job('job3'))
printer.get_job()
# CONSOLE OUTPUT (it will vary!):
# Printing pages of: Job <name: job1 pages:3>
# . . .
#
# Printing pages of: Job <name: job2 pages:2>
# . .
#
# Printing pages of: Job <name: job3 pages:5>
# . . . . .