UNPKG

@syedahoorainali/giaic

Version:

A vibrant Command-Line Interface for managing student records, built with TypeScript.

102 lines (84 loc) 3.46 kB
import chalk from 'chalk'; import inquirer from 'inquirer'; import { createSpinner } from "nanospinner"; import { Student, titleCase, sleep, students, updateStudents } from '../utils.js'; import mainStudents from '../student/index.js'; // Function to add a new admission const newAdmission = async () => { // Check if maximum capacity is reached if (students.length === 10) { console.log(chalk.yellowBright('Oops! Seats are full. Better luck next time')); return students; } // Define available courses and their fees const courses: any = { "Artificial Intelligence": 1500, "Blockchain": 2000, "Internet of Things": 1000, "Cloud Native and Mobile Web": 1000, } // Function to generate a unique roll number for the new student const rollNoGenerator = () => { const lastRollNo = parseInt(students[students.length - 1].rollNo); const newRollNo = String(lastRollNo + 1).padStart(5, '0'); return newRollNo; } console.log(''); // Prompt the user to input student details const userAnswer = await inquirer.prompt([ { message: "Enter your name:", name: "name", type: "input", validate: input => !(input.trim() === '') ? true : "Enter a valid name.", }, { message: "Enter your father name:", name: "fatherName", type: "input", validate: input => !(input.trim() === '') ? true : "Enter a valid name.", }, { message: "Select your course:", name: "course", type: "list", choices: Object.keys(courses) } ]) // Capitalize student name and father's name const name: string = titleCase(userAnswer.name).trim(); const fatherName: string = titleCase(userAnswer.fatherName).trim(); const course: string = userAnswer.course; // Display confirmation message with student details console.log(chalk.cyanBright(`\n${name} ${fatherName} want to enroll in ${course} with ${courses[course]} fees.`)); // Prompt for confirmation const { confirm } = await inquirer.prompt({ message: 'Processed?', name: 'confirm', type: 'confirm', default: true, }) // Start spinner animation for processing enrollment const spinner = createSpinner(chalk.yellowBright("Processing Enrollment...")).start(); await sleep(); // Simulate processing time // If user does not confirm, display error and return if (!confirm) { spinner.error({ text: chalk.redBright('Something went wrong') }); return students } // Create student object with generated roll number and add to students array const student: Student = { rollNo: rollNoGenerator(), name, fatherName, course, balance: 0, fees: courses[course], isPaid: false, } students.push(student); // Add student to array spinner.success({ text: chalk.greenBright(`Student with Roll No: ${student.rollNo} and Name: ${student.name} has been successfully enrolled.`) }); updateStudents(students); // Update the students data with new student await mainStudents(); // Call main function for student management } export default newAdmission;