Skip to content

Commit 5387cf1

Browse files
Course Scheduling
Course Scheduling backend coding challenge geektrust
1 parent 78bf672 commit 5387cf1

14 files changed

+2720
-0
lines changed

LICENSE

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
MIT License
2+
3+
Copyright (c) 2021 Geektrust
4+
5+
Permission is hereby granted, free of charge, to any person obtaining a copy
6+
of this software and associated documentation files (the "Software"), to deal
7+
in the Software without restriction, including without limitation the rights
8+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9+
copies of the Software, and to permit persons to whom the Software is
10+
furnished to do so, subject to the following conditions:
11+
12+
The above copyright notice and this permission notice shall be included in all
13+
copies or substantial portions of the Software.
14+
15+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21+
SOFTWARE.

README.md

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
# Pre-requisites
2+
* NodeJS 12.6.0/14.15.4/16.10.0
3+
* npm
4+
Goal
5+
6+
Your job is to build a simple command line application, which does the following:
7+
8+
9+
Add course offering
10+
A course offering has course title, instructor and date.
11+
It should also contain a minimum & maximum number of employees for the course offering.
12+
Register for the course
13+
Employees can register for the courses.
14+
If no. of employees registered for the course has reached the maximum, the result will be COURSE_FULL_ERROR.
15+
Otherwise, result of registration will be ACCEPTED.
16+
Cancel registration
17+
Employees can cancel their registration until the course allotment is completed.
18+
Course allotment
19+
This feature allots employees to course offering, before the course offering date.
20+
It should print a list of all the employees with their details along with their final course allotment status (Registration Number, Employee Name, Email, Course Offering ID, Course Name, Instructor, Date, Final Status). The list should be sorted based on the Registration number.
21+
If sufficient registrations are not received then the course offering itself gets cancelled.
22+
The employees who have registered will get confirmed unless the minimum number of registrations is not received.
23+
Even if the course offering gets canceled due to the minimum number of employees not registered, the list should be printed.
24+
25+
# How to run the code
26+
27+
We have provided scripts to execute the code.
28+
29+
Use `run.sh` if you are Linux/Unix/macOS Operating systems and `run.bat` if you are on Windows. Both the files run the commands silently and prints only output from the input file `sample_input/input1.txt`. You are supposed to add the input commands in the file from the appropriate problem statement.
30+
31+
Internally both the scripts run the following commands
32+
33+
* `npm ci --silent` - This will build the solution downloading the necessary dependencies.
34+
* Once the `npm install` from the previous build process is complete, we will execute the program using the command
35+
36+
`npm start --silent sample_input/input1.txt`
37+
38+
We expect your program to take the location to the text file as parameter. Input needs to be read from a text file, and output should be printed to the console. The text file will contain only commands in the format prescribed by the respective problem.
39+
40+
This main file, main.go should receive in the command line argument and parse the file passed in. Once the file is parsed and the application processes the commands, it should only print the output.
41+
42+
# Running the code for multiple test cases
43+
44+
Please fill `input1.txt` and `input2.txt` with the input commands and use those files in `run.bat` or `run.sh`. Replace `./geektrust sample_input/input1.txt` with `./geektrust sample_input/input2.txt` to run the test case from the second file.
45+
46+
# How to execute the unit tests
47+
48+
Mocha based test cases are executed with the following command from the root folder
49+
`mocha test`
50+
51+
Jest based test cases are executed with the following command from the root folder
52+
`jest`

addCourse.js

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
/**
2+
*
3+
* @param {*} courseName string
4+
* @param {*} instructor string
5+
* @param {*} date string
6+
* @param {*} minEmp string
7+
* @param {*} maxEmp string
8+
* @returns course
9+
*/
10+
const addCourse = (courseName, instructor, date, minEmp, maxEmp,num, status) => {
11+
if (!courseName || !instructor || !date || !minEmp || !maxEmp) {
12+
console.log('INPUT_DATA_ERROR')
13+
return 'INPUT_DATA_ERROR';
14+
}
15+
var milliseconds = new Date().getTime();
16+
let obj = {
17+
courseName,
18+
instructor,
19+
date,
20+
minEmp,
21+
maxEmp,
22+
cId: 'OFFERING-' + courseName + '-' + instructor,
23+
timestamp:milliseconds,
24+
id:num,
25+
status:true
26+
}
27+
courseList.push(obj);
28+
return 'OFFERING-' + courseName + '-' + instructor;
29+
}
30+
module.exports = {addCourse}

alloate.js

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
/**
2+
*
3+
* @param {*} course
4+
* @returns
5+
*/
6+
const courseAlloted = (course) => {
7+
let courseDetails = courseList.find(o => o.cId === course);
8+
let result = registerEmp.filter(item => item.courseId === course);
9+
result =result.reverse();
10+
let temp = []
11+
for (j = 0; j < result.length; j++) {
12+
if(result[j].status){
13+
console.log(result[j].course + ' ' + result[j].email + ' ' + result[j].courseId + ' ' + courseDetails.courseName +
14+
' ' + courseDetails.instructor + ' ' + courseDetails.date + ' CONFIRMED');
15+
}
16+
}
17+
return true;
18+
}
19+
20+
21+
module.exports = { courseAlloted }
22+

course.test.js

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
const {addCourse} = require('./addCourse');
2+
const {courseAlloted} = require('./alloate');
3+
const {registerCourse} = require('./register');
4+
const {main} = require('./geektrust');
5+
const assert =require('assert');
6+
global.courseList = [];
7+
global.registerEmp = [];
8+
9+
describe( " Course register test", () => {
10+
beforeEach(() => {
11+
console.log( "executes before every test" );
12+
});
13+
14+
it("should add course", () => {
15+
const temp= addCourse('JAVA', 'JAMES', '15062022', 1, 2 )
16+
assert.equal(temp,'OFFERING-JAVA-JAMES');
17+
});
18+
19+
it("should 2 register course", () => {
20+
const reg = registerCourse('ANDY@GMAIL.COM', 'OFFERING-JAVA-JAMES')
21+
assert.equal(reg,'REG-COURSE-ANDY-JAVA ACCEPTED');
22+
});
23+
24+
it("ahould one course allocate", () => {
25+
const reg1 = courseAlloted('OFFERING-JAVA-JAMES')
26+
assert.equal(reg1,true);
27+
});
28+
29+
});
30+

geektrust.js

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
const fs = require("fs")
2+
const {addCourse} = require('./addCourse');
3+
const {courseAlloted} = require('./alloate');
4+
const {registerCourse} = require('./register');
5+
const filename = process.argv[2]
6+
global.courseList = [];
7+
global.registerEmp = [];
8+
function main(dataInput) {
9+
var inputLines = dataInput.toString().split("\n")
10+
for (i = 0; i <inputLines.length; i++) {
11+
if (inputLines) {
12+
let input = inputLines[i].split(' ')
13+
switch (input[0]) {
14+
case 'CANCEL':
15+
let str =`${input[1]} CANCEL_ACCEPTED`
16+
for (k = 0; k < registerEmp.length; k++) {
17+
if(registerEmp[k].course == input[1].trim()){
18+
registerEmp[k].status=false
19+
}
20+
}
21+
console.log(str)
22+
break;
23+
case 'ADD-COURSE-OFFERING':
24+
let addCoursName = addCourse(input[1], input[2], input[3], parseInt(input[4]), parseInt(input[5]),i);
25+
console.log(addCoursName);
26+
break;
27+
case 'REGISTER':
28+
let registerCourse1 = registerCourse(input[1], input[2].trim());
29+
console.log(registerCourse1);
30+
break;
31+
case 'ALLOT':
32+
let courseAlloted22 = courseAlloted(input[1]);
33+
break;
34+
35+
}
36+
}
37+
}
38+
}
39+
data = fs.readFileSync(process.argv[2]).toString();
40+
main(data);
41+
42+
module.exports = { main }

0 commit comments

Comments
 (0)