diff --git a/ProjectElectricLocomotive/LocoStorageOverflowException.java b/ProjectElectricLocomotive/LocoStorageOverflowException.java new file mode 100644 index 0000000..31c486d --- /dev/null +++ b/ProjectElectricLocomotive/LocoStorageOverflowException.java @@ -0,0 +1,17 @@ +package ProjectElectricLocomotive; + +public class LocoStorageOverflowException extends RuntimeException{ + public LocoStorageOverflowException(Throwable ex){ + super(ex); + } + public LocoStorageOverflowException(int count){ + super("В наборе превышено допустимое количество локомотивов:" + count); + } + public LocoStorageOverflowException(String message){ + super(message); + } + public LocoStorageOverflowException(String message, Throwable ex){ + super(message, ex); + } + +} diff --git a/ProjectElectricLocomotive/SetGeneric.java b/ProjectElectricLocomotive/SetGeneric.java index 062252a..4be439c 100644 --- a/ProjectElectricLocomotive/SetGeneric.java +++ b/ProjectElectricLocomotive/SetGeneric.java @@ -25,8 +25,10 @@ public class SetGeneric{ public int Insert(T loco, int position) { - if(position < 0 || position > maxCount) + if(position < 0) return -1; + if(position >= maxCount) + throw new LocoStorageOverflowException(maxCount); else { _places.add(position, loco); @@ -41,6 +43,8 @@ public class SetGeneric{ else { T plane = _places.get(position); + if(plane == null) + throw new LocoNotFoundException(position); _places.set(position, null); return plane; }