-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathBookcase.java
More file actions
89 lines (70 loc) · 1.93 KB
/
Copy pathBookcase.java
File metadata and controls
89 lines (70 loc) · 1.93 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
package com.startjava.graduation;
import java.util.Arrays;
public class Bookcase {
private static final int CAPACITY = 10;
private static final double GROWTH_FACTOR = 1.5;
private Book[] books;
private int size;
public Bookcase() {
books = new Book[CAPACITY];
}
public boolean add(Book book) {
if (book == null) {
return false;
}
if (size >= CAPACITY) {
System.out.println("Ошибка: шкаф заполнен");
return false;
}
if (size == books.length) {
grow();
}
books[size] = book;
size++;
return true;
}
private void grow() {
int newCapacity = (int) (books.length * GROWTH_FACTOR) + 1;
books = Arrays.copyOf(books, newCapacity);
}
public Book find(String title) {
if (title == null || title.isBlank()) {
return null;
}
for (int i = 0; i < size; i++) {
if (books[i].getTitle().equalsIgnoreCase(title)) {
return books[i];
}
}
return null;
}
public boolean delete(String title) {
if (title == null || title.isBlank()) {
return false;
}
for (int i = 0; i < size; i++) {
if (books[i].getTitle().equalsIgnoreCase(title)) {
int numMoved = size - i - 1;
if (numMoved > 0) {
System.arraycopy(books, i + 1, books, i, numMoved);
}
books[--size] = null;
return true;
}
}
return false;
}
public Book[] getBooks() {
return Arrays.copyOf(books, size);
}
public int getSize() {
return size;
}
public int getFreeShelves() {
return CAPACITY - size;
}
public void clear() {
Arrays.fill(books, 0, size, null);
size = 0;
}
}