app.ts 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173
  1. import {Book, DamageLogger, Librarian} from './interfaces';
  2. import {Category} from "./enums";
  3. import {Encyclopedia, ReferenceItem, UniversityLibrarian} from "./classes";
  4. function GetAllBooks(): Book[] {
  5. const books = [
  6. {
  7. author: 'James Joyce',
  8. available: true,
  9. category: Category.Fiction,
  10. id: 1,
  11. title: 'Ulysses',
  12. },
  13. {
  14. author: 'Ernest Hemingway',
  15. available: false,
  16. category: Category.Fiction,
  17. id: 2,
  18. title: 'A farewall to arms',
  19. },
  20. {
  21. author: 'Maya Angelou',
  22. available: true,
  23. category: Category.Poetry,
  24. id: 3,
  25. title: 'I know why the caged bird sings',
  26. },
  27. {
  28. author: 'Herman Melville',
  29. available: true,
  30. category: Category.Fiction,
  31. id: 4,
  32. title: 'Moby Dick',
  33. }
  34. ];
  35. return books;
  36. }
  37. function LogFirstAvailable(books = GetAllBooks()): void {
  38. const numberOfBooks: number = books.length;
  39. let firstAvailable: string = '';
  40. for (let currentBook of books) {
  41. if (currentBook.available) {
  42. firstAvailable = currentBook.title;
  43. break;
  44. }
  45. }
  46. console.log(`Total number of books: ${numberOfBooks}.`);
  47. console.log(`First available: ${firstAvailable}.`);
  48. }
  49. function CheckoutBooks(customer: string, ...bookIds: number[]): string[] {
  50. console.log(`Checking out books for ${customer}.`);
  51. let booksCheckedOut: string[] = [];
  52. for (let id of bookIds) {
  53. let book = GetBookById(id);
  54. if (book.available) {
  55. booksCheckedOut.push(book.title);
  56. }
  57. }
  58. return booksCheckedOut;
  59. }
  60. function CreateCustomer(name: string, age?: number, city?: string) {
  61. console.log(`Name: ${name}.`);
  62. if (age) {
  63. console.log(`Age: ${age}.`);
  64. }
  65. if (city) {
  66. console.log(`City: ${city}.`);
  67. }
  68. }
  69. function CreateCustomerId(chars: string, nums: number): string {
  70. return chars + nums;
  71. }
  72. function GetBookTitlesByCategory(categoryFilter: Category = Category.Fiction): Array<string> {
  73. console.log(`Getting books in category: ${Category[categoryFilter]}.`);
  74. const allBooks = GetAllBooks();
  75. const filteredTitles: string[] = [];
  76. for (let currentBook of allBooks) {
  77. if (currentBook.category === categoryFilter) {
  78. filteredTitles.push(currentBook.title);
  79. }
  80. }
  81. return filteredTitles;
  82. }
  83. function GetBookById(id: number): Book {
  84. const allBooks = GetAllBooks();
  85. return allBooks.filter(book => book.id === id)[0];
  86. }
  87. function LogBookTitles(titles: string[]): void {
  88. for (let title of titles) {
  89. console.log(title);
  90. }
  91. }
  92. // Note: no implementation.
  93. function GetTitles(author: string): string[];
  94. function GetTitles(available: boolean): string[];
  95. // Now an implementation:
  96. function GetTitles(bookProperty: any): string[] {
  97. const allBooks = GetAllBooks();
  98. const foundTitles: Array<string> = [];
  99. if (typeof bookProperty == 'string') {
  100. // get books by author, add to foundTitles
  101. for (let book of allBooks) {
  102. if (book.author === bookProperty) {
  103. foundTitles.push(book.title);
  104. }
  105. }
  106. }
  107. else if (typeof bookProperty === 'boolean') {
  108. // get available books, add to foundTitles
  109. for (let book of allBooks) {
  110. if (book.available === bookProperty) {
  111. foundTitles.push(book.title);
  112. }
  113. }
  114. }
  115. else {
  116. throw new Error('Invalid property type: ' + typeof bookProperty);
  117. }
  118. return foundTitles;
  119. }
  120. function PrintBook(book: Book): void {
  121. console.log(`${book.title} by ${book.author}.`);
  122. }
  123. //******************************************************************************
  124. let myBook: Book = {
  125. author: 'Jane Austen',
  126. available: true,
  127. category: Category.Fiction,
  128. // copies: 3,
  129. id: 5,
  130. pages: 250,
  131. title: 'Pride and prejudice',
  132. // year: '1813',
  133. markDamaged: reason => console.log(`Damaged: ${reason}.`),
  134. };
  135. // PrintBook(myBook);
  136. // myBook.markDamaged('Torn back cover');
  137. // let logDamage: DamageLogger = reason => console.log(`Damage reported: ${reason}.`);
  138. // logDamage('Coffee stains');
  139. // let favoriteLibrarian: Librarian = new UniversityLibrarian();
  140. // favoriteLibrarian.name = 'Sharon';
  141. // favoriteLibrarian.assistCustomer('Lynda');
  142. // let ref: ReferenceItem = new ReferenceItem('Facts and Figures', 2016);
  143. // ref.publisher = 'Random Data publisher';
  144. // ref.printItem();
  145. // console.log(ref.publisher);
  146. let refBook = new Encyclopedia('WorldPedia', 1900, 10);
  147. refBook.printItem();