Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
64 changes: 0 additions & 64 deletions src/ArrayStorage.java

This file was deleted.

13 changes: 0 additions & 13 deletions src/Resume.java

This file was deleted.

9 changes: 7 additions & 2 deletions src/MainArray.java → src/com/urise/webapp/MainArray.java
Original file line number Diff line number Diff line change
@@ -1,9 +1,14 @@
package com.urise.webapp;

import com.urise.webapp.model.Resume;
import com.urise.webapp.storage.ArrayStorage;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;

/**
* Interactive test for ArrayStorage implementation
* Interactive test for com.urise.webapp.storage.ArrayStorage implementation
* (just run, no need to understand)
*/
public class MainArray {
Expand Down Expand Up @@ -32,7 +37,7 @@ public static void main(String[] args) throws IOException {
break;
case "save":
r = new Resume();
r.uuid = uuid;
r.setUuid(uuid);
ARRAY_STORAGE.save(r);
printAll();
break;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,28 +1,35 @@
package com.urise.webapp;

import com.urise.webapp.model.Resume;
import com.urise.webapp.storage.ArrayStorage;

/**
* Test for your ArrayStorage implementation
* Test for your com.urise.webapp.storage.ArrayStorage implementation
*/
public class MainTestArrayStorage {
static final ArrayStorage ARRAY_STORAGE = new ArrayStorage();

public static void main(String[] args) {
Resume r1 = new Resume();
r1.uuid = "uuid1";
r1.setUuid("uuid1");
Resume r2 = new Resume();
r2.uuid = "uuid2";
r2.setUuid("uuid2");
Resume r3 = new Resume();
r3.uuid = "uuid3";
r3.setUuid("uuid3");

ARRAY_STORAGE.save(r1);
ARRAY_STORAGE.save(r2);
ARRAY_STORAGE.save(r3);

System.out.println("Get r1: " + ARRAY_STORAGE.get(r1.uuid));
System.out.println("Get r1: " + ARRAY_STORAGE.get(r1.getUuid()));
System.out.println("Size: " + ARRAY_STORAGE.size());

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

printAll();
ARRAY_STORAGE.delete(r1.uuid);
ARRAY_STORAGE.delete(r1.getUuid());
printAll();
ARRAY_STORAGE.update("uuid2", "uuid0");
printAll();
ARRAY_STORAGE.clear();
printAll();
Expand Down
23 changes: 23 additions & 0 deletions src/com/urise/webapp/model/Resume.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
package com.urise.webapp.model;

/**
* Initial resume class
*/
public class Resume {

// Unique identifier
private String uuid;

public String getUuid() {
return uuid;
}

public void setUuid(String uuid) {
this.uuid = uuid;
}

@Override
public String toString() {
return uuid;
}
}
90 changes: 90 additions & 0 deletions src/com/urise/webapp/storage/ArrayStorage.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,90 @@
package com.urise.webapp.storage;

import com.urise.webapp.model.Resume;

import java.util.Arrays;

/**
* Array based storage for Resumes
*/

/* Выберите в классе java.util.Arrays подходящие методы для реализации
1. clear()
2. getAll()
*/

public class ArrayStorage {
private Resume[] storage = new Resume[10000];
private int size = 0;

public void clear() {
Arrays.fill(storage, 0, size, null);
size = 0;
}

public void update(String prevUuid, String newUuid) {
int indexOfResumeByUuid = getIndex(prevUuid);
int indexOfResumeByNewUuid = getIndex(newUuid);
if (indexOfResumeByUuid == -1) {
System.out.println("Resume is not present");
} else if (indexOfResumeByNewUuid != -1) {
System.out.println("Resume with such uuid is already exist");
} else {
storage[indexOfResumeByUuid].setUuid(newUuid);
}
}

public void save(Resume r) {
if (getIndex(r.getUuid()) != -1) {
System.out.println("Resume is already present");
} else {
if (size < storage.length) {
storage[size] = r;
size++;
} else {
System.out.println("Storage is overflown");
}
}
}

public Resume get(String uuid) {
int indexOfResumeByUuid = getIndex(uuid);
if (indexOfResumeByUuid == -1) {
System.out.println("Resume is not present");
return null;
} else {
return storage[indexOfResumeByUuid];
}
}

public void delete(String uuid) {
int indexOfResumeByUuid = getIndex(uuid);
if (indexOfResumeByUuid == -1) {
System.out.println("Resume is not present");
} else {
storage[indexOfResumeByUuid] = storage[size - 1];
storage[size - 1] = null;
size--;
}
}

/**
* @return array, contains only Resumes in storage (without null)
*/
public Resume[] getAll() {
return Arrays.copyOf(storage, size);
}

public int size() {
return size;
}

private int getIndex(String uuid) {
for (int i = 0; i < size; i++) {
if (uuid.equals(storage[i].getUuid())) {
return i;
}
}
return -1;
}
}