7 lab add class exception notFound

This commit is contained in:
ekallin 2023-12-18 18:15:38 +04:00
parent 7b1c3f2630
commit 46ce8e7f01
2 changed files with 22 additions and 1 deletions

View File

@ -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);
}
}

View File

@ -25,8 +25,10 @@ public class SetGeneric<T extends DrawingLocomotive>{
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<T extends DrawingLocomotive>{
else
{
T plane = _places.get(position);
if(plane == null)
throw new LocoNotFoundException(position);
_places.set(position, null);
return plane;
}