-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathLab11.java
More file actions
127 lines (90 loc) · 2.87 KB
/
Copy pathLab11.java
File metadata and controls
127 lines (90 loc) · 2.87 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
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
1)
import java.util.Scanner;
class Vehicle {
private String name;
private String type;
private String size;
public Vehicle(String name, String type, String size) {
this.name = name;
this.type = type;
this.size = size;
}
public String getType() {
return type;
}
public String getSize() {
return size;
}
}
class Parking {
private int levels;
private int slots;
private int[][] park;
private int[][] slotSize;
public Parking(int levels, int slots) {
this.levels = levels;
this.slots = slots;
this.park = new int[levels][slots];
this.slotSize = new int[levels][slots];
initializeSlotSizes();
}
private void initializeSlotSizes() {
for (int i = 0; i < levels; i++) {
for (int j = 0; j < slots; j++) {
if (i == 0) {
slotSize[i][j] = 0;
} else if (i == 1) {
slotSize[i][j] = 1;
} else {
slotSize[i][j] = 2;
}
}
}
}
private String getSizeDescription(int size) {
switch (size) {
case 0: return "small";
case 1: return "medium";
case 2: return "large";
default: return "unknown";
}
}
public boolean parkVehicle(Vehicle vehicle) {
int requiredSize = getSizeIndex(vehicle.getSize());
for (int i = 0; i < levels; i++) {
for (int j = 0; j < slots; j++) {
if (park[i][j] == 0 && slotSize[i][j] >= requiredSize) {
park[i][j] = 1;
System.out.println("The " + vehicle.getType() + " has been parked at level " + i + " and slot " + j);
return true;
}
}
}
System.out.println("No parking slots available for the vehicle size: " + vehicle.getSize());
return false;
}
private int getSizeIndex(String size) {
switch (size.toLowerCase()) {
case "small": return 0;
case "medium": return 1;
case "large": return 2;
default: return -1;
}
}
}
public class Tan {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
Parking parkingLot = new Parking(3, 10);
parkingLot.displayAvailableSlots();
System.out.print("Enter vehicle name: ");
String name = sc.nextLine();
System.out.print("Enter vehicle type (e.g., car, truck): ");
String type = sc.nextLine();
System.out.print("Enter vehicle size (small, medium, large): ");
String size = sc.nextLine();
Vehicle vehicle = new Vehicle(name, type, size);
parkingLot.parkVehicle(vehicle);
sc.close();
}
}