Lesson03 abstract storage & sorted storage · Basicprof/basejava@aae6590 · GitHub
Skip to content

Commit aae6590

Browse files
committed
Lesson03 abstract storage & sorted storage
1 parent 638d0d3 commit aae6590

9 files changed

Lines changed: 219 additions & 88 deletions

File tree

src/ArrayStorage.java

Lines changed: 0 additions & 62 deletions
This file was deleted.

src/Resume.java

Lines changed: 0 additions & 13 deletions
This file was deleted.

src/MainArray.java renamed to src/ru/javawebinar/basejava/MainArray.java

Lines changed: 17 additions & 6 deletions

src/MainTestArrayStorage.java renamed to src/ru/javawebinar/basejava/MainTestArrayStorage.java

Lines changed: 12 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,28 +1,33 @@
1+
package ru.javawebinar.basejava;
2+
3+
import ru.javawebinar.basejava.model.Resume;
4+
import ru.javawebinar.basejava.storage.ArrayStorage;
5+
16
/**
2-
* Test for your ArrayStorage implementation
7+
* Test ru.javawebinar.basejava.storage.ArrayStorage
38
*/
49
public class MainTestArrayStorage {
510
static final ArrayStorage ARRAY_STORAGE = new ArrayStorage();
611

712
public static void main(String[] args) {
813
Resume r1 = new Resume();
9-
r1.uuid = "uuid1";
14+
r1.setUuid("uuid1");
1015
Resume r2 = new Resume();
11-
r2.uuid = "uuid2";
16+
r2.setUuid("uuid2");
1217
Resume r3 = new Resume();
13-
r3.uuid = "uuid3";
18+
r3.setUuid("uuid3");
1419

1520
ARRAY_STORAGE.save(r1);
1621
ARRAY_STORAGE.save(r2);
1722
ARRAY_STORAGE.save(r3);
1823

19-
System.out.println("Get r1: " + ARRAY_STORAGE.get(r1.uuid));
24+
System.out.println("Get r1: " + ARRAY_STORAGE.get(r1.getUuid()));
2025
System.out.println("Size: " + ARRAY_STORAGE.size());
2126

2227
System.out.println("Get dummy: " + ARRAY_STORAGE.get("dummy"));
2328

2429
printAll();
25-
ARRAY_STORAGE.delete(r1.uuid);
30+
ARRAY_STORAGE.delete(r1.getUuid());
2631
printAll();
2732
ARRAY_STORAGE.clear();
2833
printAll();
@@ -36,4 +41,4 @@ static void printAll() {
3641
System.out.println(r);
3742
}
3843
}
39-
}
44+
}
Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
package ru.javawebinar.basejava.model;
2+
3+
/**
4+
* ru.javawebinar.basejava.model.Resume class
5+
*/
6+
public class Resume implements Comparable<Resume>{
7+
8+
// Unique identifier
9+
private String uuid;
10+
11+
public String getUuid() {
12+
return uuid;
13+
}
14+
15+
public void setUuid(String uuid) {
16+
this.uuid = uuid;
17+
}
18+
19+
@Override
20+
public boolean equals(Object o) {
21+
if (this == o) return true;
22+
if (o == null || getClass() != o.getClass()) return false;
23+
24+
Resume resume = (Resume) o;
25+
26+
return uuid.equals(resume.uuid);
27+
}
28+
29+
@Override
30+
public int hashCode() {
31+
return uuid.hashCode();
32+
}
33+
34+
@Override
35+
public String toString() {
36+
return uuid;
37+
}
38+
39+
@Override
40+
public int compareTo(Resume o) {
41+
return uuid.compareTo(o.uuid);
42+
}
43+
}
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
package ru.javawebinar.basejava.storage;
2+
3+
import ru.javawebinar.basejava.model.Resume;
4+
5+
/**
6+
* Array based storage for Resumes
7+
*/
8+
public abstract class AbstractArrayStorage implements Storage {
9+
protected static final int STORAGE_LIMIT = 10000;
10+
11+
protected Resume[] storage = new Resume[STORAGE_LIMIT];
12+
protected int size = 0;
13+
14+
public int size() {
15+
return size;
16+
}
17+
18+
public Resume get(String uuid) {
19+
int index = getIndex(uuid);
20+
if (index == -1) {
21+
System.out.println("Resume " + uuid + " not exist");
22+
return null;
23+
}
24+
return storage[index];
25+
}
26+
27+
protected abstract int getIndex(String uuid);
28+
}
Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
package ru.javawebinar.basejava.storage;
2+
3+
import ru.javawebinar.basejava.model.Resume;
4+
5+
import java.util.Arrays;
6+
7+
/**
8+
* Array based storage for Resumes
9+
*/
10+
public class ArrayStorage extends AbstractArrayStorage {
11+
12+
public void clear() {
13+
Arrays.fill(storage, 0, size, null);
14+
size = 0;
15+
}
16+
17+
public void update(Resume r) {
18+
int index = getIndex(r.getUuid());
19+
if (index == -1) {
20+
System.out.println("Resume " + r.getUuid() + " not exist");
21+
} else {
22+
storage[index] = r;
23+
}
24+
}
25+
26+
public void save(Resume r) {
27+
if (getIndex(r.getUuid()) != -1) {
28+
System.out.println("Resume " + r.getUuid() + " already exist");
29+
} else if (size >= STORAGE_LIMIT) {
30+
System.out.println("Storage overflow");
31+
} else {
32+
storage[size] = r;
33+
size++;
34+
}
35+
}
36+
37+
public void delete(String uuid) {
38+
int index = getIndex(uuid);
39+
if (index == -1) {
40+
System.out.println("Resume " + uuid + " not exist");
41+
} else {
42+
storage[index] = storage[size - 1];
43+
storage[size - 1] = null;
44+
size--;
45+
}
46+
}
47+
48+
public Resume[] getAll() {
49+
return Arrays.copyOfRange(storage, 0, size);
50+
}
51+
52+
protected int getIndex(String uuid) {
53+
for (int i = 0; i < size; i++) {
54+
if (uuid.equals(storage[i].getUuid())) {
55+
return i;
56+
}
57+
}
58+
return -1;
59+
}
60+
}
Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
package ru.javawebinar.basejava.storage;
2+
3+
import ru.javawebinar.basejava.model.Resume;
4+
5+
import java.util.Arrays;
6+
7+
public class SortedArrayStorage extends AbstractArrayStorage{
8+
@Override
9+
public void clear() {
10+
11+
}
12+
13+
@Override
14+
public void update(Resume r) {
15+
16+
}
17+
18+
@Override
19+
public void save(Resume r) {
20+
21+
}
22+
23+
@Override
24+
public void delete(String uuid) {
25+
26+
}
27+
28+
@Override
29+
public Resume[] getAll() {
30+
return new Resume[0];
31+
}
32+
33+
@Override
34+
protected int getIndex(String uuid) {
35+
Resume searchKey = new Resume();
36+
searchKey.setUuid(uuid);
37+
return Arrays.binarySearch(storage, 0, size, searchKey);
38+
}
39+
}
Lines changed: 20 additions & 0 deletions

0 commit comments

Comments
 (0)