app.ts 3.8 KB

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