/
parking-lot.component.ts
79 lines (67 loc) · 3.08 KB
/
parking-lot.component.ts
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
import {ChangeDetectionStrategy, Component, computed, effect, signal} from '@angular/core';
import {CommonModule} from '@angular/common';
import {CarType} from "./enums/cartype.enum";
import {ParkingLot} from "./interfaces/parking-lot.interface";
import {Car} from "./interfaces/car.interface";
import {CarLotComponent} from "./car-lot/car-lot.component";
@Component({
selector: 'app-parking-lot',
standalone: true,
imports: [CommonModule, CarLotComponent],
templateUrl: './parking-lot.component.html',
styleUrls: ['./parking-lot.component.scss'],
changeDetection: ChangeDetectionStrategy.OnPush
})
export class ParkingLotComponent {
protected readonly CarType = CarType;
public cars = signal(new Set<Car>());
public parkingLot = signal(new Map<number, ParkingLot>());
public isParkingFree = computed(() => [...this.parkingLot().values()].find(val => val.car === null) === undefined);
public isElectricParkingFree = computed(() => [...this.parkingLot().values()].find(val => val.car === null && val.type === CarType.electric) === undefined);
public isGasParkingFree = computed(() => [...this.parkingLot().values()].find(val => val.car === null && val.type === CarType.gas) === undefined);
public remainingParkingLots = computed(() => [...this.parkingLot().values()].filter(val => val.car === null).length);
constructor() {
this.initializeData();
effect(() => {
console.log(this.parkingLot());
})
}
addCarToParkingLot(car: Car) {
this.parkingLot.mutate((parkingLot) => {
const firstNullValue = [...parkingLot.values()].find(val => val.car === null && val.type === car.type);
if (firstNullValue) {
firstNullValue.car = car;
this.cars().delete(car);
}
})
}
removeCarFromParkingLot(lot: number) {
this.parkingLot.mutate((parkingLot) => {
const car = parkingLot.get(lot)?.car!;
parkingLot.set(lot, { type: parkingLot.get(lot)?.type!, car: null });
this.cars().add(car);
})
}
private initializeData() {
this.cars.set(new Set([
{id: 1, name: 'Tesla Model S', color: 'White', type: CarType.electric},
{id: 2, name: 'BMW i3', color: 'Blue', type: CarType.electric},
{id: 3, name: 'Toyota Prius', color: 'Silver', type: CarType.electric},
{id: 4, name: 'Nissan Leaf', color: 'Green', type: CarType.electric},
{id: 5, name: 'Honda Civic', color: 'Red', type: CarType.gas},
{id: 6, name: 'Ford Mustang', color: 'Yellow', type: CarType.gas},
{id: 7, name: 'Chevrolet Bolt', color: 'Black', type: CarType.electric},
{id: 8, name: 'Audi e-tron', color: 'Gray', type: CarType.electric},
{id: 9, name: 'Mercedes-Benz C-Class', color: 'White', type: CarType.gas},
{id: 10, name: 'Kia Soul', color: 'Orange', type: CarType.gas},
]));
this.parkingLot.set(new Map<number, ParkingLot>([
[1, {type: CarType.electric, car: null}],
[2, {type: CarType.electric, car: null}],
[3, {type: CarType.gas, car: null}],
[4, {type: CarType.gas, car: null}],
[5, {type: CarType.gas, car: null}],
[6, {type: CarType.gas, car: null}],
]))
}
}