Advertisement
Guest User

Untitled

a guest
Jan 19th, 2020
81
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.74 KB | None | 0 0
  1. class Person {
  2. constructor (nimi, ika) {
  3. this.name = nimi
  4. this.age = ika
  5. }
  6.  
  7. set name (nimi) {
  8. this._nimi = nimi
  9. }
  10.  
  11. set age (ika) {
  12. this._age = ika
  13. }
  14.  
  15. get name () {
  16. return this._nimi
  17. }
  18.  
  19. get age () {
  20. return this._age
  21. }
  22. }
  23.  
  24. const readlineSync = require('readline-sync')
  25. const figlet = require('figlet')
  26. // quit is for running the app -> App quits when quit = true
  27. var quit = false
  28.  
  29. // Creating array of persons
  30. var personArray = []
  31. // Variable for the do while loop.
  32. console.log(
  33. figlet.textSync('Person App', {
  34. font: 'Ghost',
  35. horizontalLayout: 'default',
  36. verticalLayout: 'default'
  37. })
  38. )
  39.  
  40. do {
  41. const selection = [
  42. '[1] Add Person',
  43. '[2] Delete Person',
  44. '[3] Show Persons',
  45. '[0] CANCEL'
  46. ]
  47. for (const index in selection) {
  48. console.log(selection[index])
  49. }
  50. // Asking what to do
  51. const selected = readlineSync.questionInt(
  52. '\n' + 'What do you want to do? [1, 2, 3, 0]: '
  53. )
  54. switch (selected) {
  55. // Add new person to personArray
  56. case 1: {
  57. var nameOk = false
  58. var ageOk = false
  59.  
  60. var name = readlineSync.question('Give your name: ')
  61. // If name contains numbers they will be replaced
  62. name = name.replace(/[0-9]/g, '')
  63. // Could also be readlineSync.questionInt
  64. var age = Number(readlineSync.question('Give yor age (1-100): '))
  65.  
  66. do {
  67. name.length > 0
  68. ? (nameOk = true)
  69. : (name = readlineSync.question(
  70. 'Name was not valid. Give your name: '
  71. ))
  72. age > 0 && age <= 100
  73. ? (ageOk = true)
  74. : (age = Number(
  75. readlineSync.question(
  76. 'Age was not valid. Give your age (1-100): '
  77. )
  78. ))
  79. } while (!ageOk || !nameOk)
  80. // If name and age are Ok
  81. console.log('\n' + 'Adding new person: ' + name + '\n')
  82. // Add name and age to personArray
  83. personArray.push({ name: name, age: age })
  84. // Making new objects from the updated array
  85. personArray.forEach(person => new Person(person.name, person.age))
  86.  
  87. break
  88. }
  89. // Delete person from the personArray
  90. case 2: {
  91. // Print personArray to see what names it contains
  92. console.log('List of names: ' + '\n')
  93. for (const persons in personArray) {
  94. console.log(personArray[persons].name)
  95. }
  96. let deleteName = readlineSync.question(
  97. '\n' + 'Select who to delete from the list: '
  98. )
  99.  
  100. // Is name found from the personArray -> true if found.
  101. var found = false
  102.  
  103. do {
  104. for (const index in personArray) {
  105. if (personArray[index].name === deleteName) {
  106. found = true
  107. break
  108. }
  109. }
  110. // Filttering the name from the personArray
  111. if (found) {
  112. personArray = personArray.filter(person => person.name !== deleteName)
  113. console.log('Deleted person: ' + deleteName + '\n')
  114. break
  115. } else {
  116. deleteName = readlineSync.question(
  117. 'Cant find that name. Select who to delete from the list: '
  118. )
  119. }
  120. } while (!found)
  121.  
  122. break
  123. }
  124. // Print the list of persons from personArray
  125. case 3: {
  126. console.log('\n' + 'List of persons: ' + '\n')
  127. for (const person in personArray) {
  128. console.log(
  129. 'name: ' +
  130. personArray[person].name +
  131. ', ' +
  132. 'age: ' +
  133. personArray[person].age
  134. )
  135. }
  136. console.log('\n' + '----- End of list -----' + '\n')
  137. break
  138. }
  139. case 0: {
  140. quit = true
  141. console.log(figlet.textSync('Bye!', {}))
  142. }
  143. }
  144. } while (!quit)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement