0% found this document useful (0 votes)
26 views2 pages

Java Book and Author Classes

The document contains a Java program that defines two classes: Author and Book. The Author class includes attributes for name, email, and gender, along with methods for accessing and modifying these attributes. The Book class manages a collection of authors, along with details like the book's name, price, and quantity, and includes a main method to demonstrate its functionality.

Uploaded by

Phan Minh Phát
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
26 views2 pages

Java Book and Author Classes

The document contains a Java program that defines two classes: Author and Book. The Author class includes attributes for name, email, and gender, along with methods for accessing and modifying these attributes. The Book class manages a collection of authors, along with details like the book's name, price, and quantity, and includes a main method to demonstrate its functionality.

Uploaded by

Phan Minh Phát
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
You are on page 1/ 2

package bai2;

import java.util.Scanner;

class Author {
String name;
String email;
char gender;
public Author(String name, String email, char gender) {
this.name=name;
this.email=email;
if(gender!='m'&&gender!='f')
System.out.println("The gender is invalid");
else
this.gender=gender;
}
public String getName() {
return name;
}
public String getEmail() {
return email;
}
public void setEmail(String email) {
this.email=email;
}
public char getGender() {
return gender;
}
public String toString() {
return "Author[name=" + name + ",email=" + email + ",gender=" + gender
+ "]";
}
}

public class Book {


String name;
Author[] authors;
double price;
int qty;
public Book(String name, Author[] authors, double price) {
this.name=name;
this.price=price;
qty=0;
Scanner sc=new Scanner(System.in);
System.out.print("Input number of authors: ");
int m=sc.nextInt();
this.authors=new Author[m];
for(int i=0;i<m;i++)
this.authors[i]=new
Author(authors[i].name,authors[i].email,authors[i].gender);
}
public Book(String name, Author[] authors, double price, int qty) {
this.name=name;
this.price=price;
this.qty=qty;
Scanner sc=new Scanner(System.in);
System.out.print("Input number of authors: ");
int m=sc.nextInt();
this.authors=new Author[m];
for(int i=0;i<m;i++)
this.authors[i]=new
Author(authors[i].name,authors[i].email,authors[i].gender);
}
public String getName() {
return name;
}
public Author[] getAuthors() {
return authors;
}
public double getPrice() {
return price;
}
public void setPrice(double price) {
this.price=price;
}
public int getQty() {
return qty;
}
public void setQty(int qty) {
this.qty=qty;
}
public String toString() {
String s= "Book[name=" + name + ",authors={";
for(int i=0;i<authors.length;i++) {
s=s+authors[i].toString();
if(i<authors.length-1)
s=s+",";
}
return s + "},price=" + price + ",qty=" + qty +"]";
}
public static void main(String args[]){
Author[] a=new Author[3];
a[0]=new Author("Nguyen Van A","nguyenvana@gmail.com",'m');
a[1]=new Author("Nguyen Van B","nguyenvanb@gmail.com",'m');
a[2]=new Author("Nguyen Van C","nguyenvanc@gmail.com",'m');
Book b;
b=new Book("NGK",a,100);
System.out.println(b.toString());
}
}

You might also like