app.ts 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267
  1. import {Book, Librarian, Logger as DamageLogger} from './interfaces';
  2. import {Category} from './enums';
  3. import {ReferenceItem, UniversityLibrarian} from './classes';
  4. import {
  5. CalculateLateFees as CalcFee,
  6. MaxBooksAllowed, Purge
  7. } from './lib/utilityfunctions';
  8. import refBook from './encyclopedia';
  9. import {Books, LogAndReturn} from './generics';
  10. export function GetAllBooks(): Book[] {
  11. const books = [
  12. {
  13. author: 'James Joyce',
  14. available: true,
  15. category: Category.Fiction,
  16. id: 1,
  17. title: 'Ulysses',
  18. },
  19. {
  20. author: 'Ernest Hemingway',
  21. available: false,
  22. category: Category.Fiction,
  23. id: 2,
  24. title: 'A farewall to arms',
  25. },
  26. {
  27. author: 'Maya Angelou',
  28. available: true,
  29. category: Category.Poetry,
  30. id: 3,
  31. title: 'I know why the caged bird sings',
  32. },
  33. {
  34. author: 'Herman Melville',
  35. available: true,
  36. category: Category.Fiction,
  37. id: 4,
  38. title: 'Moby Dick',
  39. }
  40. ];
  41. return books;
  42. }
  43. export function LogFirstAvailable(books = GetAllBooks()): void {
  44. const numberOfBooks: number = books.length;
  45. let firstAvailable: string = '';
  46. for (let currentBook of books) {
  47. if (currentBook.available) {
  48. firstAvailable = currentBook.title;
  49. break;
  50. }
  51. }
  52. console.log(`Total number of books: ${numberOfBooks}.`);
  53. console.log(`First available: ${firstAvailable}.`);
  54. }
  55. export function CheckoutBooks(customer: string, ...bookIds: number[]): string[] {
  56. console.log(`Checking out books for ${customer}.`);
  57. let booksCheckedOut: string[] = [];
  58. for (let id of bookIds) {
  59. let book = GetBookById(id);
  60. if (book.available) {
  61. booksCheckedOut.push(book.title);
  62. }
  63. }
  64. return booksCheckedOut;
  65. }
  66. export function CreateCustomer(name: string, age?: number, city?: string) {
  67. console.log(`Name: ${name}.`);
  68. if (age) {
  69. console.log(`Age: ${age}.`);
  70. }
  71. if (city) {
  72. console.log(`City: ${city}.`);
  73. }
  74. }
  75. export function CreateCustomerId(chars: string, nums: number): string {
  76. return chars + nums;
  77. }
  78. export function GetBookTitlesByCategory(categoryFilter: Category = Category.Fiction): Array<string> {
  79. console.log(`Getting books in category: ${Category[categoryFilter]}.`);
  80. const allBooks = GetAllBooks();
  81. const filteredTitles: string[] = [];
  82. for (let currentBook of allBooks) {
  83. if (currentBook.category === categoryFilter) {
  84. filteredTitles.push(currentBook.title);
  85. }
  86. }
  87. return filteredTitles;
  88. }
  89. export function GetBookById(id: number): Book {
  90. const allBooks = GetAllBooks();
  91. return allBooks.filter(book => book.id === id)[0];
  92. }
  93. export function LogBookTitles(titles: string[]): void {
  94. for (let title of titles) {
  95. console.log(title);
  96. }
  97. }
  98. // Note: no implementation.
  99. export function GetTitles(author: string): string[];
  100. export function GetTitles(available: boolean): string[];
  101. // Now an implementation:
  102. export function GetTitles(bookProperty: any): string[] {
  103. const allBooks = GetAllBooks();
  104. const foundTitles: Array<string> = [];
  105. if (typeof bookProperty == 'string') {
  106. // get books by author, add to foundTitles
  107. for (let book of allBooks) {
  108. if (book.author === bookProperty) {
  109. foundTitles.push(book.title);
  110. }
  111. }
  112. }
  113. else if (typeof bookProperty === 'boolean') {
  114. // get available books, add to foundTitles
  115. for (let book of allBooks) {
  116. if (book.available === bookProperty) {
  117. foundTitles.push(book.title);
  118. }
  119. }
  120. }
  121. else {
  122. throw new Error('Invalid property type: ' + typeof bookProperty);
  123. }
  124. return foundTitles;
  125. }
  126. export function PrintBook(book: Book): void {
  127. console.log(`${book.title} by ${book.author}.`);
  128. }
  129. //******************************************************************************
  130. function bookDemo() {
  131. let myBook: Book = {
  132. author: 'Jane Austen',
  133. available: true,
  134. category: Category.Fiction,
  135. // copies: 3,
  136. id: 5,
  137. pages: 250,
  138. title: 'Pride and prejudice',
  139. // year: '1813',
  140. markDamaged: reason => console.log(`Damaged: ${reason}.`),
  141. };
  142. PrintBook(myBook);
  143. myBook.markDamaged!('Torn back cover');
  144. let logDamage: DamageLogger = reason => console.log(`Damage reported: ${reason}.`);
  145. logDamage('Coffee stains');
  146. }
  147. function classDemo() {
  148. let favoriteLibrarian: Librarian = new UniversityLibrarian();
  149. favoriteLibrarian.name = 'Sharon';
  150. favoriteLibrarian.assistCustomer('Lynda');
  151. // let ref: ReferenceItem = new ReferenceItem('Facts and Figures', 2016);
  152. // ref.publisher = 'Random Data publisher';
  153. // ref.printItem();
  154. // console.log(ref.publisher);
  155. let Newspaper = class extends ReferenceItem {
  156. printCitation(): void {
  157. console.log(`Newspaper: ${this.title}.`);
  158. }
  159. };
  160. let myPaper = new Newspaper('The gazette', 2016);
  161. myPaper.printCitation();
  162. class Novel extends class { title: string } {
  163. mainCharacter: string;
  164. }
  165. let favoriteNovel = new Novel();
  166. favoriteNovel.mainCharacter = 'Hero';
  167. favoriteNovel.title = 'Plot';
  168. console.log(favoriteNovel);
  169. }
  170. function importDemo() {
  171. let fee = CalcFee(10);
  172. let max = MaxBooksAllowed(12);
  173. console.log(`Fee: ${fee}, max books: ${max}.`);
  174. let ref = new refBook('Fact Book', 2016, 1);
  175. console.log(ref);
  176. }
  177. function genericFunctionDemo() {
  178. LogAndReturn(false);
  179. LogAndReturn(42);
  180. LogAndReturn({ a: []});
  181. interface Magazine { title: string }
  182. let someString: string = LogAndReturn<string>('log this');
  183. console.log('Returned: ', someString);
  184. let newMag: Magazine = { title: 'Web dev monthly' };
  185. let someMag: Magazine = LogAndReturn<Magazine>(newMag);
  186. console.log('Returned mag', someMag);
  187. }
  188. function purgedemo() {
  189. let inventory: Books = [
  190. {
  191. author: 'K & R',
  192. available: true,
  193. category: Category.Software,
  194. id: 10,
  195. title: 'The C programming language',
  196. },
  197. {
  198. author: 'Steve McConnell',
  199. available: true,
  200. category: Category.Software,
  201. id: 11,
  202. title: 'Code Complete',
  203. },
  204. {
  205. author: 'A. B.',
  206. available: true,
  207. category: Category.Software,
  208. id: 12,
  209. title: '8-Bit graphics with Cobol',
  210. },
  211. {
  212. author: 'C. D.',
  213. available: true,
  214. category: Category.Software,
  215. id: 13,
  216. title: 'Cool autoexec.bat scripts',
  217. }
  218. ];
  219. // Inventory is accepted because Books is an alias for Array<Book>, so the
  220. // signature actually matches.
  221. let purgedBooks: Books = Purge(inventory);
  222. // Note that purgedBooks uses the aliases type after the assignment.
  223. purgedBooks.forEach(book => console.log(book.title));
  224. let purgedNums: Array<number> = Purge<number>([1, 2, 3, 4]);
  225. console.log(purgedNums);
  226. }
  227. false && bookDemo();
  228. false && classDemo();
  229. false && importDemo();
  230. false && genericFunctionDemo();
  231. purgedemo();