Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

import csv from datetime import date, datetime class Customer: def _ _ init _ _ ( self , id , name, email, phone _ number,

import csv
from datetime import date, datetime
class Customer:
def __init__(self, id, name, email, phone_number, date_of_birth, address):
self.id = id
self.name = name
self.email = email
self.phone_number = phone_number
self.date_of_birth = date_of_birth
self.address = address
def __repr__(self):
return f"Customer(id={self.id}, name='{self.name}', email='{self.email}', phone_number='{self.phone_number}', date_of_birth='{self.date_of_birth}', address='{self.address}')"
def calculate_age(self, date_of_birth):
today = date.today()
birth_date = datetime.strptime(date_of_birth, "%Y-%m-%d")
age = today.year - birth_date.year -((today.month, today.day)<(birth_date.month, birth_date.day))
return age
def get_id(self):
return self.id
def get_name(self):
return self.name
def get_email(self):
return self.email
def get_phone_number(self):
return self.phone_number
def get_address(self):
return self.address
def set_name(self, new_name):
self.name = new_name
def set_email(self, new_email):
self.email = new_email
def set_phone_number(self, new_phone_number):
self.phone_number = new_phone_number
def set_address(self, new_address):
self.address = new_address
class CustomerManager:
def __init__(self):
self.customers =[]
def add_customer(self, customer):
self.customers.append(customer)
def remove_customer(self, customer):
self.customers.remove(customer)
def get_customers_by_age_range(self, min_age, max_age):
today = date.today()
customers_in_range =[]
for customer in self.customers:
birth_date = datetime.strptime(customer.date_of_birth, "%Y-%m-%d")
age = today.year - birth_date.year -((today.month, today.day)<(birth_date.month, birth_date.day))
if min_age <= age <= max_age:
customers_in_range.append(customer)
return customers_in_range
def get_customers_by_address(self, address):
customers_with_address =[]
for customer in self.customers:
if customer.address == address:
customers_with_address.append(customer)
return customers_with_address
def export_data(self, filename):
try:
with open(filename, mode='w', newline='') as file:
writer = csv.writer(file)
writer.writerow(['ID', 'Name', 'Email', 'Phone Number', 'Date of Birth', 'Address'])
for customer in self.customers:
writer.writerow([customer.id, customer.name, customer.email, customer.phone_number, customer.date_of_birth, customer.address])
except IOError as e:
print(f"An error occurred while exporting data: {e}")
# Function to get user input for creating a new customer
def create_customer():
customer_id = int(input("Enter customer ID: "))
customer_name = input("Enter customer name: ")
customer_email = input("Enter customer email: ")
customer_phone_number = input("Enter customer phone number: ")
customer_date_of_birth = input("Enter customer date of birth (YYYY-MM-DD): ")
customer_address = input("Enter customer address: ")
return Customer(customer_id, customer_name, customer_email, customer_phone_number, customer_date_of_birth, customer_address)
# Example usage
customer_manager = CustomerManager()
while True:
choice = input("Enter 'a' to add a new customer, 'q' to quit: ")
if choice.lower()=='a':
new_customer = create_customer()
customer_manager.add_customer(new_customer)
print("Customer added successfully.")
elif choice.lower()=='q':
break
else:
print("Invalid input. Please try again.")
# Export customer data to a CSV file
customer_manager.export_data("customers.csv") get user input

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Data And Information Quality Dimensions, Principles And Techniques

Authors: Carlo Batini, Monica Scannapieco

1st Edition

3319241060, 9783319241067

More Books

Students also viewed these Databases questions

Question

Be able to explain the concept of constructive discharge

Answered: 1 week ago