Object-oriented programming · 22 / 26 Exercise Medium

Instance counter

Objective

Instance attributes are different in each object; class attributes are shared by all of them. A @classmethod works with the class and a @staticmethod is an ordinary function grouped inside it.

Instructions

  • Define the class Product with a class attribute total (it starts at 0) that increases by 1 each time a product is created, and instance attributes name and price.
  • Add a class method how_many() that returns the total, and a static method with_vat(price, vat=21) that returns the price with VAT applied (21 by default).
  • Read n and then n lines name price. Create each product and show name: price -> price with VAT (two decimals).
  • At the end show Total created: n.
Input
2
pan 1.50
leche 0.90
Output
pan: 1.50 -> 1.81
leche: 0.90 -> 1.09
Total created: 2

Open the editor in full screen: the statement on the left and your code, the output and the tests on the right.

Start exercise