good afternoon
I am trying to make a java Project for a Book, that it has 2 calsses + main class
1- the first class is the Author:
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: %s\n email: %s", getName(), getEmail());
}
}
2- second class is Book:
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: %s\n price: %.2f SAR\n %s", getTitle(), getPrice(),getAuthor());
}
}
3- the third class is the driver(main) class which called BookTest:
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");
}
}
}
the proplem in the main class that when the loop first run (i=0 ), it let me fill the name of author and the book title, but when loop go to it's second run (i=1) it does nor distingush between the title (String title) and the book author name ( String name) and let me enter only 1 of them !
see the run here :
waiting for your help and thank you very much!