recently visited the interview asked such a task. I gave me 40 minutes to her, so it is possible not so simple as it seems:
Task 1.
Implement N // Parking ID Parquet Room
Boolean Park (String ID); // Could you pass?
Void Unpark (String ID);
Assistance is needed in solving this task
Here is my decision.
We assume that an array of parking spaces comes to us from the outside in the argument of the designer
package parking;
Import java.util.arraylist;
Public Class Parking {
int size;
ArrayList & LT; String & GT; Parkingarr;
Public Parking (Int Size, ArrayList Parkingarr) {
this.size = Size;
this.parkingarr = Parkingarr;
}
Public Boolean Park (String ID) {
Return! Parkingarr.Contains (ID);
}
Public Void Unpark (String ID) {
If (Parkingarr.get (Parkingarr.Indexof (ID))! = NULL)
Parkingarr.set (Parkingarr.Indexof (ID), NULL);
}
}
Answer 1
Let’s say:
Public Class Parking {
// Collection Machinesture.
SET & LT; String & GT; Parkingplaces = new hashset & lt; & gt; ();
// Parking capacity
Final int Parking_size;
// Designer with parameter capacity
Public Parking (int parking_size) {
this.parking_size = Parking_Size;
}
// Put the car
// You can not put a car in a busy place or if the number of places exceeds the size of the parking lot
Public Boolean Park (String ID) {
if (parkingplaces.size () & gt; = parking_size || parkingplaces.contains (ID)) Return False;
Parkingplaces.add (id);
RETURN TRUE;
}
// pick up the car can always
Public Void Unpark (String ID) {
parkingplaces.remove (id);
}
}
All requirements made?
Of course it’s strange that the machinery can be called Masha or Tanya, and each time in different ways, but this is a matter of taste