Exploring the World of Car Rental Management: Building a Python Application - Part 10
Article 10: Adding New Customers
Welcome back to our journey in building a Python-based car rental management system. In Part 10, we'll focus on adding new customers to the system, explaining the process and how we generate unique customer IDs.
The Significance of Adding Customers
Adding customers to our car rental management system is a fundamental step. It enables us to keep track of customer information, reservations, and interactions. To ensure every customer has a unique identity within our system, we generate unique customer IDs.
Generating Unique Customer IDs
Let's start by generating unique customer IDs. We'll follow the convention of 'C' followed by a number. For example, the first customer will have a customer ID 'C1', the second 'C2', and so on. Here's how we achieve this:
import mysql.connector
# Establish a connection to the MySQL database (code from Part 9)
mycon = mysql.connector.connect(**db_config)
mycursor = mycon.cursor()
# SQL query to fetch the latest customer ID
sql = 'SELECT c_id FROM customer ORDER BY LENGTH(c_id), c_id DESC LIMIT 1'
mycursor.execute(sql)
cgen = mycursor.fetchone()
if cgen:
# Extract the numeric part of the customer ID and increment it
cgen = int(cgen[0][1:])
c_id = 'C' + str(cgen + 1)
else:
# If there are no existing customers, start with 'C1'
c_id = 'C1'
In this code snippet, we first establish a connection to the MySQL database (code from Part 9). We then query the database to fetch the latest customer ID. If there are existing customers, we extract the numeric part of the ID, increment it, and create a new customer ID. If there are no existing customers, we start with 'C1'.
Adding New Customers
Now, let's dive into the process of adding new customers to the system:
# Input customer details
name = input('Enter your name: ').upper()
mobile_1 = int(input('Enter mobile 1: '))
# Check if the mobile number already exists in the database
sql = 'SELECT * FROM customer WHERE mobile_1 = %d' % mobile_1
mycursor.execute(sql)
if mycursor.rowcount:
print('Mobile number already exists! Please try again.')
return
mobile_2 = int(input('Enter mobile 2: '))
# Input and validate email
while True:
email = input('Enter email address: ').lower()
sql = "SELECT * FROM customer WHERE email='%s'" % email
mycursor.execute(sql)
if mycursor.rowcount:
print("This email already exists! Please try again.")
else:
break
address = input('Enter address: ').upper()
# Prepare data for insertion
data = (c_id, name, mobile_1, mobile_2, email, address, 0, 'I')
# SQL query to insert customer data into the 'customer' table
sql = "INSERT INTO customer (c_id, name, mobile_1, mobile_2, email, address, reservation_count, status) VALUES (%s, %s, %s, %s, %s, %s, %s, %s)"
mycursor.execute(sql, data)
mycon.commit()
mycon.close()
print('\nCustomer successfully added.')
print('Your Customer ID is', c_id)
In this code block, we collect customer details such as name, mobile numbers, email, and address. We perform validation checks to ensure that mobile numbers and email addresses are unique. If they exist, we prompt the user to try again. Finally, we insert the customer data into the 'customer' table in our database.
Conclusion
In Part 10, we learned how to add new customers to our car rental management system and generate unique customer IDs. We also introduced validation checks to prevent the creation of duplicate entries.
Part 11 will continue our exploration of this project by updating Customer Information , and giving a step-by-step guide to updating customer details.
The Link to my code -> [github.com/bryanspacex/Rentals] (constantly updated)