Don't like ads? PRO users don't see any ads ;-)
Guest

Untitled

By: a guest on May 27th, 2012  |  syntax: None  |  size: 4.11 KB  |  hits: 20  |  expires: Never
download  |  raw  |  embed  |  report abuse  |  print
Text below is selected. Please press Ctrl+C to copy to your clipboard. (⌘+C on Mac)
  1. package parkinghw3;
  2.  
  3. public class Parking
  4. {
  5.         //
  6.         // Members
  7.         //
  8.         static int currentNum = 0;
  9.         Transport[] transports = new Transport[12];
  10.  
  11.         //
  12.         // Getters and setters
  13.         //
  14.         public Transport getVehicle(int index)
  15.         {
  16.                 if (index < currentNum)
  17.                 {
  18.                         return transports[index];
  19.                 }
  20.                 return null;
  21.         }
  22.  
  23.         public void addVehicle(Transport vehicle)
  24.         {
  25.                 if (currentNum == transports.length)
  26.                 {
  27.                         System.out.println("Full - No More Parking Space");
  28.                 } else
  29.                 {
  30.                         transports[currentNum++] = vehicle;
  31.                 }
  32.         }
  33.  
  34.         //
  35.         // Operation
  36.         //
  37.  
  38.         // removeVehicle - Remove Vehicle and shift array so empty spots are at the
  39.         // end
  40.         public void removeVehicle(int index)
  41.         {
  42.                 if (index == currentNum - 1)
  43.                 {
  44.                         transports[--currentNum] = null;
  45.                 }
  46.  
  47.                 if (index < currentNum)
  48.                 {
  49.                         for (int i = index; i < currentNum - 1; i++)
  50.                         {
  51.                                 transports[i] = transports[i + 1];
  52.  
  53.                         }
  54.                         transports[--currentNum] = null;
  55.                 }
  56.         }
  57.  
  58.         public String toString()
  59.         {
  60.                 String str = "\nVehicles list:\n";
  61.  
  62.                 for (int i = 0; i < currentNum; i++)
  63.                 {
  64.                         if (transports[i] instanceof Car)
  65.                         {
  66.                                 str += (i + 1) + ". Car:\n" + ((Car) transports[i]).toString();
  67.                         } else if (transports[i] instanceof Truck)
  68.                         {
  69.                                 str += (i + 1) + ". Truck:\n"
  70.                                                 + ((Truck) transports[i]).toString();
  71.                         } else if (transports[i] instanceof Bike)
  72.                         {
  73.                                 str += (i + 1) + ". Bike:\n"
  74.                                                 + ((Bike) transports[i]).toString();
  75.                         }
  76.  
  77.                         str += "****************************\n";
  78.                 }
  79.                 return str;
  80.         }
  81.  
  82.         public Transport vehicleByColor(String color)
  83.         {
  84.                 for (int i = 0; i < currentNum; i++)
  85.                 {
  86.                         if (transports[i].getColor() == color)
  87.                         {
  88.                                 return transports[i];
  89.                         }
  90.                 }
  91.                 return null;
  92.         }
  93.  
  94.         // Returns number Of Vehicles with more than two wheels
  95.         public int numOfVehicles()
  96.         {
  97.                 int sum = 0;
  98.  
  99.                 for (int i = 0; i < currentNum; i++)
  100.                 {
  101.                         if (transports[i] instanceof Vehicle)
  102.                         {
  103.                                 sum++;
  104.                         }
  105.                 }
  106.                 return sum;
  107.         }
  108.  
  109.         // Returns number Of Cars
  110.         public int numOfCars()
  111.         {
  112.                 int sum = 0;
  113.  
  114.                 for (int i = 0; i < currentNum; i++)
  115.                 {
  116.                         if (transports[i] instanceof Car)
  117.                         {
  118.                                 sum++;
  119.                         }
  120.                 }
  121.                 return sum;
  122.         }
  123.  
  124.         // Returns number Of Trucks between weight1 and weight2
  125.         public int numOfTrucksBetweenTwoWeights(double weight1, double weight2)
  126.         {
  127.                 int sum = 0;
  128.                 double currentWeight = 0;
  129.  
  130.                 for (int i = 0; i < currentNum; i++)
  131.                 {
  132.                         if (transports[i] instanceof Truck)
  133.                         {
  134.                                 currentWeight = ((Truck) transports[i]).getWeight();
  135.                                 if (currentWeight >= weight1 && currentWeight <= weight2)
  136.                                 {
  137.                                         sum++;
  138.                                 }
  139.                         }
  140.                 }
  141.                 return sum;
  142.         }
  143.        
  144.         // Returns number Of Bikes with gear between gear1 and gear2
  145.         public int numOfBikesWithGearBetweenTwoGears(int gear1, int gear2)
  146.         {
  147.                 int sum = 0;
  148.                 int currentGear = 0;
  149.  
  150.                 for (int i = 0; i < currentNum; i++)
  151.                 {
  152.                         if (transports[i] instanceof Bike)
  153.                         {
  154.                                 currentGear = ((Bike) transports[i]).getGear();
  155.                                 if (currentGear >= gear1 && currentGear <= gear2)
  156.                                 {
  157.                                         sum++;
  158.                                 }
  159.                         }
  160.                 }
  161.                 return sum;
  162.         }
  163.        
  164.         // Returns Transports Array according to max velocity
  165.         public Transport[] TransportsWithMaxVelocity(double max_velocity)
  166.         {
  167.                 int sum = 0, j=0;
  168.                 Transport[] temp=null;
  169.  
  170.                 for (int i = 0; i < currentNum; i++)
  171.                 {
  172.                         if (transports[i].getMax_velocity() == max_velocity)
  173.                         {
  174.                                 sum++;
  175.                         }
  176.                 }
  177.                
  178.                 if (sum>0)
  179.                 {
  180.                         temp = new Transport[sum];
  181.                        
  182.                         for (int i = 0; i < currentNum; i++)
  183.                         {
  184.                                 if (transports[i].getMax_velocity() == max_velocity)
  185.                                 {
  186.                                         if (transports[i] instanceof Car)
  187.                                         {
  188.                                                 temp[j++] = new Car(transports[i].getColor(),
  189.                                                                 transports[i].getMax_velocity(), ((Car)transports[i]).getModel());
  190.                                         }
  191.                                         else if (transports[i] instanceof Truck)
  192.                                         {
  193.                                                 temp[j++] = new Truck(transports[i].getColor(), transports[i].getMax_velocity(),
  194.                                                                 ((Truck)transports[i]).getModel(), ((Truck)transports[i]).getWeight());
  195.                                         }
  196.                                         else if (transports[i] instanceof Bike)
  197.                                         {
  198.                                                 temp[j++] = new Bike(transports[i].getColor(),
  199.                                                                 transports[i].getMax_velocity(), ((Bike)transports[i]).getGear());
  200.                                         }
  201.                                 }
  202.                         }
  203.                 }
  204.                
  205.                 return temp;
  206.         }
  207.  
  208. }