Skip to content

Commit 8645cfa

Browse files
committed
lesson2, attempt 2
1 parent c4e3506 commit 8645cfa

File tree

7 files changed

+128
-104
lines changed

7 files changed

+128
-104
lines changed

lesson/lesson1.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -79,7 +79,7 @@
7979

8080
![finish](https://user-images.githubusercontent.com/29703461/38275669-3e621614-379b-11e8-8b3a-8e0a3ad4c65c.png)
8181

82-
- Реализуйте класс `ArrayStorage`, организовав хранение резюме на основе массива с методами `save, get, delete, size, clear, getAll`
82+
- Реализуйте класс `com.urise.webapp.storage.ArrayStorage`, организовав хранение резюме на основе массива с методами `save, get, delete, size, clear, getAll`
8383
- Храните все резюме в начале `storage` (без дырок в виде `null`), чтобы не перебирать каждый раз все 10000 элементов
8484
```
8585
Схема хранения резюме в массиве storage (в элементах от 0 до size-1 отсутствуют null):
@@ -112,7 +112,7 @@ r1, r2, r3,..., rn, null, null,..., null
112112
4. Не злоупотребляйте пустыми строками. Они используются нечасто для логического отделения больших кусков кода.
113113
5. Удаляйте неиспользуемые импорты (`Ctrl + Alt + O`)
114114
5. Не игнорируй подсказки IDEA (подсвечивает)
115-
6. `Resume r` — давай переменным осмысленные имена, например `resume`. `r` допустимо в коротких циклах и лямбдах.
115+
6. `com.urise.webapp.model.Resume r` — давай переменным осмысленные имена, например `resume`. `r` допустимо в коротких циклах и лямбдах.
116116
7. В методе `clear()` обнуление массива предполагает именно обнуление (null), а не создание нового
117117

118118
# Дополнительно (как учиться программированию)

src/ArrayStorage.java

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

src/MainArray.java

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,12 @@
1+
import com.urise.webapp.model.Resume;
2+
import com.urise.webapp.storage.ArrayStorage;
3+
14
import java.io.BufferedReader;
25
import java.io.IOException;
36
import java.io.InputStreamReader;
47

58
/**
6-
* Interactive test for ArrayStorage implementation
9+
* Interactive test for com.urise.webapp.storage.ArrayStorage implementation
710
* (just run, no need to understand)
811
*/
912
public class MainArray {
@@ -32,7 +35,7 @@ public static void main(String[] args) throws IOException {
3235
break;
3336
case "save":
3437
r = new Resume();
35-
r.uuid = uuid;
38+
r.setUuid(uuid);
3639
ARRAY_STORAGE.save(r);
3740
printAll();
3841
break;

src/MainTestArrayStorage.java

Lines changed: 18 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,32 +1,44 @@
1+
import com.urise.webapp.model.Resume;
2+
import com.urise.webapp.storage.ArrayStorage;
3+
14
/**
2-
* Test for your ArrayStorage implementation
5+
* Test for your com.urise.webapp.storage.ArrayStorage implementation
36
*/
47
public class MainTestArrayStorage {
58
static final ArrayStorage ARRAY_STORAGE = new ArrayStorage();
69

710
public static void main(String[] args) {
811
Resume r1 = new Resume();
9-
r1.uuid = "uuid1";
12+
r1.setUuid("uuid1");
1013
Resume r2 = new Resume();
11-
r2.uuid = "uuid2";
14+
r2.setUuid("uuid2");
1215
Resume r3 = new Resume();
13-
r3.uuid = "uuid3";
16+
r3.setUuid("uuid3");
1417

1518
ARRAY_STORAGE.save(r1);
1619
ARRAY_STORAGE.save(r2);
1720
ARRAY_STORAGE.save(r3);
1821

19-
System.out.println("Get r1: " + ARRAY_STORAGE.get(r1.uuid));
22+
System.out.println("Get r1: " + ARRAY_STORAGE.get(r1.getUuid()));
2023
System.out.println("Size: " + ARRAY_STORAGE.size());
2124

2225
System.out.println("Get dummy: " + ARRAY_STORAGE.get("dummy"));
2326

2427
printAll();
25-
ARRAY_STORAGE.delete(r1.uuid);
28+
ARRAY_STORAGE.delete(r1.getUuid());
29+
printAll();
30+
31+
r1.setUuid("uuid1_upDated");
32+
ARRAY_STORAGE.update(r1);
33+
34+
r2.setUuid("uuid2_upDated");
35+
ARRAY_STORAGE.update(r2);
2636
printAll();
37+
2738
ARRAY_STORAGE.clear();
2839
printAll();
2940

41+
3042
System.out.println("Size: " + ARRAY_STORAGE.size());
3143
}
3244

src/Resume.java

Lines changed: 0 additions & 13 deletions
This file was deleted.
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
package com.urise.webapp.model;
2+
3+
/**
4+
* Initial resume class
5+
*/
6+
public class Resume {
7+
8+
// Unique identifier
9+
private String uuid;
10+
11+
@Override
12+
public String toString() {
13+
return uuid;
14+
}
15+
16+
public String getUuid() {
17+
return uuid;
18+
}
19+
20+
public void setUuid(String uuid) {
21+
this.uuid = uuid;
22+
}
23+
}
Lines changed: 80 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,80 @@
1+
package com.urise.webapp.storage;
2+
3+
import com.urise.webapp.model.Resume;
4+
5+
import java.util.Arrays;
6+
7+
/**
8+
* Array based storage for Resumes
9+
*/
10+
public class ArrayStorage {
11+
private Resume[] storage = new Resume[MAX_SIZE];
12+
private int recordCounter = 0;
13+
private static final int MAX_SIZE=10_0000;
14+
private static final String ERROR_MSG = "ERROR: record doesn't exists!";
15+
16+
public void clear() {
17+
Arrays.fill(storage,null);
18+
recordCounter = 0;
19+
}
20+
21+
public void save(Resume r) {
22+
if (getIndex(r.getUuid()) != -1 || recordCounter>MAX_SIZE) {
23+
System.out.println(ERROR_MSG);
24+
return;
25+
}
26+
storage[recordCounter++] = r;
27+
}
28+
29+
public Resume get(String uuid) {
30+
if (getIndex(uuid) >= 0) {
31+
return storage[getIndex(uuid)];
32+
}
33+
return null;
34+
}
35+
36+
private int getIndex(String uuid) {
37+
for (int i = 0; i < recordCounter; i++) {
38+
if (storage[i].getUuid().equals(uuid)) {
39+
return i;
40+
}
41+
}
42+
return -1;
43+
}
44+
45+
46+
public void delete(String uuid) {
47+
if (getIndex(uuid) >= 0) {
48+
storage[getIndex(uuid)] = storage[recordCounter - 1];
49+
storage[recordCounter - 1] = null;
50+
recordCounter--;
51+
} else {
52+
System.out.println(ERROR_MSG);
53+
}
54+
}
55+
56+
57+
/**
58+
* @return array, contains only Resumes in storage (without null)
59+
*/
60+
61+
62+
public Resume[] getAll() {
63+
return Arrays.copyOf(storage, recordCounter);
64+
}
65+
66+
public int size() {
67+
return recordCounter;
68+
}
69+
70+
71+
public boolean update(Resume resume) {
72+
int pos = getIndex(resume.getUuid());
73+
if (pos == -1) {
74+
System.out.println(ERROR_MSG);
75+
return false;
76+
}
77+
storage[pos].setUuid(resume.getUuid());
78+
return true;
79+
}
80+
}

0 commit comments

Comments
 (0)