School Commit Init
This commit is contained in:
@@ -0,0 +1,10 @@
|
||||
package Repository;
|
||||
|
||||
import Model.IVehicle;
|
||||
|
||||
public interface IRepository {
|
||||
public void add(IVehicle vehicle) throws IllegalArgumentException;
|
||||
public void remove(int index) throws IllegalArgumentException;
|
||||
public IVehicle[] getAll();
|
||||
public int getSize();
|
||||
}
|
||||
@@ -0,0 +1,58 @@
|
||||
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;
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
Reference in New Issue
Block a user