13.2 - Username Generator
Problem
A program is required to generate usernames from the users data.
Write a program that will read the data of 20 customers from customers.txt (forename, surname, dob) into an array of records. The program should then generate and display a username for each customer.
username = forename initial + surname initial + dob year
Example – John,Smith,12/03/2008
username = JS2008
Example Run
JS2008 EJ2007 MB2008 SD2007 DW2008 OT2007 JA2008 ST2007 WJ2008 AW2007 EH2008 IM2007 LT2008 MG2007 AM2008 AR2007 BC2008 HR2007 HL2008 EL2007
John,Smith,12/03/2008 Emily,Johnson,25/07/2007 Michael,Brown,18/11/2008 Sarah,Davies,02/05/2007 Daniel,Wilson,30/09/2008 Olivia,Taylor,14/01/2007 James,Anderson,22/06/2008 Sophia,Thomas,08/12/2007 William,Jackson,17/04/2008 Ava,White,03/10/2007 Ethan,Harris,29/02/2008 Isla,Martin,11/08/2007 Lucas,Thompson,06/03/2008 Mia,Garcia,21/07/2007 Alexander,Martinez,15/11/2008 Amelia,Robinson,09/01/2007 Benjamin,Clark,27/05/2008 Harper,Rodriguez,13/09/2007 Henry,Lewis,04/02/2008 Ella,Lee,19/06/2007
| 1 | Read data into array of records | IN | |
| OUT | customers(forename, surname ,dob) | ||
| 2 | Generate username for each customer | IN | customers(forename, surname ,dob) |
| OUT |
2.1 for i from 0 to length of customers - 1 2.2 set forename_initial to first letter of customers[i].forename 2.3 set surname_initial to first letter of customers[i].surname 2.4 set year to characters 6 to 9 of customers[i].dob 2.5 set username to forename_initial + surname_initial + year 2.6 display username 2.7 end for
# Main customers = read_data() generate_usernames(customers)
from dataclasses import dataclass
@dataclass
class customer:
forename: str = ""
surname: str = ""
dob: str = ""
def read_data():
customers= [customer() for i in range(20)]
file = open("customers.txt", "r")
for i in range(20):
customers[i].forename, customers[i].surname, customers[i].dob = file.readline().strip().split(",")
file.close()
return customers
def generate_usernames(customers):
for i in range(len(customers)):
forename_initial = customers[i].forename[0]
surname_initial = customers[i].surname[0]
year = customers[i].dob[6:10]
username = forename_initial + surname_initial + year
print(username)
# Main
customers = read_data()
generate_usernames(customers)
from dataclasses import dataclass
# Define a record structure to store customer details
@dataclass
class customer:
forename: str = ""
surname: str = ""
dob: str = "" # Format: DD/MM/YYYY
# Function to read data from file into an array of records
def read_data():
# Create an array of records with 3 empty entries
customers = [customer() for i in range(20)]
# Open file for reading
file = open("customers.txt", "r")
# Read each line and store values in the array of records
for i in range(20):
customers[i].forename, customers[i].surname, customers[i].dob = file.readline().strip().split(",")
# Close file
file.close()
# Return the array of records
return customers
# Function to generate usernames from the array of records
def generate_usernames(customers):
# Loop through each record in the array
for i in range(len(customers)):
# Get first letter of forename
forename_initial = customers[i].forename[0]
# Get first letter of surname
surname_initial = customers[i].surname[0]
# Extract year from date of birth
year = customers[i].dob[6:10]
# Combine to form username
username = forename_initial + surname_initial + year
# Output username
print(username)
# Main program
# Read data into array of records
customers = read_data()
# Generate and display usernames
generate_usernames(customers)
Extension 1
Add additional code so that usernames are stored within an array within the record.
Extension 2
Add an additional procedure that allows the user to search by username and display the persons details IF the username exists.
If the username can’t be found, an error message should be displayed.