59 lines
1.5 KiB
Java
59 lines
1.5 KiB
Java
package Repository;
|
|
|
|
import Model.IVehicle;
|
|
|
|
public class VehicleRepo implements IRepository {
|
|
|
|
private IVehicle[] _vehicles;
|
|
private Integer _capacity;
|
|
private Integer _size;
|
|
|
|
public VehicleRepo(Integer capacity) throws IllegalArgumentException {
|
|
if(capacity < 0) {
|
|
throw new IllegalArgumentException("Capacity must be a positive number");
|
|
}
|
|
this._capacity = capacity;
|
|
this._vehicles = new IVehicle[capacity];
|
|
this._size = 0;
|
|
}
|
|
|
|
@Override
|
|
public void add(IVehicle vehicle) throws IllegalArgumentException {
|
|
if(_size == _capacity) {
|
|
throw new IllegalArgumentException("Repository is full");
|
|
}
|
|
_vehicles[_size] = vehicle;
|
|
_size++;
|
|
}
|
|
|
|
@Override
|
|
public void remove(int index) throws IllegalArgumentException {
|
|
if(_size == 0) {
|
|
throw new IllegalArgumentException("Repository is empty");
|
|
}
|
|
if(index < 0 || index > _size) {
|
|
throw new IllegalArgumentException("Index out of bounds");
|
|
}
|
|
for(int i = index; i < _size - 1; i++) {
|
|
_vehicles[i] = _vehicles[i + 1];
|
|
}
|
|
_size--;
|
|
}
|
|
|
|
@Override
|
|
public IVehicle[] getAll() {
|
|
IVehicle[] copy = new IVehicle[_size];
|
|
for(int i = 0; i < _size; i++) {
|
|
copy[i] = _vehicles[i];
|
|
}
|
|
return copy;
|
|
}
|
|
|
|
@Override
|
|
public int getSize() {
|
|
return _size;
|
|
}
|
|
|
|
|
|
}
|