|
@@ -1,44 +1,37 @@
|
|
-enum Category {
|
|
+import {Book} from './interfaces';
|
|
- Biography,
|
|
+import {Category} from "./enums";
|
|
- Poetry,
|
|
+
|
|
- Fiction,
|
|
+function GetAllBooks(): Book[] {
|
|
-}
|
|
+ const books = [
|
|
-
|
|
+ {
|
|
-let books: ({ author: string; available: boolean; category: Category; id: number; title: string })[] = [];
|
|
+ author: 'James Joyce',
|
|
-
|
|
+ available: true,
|
|
-function GetAllBooks() {
|
|
+ category: Category.Fiction,
|
|
- if (books.length < 4) {
|
|
+ id: 1,
|
|
- books = [
|
|
+ title: 'Ulysses',
|
|
- {
|
|
+ },
|
|
- author: 'James Joyce',
|
|
+ {
|
|
- available: true,
|
|
+ author: 'Ernest Hemingway',
|
|
- category: Category.Fiction,
|
|
+ available: false,
|
|
- id: 1,
|
|
+ category: Category.Fiction,
|
|
- title: 'Ulysses',
|
|
+ id: 2,
|
|
- },
|
|
+ title: 'A farewall to arms',
|
|
- {
|
|
+ },
|
|
- author: 'Ernest Hemingway',
|
|
+ {
|
|
- available: false,
|
|
+ author: 'Maya Angelou',
|
|
- category: Category.Fiction,
|
|
+ available: true,
|
|
- id: 2,
|
|
+ category: Category.Poetry,
|
|
- title: 'A farewall to arms',
|
|
+ id: 3,
|
|
- },
|
|
+ title: 'I know why the caged bird sings',
|
|
- {
|
|
+ },
|
|
- author: 'Maya Angelou',
|
|
+ {
|
|
- available: true,
|
|
+ author: 'Herman Melville',
|
|
- category: Category.Poetry,
|
|
+ available: true,
|
|
- id: 3,
|
|
+ category: Category.Fiction,
|
|
- title: 'I know why the caged bird sings',
|
|
+ id: 4,
|
|
- },
|
|
+ title: 'Moby Dick',
|
|
- {
|
|
+ }
|
|
- author: 'Herman Melville',
|
|
+ ];
|
|
- available: true,
|
|
|
|
- category: Category.Fiction,
|
|
|
|
- id: 4,
|
|
|
|
- title: 'Moby Dick',
|
|
|
|
- }
|
|
|
|
- ];
|
|
|
|
- }
|
|
|
|
|
|
|
|
return books;
|
|
return books;
|
|
}
|
|
}
|
|
@@ -100,7 +93,7 @@ function GetBookTitlesByCategory(categoryFilter: Category = Category.Fiction): A
|
|
return filteredTitles;
|
|
return filteredTitles;
|
|
}
|
|
}
|
|
|
|
|
|
-function GetBookById(id: number) {
|
|
+function GetBookById(id: number): Book {
|
|
const allBooks = GetAllBooks();
|
|
const allBooks = GetAllBooks();
|
|
return allBooks.filter(book => book.id === id)[0];
|
|
return allBooks.filter(book => book.id === id)[0];
|
|
}
|
|
}
|
|
@@ -142,67 +135,23 @@ function GetTitles(bookProperty: any): string[] {
|
|
return foundTitles;
|
|
return foundTitles;
|
|
}
|
|
}
|
|
|
|
|
|
-
|
|
+function PrintBook(book: Book): void {
|
|
-function defaultParamsDemo() {
|
|
+ console.log(`${book.title} by ${book.author}.`);
|
|
- CreateCustomer('Michelle');
|
|
|
|
- CreateCustomer('Leigh', 6);
|
|
|
|
- CreateCustomer('Marie', 12, 'Atlanta');
|
|
|
|
-}
|
|
|
|
-
|
|
|
|
-function functionTypeDemos() {
|
|
|
|
- let idGenerator: (chars: string, nums: number) => string;
|
|
|
|
- idGenerator = CreateCustomerId;
|
|
|
|
-
|
|
|
|
- let myID: string = idGenerator('Daniel', 10);
|
|
|
|
- console.log(myID);
|
|
|
|
-
|
|
|
|
- idGenerator = (name, id) => id + name;
|
|
|
|
- myID = idGenerator('Daniel', 10);
|
|
|
|
- console.log(myID);
|
|
|
|
-}
|
|
|
|
-
|
|
|
|
-
|
|
|
|
-
|
|
|
|
-
|
|
|
|
-
|
|
|
|
-
|
|
|
|
-function functionCallDefaultParamsDemo() {
|
|
|
|
- console.log("Book count:", books.length);
|
|
|
|
- books.unshift({
|
|
|
|
- author: "Anon1",
|
|
|
|
- available: true,
|
|
|
|
- category: Category.Biography,
|
|
|
|
- id: 42,
|
|
|
|
- title: "Added1",
|
|
|
|
- });
|
|
|
|
- console.log("Book count:", books.length);
|
|
|
|
- LogFirstAvailable();
|
|
|
|
- books.unshift({
|
|
|
|
- author: "Anon2",
|
|
|
|
- available: true,
|
|
|
|
- category: Category.Biography,
|
|
|
|
- id: 42,
|
|
|
|
- title: "Added2",
|
|
|
|
- });
|
|
|
|
- console.log("Book count:", books.length);
|
|
|
|
-
|
|
|
|
- LogFirstAvailable();
|
|
|
|
}
|
|
}
|
|
|
|
|
|
-function restParamsDemo() {
|
|
+
|
|
- let myBooks = CheckoutBooks('Ripper', 1, 2, 3, 4);
|
|
|
|
- myBooks.forEach(title => console.log(title);
|
|
|
|
-}
|
|
|
|
-
|
|
|
|
-function overloadDemo() {
|
|
|
|
- console.log(GetTitles(true));
|
|
|
|
- console.log(GetTitles('Herman Melville'));
|
|
|
|
-
|
|
|
|
-
|
|
|
|
-}
|
|
|
|
|
|
|
|
-
|
|
+let myBook: Book = {
|
|
-
|
|
+ author: 'Jane Austen',
|
|
-
|
|
+ available: true,
|
|
-
|
|
+ category: Category.Fiction,
|
|
-overloadDemo();
|
|
+
|
|
|
|
+ id: 5,
|
|
|
|
+ pages: 250,
|
|
|
|
+ title: 'Pride and prejudice',
|
|
|
|
+
|
|
|
|
+ markDamaged: reason => console.log(`Damaged: ${reason}.`),
|
|
|
|
+};
|
|
|
|
+
|
|
|
|
+PrintBook(myBook);
|
|
|
|
+myBook.markDamaged('Missing back cover');
|