Guest User

Untitled

a guest
Oct 20th, 2018
98
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.30 KB | None | 0 0
  1. package booktest;
  2.  
  3. public class Author {
  4.  
  5. private String name;
  6. private String email;
  7.  
  8. public Author(){
  9.  
  10. }
  11.  
  12. public Author(String name, String email){
  13. this.name=name;
  14. this.email=email;
  15. }
  16.  
  17. public void setName(String name){
  18. this.name=name;
  19. }
  20.  
  21. public String getName(){
  22. return name;
  23. }
  24.  
  25. public void setEmail(){
  26. this.email=email;
  27. }
  28.  
  29. public String getEmail(){
  30. return email;
  31. }
  32.  
  33. public String toString(){
  34. return String.format("Author Information:n name: %sn email: %s", getName(), getEmail());
  35. }
  36.  
  37. package booktest;
  38.  
  39. public class Book {
  40.  
  41. private String title;
  42. private double price;
  43. private Author bookAuthor;
  44.  
  45. public Book(){
  46.  
  47. }
  48.  
  49. public Book(String title, double price, Author bookAuthor){
  50. this.title=title;
  51. this.price=price;
  52. this.bookAuthor=bookAuthor;
  53.  
  54. }
  55.  
  56. public void setTitle(String title){
  57. this.title=title;
  58. }
  59.  
  60. public String getTitle(){
  61. return title;
  62. }
  63.  
  64. public void setPrice(double price){
  65. this.price=price;
  66. }
  67.  
  68. public double getPrice(){
  69. return price;
  70. }
  71.  
  72. public void setAuthor(){
  73. this.bookAuthor=bookAuthor;
  74. }
  75.  
  76. public Author getAuthor(){
  77. return bookAuthor;
  78. }
  79.  
  80. public String toString(){
  81. return String.format("Book Information: n title: %sn price: %.2f SARn %s", getTitle(), getPrice(),getAuthor());
  82. }
  83.  
  84.  
  85.  
  86. }
  87.  
  88. package booktest;
  89.  
  90. import java.util.Scanner;
  91. public class BookTest {
  92.  
  93. public static void main(String[] args) {
  94. Scanner input= new Scanner(System.in);
  95.  
  96. // 1. making array of books (Array of objects):
  97. Book[] books= new Book[3];
  98.  
  99. //2. stor the values into an array of objects:
  100.  
  101. for (int i = 0; i <books.length; i++) {
  102. System.out.print("Enter book "+ (i+1)+ " title: ");
  103. String title=input.nextLine();
  104. System.out.println("Enter book "+(i+1)+" author's name:");
  105. String name=input.nextLine();
  106.  
  107. System.out.print("Enter book "+ (i+1)+ " price: ");
  108. double price=input.nextDouble();
  109.  
  110.  
  111. System.out.println("Enter book "+(i+1)+" author's email: ");
  112. String email=input.next();
  113.  
  114. Author a=new Author(name,email);
  115.  
  116. books[i]=new Book(title,price,a);
  117.  
  118. System.out.println(books[i]+"n");
  119.  
  120.  
  121.  
  122. }
  123.  
  124. }
  125.  
  126. }
Add Comment
Please, Sign In to add comment