Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- class Person {
- constructor (nimi, ika) {
- this.name = nimi
- this.age = ika
- }
- set name (nimi) {
- this._nimi = nimi
- }
- set age (ika) {
- this._age = ika
- }
- get name () {
- return this._nimi
- }
- get age () {
- return this._age
- }
- }
- const readlineSync = require('readline-sync')
- const figlet = require('figlet')
- // quit is for running the app -> App quits when quit = true
- var quit = false
- // Creating array of persons
- var personArray = []
- // Variable for the do while loop.
- console.log(
- figlet.textSync('Person App', {
- font: 'Ghost',
- horizontalLayout: 'default',
- verticalLayout: 'default'
- })
- )
- do {
- const selection = [
- '[1] Add Person',
- '[2] Delete Person',
- '[3] Show Persons',
- '[0] CANCEL'
- ]
- for (const index in selection) {
- console.log(selection[index])
- }
- // Asking what to do
- const selected = readlineSync.questionInt(
- '\n' + 'What do you want to do? [1, 2, 3, 0]: '
- )
- switch (selected) {
- // Add new person to personArray
- case 1: {
- var nameOk = false
- var ageOk = false
- var name = readlineSync.question('Give your name: ')
- // If name contains numbers they will be replaced
- name = name.replace(/[0-9]/g, '')
- // Could also be readlineSync.questionInt
- var age = Number(readlineSync.question('Give yor age (1-100): '))
- do {
- name.length > 0
- ? (nameOk = true)
- : (name = readlineSync.question(
- 'Name was not valid. Give your name: '
- ))
- age > 0 && age <= 100
- ? (ageOk = true)
- : (age = Number(
- readlineSync.question(
- 'Age was not valid. Give your age (1-100): '
- )
- ))
- } while (!ageOk || !nameOk)
- // If name and age are Ok
- console.log('\n' + 'Adding new person: ' + name + '\n')
- // Add name and age to personArray
- personArray.push({ name: name, age: age })
- // Making new objects from the updated array
- personArray.forEach(person => new Person(person.name, person.age))
- break
- }
- // Delete person from the personArray
- case 2: {
- // Print personArray to see what names it contains
- console.log('List of names: ' + '\n')
- for (const persons in personArray) {
- console.log(personArray[persons].name)
- }
- let deleteName = readlineSync.question(
- '\n' + 'Select who to delete from the list: '
- )
- // Is name found from the personArray -> true if found.
- var found = false
- do {
- for (const index in personArray) {
- if (personArray[index].name === deleteName) {
- found = true
- break
- }
- }
- // Filttering the name from the personArray
- if (found) {
- personArray = personArray.filter(person => person.name !== deleteName)
- console.log('Deleted person: ' + deleteName + '\n')
- break
- } else {
- deleteName = readlineSync.question(
- 'Cant find that name. Select who to delete from the list: '
- )
- }
- } while (!found)
- break
- }
- // Print the list of persons from personArray
- case 3: {
- console.log('\n' + 'List of persons: ' + '\n')
- for (const person in personArray) {
- console.log(
- 'name: ' +
- personArray[person].name +
- ', ' +
- 'age: ' +
- personArray[person].age
- )
- }
- console.log('\n' + '----- End of list -----' + '\n')
- break
- }
- case 0: {
- quit = true
- console.log(figlet.textSync('Bye!', {}))
- }
- }
- } while (!quit)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement