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
- 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); } }
- 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); }
- 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"); } } }
- 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(); } }
Nice work! Good to see this..
If I place Car Parking Barrier on my area will that be nice as your work? 😉
LikeLike
🙂
LikeLike
send me SRS of this project kindly…..
LikeLike
Requirements are mentioned at the start of the post!
LikeLike
This is really cool. How did you get the idea to solve this particular topic to learn development? I am a java test automation engineer on my way to transitioning into software development as well.
LikeLike
Thanks! It was an assignment given by a developer friend!
LikeLike
where is the main methode here?
LikeLike
The CarParkManagement class!
LikeLike
thanx
LikeLike
Was this a windows or console app?? Good job ..
LikeLike
Thank you!! Just a Console app.
LikeLiked by 1 person
I want u to try building web apps. M into .net not java but i could understand ur code. Oops concept are somewhat same in c# also
LikeLike
I really not enjoy programming but have to learn it for automation. So trying to develop a taste I would say. Maybe one day!!!
LikeLiked by 1 person
So what do u enjoy basically. I’m not sure about your profession .. I’m a tech enthusiast and evangelist. Hence just keen
LikeLike
I am a software tester!
LikeLiked by 1 person
There you go!! Well tester learning to develop must be appreciated ..
LikeLike
Thanks so much :). I feel a bit motivated already!
LikeLiked by 1 person
This reminds me of a tester colleague of mine who use to learn selenium and for that he developed an interest towards programming also. 🙂
LikeLike
Glad to hear that!!
LikeLiked by 1 person
Nice work! The one improvement that I can suggest in the code is to use toString( ) function instead of printDetails( ) in class Car and its subclasses. Then in your main class instead of calling vehicle.printDetails( ), you can just use System.out.println(vehicle).
This will be a better coding practice, because toString( ) function represents a standard way of representing the objects. Also as an example, in case you get a requirement to output all the parked vehicles to a file (instead of System.out), the change would only be a method in the main file and you won’t be touching all other classes.
LikeLike
Thank you so much Manish!
I will try to implement your suggestion!
LikeLike
A project for the semester is ready!! Yieee .. 😀
I am surely gonna try it.
LikeLiked by 1 person
Thanks Ankit!! Are you an engineering student??
LikeLiked by 1 person
Yes Ruma 😉
LikeLiked by 1 person
Cool!!
LikeLiked by 1 person