0% found this document useful (0 votes)
14 views8 pages

Assignment Programming

Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
14 views8 pages

Assignment Programming

Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 8

# pip install bcrypt

import bcrypt # Library for secure password hashing


import datetime

# Base(OOP concept is uesd) User class with common


attributes and methods
class User:
def __init__(self, username, password):
"""
Initialize a User with a username and hashed
password.
:param username: The user's username
:param password: The user's plaintext password
"""
self.username = username
self.password_hash =
bcrypt.hashpw(password.encode(), bcrypt.gensalt())

def verify_password(self, password):


"""
Verify if the provided password matches the stored
hash.
:param password: The plaintext password to verify
:return: True if the password is correct, False
otherwise
"""
return bcrypt.checkpw(password.encode(),
self.password_hash)

# Customer class inheriting from User(parent or base


class)
class Customer(User):
def __init__(self, username, password, email):
"""
Initialize a Customer with additional email attribute.
:param username: The customer's username
:param password: The customer's password
:param email: The customer's email address
"""
super().__init__(username, password)
self.email = email

# SalesRep class inheriting from User(parent or base


class)
class SalesRep(User):
def __init__(self, username, password):
"""
Initialize a Sales Representative.
:param username: The sales representative's
username
:param password: The sales representative's password
"""
super().__init__(username, password)
self.sales = [] # Track sales made by the sales
representative

def record_sale(self, customer_name, car_model,


price):
"""
Record a sale made by the sales representative.
:param customer_name: Name of the customer
:param car_model: Model of the car sold
:param price: Sale price of the car
"""
sale = {
'customer': customer_name,
'car_model': car_model,
'price': price,
'date': datetime.datetime.now().strftime('%Y-%m-%d
%H:%M:%S')
}
self.sales.append(sale)

# Car class to manage car details


class Car:
def __init__(self, model, year, price):
"""
Initialize a Car with model, year, price, and sold status.
:param model: Car model name
:param year: Year of manufacture
:param price: Price of the car
"""
self.model = model
self.year = year
self.price = price
self.sold = False # Indicates if the car is sold

# Main Car Sales Management System class


class CarSalesSystem:
def __init__(self):
"""
Initialize the Car Sales System with lists for users and
cars.
"""
self.customers = []
self.sales_reps = []
self.cars = []

def add_customer(self, username, password, email):


"""
Add a new customer to the system.
:param username: Customer's username
:param password: Customer's password
:param email: Customer's email
"""
self.customers.append(Customer(username,
password, email))

def add_sales_rep(self, username, password):


"""
Add a new sales representative to the system.
:param username: Sales representative's username
:param password: Sales representative's password
"""
self.sales_reps.append(SalesRep(username,
password))
def add_car(self, model, year, price):
"""
Add a new car to the system.
:param model: Car model name
:param year: Year of manufacture
:param price: Price of the car
"""
self.cars.append(Car(model, year, price))

def authenticate_user(self, username, password):


"""
Authenticate a user (customer or sales
representative).
:param username: Username of the user
:param password: Password of the user
:return: User object if authentication is successful,
None otherwise
"""
for user in self.customers + self.sales_reps:
if user.username == username and
user.verify_password(password):
return user
return None

def browse_cars(self):
"""
Display a list of available cars.
"""
print("\nAvailable Cars:")
for car in self.cars:
if not car.sold:
print(f"Model: {car.model}, Year: {car.year}, Price: $
{car.price}")
print()

def sell_car(self, sales_rep, customer_name,


car_model):
"""
Mark a car as sold and record the transaction.
:param sales_rep: SalesRep object
:param customer_name: Name of the customer buying
the car
:param car_model: Model of the car being sold
"""
for car in self.cars:
if car.model == car_model and not car.sold:
car.sold = True
sales_rep.record_sale(customer_name, car.model,
car.price)
print(f"Car '{car.model}' sold to {customer_name} by
{sales_rep.username}.\n")
return
print("Car not found or already sold.\n")

def show_sales(self, sales_rep):


"""
Display sales made by a sales representative.
:param sales_rep: SalesRep object
"""
print(f"\nSales by {sales_rep.username}:")
for sale in sales_rep.sales:
print(f"Customer: {sale['customer']}, Car:
{sale['car_model']}, Price: ${sale['price']}, Date:
{sale['date']}")
print()

# Simulate system usage


if __name__ == '__main__':
# Initialize the system
system = CarSalesSystem()

# Add sample data


system.add_customer("customer01", "Custom@01",
"customer01@gmail.com")
system.add_sales_rep("salesrep01", "Sales@01")
system.add_car("Toyota", 2021, 50000)
system.add_car("BMW", 2024, 300000)
system.add_car("Ferrari", 2018, 80000)

# User interaction loop


print("Welcome to the Car Sales Management
System")
while True:
print("\n1. Login\n2. Browse Cars\n3. Exit")
choice = input("Enter your choice: ")

if choice == "1":
username = input("Enter username: ")
password = input("Enter password: ")
user = system.authenticate_user(username,
password)

if user:
if isinstance(user, Customer):
print(f"\nWelcome, Customer {user.username}!")
system.browse_cars()
elif isinstance(user, SalesRep):
print(f"\nWelcome, Sales Representative
{user.username}!")
while True:
print("\n1. View Available Cars\n2. Sell a Car\n3. View
Sales\n4. Logout")
rep_choice = input("Enter your choice: ")
if rep_choice == "1":
system.browse_cars()
elif rep_choice == "2":
customer_name = input("Enter customer name: ")
car_model = input("Enter car model to sell: ")
system.sell_car(user, customer_name, car_model)
elif rep_choice == "3":
system.show_sales(user)
elif rep_choice == "4":
break
else:
print("Invalid choice. Try again.")
else:
print("Invalid username or password. Try again.")
elif choice == "2":
system.browse_cars()
elif choice == "3":
print("Exiting the system. Goodbye!")
break
else:
print("Invalid choice. Try again.")

# Test Data for the Car Sales Management System

# Customers
# Username: customer01, Password: Custom@01,
Email: customer01@gmail.com
# Username: customer02, Password: Custom@02,
Email: customer02@gmail.com

# Sales Representatives
# Username: salesrep01, Password: Sales@01
# Username: salesrep02, Password: Sales@02

# Cars
# Model: Toyota, Year: 2021, Price: $50,000, Sold: No
# Model: BMW, Year: 2024, Price: $300,000, Sold: No
# Model: Ferrari, Year: 2018, Price: $80,000, Sold: No

# How to Use Test Data

# Scenario 1: Customer Login


# 1. Enter:
# - Username: customer01
# - Password: Custom@01
# 2. Browse the list of available cars.

# Scenario 2: Sales Representative Login


# 1. Enter:
# - Username: salesrep01
# - Password: Sales@01
# 2. After login, the Sales Representative can:
# - View available cars.
# - Sell a car (e.g., sell BMW to a customer like
customer01).
# - View sales transactions.

# Scenario 3: Car Sale


# 1. Login as salesrep01 (Sales Representative).
# 2. Sell Toyota to customer01:
# - Enter customer name: customer01
# - Enter car model: Toyota

# After the sale, the Toyota car should no longer


appear in the available cars list.

# Scenario 4: View Sales Transactions


# 1. Login as salesrep01.
# 2. View sales transactions to verify:
# - Customer: customer01
# - Car: Toyota
# - Price: $50,000
# - Date: Automatically recorded during the
transaction.

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy