A) Algorithm Explanation
A) Algorithm Explanation
Pseudo Code:
Class Customer:
Method __init__(id, name, age, email, phone):
Set self.id = id
Set self.name = name
Set self.age = age
Set self.email = email
Set self.phone = phone
Method __repr__():
Return formatted string with customer details
Class CustomerDataProcessor:
Method __init__(csv_file, json_file):
Set self.csv_file = csv_file
Set self.json_file = json_file
Initialize self.customers with predefined customer data
Method read_data():
Call read_from_csv()
Call read_from_json()
Method read_from_csv():
If file exists at self.csv_file:
Open self.csv_file in read mode
Create CSV reader object
Create a set of existing customer IDs
For each row in reader:
If row['id'] not in existing_ids:
Append new Customer object to self.customers
Method read_from_json():
If file exists at self.json_file:
Open self.json_file in read mode
Load JSON data
Create a set of existing customer IDs
For each customer in data:
If customer['id'] not in existing_ids:
Append new Customer object to self.customers
Method write_data():
Call write_to_csv()
Call write_to_json()
Method write_to_csv():
Try:
Open self.csv_file in write mode
Create CSV writer object
Write CSV header
Write customer data to CSV
Print success message
Except Exception:
Print error message
Method write_to_json():
Try:
Open self.json_file in write mode
Write customer data to JSON
Print success message
Except Exception:
Print error message
Method add_customer():
Try:
Generate new customer ID
Prompt user for customer name
Validate and prompt user for customer age
Prompt user for customer email
Prompt user for customer phone
Append new Customer object to self.customers
Print success message
Except Exception:
Print error message
Method update_customer():
Try:
Prompt user for customer ID to update
Find customer by ID
If customer found:
Prompt user for new customer name or use existing
Validate and prompt user for new customer age or use existing
Prompt user for new customer email or use existing
Prompt user for new customer phone or use existing
Print success message
Else:
Print customer not found message
Except Exception:
Print error message
Method delete_customer():
Try:
Prompt user for customer ID to delete
Remove customer with matching ID from self.customers
Print success message
Except Exception:
Print error message
Method display_customers():
If self.customers is empty:
Print no customers to display
Else:
For each customer in self.customers:
Print customer details
Method validate_integer_input(prompt, default=None):
While True:
Try:
Prompt user for input
If input is empty and default is not None:
Return default
Return input converted to integer
Except ValueError:
Print invalid input message
Function main():
Define csv_file and json_file paths
Create instance of CustomerDataProcessor
Call processor.read_data()
While True:
Print menu options
Prompt user for choice
If choice is '1':
Call processor.display_customers()
Else if choice is '2':
Call processor.add_customer()
Else if choice is '3':
Call processor.update_customer()
Else if choice is '4':
Call processor.delete_customer()
Else if choice is '5':
Call processor.write_data()
Else if choice is '6':
Break loop
Else:
Print invalid choice message
Call main()
Algorithm Explanation:
The Customer Records Management System is designed to facilitate the management of customer
data through various operations such as adding, updating, and deleting customer records, as well
as displaying all existing records and saving data to CSV and JSON files.
Customer Class:
This class represents a customer entity with attributes such as ID, name, age, email, and phone
number. The __init__ method initializes customer objects with provided attributes, while the
__repr__ method provides a string representation of customer details.
CustomerDataProcessor Class:
Responsible for handling customer data processing, this class includes methods for reading from
and writing to CSV and JSON files, managing customer operations (add, update, delete), validating
user input, and displaying customer records.
Main Function:
The main function serves as the entry point of the program. It creates an instance of the
CustomerDataProcessor class, reads existing data from files (if any), and presents a menu to the
user for performing various operations on customer records.
b) Coding Practice:
Modularization:
The program is structured into classes and functions, promoting modularity and code reuse. Each
class encapsulates specific functionalities, enhancing maintainability and readability.
Meaningful Naming:
Descriptive names are employed for variables, methods, and classes, contributing to code clarity
and comprehension. For instance, variables like csv_file and method names like add_customer
convey their purposes effectively.
Error Handling:
The program incorporates robust error handling mechanisms to catch and manage exceptions
gracefully. Informative error messages guide users in rectifying input mistakes or addressing file-
related issues.
Input Validation:
User input validation is implemented to ensure the integrity of data. Techniques like validating
integer inputs prevent invalid data from causing errors and maintain data consistency.
List Comprehensions:
List comprehension is utilized efficiently to process lists and dictionaries, resulting in concise and
readable code.
File Operations:
Managing file operations such as reading and writing CSV and JSON files required careful error
handling. By employing try-except blocks, the program gracefully handles file-related exceptions
and provides informative error messages to users.
Conclusion:
The Customer Records Management System provides essential features for CRUD operations on
customer records, ensuring data integrity and ease of use. Its modular design, adherence to coding
best practices, and comprehensive error handling make it a reliable and user-friendly solution for
managing customer data. Future enhancements could include additional functionalities like sorting,
searching, or filtering customer records to meet evolving business requirements.