Преглед на файлове

9.7: creating and using a generic class.

Frederic G. MARAND преди 5 години
родител
ревизия
7cdf2fea4f
променени са 3 файла, в които са добавени 81 реда и са изтрити 26 реда
  1. 41 9
      app/app.ts
  2. 18 17
      app/interfaces.ts
  3. 22 0
      app/shelf.ts

+ 41 - 9
app/app.ts

@@ -1,12 +1,14 @@
-import {Book, Librarian, Logger as DamageLogger} from './interfaces';
+import {Book, Librarian, Logger as DamageLogger, Magazine} from './interfaces';
 import {Category} from './enums';
-import {Catalog, ReferenceItem, UniversityLibrarian} from './classes';
+import {ReferenceItem, UniversityLibrarian} from './classes';
 import {
   CalculateLateFees as CalcFee,
-  MaxBooksAllowed, Purge
+  MaxBooksAllowed,
+  Purge
 } from './lib/utilityfunctions';
 import refBook from './encyclopedia';
 import {Books, LogAndReturn} from './generics';
+import Shelf from "./shelf";
 
 export function GetAllBooks(): Book[] {
   const books = [
@@ -218,8 +220,8 @@ function genericFunctionDemo() {
   console.log('Returned mag', someMag);
 }
 
-function purgedemo() {
-  let inventory: Books = [
+function getBooksInventory(): Books {
+  return [
     {
       author: 'K & R',
       available: true,
@@ -249,7 +251,27 @@ function purgedemo() {
       title: 'Cool autoexec.bat scripts',
     }
   ];
+}
+
+function getMagazinesInventory(): Array<Magazine> {
+  return [
+    {
+      title: 'Programming Language Monthly',
+      publisher: 'Code Mags',
+    },
+    {
+      publisher: 'College Press',
+      title: 'Literary Fiction Quarterly',
+    },
+    {
+      publisher: 'GSU',
+      title: 'Give Points',
+    }
+  ];
+}
 
+function purgedemo() {
+  let inventory: Array<Book> = getBooksInventory();
   // Inventory is accepted because Books is an alias for Array<Book>, so the
   // signature actually matches.
   let purgedBooks: Books = Purge(inventory);
@@ -261,9 +283,19 @@ function purgedemo() {
 }
 
 function genericClassDemo() {
-  let cat = new Catalog<Book>();
-  let books = GetAllBooks();
-  cat.addItem(books[0]);
+  let bookShelf: Shelf<Book> = new Shelf<Book>();
+  getBooksInventory().forEach(book => bookShelf.add(book));
+  let firstBook: Book = bookShelf.getFirst();
+  console.log(firstBook);
+
+  let magazineShelf: Shelf<Magazine> = new Shelf<Magazine>();
+  getMagazinesInventory().forEach(mag => magazineShelf.add(mag));
+  let firstMagazine: Magazine = magazineShelf.getFirst();
+  console.log(firstMagazine);
+
+  let numberShelf: Shelf<number> = new Shelf<number>();
+  [5, 10, 15].forEach(num => numberShelf.add(num));
+  console.log(numberShelf);
 }
 
 false && bookDemo();
@@ -271,4 +303,4 @@ false && classDemo();
 false && importDemo();
 false && genericFunctionDemo();
 false && purgedemo();
-false && genericClassDemo();
+genericClassDemo();

+ 18 - 17
app/interfaces.ts

@@ -1,7 +1,7 @@
 import { Category } from "./enums";
 
-interface DamageLogger {
-  (reason: string): void;
+interface Author extends Person {
+  numBooksPublished: number;
 }
 
 interface Book {
@@ -9,35 +9,35 @@ interface Book {
   available: boolean,
   category: Category;
   id: number,
+  markDamaged?: DamageLogger,
   pages?: number,
   title: string,
-
-  markDamaged?: DamageLogger,
 }
 
-interface Person {
-  name: string;
-  email: string;
+interface DamageLogger {
+  (reason: string): void;
 }
 
-interface Author extends Person {
-  numBooksPublished: number;
+interface Inventory<T> {
+  addItem: (newItem: T) => void;
+  getAllItems: () => Array<T>;
+  getNewestItem: () => T;
 }
 
 interface Librarian extends Person {
-  department: string,
   assistCustomer: (custName: string) => void,
+  department: string,
 }
 
-interface Inventory<T> {
-  getNewestItem: () => T;
-  addItem: (newItem: T) => void;
-  getAllItems: () => Array<T>;
+interface Magazine {
+  publisher: string;
+  title: string;
 }
 
-// let bookInventory: Inventory<Book>;
-// (snip...populate bookInventory)
-// let allBooks: Books = bookInventory.getAllItems();
+interface Person {
+  email: string;
+  name: string;
+}
 
 export {
   Author,
@@ -45,4 +45,5 @@ export {
   DamageLogger as Logger,
   Inventory,
   Librarian,
+  Magazine,
 }

+ 22 - 0
app/shelf.ts

@@ -0,0 +1,22 @@
+/* There are lots of shelves in the library, and they can contain different
+types of objects.
+ */
+export default class Shelf<T> {
+  private _items: Array<T> = new Array<T>();
+
+  add(item: T): void {
+    this._items.push(item);
+  }
+
+  find(title: string): T {
+    return this._items.filter(item => item.title === title)[0];
+  }
+
+  getFirst(): T {
+    return this._items[0];
+  }
+
+  printTitles(): void {
+    this._items.forEach(item => console.log(item.title));
+  }
+}