mirror of
https://github.com/Raghu-Ch/angular-challenges.git
synced 2026-02-12 13:53:03 -05:00
feat(challenge15): function overload
This commit is contained in:
14
apps/overload/src/app/app.component.ts
Normal file
14
apps/overload/src/app/app.component.ts
Normal file
@@ -0,0 +1,14 @@
|
||||
import { Component } from '@angular/core';
|
||||
import { createVehicle } from './teacher.utils';
|
||||
|
||||
@Component({
|
||||
standalone: true,
|
||||
selector: 'app-root',
|
||||
template: ``,
|
||||
})
|
||||
export class AppComponent {
|
||||
car = createVehicle('car', 'diesel');
|
||||
bus = createVehicle('bus', undefined, 20);
|
||||
boat = createVehicle('boat', undefined, 300, true);
|
||||
bicycle = createVehicle('bicycle');
|
||||
}
|
||||
55
apps/overload/src/app/teacher.utils.ts
Normal file
55
apps/overload/src/app/teacher.utils.ts
Normal file
@@ -0,0 +1,55 @@
|
||||
type VehicleType = 'bus' | 'car' | 'moto' | 'bicycle' | 'boat';
|
||||
type Fuel = 'diesel' | 'petrol' | 'electric';
|
||||
|
||||
interface Bicycle {
|
||||
type: 'bicycle';
|
||||
}
|
||||
|
||||
interface Car {
|
||||
fuel: Fuel;
|
||||
type: 'car';
|
||||
}
|
||||
|
||||
interface Moto {
|
||||
fuel: Fuel;
|
||||
type: 'moto';
|
||||
}
|
||||
|
||||
interface Bus {
|
||||
capacity: number;
|
||||
isPublicTransport: boolean;
|
||||
type: 'bus';
|
||||
}
|
||||
|
||||
interface Boat {
|
||||
capacity: number;
|
||||
type: 'boat';
|
||||
}
|
||||
|
||||
type Vehicle = Bicycle | Car | Moto | Bus | Boat;
|
||||
|
||||
export function createVehicle(
|
||||
type: VehicleType,
|
||||
fuel?: Fuel,
|
||||
capacity?: number,
|
||||
isPublicTransport?: boolean
|
||||
): Vehicle {
|
||||
switch (type) {
|
||||
case 'bicycle':
|
||||
return { type };
|
||||
case 'car':
|
||||
case 'moto':
|
||||
if (!fuel) throw new Error(`fuel property is missing for type ${type}`);
|
||||
return { fuel, type };
|
||||
case 'boat':
|
||||
if (!capacity)
|
||||
throw new Error(`capacity property is missing for type boat`);
|
||||
return { capacity, type };
|
||||
case 'bus':
|
||||
if (!capacity)
|
||||
throw new Error(`capacity property is missing for type bus`);
|
||||
if (!isPublicTransport)
|
||||
throw new Error(`isPublicTransport property is missing for type bus`);
|
||||
return { capacity, isPublicTransport, type };
|
||||
}
|
||||
}
|
||||
0
apps/overload/src/assets/.gitkeep
Normal file
0
apps/overload/src/assets/.gitkeep
Normal file
BIN
apps/overload/src/favicon.ico
Normal file
BIN
apps/overload/src/favicon.ico
Normal file
Binary file not shown.
|
After Width: | Height: | Size: 15 KiB |
13
apps/overload/src/index.html
Normal file
13
apps/overload/src/index.html
Normal file
@@ -0,0 +1,13 @@
|
||||
<!DOCTYPE html>
|
||||
<html lang="en">
|
||||
<head>
|
||||
<meta charset="utf-8" />
|
||||
<title>Overload</title>
|
||||
<base href="/" />
|
||||
<meta name="viewport" content="width=device-width, initial-scale=1" />
|
||||
<link rel="icon" type="image/x-icon" href="favicon.ico" />
|
||||
</head>
|
||||
<body>
|
||||
<app-root></app-root>
|
||||
</body>
|
||||
</html>
|
||||
4
apps/overload/src/main.ts
Normal file
4
apps/overload/src/main.ts
Normal file
@@ -0,0 +1,4 @@
|
||||
import { bootstrapApplication } from '@angular/platform-browser';
|
||||
import { AppComponent } from './app/app.component';
|
||||
|
||||
bootstrapApplication(AppComponent).catch((err) => console.error(err));
|
||||
1
apps/overload/src/styles.scss
Normal file
1
apps/overload/src/styles.scss
Normal file
@@ -0,0 +1 @@
|
||||
/* You can add global styles to this file, and also import other style files */
|
||||
Reference in New Issue
Block a user