app.ts 4.8 KB

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