We are going to train our OOP skill by implementing a few classes, which will represent a cash desk.
The cash desk will do the following things:
- Take money as single bills
- Take money as batches (пачки!)
- Keep a total count
- Tell us some information about the bills it has
Create a class, called Bill which takes one parameter to its constructor - the amount of the bill - an integer.
This class will only have dunders so you wont be afraid of them anymore!
The class should implement:
__str__and__repr____int____eq__and__hash__- If amount is negative number, raise an
ValueErrorerror. - If type of amount isn't
int, raise anTypeErrorerror.
Here is an example usage:
from cashdesk import Bill
a = Bill(10)
b = Bill(5)
c = Bill(10)
int(a) # 10
str(a) # "A 10$ bill"
print(a) # A 10$ bill
a == b # False
a == c # True
money_holder = {}
money_holder[a] = 1 # We have one 10% bill
if c in money_holder:
money_holder[c] += 1
print(money_holder) # { "A 10$ bill": 2 }We are going to implement a class, which represents more than one bill. A BatchBill!
The class takes a list of Bills as the single constructor argument.
The class should have the following methods:
__len__(self)- returns the number ofBillsin the batchtotal(self)- returns the total amount of allBillsin the batch
We should be able to iterate the BatchBill class with a for-loop.
Here is an example:
from cashdesk import Bill, BillBatch
values = [10, 20, 50, 100]
bills = [Bill(value) for value in values]
batch = BillBatch(bills)
for bill in batch:
print(bill)
# A 10$ bill
# A 20$ bill
# A 50$ bill
# A 100$ billIn order to do that, you need to implement the following method:
def __getitem__(self, index):
passFinally, implement a CashDesk class, which has the following methods:
take_money(money), wheremoneycan be eitherBillorBatchBillclasstotal()- returns the total amount of money currenly in the deskinspect()- prints a table representation of the money - for each bill, how many copies of it we have.
For example:
from cashdesk import Bill, BillBatch, CashDesk
values = [10, 20, 50, 100, 100, 100]
bills = [Bill(value) for value in values]
batch = BillBatch(bills)
desk = CashDesk()
desk.take_money(batch)
desk.take_money(Bill(10))
print(desk.total()) # 390
desk.inspect()
# We have a total of 390$ in the desk
# We have the following count of bills, sorted in ascending order:
# 10$ bills - 2
# 20$ bills - 1
# 50$ bills - 1
# 100$ bills - 3