Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package booktest;
- public class Author {
- private String name;
- private String email;
- public Author(){
- }
- public Author(String name, String email){
- this.name=name;
- this.email=email;
- }
- public void setName(String name){
- this.name=name;
- }
- public String getName(){
- return name;
- }
- public void setEmail(){
- this.email=email;
- }
- public String getEmail(){
- return email;
- }
- public String toString(){
- return String.format("Author Information:n name: %sn email: %s", getName(), getEmail());
- }
- package booktest;
- public class Book {
- private String title;
- private double price;
- private Author bookAuthor;
- public Book(){
- }
- public Book(String title, double price, Author bookAuthor){
- this.title=title;
- this.price=price;
- this.bookAuthor=bookAuthor;
- }
- public void setTitle(String title){
- this.title=title;
- }
- public String getTitle(){
- return title;
- }
- public void setPrice(double price){
- this.price=price;
- }
- public double getPrice(){
- return price;
- }
- public void setAuthor(){
- this.bookAuthor=bookAuthor;
- }
- public Author getAuthor(){
- return bookAuthor;
- }
- public String toString(){
- return String.format("Book Information: n title: %sn price: %.2f SARn %s", getTitle(), getPrice(),getAuthor());
- }
- }
- package booktest;
- import java.util.Scanner;
- public class BookTest {
- public static void main(String[] args) {
- Scanner input= new Scanner(System.in);
- // 1. making array of books (Array of objects):
- Book[] books= new Book[3];
- //2. stor the values into an array of objects:
- for (int i = 0; i <books.length; i++) {
- System.out.print("Enter book "+ (i+1)+ " title: ");
- String title=input.nextLine();
- System.out.println("Enter book "+(i+1)+" author's name:");
- String name=input.nextLine();
- System.out.print("Enter book "+ (i+1)+ " price: ");
- double price=input.nextDouble();
- System.out.println("Enter book "+(i+1)+" author's email: ");
- String email=input.next();
- Author a=new Author(name,email);
- books[i]=new Book(title,price,a);
- System.out.println(books[i]+"n");
- }
- }
- }
Add Comment
Please, Sign In to add comment