Bai 2

Download as txt, pdf, or txt
Download as txt, pdf, or txt
You are on page 1of 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","[email protected]",'m');
a[1]=new Author("Nguyen Van B","[email protected]",'m');
a[2]=new Author("Nguyen Van C","[email protected]",'m');
Book b;
b=new Book("NGK",a,100);
System.out.println(b.toString());
}
}

You might also like