Repository navigation
Expand file tree
/
Copy pathArrayStorage.java
More file actions
83 lines (73 loc) · 2.14 KB
/
Copy pathArrayStorage.java
File metadata and controls
83 lines (73 loc) · 2.14 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
package com.javaops.webapp.storage;
import com.javaops.webapp.model.Resume;
import java.util.Arrays;
public class ArrayStorage implements Storage {
private final Resume[] storage = new Resume[10000];
private int size = 0;
public void clear() {
Arrays.fill(storage, 0 , size, null);
size = 0;
}
private boolean inStorage(String uuid) {
for (int i = 0; i < size; i++) {
if (storage[i].getUuid().equals(uuid)) return true;
}
return false;
}
public void save(Resume r) {
if (inStorage(r.getUuid())) {
System.out.println("Already in storage");
} else {
if (size < storage.length) {
storage[size] = r;
size++;
} else {
System.out.println("Storage is full");
}
}
}
public Resume get(String uuid) {
if (!inStorage(uuid)) {
System.out.println("Not in storage");
return null;
}
Resume result = null;
for (int i = 0; i < size; i++) {
if (storage[i].getUuid().equals(uuid)) result = storage[i];
}
return result;
}
public void delete(String uuid) {
if (inStorage(uuid)) {
for (int i = 0; i < size; i++) {
if (storage[i].getUuid().equals(uuid)) {
storage[i] = storage[size - 1];
storage[size - 1] = null;
size--;
}
}
} else {
System.out.println("Not in storage");
}
}
public void update(Resume resume) {
if (inStorage(resume.getUuid())) {
for (int i = 0; i < size; i++) {
if (storage[i].getUuid().equals(resume.getUuid())) {
storage[i] = resume;
}
}
} else {
System.out.println("Not in storage");
}
}
/**
* @return array, contains only Resumes in storage (without null)
*/
public Resume[] getAll() {
return Arrays.copyOf(storage, size);
}
public int size() {
return size;
}
}