Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add Python version #4

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions python/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
__pycache__
Empty file added python/movierental/__init__.py
Empty file.
43 changes: 43 additions & 0 deletions python/movierental/customer.py
Original file line number Diff line number Diff line change
@@ -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
8 changes: 8 additions & 0 deletions python/movierental/movie.py
Original file line number Diff line number Diff line change
@@ -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
4 changes: 4 additions & 0 deletions python/movierental/rental.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
class Rental:
def __init__(self, movie, *, days_rented):
self.movie = movie
self.days_rented = days_rented
89 changes: 89 additions & 0 deletions python/tests/test_customer.py
Original file line number Diff line number Diff line change
@@ -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