Task - TypeScript - Library Management System #147
Replies: 14 comments
-
Codeclass Author {
name: string;
birthYear: number;
constructor(name: string, birthYear: number) {
this.name = name;
this.birthYear = birthYear;
}
}
class Book {
title: string;
author: Author;
publicationYear: number;
constructor(title: string, author: Author, publicationYear: number) {
this.title = title;
this.author = author;
this.publicationYear = publicationYear;
}
}
const author1 = new Author("George Orwell", 1903);
const book1 = new Book("1984", author1, 1949);
console.log(`Title: ${book1.title}, Author: ${book1.author.name}, Publication Year: ${book1.publicationYear}`);Output |
Beta Was this translation helpful? Give feedback.
-
CODEclass AuthorTS {
name: string = '';
birthYear: number = 0;
constructor(name: string = '', birthYear: number = 0) {
this.name = name;
this.birthYear = birthYear;
}
}
class BookTS {
title: string = '';
author: Author = new Author();
publicationYear: number = 0;
constructor(title: string = '', author: Author = new Author(), publicationYear: number = 0) {
this.title = title;
this.author = author;
this.publicationYear = publicationYear;
}
}
const author1 = new AuthorTS("George Orwell", 1903);
const book1 = new BookTS("1984", author1, 1949);
console.log(`Title: ${book1.title}, Author: ${book1.author.name}, Publication Year: ${book1.publicationYear}`);
OUTPUT |
Beta Was this translation helpful? Give feedback.
-
Codeclass Book2Ts{
title: string;
author: AuthorTs;
publicationYear: number;
constructor(title: string,author: AuthorTs, publicationYear: number){
this.title = title;
this.author = author;
this.publicationYear = publicationYear;
}
displayInfo(){
return `Title: ${this.title}, Author: + ${this.author}.`
}
};
class AuthorTs{
name: string;
birthYear: number;
constructor(name: string,birthYear: number){
this.name = name;
this.birthYear = birthYear;
}
}
let author1ts = new AuthorTs("George Orwell", 1903)
const book2ts = new Book2Ts("1984", author1ts, 1949)
console.log(`Title: ${book2ts.title}, Author: ${book2ts.author.name}, Publication Year: ${book2ts.publicationYear}`);
|
Beta Was this translation helpful? Give feedback.
-
class Author {
name: string;
birthYear: number;
constructor(name: string, birthYear: number) {
this.name = name;
this.birthYear = birthYear;
}
}
class Book {
title: string;
author: Author;
publicationYear: number;
constructor(title: string, author: Author, publicationYear: number) {
this.title = title;
this.author = author;
this.publicationYear = publicationYear;
}
}
const author1 = new Author("George Orwell", 1903);
const book1 = new Book("1984", author1, 1949);
console.log(`Title: ${book1.title}, Author: ${book1.author.name}, Publication Year: ${book1.publicationYear}`); |
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);
class BookTs {
title : string;
author : AuthorTs;
publicationYear : number;
constructor(title : string, author : AuthorTs, publicationYear : number) {
this.title = title;
this.author = author;
this.publicationYear = publicationYear;
}
}
const book1 = new BookTs("1984", author1, 1949);
console.log(book1);output |
Beta Was this translation helpful? Give feedback.
-
|
class Author { } class Book { } const author1 = new Author("George Orwell", 1903); console.log(author1); const book1 = new Book("1984", author1, 1949); Output |
Beta Was this translation helpful? Give feedback.
-
Codeclass AuthorTS {
name: string;
birthYear: number;
constructor(name: string, birthYear:number) {
this.name = name;
this.birthYear = birthYear;
}
}
class BookTS {
title: string;
author: AuthorTS;
publicationYear: number;
constructor(title: string,author: AuthorTS,publicationYear: number) {
this.title = title;
this.author = author;
this.publicationYear = publicationYear;
}
getBookInfo(): string {
return ("Title: " + this.title + ", Author: " + this.author.name + ", Publication Year: " + this.publicationYear);
}
}
const bookts = new BookTS("The Alchemist", new AuthorTS("Paulo Coelho", 1947), 1988);
console.log(bookts.getBookInfo());
const bookts1 = new BookTS("1984", new AuthorTS("George Orwell", 1903), 1949);
console.log(bookts1.getBookInfo());Screenshot |
Beta Was this translation helpful? Give feedback.
-
class Author{
name:string;
birthYear:number;
constructor(name:string,birthYear:number){
this.name=name;
this.birthYear=birthYear;
}
}
const Author1=new Author("George Orwell", 1903);
console.log(`name: ${author1.title}, birthYear: ${Author1.birthYear}`);
class Book{
title:string;
author:Author;
publicationYear:number;
constructor(title:string,author:Author,publicationYear:number){
this.title=title;
this.author=author;
this.publicationYear=publicationYear;
}
}
const book1 = new Book("1984", author1, 1949);
console.log(`Title: ${book1.title}, Author: ${book1.author.name}, Publication Year: ${book1.publicationYear}`); |
Beta Was this translation helpful? Give feedback.
-
|
class AuthorTS { } console.log( |
Beta Was this translation helpful? Give feedback.
-
codeclass AuthorTs{ constructor(name:string,birthYear:number){ } } const author1 = new AuthorTs("George Orwell", 1903); console.log( |
Beta Was this translation helpful? Give feedback.
-
|
class Author { constructor(name: string, birthYear: number) { class Book { constructor(title: string, author: Author, publicationYear: number) { const author1 = new Author("George Orwell", 1903); console.log( |
Beta Was this translation helpful? Give feedback.
-
codeclass Author {
name: string;
birthYear: number;
constructor(name: string, birthYear: number) {
this.name = name;
this.birthYear = birthYear;
}
}
class Book {
title: string;
author: Author;
publicationYear: number;
constructor(title: string, author: Author, publicationYear: number) {
this.title = title;
this.author = author;
this.publicationYear = publicationYear;
}
}
// instances of authors
const author1 = new Author('J.K. Rowling', 1965);
const author2 = new Author('George R.R. Martin', 1948);
// instances of books
const book1 = new Book('Harry Potter and the Philosopher\'s Stone', author1, 1997);
const book2 = new Book('A Game of Thrones', author2, 1996);
const book3 = new Book('Harry Potter and the Chamber of Secrets', author1, 1998);
// display book and author information
console.log(`Book: ${book1.title}, Author: ${book1.author.name}, Publication Year: ${book1.publicationYear}`);
console.log(`Book: ${book2.title}, Author: ${book2.author.name}, Publication Year: ${book2.publicationYear}`);
console.log(`Book: ${book3.title}, Author: ${book3.author.name}, Publication Year: ${book3.publicationYear}`);ScreenShot |
Beta Was this translation helpful? Give feedback.
-
|
/* You have been hired to create a library management system for a small local library. class Author_Ts{ console.log( |
Beta Was this translation helpful? Give feedback.
-
//Task - TypeScript - Library Management System · akash-coded/angular · Discussion #147 (github.com)
// Create two classes, Author and Book, with the following specifications:
// Author class:
// Properties:
// name: string - The name of the author.
// birthYear: number - The birth year of the author.
// Constructor:
// Accepts two arguments, name and birthYear, and initializes the respective properties.
class Author_ts {
name :string;
birthYear : number;
constructor(name:string,birthYear:number)
{
this.name= name;
this.birthYear = birthYear;
}
}
// Book class:
// Properties:
// title: string - The title of the book.
// author: Author - The author of the book.
// publicationYear: number - The publication year of the book.
// Constructor:
// Accepts three arguments, title, author, and publicationYear, and initializes the respective properties.
class Book_ts {
title :string;
author :Author_ts ;
publicationYear:number;
constructor(title:string,author:Author_ts , publicationYear:number)
{
this.title = title;
this.author = author;
this.publicationYear=publicationYear;
}
}
// Create a few instances of the Author and Book classes to simulate adding
// new authors and books to the library's collection.
// Then, display the
// information about the books and their authors in a human-readable format.
const author1 = new Author_ts("ChetanBhgarat",1908);
const author2 = new Author_ts("AS",1981);
const book1published = new Book_ts("MEF",author1,1990);
const book2published = new Book_ts("MESC",author2,1989);
console.log(
`Title: ${book1published.title}, Author: ${book1published.author.name} written in Year ${book1published.author.birthYear} ,with Publication Year as ${book1published.publicationYear}`);
console.log(
`Title: ${book2published.title}, Author: ${book2published.author.name} written in Year ${book2published.author.birthYear} ,with Publication Year as ${book2published.publicationYear}`); |
Beta Was this translation helpful? Give feedback.









Uh oh!
There was an error while loading. Please reload this page.
-
You have been hired to create a library management system for a small local library. The library wants to keep track of their books and authors, as well as provide a simple way to add new books and authors to their system.
Create two classes,
AuthorandBook, with the following specifications:Authorclass:name: string - The name of the author.birthYear: number - The birth year of the author.Accepts two arguments,
nameandbirthYear, and initializes the respective properties.Bookclass:title: string - The title of the book.author: Author - The author of the book.publicationYear: number - The publication year of the book.Accepts three arguments,
title,author, andpublicationYear, and initializes the respective properties.Create a few instances of the
AuthorandBookclasses to simulate adding new authors and books to the library's collection. Then, display the information about the books and their authors in a human-readable format.Example:
Output:
Beta Was this translation helpful? Give feedback.
All reactions