To create a JavaScript program that handles user data entries with functionalities to save, display, search, and delete records, you can structure your code in the following way. We'll use an in-memory array to simulate a database. This example is kept simple for demonstration, and in a real-world application, you would typically interact with a server-side database using APIs.
Here's a basic implementation:
// Simulated in-memory database
let database = [];
// Function to add a new record
function addRecord(name, fatherName, nationalNumber, dateOfBirth, educationalQualification, telephone, position, address) {
const newRecord = {
name,
fatherName,
nationalNumber,
dateOfBirth,
educationalQualification,
telephone,
position,
address
};
database.push(newRecord);
console.log('Record added successfully!');
}
// Function to display all records
function displayRecords() {
console.log('Database Records:');
if (database.length === 0) {
console.log('No records found.');
} else {
database.forEach((record, index) => {
console.log(`Record ${index + 1}:`, record);
});
}
}
// Function to search records (case insensitive search)
function searchRecords(query) {
console.log(`Searching for records containing: "${query}"`);
const results = database.filter(record => {
return Object.values(record).some(value =>
String(value).toLowerCase().includes(query.toLowerCase())
);
});
if (results.length === 0) {
console.log('No matching records found.');
} else {
results.forEach((record, index) => {
console.log(`Found Record ${index + 1}:`, record);
});
}
}
// Function to delete a record by national number
function deleteRecord(nationalNumber) {
const initialLength = database.length;
database = database.filter(record => record.nationalNumber !== nationalNumber);
if (database.length < initialLength) {
console.log(`Record with national number ${nationalNumber} deleted successfully!`);
} else {
console.log(`No record found with national number: ${nationalNumber}`);
}
}
// Example Usage
addRecord('John Doe', 'Richard Roe', '1234567890', '1990-01-01', 'BSc Computer Science', '123456789', 'Software Engineer', '123 Main St, Anytown');
addRecord('Jane Smith', 'Alan Smith', '0987654321', '1992-02-02', 'MSc Information Systems', '987654321', 'Data Analyst', '456 Elm St, Anycity');
displayRecords();
searchRecords('Jane');
deleteRecord('1234567890');
displayRecords();
In this code:
addRecord adds a new entry to the database array.
displayRecords prints all records to the console.
searchRecords searches through records based on a given query string and prints out matching entries.
deleteRecord removes a specific record from the database based on the nationalNumber.
This example uses straightforward console.log statements for simplicity. In practice, you might use a user interface and possibly integrate with a backend database for storage and management of more complex datasets.