Task - TypeScript - Enhanced Library Management System #148
Replies: 13 comments
-
Codeinterface LibraryItem {
id: number;
title: string;
displayInfo(): string;
}
class AuthorX {
id: number;
name: string;
birthYear: number;
constructor(id: number, name: string, birthYear: number) {
this.id = id;
this.name = name;
this.birthYear = birthYear;
}
}
class BookX implements LibraryItem {
id: number;
title: string;
author: AuthorX;
publicationYear: number;
constructor(
id: number,
title: string,
author: AuthorX,
publicationYear: number
) {
this.id = id;
this.title = title;
this.author = author;
this.publicationYear = publicationYear;
}
displayInfo(): string {
return `Book ID: ${this.id}, Title: ${this.title}, Author: ${this.author.name}, Publication Year: ${this.publicationYear}`;
}
}
class Magazine implements LibraryItem {
id: number;
title: string;
constructor(id: number, title: string) {
this.id = id;
this.title = title;
}
displayInfo(): string {
return `Magazine ID: ${this.id}, Title: ${this.title}`;
}
}
class Newspaper implements LibraryItem {
id: number;
title: string;
constructor(id: number, title: string) {
this.id = id;
this.title = title;
}
displayInfo(): string {
return `Newspaper ID: ${this.id}, Title: ${this.title}`;
}
}
const authorX = new AuthorX(1, "George Orwell", 1903);
const bookX = new BookX(101, "1984", authorX, 1949);
console.log(bookX.displayInfo());Output |
Beta Was this translation helpful? Give feedback.
-
|
code interface LibraryItem {
id: number;
title: string;
displayInfo(): string;
}
class Author {
constructor(public id: number, public name: string, public birthYear: number) {}
}
class Book implements LibraryItem {
constructor(public id: number, public title: string, public author: Author, public publicationYear: number) {}
displayInfo(): string {
return `Book ID: ${this.id}, Title: ${this.title}, Author: ${this.author.name}, Publication Year: ${this.publicationYear}`;
}
}
class Magazine implements LibraryItem {
constructor(public id: number, public title: string, public issueNumber: number, public publicationYear: number) {}
displayInfo(): string {
return `Magazine ID: ${this.id}, Title: ${this.title}, Issue Number: ${this.issueNumber}, Publication Year: ${this.publicationYear}`;
}
}
class Newspaper implements LibraryItem {
constructor(public id: number, public title: string, public publicationDate: Date) {}
displayInfo(): string {
return `Newspaper ID: ${this.id}, Title: ${this.title}, Publication Date: ${this.publicationDate.toDateString()}`;
}
}
const author1 = new Author(1, "George Orwell", 1903);
const book1 = new Book(101, "1984", author1, 1949);
const magazine1 = new Magazine(201, "National Geographic", 100, 2022);
const newspaper1 = new Newspaper(301, "The New York Times", new Date("2023-04-23"));
console.log(book1.displayInfo());
console.log(magazine1.displayInfo());
console.log(newspaper1.displayInfo()); |
Beta Was this translation helpful? Give feedback.
-
CODEinterface LibraryItem {
id: number;
title: string;
displayInfo(): string;
}
class AuthorTs {
id: number;
name: string = '';
birthYear: number = 0;
constructor(id: number, name: string = '', birthYear: number = 0) {
this.id = id;
this.name = name;
this.birthYear = birthYear;
}
}
class BookTs implements LibraryItem {
id: number;
title: string = '';
author: AuthorTs = new AuthorTs(0);
publicationYear: number = 0;
constructor(id: number, title: string = '', author: AuthorTs = new AuthorTs(0), publicationYear: number = 0) {
this.id = id;
this.title = title;
this.author = author;
this.publicationYear = publicationYear;
}
copiesAvailable: number;
borrowCopy(): void {
throw new Error("Method not implemented.");
}
returnCopy(): void {
throw new Error("Method not implemented.");
}
displayInfo(): string {
return `Book ID: ${this.id}, Title: ${this.title}, Author: ${this.author.name}, Publication Year: ${this.publicationYear}`;
}
}
class MagazineTs implements LibraryItem {
id: number;
title: string = '';
issueNumber: number = 0;
publicationDate: Date = new Date();
constructor(id: number, title: string = '', issueNumber: number = 0, publicationDate: Date = new Date()) {
this.id = id;
this.title = title;
this.issueNumber = issueNumber;
this.publicationDate = publicationDate;
}
author: AuthorTs;
publicationYear: number;
copiesAvailable: number;
borrowCopy(): void {
throw new Error("Method not implemented.");
}
returnCopy(): void {
throw new Error("Method not implemented.");
}
displayInfo(): string {
return `Magazine ID: ${this.id}, Title: ${this.title}, Issue Number: ${this.issueNumber}, Publication Date: ${this.publicationDate}`;
}
}
class NewspaperTs implements LibraryItem {
id: number;
title: string = '';
publicationDate: Date = new Date();
constructor(id: number, title: string = '', publicationDate: Date = new Date()) {
this.id = id;
this.title = title;
this.publicationDate = publicationDate;
}
author: AuthorTs;
publicationYear: number;
copiesAvailable: number;
borrowCopy(): void {
throw new Error("Method not implemented.");
}
returnCopy(): void {
throw new Error("Method not implemented.");
}
displayInfo(): string {
return `Newspaper ID: ${this.id}, Title: ${this.title}, Publication Date: ${this.publicationDate}`;
}
}
const author5 = new AuthorTs(1, "George Orwell", 1903);
const book5 = new BookTs(101, "1984", author5, 1949);
console.log(book5.displayInfo());
const magazine5 = new MagazineTs(201, "National Geographic", 123, new Date(2023, 3, 1));
console.log(magazine5.displayInfo());
const newspaper5 = new NewspaperTs(301, "The New York Times", new Date(2023, 3, 23));
console.log(newspaper5.displayInfo());OUTPUT |
Beta Was this translation helpful? Give feedback.
-
Codeinterface LibraryItem {
id: number,
title: string
}
class AuthorTS1 {
id: number;
name: string;
birthYear: number;
constructor(id: number,name: string, birthYear:number) {
this.id = id;
this.name = name;
this.birthYear = birthYear;
}
}
class BookTS1 {
libraryitem: LibraryItem;
author: AuthorTS1;
publicationYear: number;
constructor(libraryitem: LibraryItem,author: AuthorTS1,publicationYear: number) {
this.libraryitem = libraryitem;
this.author = author;
this.publicationYear = publicationYear;
}
displayInfo(): string {
return ("Book ID: " + this.libraryitem.id + ", Title: " + this.libraryitem.title + ", Author: " + this.author.name + ", Publication Year: " + this.publicationYear);
}
}
//create magazine and newspaper classes
class Magazine {
libraryitem: LibraryItem;
constructor(libraryitem: LibraryItem) {
this.libraryitem = libraryitem;
}
}
class Newspaper {
libraryitem: LibraryItem;
constructor(libraryitem: LibraryItem) {
this.libraryitem = libraryitem;
}
}
//create instances of book and author class
const authorts1 = new AuthorTS1(1,"George Orwell", 1903);
const bookts1 = new BookTS1({id:101,title:"1984"}, authorts1, 1949);
console.log(bookts1.displayInfo());
const bookts2 = new BookTS1({id:102,title:"Animal Farm"}, authorts1, 1945);
console.log(bookts2.displayInfo());
const bookts3 = new BookTS1({id:103,title:"The Alchemist"}, new AuthorTS1(2,"Paulo Coelho", 1947), 1988);
console.log(bookts3.displayInfo());Screenshot |
Beta Was this translation helpful? Give feedback.
-
|
interface LibraryItem { class AuthorX { constructor(id: number, name: string, birthYear: number) { class BookX implements LibraryItem { constructor(id: number, title: string, author: AuthorX, publicationYear: number) { displayInfo(): string { const authorx = new AuthorX(1, "George Orwell", 1903); console.log(bookx.displayInfo()); |
Beta Was this translation helpful? Give feedback.
-
|
interface LibraryItem { class AuthorX { constructor(id: number, name: string, birthYear: number) { class BookX implements LibraryItem { constructor( displayInfo(): string { class Magazine implements LibraryItem { constructor(id: number, title: string) { displayInfo(): string { class Newspaper implements LibraryItem { constructor(id: number, title: string) { displayInfo(): string { const authorX = new AuthorX(1, "George Orwell", 1903); console.log(bookX.displayInfo()); |
Beta Was this translation helpful? Give feedback.
-
codeinterface LibraryItem {
id: number;
title: string;
displayInfo(): string;
}
class AuthorX {
id: number;
name: string;
birthYear: number;
constructor(id: number, name: string, birthYear: number) {
this.id = id;
this.name = name;
this.birthYear = birthYear;
}
}
class BookX implements LibraryItem {
id: number;
title: string;
author: AuthorX;
publicationYear: number;
constructor(id: number, title: string, author: AuthorX, publicationYear: number ) {
this.id = id;
this.title = title;
this.author = author;
this. publicationYear = publicationYear;
}
displayInfo(): string {
return `Book ID: ${this.id}, Title: ${this.title}, Author: ${this.author.name}, Publication Year: ${this.publicationYear}`;
}
}
class Magazine implements LibraryItem {
id: number;
title: string;
constructor(id: number, title: string) {
this.id = id;
this.title = title;
}
displayInfo(): string {
return `Magazine ID: ${this.id}, Title: ${this.title}`;
}
}
class Newspaper implements LibraryItem {
id: number;
title: string;
date: number;
editor: string;
constructor(id: number, title: string, date: number, editor: string) {
this.id = id;
this.title = title;
this.date = date;
this.editor = editor;
}
displayInfo(): string {
return `Newspaper: ${this.title}, Id: ${this.id}, Date:${this.date}, Editor: ${this.editor}`;
}
}
const authorX = new AuthorX(1525, "J.K. Rowling", 1965);
const bookX = new BookX(1519, "Harry Potter and the Philosopher's Stone", authorX, 1997);
console.log(bookX.displayInfo());
ScreenShot |
Beta Was this translation helpful? Give feedback.
-
Codeclass AuthorTs {
name : string;
birthYear : number;
constructor(name: string, birthYear: number) {
this.name = name;
this.birthYear = birthYear;
}
}
const author1 = new AuthorTs("George Orwell", 1903);
const author2 = new AuthorTs("George Borsing", 1902);
class BookTs implements LibraryItem {
id: number;
title : string;
author : AuthorTs;
publicationYear : number;
constructor(id: number, title : string, author : AuthorTs, publicationYear : number) {
this.id = id;
this.title = title;
this.author = author;
this.publicationYear = publicationYear;
}
displayInfo(): string {
return `id :, ${this.id}, title :, ${this.title} author :, ${this.author.name}, publicationYear : ${this.publicationYear}`;
}
}
class Magazine implements LibraryItem {
id: number;
title: string;
issue: string
publicationYear: number;
constructor(id, title, issue, publicationYear) {
this.id = id
this.title = title
this.issue = issue
this.publicationYear = publicationYear
}
displayInfo(): string {
return `Magazine id: ${this.id}, Magazine title: ${this.title}, Magazine issue: ${this.issue}, Magazine publicationYear : ${this.publicationYear}`;
}
}
class Newspaper implements LibraryItem {
id : number;
title : string;
date : Date;
constructor(id : number, title : string, date : Date) {
this.id = id;
this.title = title;
this.date = date;
}
displayInfo(): string {
return `Newspaper id : ${this.id}, Newspaper title : ${this.title}, Newspaper date : ${this.date}`;
}
}
const book1 = new BookTs(101,"1984", author1, 1949);
const book2 = new BookTs(102,"1986", author2, 1959);
const book3 = new BookTs(103, "1985", author1, 1946);
console.log(book1);
console.log(book1.displayInfo());
console.log(book2);
console.log(book2.displayInfo());
console.log(book3);
console.log(book3.displayInfo());
const magazine1 = new Magazine(1, "1984", "printing mistake", 1994)
const magazine2 = new Magazine(2, "Outer space", "spelling misstake", 2004)
const magazine3 = new Magazine(3, "Lost in thinking", "title name space", 2001)
console.log(magazine1.displayInfo());
console.log(magazine2.displayInfo());
console.log(magazine3.displayInfo());
const newsletter1 = new Newspaper(1, "The new york times", new Date(2023, 3, 28, 5, 14, 30));
const newsletter2 = new Newspaper(2, "indian economy", new Date(2023, 2, 28, 5, 14, 30));
console.log(newsletter1.displayInfo());
console.log(newsletter2.displayInfo());Output |
Beta Was this translation helpful? Give feedback.
-
|
interface LibraryItem { class Authors { constructor(id: number, name: string, birthYear: number) { class Books implements LibraryItem { constructor( displayInfo(): string { class Magazine implements LibraryItem { constructor(id: number, title: string) { displayInfo(): string { class Newspaper implements LibraryItem { constructor(id: number, title: string) { displayInfo(): string { const authorX = new Authors(1, "George Orwell", 1903); console.log(books.displayInfo()); |
Beta Was this translation helpful? Give feedback.
-
|
interface LibraryItem { class AuthorEL { constructor(id: number, name: string, birthYear: number) { class BookEL implements LibraryItem { constructor( displayInfo(): string { class MagazineEL implements LibraryItem { constructor(id: number, title: string, issue: number) { displayInfo(): string { class NewspaperEL implements LibraryItem { constructor(id: number, title: string, date: string) { displayInfo(): string { const authorEL = new AuthorEL(1, "George Orwell", 1903); console.log(bookEL.displayInfo()); |
Beta Was this translation helpful? Give feedback.
-
Beta Was this translation helpful? Give feedback.
-
// Task - TypeScript - Enhanced Library Management System #148
// You have been asked to enhance the library management system you previously created in discussion #147.
//The library wants to include new types of items, such as magazines and newspapers, in addition to books.
//They also want to ensure that each item in their collection has a unique identifier and a method to display
//its information in a human-readable format.
// Create an interface LibraryItem with the following specifications:
// Properties:
// id: number - A unique identifier for the library item.
// title: string - The title of the library item.
// Method:
// displayInfo(): string - A method that returns a string containing the information about the library item in a human-readable format.
interface LibraryItemEL{
id:number;
title:string;
displayInfo(): string;
}
// Implement this interface in the Book class and create two new classes, Magazine and Newspaper, which also implement the LibraryItem interface.
// Update the Author class to include a unique identifier as well.
// Output:
class myAuthorEL {
name :string;
birthYear : number;
constructor(name:string,birthYear:number)
{
this.name= name;
this.birthYear = birthYear;
}
}
//Books
class mybookEL implements LibraryItemEL{
id:number;
title :string;
author :myAuthorEL ;
publicationYear:number;
constructor(id:number,title:string,author:myAuthorEL , publicationYear:number)
{ this.id=id;
this.title = title;
this.author = author;
this.publicationYear=publicationYear;
}
displayInfo():string{
return "Book ID: "+this.id+", Title: "+this.title+", Author: "+this.author.name+", Publication Year: "+this.publicationYear;
}
}
//For Magazines
class mymagEL implements LibraryItemEL{
id:number;
title :string;
issue:number;
constructor(id:number,title:string, issue:number)
{ this.id=id;
this.title = title;
this.issue=issue;
}
displayInfo():string{
// return "MagID: "+this.id+", Title: "+this.title+", Issue: "+this.issue;
return `MagID: ${this.id} ,Author : ${this.title} , Issue: ${this.issue}'; `
}
}
//Newspaper
class mynewspaperEL implements LibraryItemEL{
id:number;
title :string;
date:string;
constructor(id:number,title:string,date:string)
{ this.id=id;
this.title = title;
this.date= date;
}
displayInfo():string{
return "Newspaper ID: "+this.id+", Title: "+this.title+", Date: "+this.date;
}
}
// Example:
// const author1 = new Author(1, "George Orwell", 1903);
// const book1 = new Book(101, "1984", author1, 1949);
// console.log(book1.displayInfo());
const myauthor1 = new myAuthorEL("George Orwell", 1903);
const mybook1 = new mybookEL(101, "1984", myauthor1, 1949);
console.log(mybook1.displayInfo());
// Book ID: 101, Title: 1984, Author: George Orwell, Publication Year: 1949
const magobj = new mymagEL(101,"The Vogue",4023);
console.log(magobj.displayInfo());
const npobj = new mynewspaperEL(102,"The Indian Express","04th May 2023");
console.log(npobj.displayInfo()); |
Beta Was this translation helpful? Give feedback.
-
CODEinterface LibraryItem {
id: number;
title: string;
displayInfo(): string;
}
class EBook2Ts implements LibraryItem{
id: number;
title: string;
author: EAuthorTs;
publicationYear: number;
constructor(id: number, title: string, author: EAuthorTs, publicationYear: number){
this.title = title;
this.author = author;
this.publicationYear = publicationYear;
this.id = id;
}
displayInfo(): string{
return `Book ID: ${this.id} Title: ${this.title}, Author: ${this.author.name}, Publication Year: ${this.publicationYear}.`
}
};
class Magazine implements LibraryItem{
id: number;
title: string;
constructor(id: number, title: string){
this.id = id;
this.title = title;
}
displayInfo():string{
return `Book ID: ${this.id} Title: ${this.title}`
}
}
class Newspapaer implements LibraryItem{
id: number;
title: string;
constructor(id: number, title: string){
this.id = id;
this.title = title;
}
displayInfo(): string{
return "true"
}
}
class EAuthorTs{
id: number;
name: string;
birthYear: number;
constructor(id: number,name: string,birthYear: number){
this.id = id;
this.name = name;
this.birthYear = birthYear;
}
}
const Eauthor1ts = new EAuthorTs(1, "George Orwell", 1903);
const Ebook2ts = new EBook2Ts(101, "1984", Eauthor1ts , 1949);
//console.log(Eauthor1ts)
console.log(Ebook2ts.displayInfo());screenshot |
Beta Was this translation helpful? Give feedback.









Uh oh!
There was an error while loading. Please reload this page.
-
You have been asked to enhance the library management system you previously created in discussion #147. The library wants to include new types of items, such as magazines and newspapers, in addition to books. They also want to ensure that each item in their collection has a unique identifier and a method to display its information in a human-readable format.
Create an interface
LibraryItemwith the following specifications:Properties:
id: number - A unique identifier for the library item.title: string - The title of the library item.Method:
displayInfo(): string - A method that returns a string containing the information about the library item in a human-readable format.Implement this interface in the
Bookclass and create two new classes,MagazineandNewspaper, which also implement theLibraryIteminterface. Update theAuthorclass to include a unique identifier as well.Example:
Output:
Beta Was this translation helpful? Give feedback.
All reactions