mirror of
https://github.com/Raghu-Ch/es6play.git
synced 2026-02-10 04:43:02 -05:00
Class Basic, Inheritance & Data Service
This commit is contained in:
15
.vscode/launch.json
vendored
Normal file
15
.vscode/launch.json
vendored
Normal file
@@ -0,0 +1,15 @@
|
|||||||
|
{
|
||||||
|
// Use IntelliSense to learn about possible attributes.
|
||||||
|
// Hover to view descriptions of existing attributes.
|
||||||
|
// For more information, visit: https://go.microsoft.com/fwlink/?linkid=830387
|
||||||
|
"version": "0.2.0",
|
||||||
|
"configurations": [
|
||||||
|
{
|
||||||
|
"type": "chrome",
|
||||||
|
"request": "launch",
|
||||||
|
"name": "Launch Chrome against localhost",
|
||||||
|
"url": "http://localhost:8080",
|
||||||
|
"webRoot": "${workspaceFolder}"
|
||||||
|
}
|
||||||
|
]
|
||||||
|
}
|
||||||
@@ -6,9 +6,16 @@
|
|||||||
<meta name="viewport" content="width=device-width, initial-scale=1.0">
|
<meta name="viewport" content="width=device-width, initial-scale=1.0">
|
||||||
<meta http-equiv="X-UA-Compatible" content="ie=edge">
|
<meta http-equiv="X-UA-Compatible" content="ie=edge">
|
||||||
<title>Drones</title>
|
<title>Drones</title>
|
||||||
|
<link rel="stylesheet" href="style.css">
|
||||||
</head>
|
</head>
|
||||||
|
|
||||||
<body>
|
<body>
|
||||||
|
<main>
|
||||||
|
<div class="container">
|
||||||
|
<h2>Object Oriented programming in ES6</h2>
|
||||||
|
<p>It's a great start:)</p>
|
||||||
|
</div>
|
||||||
|
</main>
|
||||||
<script src="node_modules/traceur/bin/traceur.js"></script>
|
<script src="node_modules/traceur/bin/traceur.js"></script>
|
||||||
<script src="node_modules/es6-module-loader/dist/es6-module-loader-dev.js"></script>
|
<script src="node_modules/es6-module-loader/dist/es6-module-loader-dev.js"></script>
|
||||||
<script>
|
<script>
|
||||||
|
|||||||
31
src/app.js
31
src/app.js
@@ -4,9 +4,32 @@ import {
|
|||||||
import {
|
import {
|
||||||
Drone
|
Drone
|
||||||
} from './classes/drone.js';
|
} from './classes/drone.js';
|
||||||
|
import {
|
||||||
|
fleet
|
||||||
|
} from './fleet-data.js';
|
||||||
|
import {
|
||||||
|
FleetDataService
|
||||||
|
} from './services/fleet-data-service.js';
|
||||||
|
|
||||||
let c = new Car();
|
let dataService = new FleetDataService();
|
||||||
let d = new Drone();
|
dataService.loadData(fleet);
|
||||||
|
|
||||||
console.log(c);
|
// for (let car of dataService.cars) {
|
||||||
console.log(d);
|
// console.log(car.license);
|
||||||
|
// }
|
||||||
|
|
||||||
|
// for (let e of dataService.errors) {
|
||||||
|
// console.log(e.message);
|
||||||
|
// }
|
||||||
|
|
||||||
|
// let car = dataService.getCarByLicense('AT9900');
|
||||||
|
|
||||||
|
// let cars = dataService.getCarsSortedByLicense();
|
||||||
|
// for (let car of cars) {
|
||||||
|
// console.log(car.license);
|
||||||
|
// }
|
||||||
|
|
||||||
|
let cars = dataService.filterCarsByMake('b');
|
||||||
|
for (let car of cars) {
|
||||||
|
console.log(car.make);
|
||||||
|
}
|
||||||
@@ -2,4 +2,10 @@ import {
|
|||||||
Vehicle
|
Vehicle
|
||||||
} from './vehicle.js';
|
} from './vehicle.js';
|
||||||
|
|
||||||
export class Car extends Vehicle {}
|
export class Car extends Vehicle {
|
||||||
|
constructor(license, model, latLong) {
|
||||||
|
super(license, model, latLong);
|
||||||
|
this.miles = null;
|
||||||
|
this.make = null;
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -2,4 +2,10 @@ import {
|
|||||||
Vehicle
|
Vehicle
|
||||||
} from './vehicle.js';
|
} from './vehicle.js';
|
||||||
|
|
||||||
export class Drone extends Vehicle {}
|
export class Drone extends Vehicle {
|
||||||
|
constructor(license, model, latLong) {
|
||||||
|
super(license, model, latLong);
|
||||||
|
this.airTimeHours = null;
|
||||||
|
this.base = null;
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -1 +1,7 @@
|
|||||||
export class Vehicle {}
|
export class Vehicle {
|
||||||
|
constructor(license, model, latLong) {
|
||||||
|
this.license = license;
|
||||||
|
this.model = model;
|
||||||
|
this.latLong = latLong;
|
||||||
|
}
|
||||||
|
}
|
||||||
73
src/fleet-data.js
Normal file
73
src/fleet-data.js
Normal file
@@ -0,0 +1,73 @@
|
|||||||
|
export let fleet = [{
|
||||||
|
license: 'ABC123',
|
||||||
|
type: 'drone',
|
||||||
|
model: 'Amazon 1254',
|
||||||
|
airTimeHours: '6050',
|
||||||
|
base: 'New York',
|
||||||
|
latLong: '40.775596 -73.974615'
|
||||||
|
},
|
||||||
|
{
|
||||||
|
license: 'XYZ478',
|
||||||
|
type: 'drone',
|
||||||
|
model: 'Amazon 1554',
|
||||||
|
airTimeHours: '2100',
|
||||||
|
base: 'New York',
|
||||||
|
latLong: '40.771956 -73.978531'
|
||||||
|
},
|
||||||
|
{
|
||||||
|
license: 'PQR823',
|
||||||
|
type: 'drone',
|
||||||
|
model: 'Google 3900',
|
||||||
|
airTimeHours: '600',
|
||||||
|
base: 'New York',
|
||||||
|
latLong: '40.779423 -73.969411'
|
||||||
|
},
|
||||||
|
{
|
||||||
|
license: 'AT9900',
|
||||||
|
type: 'car',
|
||||||
|
make: 'Tesla',
|
||||||
|
model: 'Quick Transport',
|
||||||
|
miles: '15600',
|
||||||
|
latLong: '40.773272 -73.968875'
|
||||||
|
},
|
||||||
|
{
|
||||||
|
license: 'JX2018',
|
||||||
|
type: 'car',
|
||||||
|
make: 'Tata',
|
||||||
|
model: 'Quick Transport',
|
||||||
|
miles: '23600',
|
||||||
|
latLong: '40.733252 -73.928375'
|
||||||
|
},
|
||||||
|
{
|
||||||
|
license: 'FR2000',
|
||||||
|
type: 'car',
|
||||||
|
make: 'Ford',
|
||||||
|
model: 'Sport',
|
||||||
|
miles: '1600',
|
||||||
|
latLong: '40.775272 -73.963475'
|
||||||
|
},
|
||||||
|
{
|
||||||
|
license: 'UB1019',
|
||||||
|
type: 'car',
|
||||||
|
make: 'Uber',
|
||||||
|
model: 'Zip Trip',
|
||||||
|
miles: '3600',
|
||||||
|
latLong: '40.778878 -73.963435'
|
||||||
|
},
|
||||||
|
{
|
||||||
|
license: 'LY1319',
|
||||||
|
type: 'car',
|
||||||
|
make: 'Lyft',
|
||||||
|
model: 'Lyft Pool',
|
||||||
|
miles: '13400',
|
||||||
|
latLong: '40.775872 -73.963575'
|
||||||
|
},
|
||||||
|
{
|
||||||
|
license: 'UB6004',
|
||||||
|
type: 'car',
|
||||||
|
make: 'Uber',
|
||||||
|
model: 'Pick You Up',
|
||||||
|
miles: '600',
|
||||||
|
latLong: '40.774036 -73.967319'
|
||||||
|
}
|
||||||
|
];
|
||||||
6
src/services/data-error.js
Normal file
6
src/services/data-error.js
Normal file
@@ -0,0 +1,6 @@
|
|||||||
|
export class DataError {
|
||||||
|
constructor(message, data) {
|
||||||
|
this.message = message;
|
||||||
|
this.data = data;
|
||||||
|
}
|
||||||
|
}
|
||||||
95
src/services/fleet-data-service.js
Normal file
95
src/services/fleet-data-service.js
Normal file
@@ -0,0 +1,95 @@
|
|||||||
|
import {
|
||||||
|
Car
|
||||||
|
} from '../classes/car.js';
|
||||||
|
import {
|
||||||
|
Vehicle
|
||||||
|
} from '../classes/vehicle.js';
|
||||||
|
import {
|
||||||
|
DataError
|
||||||
|
} from './data-error.js';
|
||||||
|
|
||||||
|
export class FleetDataService {
|
||||||
|
constructor() {
|
||||||
|
this.cars = [];
|
||||||
|
this.drones = [];
|
||||||
|
this.errors = [];
|
||||||
|
}
|
||||||
|
// Get car by license
|
||||||
|
getCarByLicense(license) {
|
||||||
|
return this.cars.find(car => {
|
||||||
|
return car.license === license;
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
// get sorted cars
|
||||||
|
getCarsSortedByLicense() {
|
||||||
|
return this.cars.sort((car1, car2) => {
|
||||||
|
if (car1.license < car2.license) {
|
||||||
|
return -1;
|
||||||
|
}
|
||||||
|
if (car1.license > car2.license) {
|
||||||
|
return 1;
|
||||||
|
}
|
||||||
|
return 0;
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
// Filter car by make
|
||||||
|
filterCarsByMake(filter) {
|
||||||
|
return this.cars.filter(car => car.make.indexOf(filter) >= 0);
|
||||||
|
}
|
||||||
|
|
||||||
|
loadData(fleet) {
|
||||||
|
for (let data of fleet) {
|
||||||
|
switch (data.type) {
|
||||||
|
case 'car':
|
||||||
|
if (this.validateCarData(data)) {
|
||||||
|
let car = this.loadCar(data);
|
||||||
|
if (car)
|
||||||
|
this.cars.push(car);
|
||||||
|
} else {
|
||||||
|
let e = new DataError('invalid car data', data);
|
||||||
|
this.errors.push(e);
|
||||||
|
}
|
||||||
|
break;
|
||||||
|
case 'drone':
|
||||||
|
this.drones.push(data);
|
||||||
|
break;
|
||||||
|
default:
|
||||||
|
let e = new DataError('Invalid vehicle type', data);
|
||||||
|
this.errors.push(e);
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
loadCar(car) {
|
||||||
|
try {
|
||||||
|
let c = new Car(car.license, car.model, car.latLong);
|
||||||
|
c.miles = car.miles;
|
||||||
|
c.make = car.make;
|
||||||
|
return c;
|
||||||
|
} catch (e) {
|
||||||
|
this.errors.push(new DataError('error loadding in car', car));
|
||||||
|
}
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
|
||||||
|
validateCarData(car) {
|
||||||
|
let requiredProps = 'license model latLong miles make'.split(' ');
|
||||||
|
let hasErrors = false;
|
||||||
|
|
||||||
|
for (let field of requiredProps) {
|
||||||
|
if (!car[field]) {
|
||||||
|
this.errors.push(new DataError(`invalid field ${field}`, car));
|
||||||
|
hasErrors = true;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
if (Number.isNaN(Number.parseFloat(car.miles))) {
|
||||||
|
this.errors.push(new DataError('invalid milage', car));
|
||||||
|
hasErrors = true;
|
||||||
|
}
|
||||||
|
|
||||||
|
return !hasErrors;
|
||||||
|
}
|
||||||
|
}
|
||||||
31
style.css
Normal file
31
style.css
Normal file
@@ -0,0 +1,31 @@
|
|||||||
|
* {
|
||||||
|
margin: 0;
|
||||||
|
box-sizing: border-box;
|
||||||
|
}
|
||||||
|
|
||||||
|
body,
|
||||||
|
html {
|
||||||
|
font-family: "rooney-sans", Avenir-Book, Calibri, sans-serif;
|
||||||
|
color: #424242;
|
||||||
|
line-height: 1.4;
|
||||||
|
}
|
||||||
|
|
||||||
|
.container {
|
||||||
|
margin: 2rem auto;
|
||||||
|
width: 80%;
|
||||||
|
text-align: center;
|
||||||
|
box-shadow: 0 3px 10px rgba(0, 0, 0, 0.17);
|
||||||
|
background: #eceff1;
|
||||||
|
}
|
||||||
|
|
||||||
|
h2 {
|
||||||
|
font-family: "rooney-web", 'AmericanTypewriter', Rockwell, serif;
|
||||||
|
font-size: 2.5em;
|
||||||
|
font-weight: bold;
|
||||||
|
padding: 1rem;
|
||||||
|
}
|
||||||
|
|
||||||
|
p {
|
||||||
|
padding: .2rem;
|
||||||
|
font-family: "rooney-web", 'AmericanTypewriter', Rockwell, serif;
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user