Created
February 10, 2016 17:02
-
-
Save juan-m-medina/f326aec9fc58228e92fa to your computer and use it in GitHub Desktop.
Lunch N Learn Typescript Basics
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
enum OrderType { | |
stationery, | |
food, | |
electronics | |
} | |
enum Priority { | |
low, | |
medium, | |
high | |
} | |
interface Item { | |
id : number, | |
description : string | |
} | |
interface Order { | |
id : number, | |
type : OrderType, | |
priority : Priority, | |
items : Item[] | |
} | |
interface Repository<T,U> { | |
Add(entity : U) : number; | |
Get(id : T) : U; | |
GetAll() : U[]; | |
Put(id : T, entity : U) : void; | |
} | |
class StationeryOrder implements Order { | |
static instanceCount : number = 0; | |
id : number; | |
type : OrderType = OrderType.stationery; | |
priority : Priority; | |
items : Item[] = [ | |
{ id: 1, description : "Staple"}, | |
{ id: 2, description : "Pencil"}, | |
{ id: 3, description : "Notepad"} | |
]; | |
constructor(priority : Priority) { | |
StationeryOrder.instanceCount += 1; | |
this.id = StationeryOrder.instanceCount; | |
} | |
} | |
class FoodOrder implements Order { | |
static instanceCount : number = 0; | |
id : number; | |
type : OrderType = OrderType.food; | |
priority : Priority; | |
items : Item[] = [ | |
{ id: 1, description : "Sandwich"}, | |
{ id: 2, description : "Fries"}, | |
{ id: 3, description : "Soda"} | |
]; | |
constructor(priority : Priority) { | |
FoodOrder.instanceCount += 1; | |
this.id = FoodOrder.instanceCount; | |
this.priority = priority; | |
} | |
} | |
class ElectronicsOrder implements Order { | |
static instanceCount : number = 0; | |
id : number; | |
type : OrderType = OrderType.electronics; | |
priority : Priority; | |
items : Item[] = [ | |
{ id: 1, description : "Keyboard"}, | |
{ id: 2, description : "USB drive"}, | |
{ id: 3, description : "Headset"} | |
]; | |
constructor(); | |
constructor(priority? : Priority) { | |
ElectronicsOrder.instanceCount += 1; | |
this.id = ElectronicsOrder.instanceCount; | |
this.priority = priority || Priority.medium ; | |
} | |
} | |
class NumberBaseRepository<U> implements Repository<number, U> { | |
protected items : U[]; | |
Add(item : U) { | |
this.items.push(item); | |
return this.items.length - 1; | |
}; | |
Get(id : number) { | |
return this.items[id]; | |
} | |
GetAll() { | |
return this.items; | |
} | |
Put(id : number, item : U) { | |
this.items[id] = item; | |
} | |
} | |
class DevRepository extends NumberBaseRepository<string> { | |
constructor() { | |
this.items = ["Supriya", "Juan", "Andy", "Matthews"]; | |
super(); | |
} | |
}; | |
class OrderRepository extends NumberBaseRepository<Order> { | |
constructor() { | |
this.items = []; | |
super(); | |
} | |
} | |
var devRepositoryInstance = new DevRepository(); | |
devRepositoryInstance.Add("Joanne"); | |
devRepositoryInstance.Put(1, "Juan, the Awesome One!") | |
var orderRepositoryInstance = new OrderRepository(); | |
orderRepositoryInstance.Add(new StationeryOrder(Priority.low)); | |
orderRepositoryInstance.Add(new StationeryOrder(Priority.high)); | |
orderRepositoryInstance.Add(new FoodOrder(Priority.medium)); | |
orderRepositoryInstance.Add(new ElectronicsOrder()); | |
var orderOutput = JSON.stringify(orderRepositoryInstance, null, ' '); | |
var devUIList = document.createElement("UL"); | |
for (var dev of devRepositoryInstance.GetAll()) { | |
var devUIItem = document.createElement("LI"); | |
var devTextNode = document.createTextNode(dev); | |
devUIItem.appendChild(devTextNode); | |
devUIList.appendChild(devUIItem); | |
} | |
document.body.appendChild(devUIList); | |
var orderDump = document.createElement("DIV"); | |
orderDump.innerText = orderOutput; | |
document.body.appendChild(orderDump); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment