diff --git a/src/DoubleDeckerBus/Generics/HardGeneric.java b/src/DoubleDeckerBus/Generics/HardGeneric.java index 61c48fc..bcb1f21 100644 --- a/src/DoubleDeckerBus/Generics/HardGeneric.java +++ b/src/DoubleDeckerBus/Generics/HardGeneric.java @@ -4,14 +4,16 @@ import DoubleDeckerBus.DrawningObjects.DrawningBus; import DoubleDeckerBus.DrawningObjects.IDraw; import DoubleDeckerBus.Entities.EntityBus; +import java.util.ArrayList; import java.util.Random; public class HardGeneric { - T[] busArray; - U[] doorArray; + ArrayList Buses; + ArrayList Doors; - private int currentSize; + private int CountBuses; private int MaxCountBuses; + private int CountDoors; private int MaxCountDoors; private int pictureBoxWidth; @@ -19,44 +21,47 @@ public class HardGeneric { private int pictureBoxHeight; public HardGeneric(int maxCountBuses, int maxCountDoors, int width, int height) { - currentSize = 0; MaxCountBuses = maxCountBuses; MaxCountDoors = maxCountDoors; - busArray = (T[]) new EntityBus[MaxCountBuses]; - doorArray = (U[]) new IDraw[MaxCountDoors]; + Buses = new ArrayList(MaxCountBuses); + Doors = new ArrayList(MaxCountDoors); + CountBuses = 0; + CountDoors = 0; pictureBoxHeight = height; pictureBoxWidth = width; } - public int insertBus(T entityBus) { - if (busArray[MaxCountBuses - 1] != null) { - return -1; + public boolean insertBus(T bus) { + if (bus == null) { + return false; } - for (int i = currentSize - 1; i >= 0; i--) { - busArray[i + 1] = busArray[i]; - doorArray[i + 1] = doorArray[i]; + if (CountBuses > MaxCountBuses) { + return false; } - currentSize++; - busArray[0] = entityBus; - - return 0; + Buses.add(CountBuses++, bus); + return true; } - public int insertDoor(U door) { - if (doorArray[MaxCountDoors - 1] != null) { - return -1; + public boolean insertDoor(U door) { + if (door == null) { + return false; } - doorArray[0] = door; - return 0; + if (CountDoors > MaxCountDoors) { + return false; + } + Doors.add(CountDoors++, door); + return true; } public DrawningBus MakeObject() { + if (CountBuses == 0 || CountDoors == 0) { + return null; + } Random rand = new Random(); - int indBus = rand.nextInt(0, currentSize); - int indDoors = rand.nextInt(0,currentSize); - EntityBus entity = busArray[indBus]; - IDraw door = doorArray[indDoors]; - return new DrawningBus(entity.Speed(), entity.Weight(), entity.BodyColor(), - pictureBoxWidth, pictureBoxHeight); + int indBus = rand.nextInt(0, CountBuses); + int indDoors = rand.nextInt(0,CountDoors); + T entity = Buses.get(indBus); + U door = Doors.get(indDoors); + return new DrawningBus(entity.Speed(), entity.Weight(), entity.BodyColor(), pictureBoxWidth, pictureBoxHeight); } } diff --git a/src/DoubleDeckerBus/HardForm.java b/src/DoubleDeckerBus/HardForm.java index 1b6a68b..7f5e1d6 100644 --- a/src/DoubleDeckerBus/HardForm.java +++ b/src/DoubleDeckerBus/HardForm.java @@ -1,8 +1,6 @@ package DoubleDeckerBus; -import DoubleDeckerBus.DrawningObjects.DrawningBus; -import DoubleDeckerBus.DrawningObjects.DrawningDoor; -import DoubleDeckerBus.DrawningObjects.IDraw; +import DoubleDeckerBus.DrawningObjects.*; import DoubleDeckerBus.Entities.EntityBus; import DoubleDeckerBus.Generics.HardGeneric; @@ -12,7 +10,7 @@ import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import java.util.Random; -public class HardForm { +public class HardForm extends JFrame { private Canvas canvas; private int pictureBoxWidth; @@ -21,13 +19,13 @@ public class HardForm { private EntityBus createRandomEntityBus() { Random rand = new Random(); return new EntityBus( - rand.nextInt(100, 300), - rand.nextDouble(1000, 3000), - Color.getHSBColor( - rand.nextInt(0, 301), - rand.nextInt(0, 301), - rand.nextInt(0, 301) - ) + rand.nextInt(100, 300), + rand.nextDouble(1000, 3000), + Color.getHSBColor( + rand.nextInt(0, 301), + rand.nextInt(0, 301), + rand.nextInt(0, 301) + ) ); }