# Invoice.py # Chapter 5 class Invoice: def __init__(self, subtotal, tax_rate): # pass the tax_rate 0.07 for 7% self.subtotal = subtotal self.tax_rate = tax_rate # instance methods page 55 ''' Level 1 defining and calling a function def total(st,tr): formula total(100,.07) ''' # level 2 use an instance method on the class def calculate_tax(self): return self.subtotal * self.tax_rate # (100,.07) assert 7.0 # instance method AND call another instance method def calculate_total(self): return self.subtotal + self.calculate_tax() # 100 + return 7.0 # instance method to display the invoice details # Subtotal: $100.00, Tax: $7.00, Total: $107.00 def display_invoice(self): return f"Subtotal: ${self.subtotal:.2f}, Tax: ${self.calculate_tax():.2f}, Total: ${self.calculate_total():.2f}" # program class invoice = Invoice(100, .07) print(invoice.display_invoice()) # Chapter 6 datetime class standard modules ''' datetime module several common classes to use from datetime module datetime class -> date and time values yyyy-mm-dd hh:mm:ss.mmmmmm date class -> date with time as empty yyyy-mm-dd 12:00:00... or 00:00:00 time class -> time hh:mm:ss.mmmmmm timedelta -> calculate time differences, add/sub date/time standard module that needs to be imported import datetime as dt import modulename as namespace dt.class from datetime import timedelta, datetime from modulename import objects import datetime import modulename ex. datetime.datetime.objName ''' import datetime as dt currentDateTime = dt.datetime.now() # module.class.function() print(currentDateTime) # 2024-12-12 15:44:11.723126 # date formatter date_format = '%m/%d/%Y' # create an invoice date from datetime stamp invoice_date = currentDateTime.strftime(date_format) print(invoice_date) # 12/12/2024 # calculate a due date # timedelta class to do date/time calculations due_date = currentDateTime + dt.timedelta(days=30) format_due_date = due_date.strftime(date_format) print(format_due_date) # 01/11/2025 # strftime vs strptime # strftime -> formats a datetime to a string data type # strptime -> parses a string data type and converts to datetime # Enter a ship date for the order # ALL inputs are data type as strings input_shipDate = input("Enter the ship date (YYYY-MM-DD): ") # convert the input to a datetime using data validation try: ship_date = dt.datetime.strptime(input_shipDate, '%Y-%m-%d') except ValueError: print("Invalid date format") print(ship_date) # 2024-12-12 00:00:00 # EOF Invoice.py