app.ts 4.9 KB

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