forked from DanWahlin/TypeScriptDemos
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathinterfaces.ts
More file actions
137 lines (113 loc) · 3.79 KB
/
interfaces.ts
File metadata and controls
137 lines (113 loc) · 3.79 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
128
129
130
131
132
133
134
135
136
137
namespace Interfaces {
export interface IEngine {
start(callback: (startStatus: boolean, engineType: string) => void): void;
stop(callback: (stopStatus: boolean, engineType: string) => void): void;
}
export interface IAutoOptions {
basePrice: number;
engine: IEngine;
state: string;
make: string;
model: string;
year: number;
}
export interface ITruckOptions extends IAutoOptions {
bedLength: string;
fourByFour: boolean;
}
export class Engine implements IEngine {
constructor(public horsePower: number, public engineType: string) { }
start(callback: (startStatus: boolean, engineType: string) => void) {
window.setTimeout(() => {
callback(true, this.engineType);
}, 1000);
}
stop(callback: (stopStatus: boolean, engineType: string) => void) {
window.setTimeout(() => {
callback(true, this.engineType);
}, 1000);
}
}
export class CustomEngine implements IEngine {
start(callback: (startStatus: boolean, engineType: string) => void) {
window.setTimeout(() => {
callback(true, 'Custom Engine');
}, 1000);
}
stop(callback: (stopStatus: boolean, engineType: string) => void) {
window.setTimeout(() => {
callback(true, 'Custom Engine');
}, 1000);
}
}
export class Accessory {
constructor(public accessoryNumber: number, public title: string) { }
}
export class Auto {
private _basePrice: number;
private _engine: IEngine;
state: string;
make: string;
model: string;
year: number;
accessoryList: string;
constructor(options: IAutoOptions) {
this.engine = options.engine;
this.basePrice = options.basePrice;
this.state = options.state;
this.make = options.make;
this.model = options.model;
this.year = options.year;
}
calculateTotal(): number {
var taxRate = .08;
return this.basePrice + (taxRate * this.basePrice);
}
addAccessories(...accessories: Accessory[]) {
this.accessoryList = '';
for (var i = 0; i < accessories.length; i++) {
var ac = accessories[i];
this.accessoryList += ac.accessoryNumber + ' ' + ac.title + '<br />';
}
}
getAccessoryList(): string {
return this.accessoryList;
}
get basePrice(): number {
return this._basePrice;
}
set basePrice(value: number) {
if (value <= 0) throw 'price must be >= 0';
this._basePrice = value;
}
get engine(): IEngine {
return this._engine;
}
set engine(value: IEngine) {
if (value == undefined) throw 'Please supply an engine.';
this._engine = value;
}
}
export class Truck extends Auto {
bedLength: string;
fourByFour: boolean;
constructor(options: ITruckOptions) {
super(options);
this.bedLength = options.bedLength;
this.fourByFour = options.fourByFour;
}
}
}
window.onload = function () {
var truck = new Interfaces.Truck({
engine: new Interfaces.Engine(250, 'V6'),
basePrice: 45000,
state: 'Arizona',
make: 'Ford',
model: 'F-150',
year: 2013,
bedLength: 'Short bed',
fourByFour: true
});
alert(truck.bedLength);
};