Parking Lot Design JavaOne of the most prominent questions asked in a Java interview is the parking lot design in Java. Parking lot design in Java is a design problem that deals with how the vehicles are parked in a parking lot. It is mainly asked in the HLD or LLD (High Level or Low-Level Design) round of the top multinational companies such as Amazon, Google, Facebook, etc. Note that there is no right or wrong answer to this design problem. Therefore, such type of design problem requires a good discussion with the interviewer that what kind of parking design the interviewer wants. For example, one interviewer may want a parking lot to be of 5 floors, whereas another interviewer is fine with a single floor parking lot. In this section, we will use the object-oriented paradigm of Java to design the parking lot. Before going through this section, readers are required to have a good understanding of the object-oriented principles of Java. Designing the Parking lotWe are going to write the code for the parking lot as per the design described below. The objects present in our design are the Vehicles, ParkingLot, Slots, and Levels. ParkingLot: There are 'x' levels or floors and 'y' slots per floor. Levels: Each level is an independent entity with a floor number, its slots and the lanes within it. The number of lanes is designed on the basis of number of slots. In our case, 10 Slots comprise 1 lane. Slots: One slot is independent of the other slot. The slot size matters for a vehicle to fill the slot. For example, a small slot cannot be filled by a large vehicle. Vehicles: Object with the company name, vehicle number, and their type. A vehicle has the number plate and the properties of the company it is from. We have considered the Slots and Levels as entities that are not dependent so that a level can be added by any number of slots later. Whenever a vehicle goes out or comes in, the list of vehicles that represents the company of which the recent vehicle has left or come in is updated. Since a vehicle has entered or left, the number of available slots is also updated. Methods:parkVehicle(): The parkVehicle() method parks a vehicle accordingly, also take into consideration the company that has made the vehicle. leaveOperation(): The operation exits a vehicle 'V' at a level 'x'. companyParked(): The CompanyParked() method facilitates the user to observe the list of vehicles parked by a specific company. Parking Lot Java ProgramAfter accomplishing the design part, now we can write the code. The following program used the above-mentioned design to create a parking lot. FileName: VSize.java FileName: Vehicle.java FileName: Car.java FileName: Motorcycle.java FileName: Lvl.java FileName: ParkingSlot.java FileName: ParkingLot.java FileName: Main.java Output: Level 0 created with 2 slots Level 1 created with 2 slots ------------------------------------ It is a Car parked in Lane Number 0, Slot Number 0 Level 0 with Vehicle Number 1234 from Microsoft ------------------------------------ It is a Motorcycle parked in Lane Number 0, Slot Number 1 Level 0 with Vehicle Number 4016 from Microsoft ------------------------------------ It is a Car parked in Lane Number 0, Slot Number 0 Level 1 with Vehicle Number 1609 from Google ------------------------------------------ The vehicles of Microsoft: 1234 4016 ------------------------------------------ The vehicles of Google: 1609 ------------------------------------------ Available Slots in the current level :1 Slot freed from Level 0 and exited 4016 of Microsoft ------------------------------------------ The vehicles of Microsoft: 1234 ------------------------------------ It is a Motorcycle parked in Lane Number 0, Slot Number 1 Level 1 with Vehicle Number 1389 from Google ------------------------------------ PARKING IS FULL Next TopicBoyer Moore Java |
We provides tutorials and interview questions of all technology like java tutorial, android, java frameworks
G-13, 2nd Floor, Sec-3, Noida, UP, 201301, India