-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path01603. Design Parking System.rs
More file actions
56 lines (51 loc) · 1.39 KB
/
Copy path01603. Design Parking System.rs
File metadata and controls
56 lines (51 loc) · 1.39 KB
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
// Create a simple object of 3 ints that keep a track of remaining parking positions for big, medium & small cars
struct ParkingSystem {
bigCars: i32,
mediumCars: i32,
smallCars: i32
}
/**
* `&self` means the method takes an immutable reference.
* If you need a mutable reference, change it to `&mut self` instead.
*/
impl ParkingSystem {
fn new(big: i32, medium: i32, small: i32) -> Self {
return ParkingSystem {
bigCars: big,
mediumCars: medium,
smallCars: small
}
}
fn add_car(&mut self, car_type: i32) -> bool {
if car_type == 1 {
if self.bigCars == 0 {
return false;
} else {
self.bigCars -= 1;
return true;
}
}
if car_type == 2 {
if self.mediumCars == 0 {
return false;
} else {
self.mediumCars -= 1;
return true;
}
}
if car_type == 3 {
if self.smallCars == 0 {
return false;
} else {
self.smallCars -= 1;
return true;
}
}
return false;
}
}
/**
* Your ParkingSystem object will be instantiated and called as such:
* let obj = ParkingSystem::new(big, medium, small);
* let ret_1: bool = obj.add_car(carType);
*/