Skip to content

Instantly share code, notes, and snippets.

@chrismckelt
Last active August 29, 2015 14:05
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save chrismckelt/a29224804af1a1ae30d8 to your computer and use it in GitHub Desktop.
Save chrismckelt/a29224804af1a1ae30d8 to your computer and use it in GitHub Desktop.
Code Dojo - TypeScript - Simple checkout with alternate discounts
/* tslint:disable */
var files = {
jquery: "./app/scripts/jquery-1.10.2.js",
angular: "./app/scripts/angular.js",
angularAnimate: "./app/scripts/angular-animate.js",
angularSanitize: "./app/scripts/angular/angular-sanitize",
angularUIRouter: "./app/scripts/angular-ui/angular-ui-router",
angularMocks: "./app/scripts/angular/angular-mocks",
underscore: "./app/scripts/underscore",
app: "./app/scripts/",
};
module store {
export class global {
public static id: number = 0;
}
export enum ProductType {
apples = 0,
cherries = 1,
mangos = 2
}
export class Product {
id : number;
productType: ProductType;
cost: number;
isProcessed:boolean
}
export function createProduct(pt: store.ProductType, cost: number) {
var prod = new Product();
prod.id = store.global.Id;
prod.productType = pt;
prod.cost = cost;
prod.isProcessed = false;
return prod;
}
export class Order {
products: Array<Product>
applyDiscounts : Function[]
}
export class Checkout {
private orderTotal: number = 0;
private pop = () => this.cherryPop();
private calc = () => this.calculateBase();
private onefree = () => this.applesbuy2Get3rdFree();
constructor(public order: Order) {
order.applyDiscounts = [
this.pop,
this.calc,
this.onefree //todo cycle thru passed in discounts & apply
];
}
public processOrder() {
this.orderTotal += this.onefree(); // just run through all for exerise
this.orderTotal += this.pop();
this.orderTotal += this.calc();
return this.orderTotal;
}
private applesbuy2Get3rdFree() {
var total = 0;
var count: number = 0;
for (var x = 0; x < this.order.products.length; x++) {
if (this.order.products[x].isProcessed) continue;
if (this.order.products[x].productType == store.ProductType.apples) {
if (count != 2) {
this.orderTotal += this.order.products[x].cost;
count++;
} else {
count = 0;
}
this.order.products[x].isProcessed = true;
}
}
return total;
}
private cherryPop() {
var total = 0;
var count: number = 0;
for (var x = 0; x < this.order.products.length; x++) {
if (!this.order.products[x].isProcessed) {
if (this.order.products[x].productType == store.ProductType.cherries) {
total += this.order.products[x].cost;
this.order.products[x].isProcessed = true;
count++;
if (count === 3) {
count = 0;
total = (total - 7.50);
}
}
}
}
return total;
}
private calculateBase() {
var total: number = 0;
for (var i = 0; i < this.order.products.length; i++) {
if (this.order.products[i].isProcessed) continue;
if (this.order.products[i].productType !== store.ProductType.apples) {
total += this.order.products[i].cost;
this.order.products[i].isProcessed = true;
}
}
return total;
}
}
}
describe("1 of each item ", () => {
jasmine.HtmlReporter = true;
it("should calculate order total", () => {
var products = new Array<store.Product>();
products.push(
store.createProduct(store.ProductType.apples, .5),
store.createProduct(store.ProductType.cherries, 5),
store.createProduct(store.ProductType.mangos, 3)
);
var order = new store.Order();
order.products = products;
console.log(order);
var checkout = new store.Checkout(order);
expect(8).toEqual(checkout.processOrder());
});
describe("3 cherries for 7 ", () => {
jasmine.HtmlReporter = true;
it("should calculate order total", () => {
var products = new Array<store.Product>();
products.push(
store.createProduct(store.ProductType.cherries, 5),
store.createProduct(store.ProductType.cherries, 5),
store.createProduct(store.ProductType.cherries, 5)
);
var order = new store.Order();
order.products = products;
console.log(order);
var checkout = new store.Checkout(order);
expect(7).toEqual(checkout.processOrder());
});
});
})
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment