Advertisement
Guest User

Untitled

a guest
Oct 21st, 2019
74
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.18 KB | None | 0 0
  1. class Book
  2. {
  3. public String title;
  4. public String author;
  5.  
  6. public Book(String t, String a)
  7. {
  8. title = t;
  9. author = a;
  10. }
  11.  
  12. public String toString()
  13. {
  14. return title + " " + author;
  15. }
  16.  
  17. public boolean equals(Book book)
  18. {
  19. return (book instanceof Book) && ((Book)book).title == title && ((Book)book).author == author;
  20. }
  21. }
  22.  
  23. class Set
  24. {
  25. private int capacity;
  26. Book[] books;
  27. private int i;
  28.  
  29. public Set(int c)
  30. {
  31. capacity = c;
  32. books = new Book[capacity];
  33. }
  34.  
  35. public boolean addBook(Book book)
  36. {
  37. if(i==0)
  38. {
  39. books[i] = book;
  40. i++;
  41. return true;
  42. }
  43.  
  44.  
  45. if(i==capacity)
  46. {
  47. return false;
  48. }
  49.  
  50. for(i=1 ; i<=capacity ; i++)
  51. {
  52. if((books[i].equals(book))==true)
  53. {
  54. return false;
  55. }
  56. }
  57.  
  58. if(books[i].equals(book)==false)
  59. {
  60. books[i] = book;
  61. i++;
  62. }
  63. return true;
  64. }
  65. }
  66.  
  67. class setMain
  68. {
  69. public static void main (String []args)
  70. {
  71. Set s1 = new Set(3);
  72. Book b1 = new Book("Aa","Bb");
  73. Book b2 = new Book("Cc","Dd");
  74. Book b3 = new Book("Aa","Bb");
  75.  
  76. System.out.println(b1);
  77. System.out.println(b2);
  78. System.out.println(b3);
  79.  
  80. s1.addBook(b1);
  81. s1.addBook(b2);
  82. s1.addBook(b3);
  83. }
  84. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement