-
Notifications
You must be signed in to change notification settings - Fork 21
Apurva Shukla Module4 #70
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
apurvaone
wants to merge
18
commits into
hotwax:main
Choose a base branch
from
apurvaone:ApurvaShukla-Module4
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
18 commits
Select commit
Hold shift + click to select a range
26a63ea
Implemeted word count program
apurvaone dcf1735
Implemeted Employee Management program
apurvaone 425669f
Implemented Multithreading Program
apurvaone f9f7dfc
Added Serialization - Deserialization program
apurvaone 6321073
ScreenShots Added
apurvaone f50c26a
Unit tested Problem 1 and handled the edge cases: File not found, No …
apurvaone 235ce9e
Unit tests added, Handled edge case: File not found
apurvaone b7d6cb4
unit tests added
apurvaone 92eaba1
Replced the usage of let with const, made code more declerative
apurvaone 806bce8
Added unique email validation,added process.exit(0) to exit,used more…
apurvaone 6238fbf
Added descriptive variable name in Employee Management program
apurvaone 3d20755
Optimized the loading of Emails,added comments in Employee Management…
apurvaone cb673d2
Made variable names more descriptive in WordScraper program
apurvaone b5859ed
Made Multithreading program menu driven
apurvaone b4d8a60
Made variable name more decriptive in Multithreading program
apurvaone c85923b
Screenshots updated in Multithreading Program
apurvaone bf80505
Made Serialization Program menu driven
apurvaone 40c5fee
Screenshots updated in Serialization Program
apurvaone File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,298 @@ | ||
| // Import required modules | ||
| const filesystem = require('fs'); | ||
| const readline = require('readline'); | ||
| const fileName = 'employees.txt'; | ||
|
|
||
| // set to stored emails of registerd employees | ||
| const allEmails= new Set(); | ||
|
|
||
|
|
||
| // Define the Employee class | ||
| class Employee { | ||
| constructor(name, email, age, dob) { | ||
|
|
||
| // Set the instance variables | ||
| this.name = name; | ||
| this.email = email; | ||
| this.age = age; | ||
| this.dob = dob; | ||
| } | ||
| } | ||
|
|
||
| // Create a readline interface to read user input | ||
| const inputOutputReader = readline.createInterface({ | ||
| input: process.stdin, | ||
| output: process.stdout | ||
| }); | ||
|
|
||
|
|
||
| // Add an employee to the file | ||
| function addEmployee(employee) { | ||
|
|
||
|
|
||
| // Validate the input parameters | ||
| const { name, email, age, dob } = employee; | ||
|
|
||
| if (!/^[a-zA-Z ]+$/.test(name)) { | ||
| console.log('Name should only contain letters.'); | ||
| return; | ||
| } | ||
| if (!/\S+@\S+\.\S+/.test(email)) { | ||
| console.log('Email is not valid.'); | ||
| return; | ||
| } | ||
|
|
||
| // Check if email already exists | ||
| if (allEmails.has(email)) { | ||
| console.log('Email already exists.'); | ||
| return; | ||
| } | ||
|
|
||
| // Add email to the array | ||
|
|
||
| allEmails.add(email); | ||
|
|
||
| if (!Number.isInteger(age) ||age < 18 || age > 65) { | ||
| console.log('Age should be and between 18 and 65.'); | ||
| return; | ||
| } | ||
| if (!isValidDOB(dob)) { | ||
| console.log('Invalid date of birth.'); | ||
| return; | ||
| } | ||
|
|
||
| const employeeData = `${employee.name},${employee.email},${employee.age},${employee.dob}`; | ||
|
|
||
| filesystem.appendFileSync(fileName, `\n${employeeData}`, (err) => { | ||
| if (err) throw err; | ||
| console.log(`\n${employee.name} added successfully.`); | ||
| }); | ||
| } | ||
|
|
||
| // Delete an employee with the given email address | ||
| function deleteEmployee(email) { | ||
| if (!/\S+@\S+\.\S+/.test(email)) { | ||
| console.log('Email is not valid.'); | ||
| return; | ||
| } | ||
| const employees = loadEmployeesFromFile(fileName); | ||
| const index = employees.findIndex((employee) => employee.email === email); | ||
| if (index !== -1) { | ||
| employees.splice(index, 1); | ||
| writeEmployeesToFile(employees); | ||
| console.log('Employee deleted successfully.'); | ||
| } else { | ||
| console.log('Employee not found.'); | ||
| } | ||
| } | ||
|
|
||
|
|
||
| // Search for employees with the given query string | ||
| function searchEmployees(query, orderBy, direction) { | ||
|
|
||
| // Validate the orderBy and direction parameters | ||
| let validOrderBy = ['name', 'email', 'age', 'dob']; | ||
| if (!validOrderBy.includes(orderBy)) { | ||
| throw new Error('Invalid orderBy.'); | ||
| } | ||
|
|
||
| let validDirection = ['asc', 'desc']; | ||
| if (!validDirection.includes(direction)) { | ||
| throw new Error('Invalid direction.'); | ||
| } | ||
|
|
||
| const employees= loadEmployeesFromFile(); | ||
| let results = employees.filter((employee) => | ||
| JSON.stringify(employee).toLowerCase().includes(query.toLowerCase()) | ||
| ); | ||
| if (orderBy) { | ||
| results = results.sort((firstEmployee, secondEmployee) => { | ||
| const firstPropertyValue = firstEmployee[orderBy]; | ||
| const secondPropertyValue = secondEmployee[orderBy]; | ||
| if (typeof firstPropertyValue === 'string') { | ||
| return direction === 'asc' | ||
| ? firstPropertyValue.localeCompare(secondPropertyValue) | ||
| : secondPropertyValue.localeCompare(firstPropertyValue); | ||
| } else { | ||
| return direction === 'asc' ? firstPropertyValue - secondPropertyValue : secondPropertyValue - firstPropertyValue; | ||
| } | ||
| }); | ||
| } | ||
| return results; | ||
| } | ||
|
|
||
| // funtion to load employees from the file | ||
| function loadEmployeesFromFile(){ | ||
| let employees = []; | ||
|
|
||
| try{ | ||
| const data = filesystem.readFileSync(fileName, 'utf8'); | ||
| employees = data.split('\n').map((line) => { | ||
| const [name, email, age, dob] = line.split(','); | ||
| return new Employee(name, email, parseInt(age), dob); | ||
| }); | ||
|
|
||
| return employees;} | ||
| catch(err){ | ||
| if (err.code === 'ENOENT') { | ||
| console.log('File not found.'); | ||
| return []; | ||
| } | ||
| else{ | ||
| console.log("Error in reading file", err); | ||
| return [];} | ||
| } | ||
| } | ||
|
|
||
|
|
||
| // function to load emails into the set | ||
| function loadEmailsFromFile(){ | ||
| let employees = []; | ||
|
|
||
| try{ | ||
| const data = filesystem.readFileSync(fileName, 'utf8'); | ||
| employees = data.split('\n').map((line) => { | ||
| const [name, email, age, dob] = line.split(','); | ||
| return new Employee(name, email, parseInt(age), dob); | ||
| }); | ||
|
|
||
| // load emails into the set | ||
| employees.forEach((employee) => allEmails.add(employee.email)); | ||
| } | ||
| catch(err){ | ||
| if (err.code === 'ENOENT') { | ||
| console.log('Error getting employees emails from file.'); | ||
| return []; | ||
| } | ||
| else{ | ||
| console.log("Error in reading file", err); | ||
| return [];} | ||
| } | ||
| } | ||
|
|
||
|
|
||
|
|
||
| // function to write employees to the file | ||
| function writeEmployeesToFile(employees) { | ||
| try{ | ||
| const data = employees | ||
| .map((employee) => { | ||
| const { name, email, age, dob } = employee; | ||
|
|
||
| return `${name},${email},${age},${dob}`; | ||
| }) | ||
| .join('\n'); | ||
| filesystem.writeFileSync(fileName, data);} | ||
| catch(err){ | ||
|
|
||
| console.log("Error in writing file", err); | ||
| } | ||
| } | ||
|
|
||
| // Validate the date of birth | ||
| function isValidDOB(dob){ | ||
|
|
||
| if (!/^\d{4}-\d{2}-\d{2}$/.test(dob)) { | ||
| console.log('\nDate of birth should be in the format YYYY-MM-DD.'); | ||
| return false; | ||
| } | ||
|
|
||
| const [year, month, day] = dob.split('-').map(Number); | ||
|
|
||
| if (isNaN(year) || isNaN(month) || isNaN(day)) { | ||
| console.log('\nDate of birth should contain only numbers.'); | ||
| return false; | ||
| } | ||
|
|
||
| if (month < 1 || month > 12) { | ||
| console.log('\nMonth should be between 1 and 12.'); | ||
| return false; | ||
| } | ||
|
|
||
| if (day < 1 || day > 31) { | ||
| console.log('\nDay should be between 1 and 31.'); | ||
| return false; | ||
| } | ||
|
|
||
| return true; | ||
|
|
||
| } | ||
|
|
||
|
|
||
| // Display all employees | ||
| function displayEmployees() { | ||
|
|
||
| console.log('Employees:'); | ||
| console.table(loadEmployeesFromFile()); | ||
| } | ||
|
|
||
|
|
||
| // Define the main program | ||
| function menu(){ | ||
|
|
||
| // loading emails into the all emails set | ||
| loadEmailsFromFile(); | ||
|
|
||
| inputOutputReader.setPrompt('Menu:\n1. Add employee\n2. Delete employee\n3. Search employees\n4. Display \n5.Exit\nEnter your choice:'); | ||
| inputOutputReader.prompt(); | ||
|
|
||
| inputOutputReader.on('line', (choice) => { | ||
| switch (choice.trim()) { | ||
| case '1': | ||
| inputOutputReader.question('Enter name:', (name) => { | ||
| inputOutputReader.question('Enter email:', (email) => { | ||
| inputOutputReader.question('Enter age:', (age) => { | ||
| age = parseInt(age); | ||
| inputOutputReader.question('Enter date of birth (YYYY-MM-DD):', (dobStr) => { | ||
| const employee = new Employee(name, email, age, dobStr); | ||
| addEmployee(employee); | ||
| inputOutputReader.prompt(); | ||
| }); | ||
| }); | ||
| }); | ||
| }); | ||
| break; | ||
|
|
||
| case '2': | ||
| inputOutputReader.question('Enter employee email to delete:', (emailToDelete) => { | ||
| deleteEmployee(emailToDelete); | ||
| inputOutputReader.prompt(); | ||
| }); | ||
| break; | ||
|
|
||
| case '3': | ||
| inputOutputReader.question('Enter search query:', (query) => { | ||
| inputOutputReader.question('Enter sort order (name,age,email,dob):', (orderBy) => { | ||
| inputOutputReader.question('Enter sort direction (asc/desc):', (direction) => { | ||
| const results = searchEmployees(query, orderBy, direction); | ||
| console.log('Search results:'); | ||
| console.table(results); | ||
| inputOutputReader.prompt(); | ||
| }); | ||
| }); | ||
| }); | ||
| break; | ||
|
|
||
| case '4': | ||
| displayEmployees(); | ||
| inputOutputReader.prompt(); | ||
| break; | ||
|
|
||
| case '5': | ||
| console.log('Exiting program.'); | ||
| inputOutputReader.close(); | ||
| process.exit(0); | ||
|
|
||
| default: | ||
| console.log('Invalid choice.'); | ||
| inputOutputReader.prompt(); | ||
| break; | ||
| } | ||
| }); | ||
|
|
||
|
|
||
| } | ||
|
|
||
|
|
||
| // Start the program | ||
| menu(); | ||
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,5 @@ | ||
| Rohit Sharma,rohitritika@gmail.com,36,1989-10-14 | ||
| Virat Kohli,viratbhai@gmail.com,35,1989-12-29 | ||
| Suryakumar Yadav,suryatatasky@gmail.com,37,1985-12-15 | ||
| Suryakumar Zadav,suryatataskyz@gmail.com,36,1987-12-15 | ||
| Arshdeep Singh,arshupaaji@gmail.com,28,2000-09-08 |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Please use 'process.exit(0)' in place of 'break' to exit the program here.