Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Implement the Storage of the key-value type #1684

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
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
33 changes: 32 additions & 1 deletion src/main/java/core/basesyntax/impl/StorageImpl.java
Original file line number Diff line number Diff line change
@@ -1,19 +1,50 @@
package core.basesyntax.impl;

import core.basesyntax.Storage;
import java.util.Objects;

public class StorageImpl<K, V> implements Storage<K, V> {
private static final int capacity = 10;
private Object[] keys;
private Object[] values;
private int size;

public StorageImpl() {
this.keys = new Object[capacity];
this.values = new Object[capacity];
this.size = 0;
}

@Override
public void put(K key, V value) {
for (int i = 0; i < size; i++) {
if (Objects.equals(keys[i], key)) {
values[i] = value;
return;
}
}

if (size < capacity) {
this.keys[size] = key;
this.values[size] = value;
size++;
} else {
throw new RuntimeException("Storage is full");
}
}

@Override
public V get(K key) {
for (int i = 0; i < size; i++) {
if (Objects.equals(keys[i], key)) {
return (V) values[i];
}
}
return null;
}

@Override
public int size() {
return -1;
return this.size;
}
}