const readline = require('readline');
// Create the interface
const rl = readline.createInterface({
input: process.stdin,
output: process.stdout
});
// Menu options
const menuOptions = [
{ id: 1, name: 'View Profile' },
{ id: 2, name: 'Edit Settings' },
{ id: 3, name: 'Check Messages' },
{ id: 4, name: 'Log Out' },
{ id: 5, name: 'Exit' }
];
// Current user data (simulated)
let userData = {
username: 'johndoe',
email: 'john@example.com',
messages: [
{ id: 1, from: 'admin', subject: 'Welcome!', read: false },
{ id: 2, from: 'support', subject: 'Your ticket #1234', read: true }
],
settings: {
theme: 'light',
notifications: true,
language: 'en'
}
};
// Display the menu
function displayMenu() {
console.log('\n===== MAIN MENU =====');
menuOptions.forEach(option => {
console.log(`${option.id}. ${option.name}`);
});
console.log('====================');
}
// Process the selected option
function processOption(option) {
const selectedOption = menuOptions.find(item => item.id === parseInt(option));
if (!selectedOption) {
console.log('Invalid option. Please try again.');
return promptUser();
}
switch(selectedOption.id) {
case 1: // View Profile
console.log('\n=== YOUR PROFILE ===');
console.log(`Username: ${userData.username}`);
console.log(`Email: ${userData.email}`);
console.log(`Member since: January 2023`);
break;
case 2: // Edit Settings
console.log('\n=== EDIT SETTINGS ===');
console.log('1. Change Theme');
console.log('2. Toggle Notifications');
console.log('3. Change Language');
console.log('4. Back to Main Menu');
rl.question('\nSelect an option (1-4): ', (settingOption) => {
switch(settingOption) {
case '1':
const theme = userData.settings.theme === 'light' ? 'dark' : 'light';
userData.settings.theme = theme;
console.log(`Theme changed to ${theme} mode.`);
break;
case '2':
userData.settings.notifications = !userData.settings.notifications;
console.log(`Notifications ${userData.settings.notifications ? 'enabled' : 'disabled'}.`);
break;
case '3':
console.log('\nAvailable languages:');
console.log('1. English (en)');
console.log('2. Spanish (es)');
console.log('3. French (fr)');
rl.question('Select language (1-3): ', (langOption) => {
const languages = { '1': 'en', '2': 'es', '3': 'fr' };
if (languages[langOption]) {
userData.settings.language = languages[langOption];
console.log(`Language changed to ${languages[langOption]}.`);
} else {
console.log('Invalid language option.');
}
promptUser();
});
return; // Don't call promptUser() here as we're handling it in the callback
case '4':
// Just continue to promptUser() below
break;
default:
console.log('Invalid option.');
}
promptUser();
});
return; // Return early to prevent the default promptUser() call
case 3: // Check Messages
console.log('\n=== YOUR MESSAGES ===');
if (userData.messages.length === 0) {
console.log('No new messages.');
} else {
userData.messages.forEach(msg => {
console.log(`[${msg.read ? ' ' : '!'}] ${msg.id}. From: ${msg.from} - ${msg.subject}`);
});
}
break;
case 4: // Log Out
console.log('\nLogging out...');
console.log('Goodbye!');
rl.close();
return; // Exit early
case 5: // Exit
console.log('\nExiting application...');
rl.close();
return; // Exit early
}
// Return to the menu after a short delay
setTimeout(() => {
promptUser();
}, 1000);
}
// Prompt the user for input
function promptUser() {
displayMenu();
rl.question('\nSelect an option (1-5): ', (answer) => {
processOption(answer);
});
}
// Handle application exit
rl.on('close', () => {
console.log('\nThank you for using our application!');
process.exit(0);
});
// Start the application
console.log('Welcome to the Interactive Menu Demo!');
console.log('Use the menu below to navigate:');
promptUser();