Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- class Book
- {
- public String autor, titlu;
- public Book(String autor, String titlu)
- {
- this.autor = autor;
- this.titlu = titlu;
- }
- public boolean equals(Object o)
- {
- return (o instanceof Book) && ((Book)o).autor.equals(this.autor) && ((Book)o).titlu.equals(this.titlu);
- }
- }
- class Set
- {
- private int max_elements, capacity = 0;
- private Book vector[];
- public Set(int x)
- {
- max_elements = x;
- vector = new Book[max_elements];
- }
- public boolean addBook(Book new_book)
- {
- if(capacity == max_elements)
- return false;
- else
- {
- if(capacity != 0)
- for(int i = 0; i < capacity; i++)
- {
- if(vector[i].equals(new_book))
- return false;
- }
- vector[capacity] = new_book;
- capacity++;
- }
- return true;
- }
- public String toString()
- {
- String buffer = "";
- for(int i = 0; i < capacity; i++)
- buffer += (i+1) + ". " + vector[i].autor + " : " + vector[i].titlu + "\n";
- return buffer;
- }
- public Set union(Set set_2)
- {
- int new_capacity = this.capacity + set_2.capacity;
- Set new_set = new Set(new_capacity);
- for(int i = 0 ; i < this.capacity; i++)
- {
- new_set.addBook(this.vector[i]);
- }
- for(int j = 0; j < set_2.capacity; j++)
- {
- new_set.addBook(set_2.vector[j]);
- }
- return new_set;
- }
- }
- public class Main
- {
- public static void main(String[] args)
- {
- Book b1, b2, b3, b4;
- b1 = new Book("autor1","carte1");
- b2 = new Book("autor2","carte2");
- b3 = new Book("autor3","carte3");
- b4 = new Book("autor4","carte4");
- Set s1, s2;
- s1 = new Set(3);
- s2 = new Set(2);
- s1.addBook(b1);
- s1.addBook(b2);
- s1.addBook(b3);
- s2.addBook(b1);
- s2.addBook(b2);
- System.out.print(s1);
- System.out.print(s2);
- System.out.println("");
- System.out.println(s1.union(s2));
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement