Task - TypeScript - Advanced Library Management System #149
Replies: 8 comments
-
CODEinterface LibraryItem {
id: number;
title: string;
author: AuthorTs;
publicationYear: number;
copiesAvailable: number;
displayInfo(): string;
borrowCopy(): void;
returnCopy(): void;
}
class Author5 {
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 Book implements LibraryItem {
constructor(
public id: number,
public title: string,
public author: AuthorTs,
public publicationYear: number,
public copiesAvailable: number
) {}
displayInfo() {
return `Book ID: ${this.id}, Title: ${this.title}, Author: ${this.author.name}, Publication Year: ${this.publicationYear}, Copies Available: ${this.copiesAvailable}`;
}
borrowCopy() {
if (this.copiesAvailable > 0) {
this.copiesAvailable--;
} else {
console.log("No copies available to borrow");
}
}
returnCopy() {
this.copiesAvailable++;
}
}
class Magazine implements LibraryItem {
constructor(
public id: number,
public title: string,
public publicationYear: number,
public copiesAvailable: number
) {}
author: AuthorTs;
displayInfo() {
return `Magazine ID: ${this.id}, Title: ${this.title}, Publication Year: ${this.publicationYear}, Copies Available: ${this.copiesAvailable}`;
}
borrowCopy() {
if (this.copiesAvailable > 0) {
this.copiesAvailable--;
} else {
console.log("No copies available to borrow");
}
}
returnCopy() {
this.copiesAvailable++;
}
}
class Newspaper implements LibraryItem {
constructor(
public id: number,
public title: string,
public publicationYear: number,
public copiesAvailable: number
) {}
author: AuthorTs;
displayInfo() {
return `Newspaper ID: ${this.id}, Title: ${this.title}, Publication Year: ${this.publicationYear}, Copies Available: ${this.copiesAvailable}`;
}
borrowCopy() {
if (this.copiesAvailable > 0) {
this.copiesAvailable--;
} else {
console.log("No copies available to borrow");
}
}
returnCopy() {
this.copiesAvailable++;
}
}
class LibraryMember {
borrowedItems: LibraryItem[] = [];
constructor(public id: number, public name: string) {}
borrowItem(item: LibraryItem) {
if (item.copiesAvailable > 0) {
item.borrowCopy();
this.borrowedItems.push(item);
} else {
console.log("No copies available to borrow");
}
}
returnItem(item: LibraryItem) {
const index = this.borrowedItems.indexOf(item);
if (index >= 0) {
item.returnCopy();
this.borrowedItems.splice(index, 1);
} else {
console.log("Item not found in borrowed items list");
}
}
}
const author1 = new Author5(1, "George Orwell", 1903);
const book1 = new Book(101, "1984", author1, 1949, 10);
const magazine1 = new Magazine(201, "National Geographic", 2022, 5);
const newspaper1 = new Newspaper(301, "New York Times", 2022, 2);
const member1 = new LibraryMember(1, "John Doe");
member1.borrowItem(book1);
console.log(book1.displayInfo());
member1.borrowItem(magazine1);
console.log(magazine1.displayInfo());
member1.borrowItem(newspaper1);
console.log(newspaper1.displayInfo());
member1.returnItem(book1);
console.log(book1.displayInfo());
member1.returnItem(magazine1);
console.log(magazine1.displayInfo());
member1.returnItem(newspaper1);
console.log(newspaper1.displayInfo());OUTPUT |
Beta Was this translation helpful? Give feedback.
-
Codeinterface LibraryItem {
id: number;
title: string;
copiesAvailable: number;
displayInfo(): string;
borrowCopy(): void;
returnCopy(): void;
}
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 LibraryMember {
id: number;
name: string;
borrowedItems?: BookTS1[];
constructor(id: number,name: string,borrowedItems: BookTS1[]=[]) {
this.id = id;
this.name = name;
this.borrowedItems = borrowedItems
}
borrowItem(item: BookTS1): void {
if (item.copiesAvailable > 0) {
item.borrowCopy();
this.borrowedItems.push(item);
}
else {
console.log("No copies of " + item.title + " available to borrow");
}
}
returnItem(item: BookTS1): void {
const index: number = this.borrowedItems.indexOf(item);
if (index >= 0 && index < this.borrowedItems.length) {
item.returnCopy();
this.borrowedItems.splice(index,1);
}
else {
console.log(item.title + " not found in user's borrowed list");
}
}
}
class BookTS1 implements LibraryItem {
id: number;
title: string;
author: AuthorTS1;
publicationYear: number;
copiesAvailable: number;
constructor(id: number,title: string, author: AuthorTS1,publicationYear: number,copiesAvailable: number) {
this.id = id;
this.title = title;
this.author = author;
this.publicationYear = publicationYear;
this.copiesAvailable = copiesAvailable;
}
borrowCopy(): void {
this.copiesAvailable -= 1;
}
returnCopy(): void {
this.copiesAvailable += 1;
}
displayInfo(): string {
return ("Book ID: " + this.id + ", Title: " + this.title + ", Author: " + this.author.name + ", Publication Year: " + this.publicationYear + ", Copies Available: " + this.copiesAvailable);
}
}
//create magazine and newspaper classes
class Magazine implements LibraryItem {
id: number;
title: string;
issue: number;
copiesAvailable: number;
constructor(id: number, title: string, issue: number, copiesAvailable: number) {
this.id = id;
this.title = title;
this.issue = issue;
this.copiesAvailable = copiesAvailable;
}
borrowCopy(): void {
this.copiesAvailable -= 1;
}
returnCopy(): void {
this.copiesAvailable += 1;
}
displayInfo(): string {
return ("Magazine ID: " + this.id + ", Title: " + this.title + ", Issue: " + this.issue + ", Copies Available: " + this.copiesAvailable);
}
}
class Newspaper implements LibraryItem {
id: number;
title: string;
date: string;
copiesAvailable: number;
constructor(id: number, title: string, date: string, copiesAvailable: number) {
this.id = id;
this.title = title;
this.date = date;
this.copiesAvailable = copiesAvailable;
}
borrowCopy(): void {
this.copiesAvailable -= 1;
}
returnCopy(): void {
this.copiesAvailable += 1;
}
displayInfo(): string {
return ("Newspaper ID: " + this.id + ", Title: " + this.title + ", Date: " + this.date + ", Copies Available: " + this.copiesAvailable);
}
}
//create instances of book and author class
const authorts1 = new AuthorTS1(1,"George Orwell", 1903);
const bookts1 = new BookTS1(101,"1984", authorts1, 1949,5);
const bookts2 = new BookTS1(102,"Animal Farm", authorts1, 1945,10);
const bookts3 = new BookTS1(103,"The Alchemist", new AuthorTS1(2,"Paulo Coelho", 1947), 1988,2);
//create instance for magazine and newspaper class
const magazinets1 = new Magazine(1, "National Geographic",1,10);
const newspaperts1 = new Newspaper(1,"The Hindu",new Date().toDateString(),10);
//create instances of library member
const member1 = new LibraryMember(1,'John Doe');
//display Books avaliable
console.log(magazinets1.displayInfo());
console.log(newspaperts1.displayInfo());
console.log(bookts1.displayInfo());
console.log(bookts2.displayInfo());
console.log(bookts3.displayInfo());
//member1 borrow and returns
member1.borrowItem(bookts1);
member1.borrowItem(bookts2);
console.log(bookts1.displayInfo());
console.log(bookts2.displayInfo());
member1.returnItem(bookts1);
member1.returnItem(bookts2);
console.log(bookts1.displayInfo());
console.log(bookts2.displayInfo());Screenshot |
Beta Was this translation helpful? Give feedback.
-
|
interface LibraryItem { class AuthorTS1 { class LibraryMember { class BookTS1 implements LibraryItem { |
Beta Was this translation helpful? Give feedback.
-
Codeinterface LibraryItem {
id : number;
title : string;
copiesAvailable : number;
displayInfo() : string; // displayInfo(): string - A method that returns a string containing the information about the library item in a human-readable format.
borrowCopy() : void; // borrowItem(item: LibraryItem): void - A method that adds the given item to the borrowedItems array.
returnCopy() : void; // returnItem(item: LibraryItem): void - A method that removes the given item from the borrowedItems array.
}
class 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;
copiesAvailable : number;
constructor(id: number, title : string, author : AuthorTs, publicationYear : number, copiesAvailable : number) {
this.id = id;
this.title = title;
this.author = author;
this.publicationYear = publicationYear;
this.copiesAvailable = copiesAvailable;
}
borrowCopy(): void {
if (this.copiesAvailable > 0) {
this.copiesAvailable -= 1;
}
}
returnCopy(): void {
this.copiesAvailable += 1;
}
displayInfo(): string {
return `id :, ${this.id}, title :, ${this.title} author :, ${this.author.name}, publicationYear : ${this.publicationYear}, books copies :, ${this.copiesAvailable}`;
}
}
class Magazine implements LibraryItem {
id: number;
title: string;
issue: string
publicationYear: number;
copiesAvailable : number;
constructor(id : number, title : string, issue : string, publicationYear : number, copiesAvailable : number) {
this.id = id
this.title = title
this.issue = issue
this.publicationYear = publicationYear
this.copiesAvailable = copiesAvailable
}
borrowCopy(): void {
if (this.copiesAvailable > 1) {
this.copiesAvailable -= 1;
}
}
returnCopy(): void {
this.copiesAvailable += 1;
}
displayInfo(): string {
return `Magazine id: ${this.id}, Magazine title: ${this.title}, Magazine issue: ${this.issue}, Magazine publicationYear : ${this.publicationYear}, Magazine copies : ${this.copiesAvailable}`;
}
}
class Newspaper implements LibraryItem {
id : number;
title : string;
date : Date;
copiesAvailable : number;
constructor(id : number, title : string, date : Date, copiesAvailable : number) {
this.id = id;
this.title = title;
this.date = date;
this.copiesAvailable = copiesAvailable
}
borrowCopy(): void {
if (this.copiesAvailable > 1) {
this.copiesAvailable -= 1;
}
}
returnCopy(): void {
this.copiesAvailable += 1;
}
displayInfo(): string {
return `Newspaper id : ${this.id}, Newspaper title : ${this.title}, Newspaper date : ${this.date}, Newspaper copiesAvailable : ${this.copiesAvailable}`;
}
}
class LibraryMember {
id : number;
name : string;
borrowedItems : LibraryItem[] = [];
constructor(id : number, name : string, borrowedItems : LibraryItem[] = []) {
this.id = id;
this.name = name;
this.borrowedItems = borrowedItems;
}
borrowItem(item : LibraryItem) : void {
if (item.copiesAvailable > 0) {
item.borrowCopy();
this.borrowedItems.push(item)
} else {
console.log("No copies of " + item.title + " available to borrow");
}
}
returnItem(item : LibraryItem) : void {
const index : number = this.borrowedItems.indexOf(item);
if (index >= 0 && index <= this.borrowedItems.length) {
item.returnCopy();
this.borrowedItems.splice(index, 1);
} else {
console.log(item.title + " not found in user's borrowed list");
}
}
}
const member1 = new LibraryMember(1, "George Orwell")
const book1 = new BookTs(101,"1984", author1, 1949, 2);
const book2 = new BookTs(102,"1986", author2, 1959, 0);
const book3 = new BookTs(103, "1985", author1, 1946, 6);
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, 250)
const magazine2 = new Magazine(2, "Outer space", "spelling misstake", 2004, 780)
const magazine3 = new Magazine(3, "Lost in thinking", "title name space", 2001, 999)
console.log(magazine1.displayInfo());
console.log(member1.borrowItem(magazine1));
console.log(magazine1.displayInfo());
console.log(member1.returnItem(magazine1));
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), 23);
const newsletter2 = new Newspaper(2, "indian economy", new Date(2023, 2, 28, 5, 14, 30), 54);
console.log(newsletter1.displayInfo());
console.log(member1.borrowItem(newsletter1));
console.log(newsletter1.displayInfo());
console.log(newsletter2.displayInfo());
console.log(book1.displayInfo());
console.log(member1.borrowItem(book1));
console.log(book1.displayInfo());
console.log(member1.returnItem(book1));
console.log(book1.displayInfo());Output |
Beta Was this translation helpful? Give feedback.
-
|
interface LibraryItem { class AuthorTs { } class BookTs implements LibraryItem { } class MagazineTs implements LibraryItem { } class NewspaperTs implements LibraryItem { } const author5 = new AuthorTs(1, "George Orwell", 1903); const magazine5 = new MagazineTs(201, "National Geographic", 123, new Date(2023, 3, 1)); const newspaper5 = new NewspaperTs(301, "The New York Times", new Date(2023, 3, 23)); |
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());Output: |
Beta Was this translation helpful? Give feedback.
-
|
interface LibraryItem { class Author { class Book implements LibraryItem { displayInfo(): string { borrowCopy(): void { returnCopy(): void { class Magazine implements LibraryItem { displayInfo(): string { borrowCopy(): void { returnCopy(): void { class Newspaper implements LibraryItem { displayInfo(): string { borrowCopy(): void { returnCopy(): void { class LibraryMember { constructor(public id: number, public name: string) {} borrowItem(item: LibraryItem): void { returnItem(item: LibraryItem): void { member1.borrowItem(book1); member1.returnItem(book1); |
Beta Was this translation helpful? Give feedback.
-
|
interface LibraryItem { } class Magazine implements LibraryItem{ displayInfo(): string { } } } class LibraryMember { } console.log( member1.borrowItem(book); console.log( member1.returnItem(book); console.log( |
Beta Was this translation helpful? Give feedback.




Uh oh!
There was an error while loading. Please reload this page.
-
You have been asked to further enhance the library management system you previously created in discussion #148. The library wants to keep track of the number of copies available for each item and manage the borrowing and returning process of the items. They also want to maintain a list of library members and their borrowed items.
Create a class
LibraryMemberwith the following specifications:Properties:
id: number - A unique identifier for the library member.name: string - The name of the library member.borrowedItems: LibraryItem[] - An array of borrowed library items.Methods:
borrowItem(item: LibraryItem): void - A method that adds the given item to theborrowedItemsarray.returnItem(item: LibraryItem): void - A method that removes the given item from theborrowedItemsarray.Update the
LibraryIteminterface and its implementations to include the following property:Property:
copiesAvailable: number - The number of copies available for the library item.Methods:
borrowCopy(): void - A method that decreases thecopiesAvailablecount by 1.returnCopy(): void - A method that increases thecopiesAvailablecount by 1.Example:
Output:
Beta Was this translation helpful? Give feedback.
All reactions