- package parkinghw3;
- public class Parking
- {
- //
- // Members
- //
- static int currentNum = 0;
- Transport[] transports = new Transport[12];
- //
- // Getters and setters
- //
- public Transport getVehicle(int index)
- {
- if (index < currentNum)
- {
- return transports[index];
- }
- return null;
- }
- public void addVehicle(Transport vehicle)
- {
- if (currentNum == transports.length)
- {
- System.out.println("Full - No More Parking Space");
- } else
- {
- transports[currentNum++] = vehicle;
- }
- }
- //
- // Operation
- //
- // removeVehicle - Remove Vehicle and shift array so empty spots are at the
- // end
- public void removeVehicle(int index)
- {
- if (index == currentNum - 1)
- {
- transports[--currentNum] = null;
- }
- if (index < currentNum)
- {
- for (int i = index; i < currentNum - 1; i++)
- {
- transports[i] = transports[i + 1];
- }
- transports[--currentNum] = null;
- }
- }
- public String toString()
- {
- String str = "\nVehicles list:\n";
- for (int i = 0; i < currentNum; i++)
- {
- if (transports[i] instanceof Car)
- {
- str += (i + 1) + ". Car:\n" + ((Car) transports[i]).toString();
- } else if (transports[i] instanceof Truck)
- {
- str += (i + 1) + ". Truck:\n"
- + ((Truck) transports[i]).toString();
- } else if (transports[i] instanceof Bike)
- {
- str += (i + 1) + ". Bike:\n"
- + ((Bike) transports[i]).toString();
- }
- str += "****************************\n";
- }
- return str;
- }
- public Transport vehicleByColor(String color)
- {
- for (int i = 0; i < currentNum; i++)
- {
- if (transports[i].getColor() == color)
- {
- return transports[i];
- }
- }
- return null;
- }
- // Returns number Of Vehicles with more than two wheels
- public int numOfVehicles()
- {
- int sum = 0;
- for (int i = 0; i < currentNum; i++)
- {
- if (transports[i] instanceof Vehicle)
- {
- sum++;
- }
- }
- return sum;
- }
- // Returns number Of Cars
- public int numOfCars()
- {
- int sum = 0;
- for (int i = 0; i < currentNum; i++)
- {
- if (transports[i] instanceof Car)
- {
- sum++;
- }
- }
- return sum;
- }
- // Returns number Of Trucks between weight1 and weight2
- public int numOfTrucksBetweenTwoWeights(double weight1, double weight2)
- {
- int sum = 0;
- double currentWeight = 0;
- for (int i = 0; i < currentNum; i++)
- {
- if (transports[i] instanceof Truck)
- {
- currentWeight = ((Truck) transports[i]).getWeight();
- if (currentWeight >= weight1 && currentWeight <= weight2)
- {
- sum++;
- }
- }
- }
- return sum;
- }
- // Returns number Of Bikes with gear between gear1 and gear2
- public int numOfBikesWithGearBetweenTwoGears(int gear1, int gear2)
- {
- int sum = 0;
- int currentGear = 0;
- for (int i = 0; i < currentNum; i++)
- {
- if (transports[i] instanceof Bike)
- {
- currentGear = ((Bike) transports[i]).getGear();
- if (currentGear >= gear1 && currentGear <= gear2)
- {
- sum++;
- }
- }
- }
- return sum;
- }
- // Returns Transports Array according to max velocity
- public Transport[] TransportsWithMaxVelocity(double max_velocity)
- {
- int sum = 0, j=0;
- Transport[] temp=null;
- for (int i = 0; i < currentNum; i++)
- {
- if (transports[i].getMax_velocity() == max_velocity)
- {
- sum++;
- }
- }
- if (sum>0)
- {
- temp = new Transport[sum];
- for (int i = 0; i < currentNum; i++)
- {
- if (transports[i].getMax_velocity() == max_velocity)
- {
- if (transports[i] instanceof Car)
- {
- temp[j++] = new Car(transports[i].getColor(),
- transports[i].getMax_velocity(), ((Car)transports[i]).getModel());
- }
- else if (transports[i] instanceof Truck)
- {
- temp[j++] = new Truck(transports[i].getColor(), transports[i].getMax_velocity(),
- ((Truck)transports[i]).getModel(), ((Truck)transports[i]).getWeight());
- }
- else if (transports[i] instanceof Bike)
- {
- temp[j++] = new Bike(transports[i].getColor(),
- transports[i].getMax_velocity(), ((Bike)transports[i]).getGear());
- }
- }
- }
- }
- return temp;
- }
- }