From 542732984fb53556c6fd5d6f03563fcc9f41bf3e Mon Sep 17 00:00:00 2001 From: Dimitri Merejkowsky Date: Thu, 14 Jul 2022 15:51:20 +0200 Subject: [PATCH] Add Python version --- python/.gitignore | 1 + python/movierental/__init__.py | 0 python/movierental/customer.py | 43 ++++++++++++++++ python/movierental/movie.py | 8 +++ python/movierental/rental.py | 4 ++ python/tests/test_customer.py | 89 ++++++++++++++++++++++++++++++++++ 6 files changed, 145 insertions(+) create mode 100644 python/.gitignore create mode 100644 python/movierental/__init__.py create mode 100644 python/movierental/customer.py create mode 100644 python/movierental/movie.py create mode 100644 python/movierental/rental.py create mode 100644 python/tests/test_customer.py diff --git a/python/.gitignore b/python/.gitignore new file mode 100644 index 0000000..bee8a64 --- /dev/null +++ b/python/.gitignore @@ -0,0 +1 @@ +__pycache__ diff --git a/python/movierental/__init__.py b/python/movierental/__init__.py new file mode 100644 index 0000000..e69de29 diff --git a/python/movierental/customer.py b/python/movierental/customer.py new file mode 100644 index 0000000..17ae1ae --- /dev/null +++ b/python/movierental/customer.py @@ -0,0 +1,43 @@ +from .movie import Movie + + +class Customer: + def __init__(self, name): + self.name = name + self.rentals = [] + + def statement(self): + total_amount = 0 + frequent_renter_points = 0 + result = f"Rental Record for {self.name}\n" + for each in self.rentals: + this_amount = 0 + + # determine amounts for each line + if each.movie.price_code == Movie.REGULAR: + this_amount += 2 + if each.days_rented > 2: + this_amount += (each.days_rented - 2) * 1.5 + elif each.movie.price_code == Movie.NEW_RELEASE: + this_amount += each.days_rented * 3 + elif each.movie.price_code == Movie.CHILDRENS: + this_amount += 1.5 + if each.days_rented > 3: + this_amount += (each.days_rented - 3) * 1.5 + + # add frequent renter points + frequent_renter_points += 1 + + # add bonus for a two day new release rental + if each.movie.price_code == Movie.NEW_RELEASE and each.days_rented > 1: + frequent_renter_points += 1 + + # show figures for this rental + result += f"\t{each.movie.title}\t{this_amount:.1f}\n" + total_amount += this_amount + + # add footer lines + result += f"Amount owed is {total_amount:.1f}\n" + result += f"You earned {frequent_renter_points} frequent renter points" + + return result diff --git a/python/movierental/movie.py b/python/movierental/movie.py new file mode 100644 index 0000000..c17ed60 --- /dev/null +++ b/python/movierental/movie.py @@ -0,0 +1,8 @@ +class Movie: + CHILDRENS = 2 + NEW_RELEASE = 1 + REGULAR = 0 + + def __init__(self, title, *, price_code): + self.title = title + self.price_code = price_code diff --git a/python/movierental/rental.py b/python/movierental/rental.py new file mode 100644 index 0000000..fdaf609 --- /dev/null +++ b/python/movierental/rental.py @@ -0,0 +1,4 @@ +class Rental: + def __init__(self, movie, *, days_rented): + self.movie = movie + self.days_rented = days_rented diff --git a/python/tests/test_customer.py b/python/tests/test_customer.py new file mode 100644 index 0000000..06b24f6 --- /dev/null +++ b/python/tests/test_customer.py @@ -0,0 +1,89 @@ +from movierental.customer import Customer +from movierental.movie import Movie +from movierental.rental import Rental + + +def test_add_rental(): + customer = Customer("Julia") + movie = Movie("Gone with the Wind", price_code=Movie.REGULAR) + rental = Rental(movie, days_rented=3) + customer.rentals.append(rental) + + assert len(customer.rentals) == 1 + + +def test_statement_for_regular_movie(): + customer = Customer("Sallie") + movie = Movie("Gone with the Wind", price_code=Movie.REGULAR) + rental = Rental(movie, days_rented=3) + customer.rentals.append(rental) + + expected = ( + "Rental Record for Sallie\n" + + "\tGone with the Wind\t3.5\n" + + "Amount owed is 3.5\n" + + "You earned 1 frequent renter points" + ) + + actual = customer.statement() + + assert actual == expected + + +def test_statement_for_new_release_movie(): + customer = Customer("Sallie") + movie = Movie("Star Wars", price_code=Movie.NEW_RELEASE) + rental = Rental(movie, days_rented=3) + customer.rentals.append(rental) + + expected = ( + "Rental Record for Sallie\n" + + "\tStar Wars\t9.0\n" + + "Amount owed is 9.0\n" + + "You earned 2 frequent renter points" + ) + + actual = customer.statement() + + assert actual == expected + + +def test_statement_for_childrens_movie(): + customer = Customer("Sallie") + movie = Movie("Madagascar", price_code=Movie.CHILDRENS) + rental = Rental(movie, days_rented=3) + customer.rentals.append(rental) + + expected = ( + "Rental Record for Sallie\n" + + "\tMadagascar\t1.5\n" + + "Amount owed is 1.5\n" + + "You earned 1 frequent renter points" + ) + + actual = customer.statement() + + assert actual == expected + + +def test_statement_for_many_movies(): + movie1 = Movie("Madagascar", price_code=Movie.CHILDRENS) + rental1 = Rental(movie1, days_rented=6) + movie2 = Movie("Star Wars", price_code=Movie.NEW_RELEASE) + rental2 = Rental(movie2, days_rented=2) + movie3 = Movie("Gone with the Wind", price_code=Movie.REGULAR) + rental3 = Rental(movie3, days_rented=8) + + customer = Customer("David") + customer.rentals = [rental1, rental2, rental3] + + expected = ( + "Rental Record for David\n" + + "\tMadagascar\t6.0\n" + + "\tStar Wars\t6.0\n" + + "\tGone with the Wind\t11.0\n" + + "Amount owed is 23.0\n" + + "You earned 4 frequent renter points" + ) + + assert customer.statement() == expected