Tags

,

Requirements:

— Define a maximum number of cars that can be parked.

— When you try to park more than above, number, user should get a message.

— Print details of all parked cars.

How I did it?

Below are the steps and code

  1. Create a Vehicle Class where
  • Define all properties and methods for it.
  • Define a default Constructor
  • Define a constructor with Arguments
  • Define getters and setters for all properties
  • Define methods


// super class
public class Vehicle {

private String model;
 private String color;
 private int numOfWheels;
 private String transmission;


// default constructor
public Vehicle(){
 
}

// constructor
public Vehicle(String model, String color, int numOfWheels, String transmission){
 this.model = model;
 this.color = color;
 this.numOfWheels = numOfWheels;
 this.transmission = transmission;
}

//getters
 public String getModel(){
 return model;
 }

public String getColor(){
 return color;
 }
 
 public int NumOfWheels(){
 return numOfWheels;
 }
 
 public String transmission(){
 return transmission;
 }
 
 //setters
 public void setNumOfWheels (int numOfWheels){
 this.numOfWheels = numOfWheels;
 }
 
 public void setModel(String model){
 this.model = model;
 }
 
 public void setColor(String color){
 this.color = color;
 }
 
 public void setTransmission(String transmission){
 this.transmission = transmission;
 }
 
 // Print Method
 public void printDetails(){
 System.out.println("The color of this vehicle is " + this.color);
 System.out.println("The model of this vehicle is " + this.model);
 System.out.println("The transmission for this vehicle is " + this.transmission);
 System.out.println("The number of wheels in this vehicle is "+ this.numOfWheels);
 }
 
}

  1. Define sub classes like car, truck and bike which will inherit from the super class Vehicle.
  •    In each class, try to include a new property (not inherited from Vehicle class)
  •    Define a Constructor which calls superclass constructor
  •    Define another constructor which has arguments.
  •    Define getters and setters for the child class new property.
  •    Define method for the child class if any.

Bike.java


public class Bike extends Vehicle {

private boolean suspension;

public Bike(){
super();
}

public Bike(String model, String color, int numOfWheels, boolean suspension){
super( model, color, numOfWheels, "na"); // na for transmission as Bike class does not need a suspension property
this.suspension = suspension;
}

//getter
public boolean getSuspension(){
return suspension;
}

//setter
public void setSuspension(){
this.suspension = suspension;
}

//method
public void printBikeDetails(){
super.printDetails();
System.out.println("Does this bike have suspension ?? " + this.suspension);
}
}

Car.java


public class Car extends Vehicle{

private Boolean roof;

// Constructors
public Car(){
super();
}

public Car(String model, String color, int numOfWheels, String transmission, Boolean roof){

super(model, color, numOfWheels, transmission);
this.roof = roof;

}

//getter
public boolean getRoof(){
return roof;
}

// setter
public void setRoof(boolean roof){
this.roof=roof;
}

// Methods
public void printdetailscar(){
super.printDetails(); // Calling superclass method
System.out.println(" does this car has a roof: " + this.roof);
}
}

Truck.java


public class Truck extends Vehicle{

private int numOfContainers;

public Truck(){
super();
}

public Truck(String model, String color, int numOfWheels, String transmission, int numOfContainers){
super ( model, color, numOfWheels, transmission) ;
this.numOfContainers = numOfContainers;
}

//getters

public int getnumOfContainers(){
return numOfContainers;
}

//setter
public void setnumOfContainers(){
this.numOfContainers = numOfContainers;
}

//Method
public void printTruckdetails(){
super.printDetails();
System.out.println(" Number of Containers in this Truck are: " + this.numOfContainers);

}

  1. Define a class Car Park
  •  Define capacity of Car Park here and create an arraylist to hold the value of parked cars
  •  Define Constructor
  •  Define all the methods for calculating available spots, occupied spots, to add vehicles and to print details of the vehicles

<strong></strong>

import java.util.ArrayList;

public class CarPark {

private static final int maxCapacity = 3;
private ArrayList <Vehicle>carParkArray; // Every member of carParkArray will be of type Vehicle

//constructor
public CarPark(){

this.carParkArray = new ArrayList<Vehicle>(maxCapacity);
//This ensures car PArk has three vehicles max

}

// Method to find maximum capacity of the car park
public static int getMaxCapacity(){
return maxCapacity;
}

// Method to find available spots in the carpark
public int numOfAvailableSpots(){
return this.maxCapacity - this.carParkArray.size();
}

// Method to find number of occupied spaces in the carpark
public int numOfOccupiedSpots(){
return this.carParkArray.size();
}

//Method to add vehicles to the carpark
public void addVehicles(Vehicle vehicle){

if(this.numOfAvailableSpots() > 0){
this.carParkArray.add(vehicle);
vehicle.printDetails();
System.out.println("++++++++++++ \n");
}else {
System.out.println("Car Park is full");
}

}
//Method to print parked vehicle details
public void printParkedVehicleDetails(){

// For every vehicle in array carParkArray, do blah blah
for(Vehicle vehicle: this.carParkArray){
vehicle.printDetails();
System.out.println("++++++++++++++++ \n");

}

}
}

  1. Define a CarParkManagement Class
  • Instantiate your classes here for vehicles
  • Instantiate carPark
  • Add vehicles
  • Call methods to print information

public class CarParkManagement {

public static void main(String[] args) {

Car BMW = new Car("Z4", "red",4, "Auto",false);

Bike Superfly = new Bike("XTR", "Black",2,true );

Truck Mercedes = new Truck("8X4", "Blue", 12,"Manual", 4 );

Car Hyundai = new Car("i30","Light Blue",4,"Auto",true);

CarPark myCarPark = new CarPark();

//Parking Vehicles
myCarPark.addVehicles(BMW);
myCarPark.addVehicles(Hyundai);
myCarPark.addVehicles(Mercedes);
myCarPark.addVehicles(Superfly);

// Comment some of the above to see if it works

System.out.println(" No. of Parked vehicles: " + myCarPark.numOfOccupiedSpots());

System.out.println("No. of Available Spots: "+ myCarPark.numOfAvailableSpots());

// Printing details
System.out.println("====================\n");
System.out.println("List of All Parked Vehicles is as folows: ");
myCarPark.printParkedVehicleDetails();


}

}

CarParkManagement