-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathDesignParkingSystem.java
62 lines (47 loc) · 1.79 KB
/
DesignParkingSystem.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
class DesignParkingSystem{
/**
*
*
* ParkingSystem(int big, int medium, int small) Initializes object of the ParkingSystem class. The number of slots for each parking space are given as part of the constructor.
bool addCar(int carType) Checks whether there is a parking space of carType for the car that wants to get into the parking lot. carType can be of three kinds: big, medium, or small, which are represented by 1, 2, and 3 respectively. A car can only park in a parking space of its carType. If there is no space available, return false, else park the car in that size space and return true.
*/
static class ParkingSystem {
int big, medium, small;
public ParkingSystem(int big, int medium, int small) {
this.big = big;
this.small= small;
this.medium=medium;
}
public boolean addCar(int carType) {
switch(carType){
case 1:
if(this.big==0) return false;
else this.big--;
return true;
case 2:
if(this.medium==0) return false;
else this.medium--;
return true;
case 3:
if(this.small==0) return false;
else this.small--;
return true;
default:
return false;
}
}
}
public static void main(String[] args) {
int types[]={1,3,2,1,2,3,1,2,3,3,2,1};
Car cars[]=new Car[types.length];
for(int i=0;i<types.length;i++){
cars[i]=new Car(types[i]);
}
}
static class Car{
int type;
public Car(int type) {
this.type = type;
}
}
}