app.ts 5.1 KB

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