Skip to content

Instantly share code, notes, and snippets.

View basket.ts
export class Basket {
//...
private applyDiscount(totalOfSameBookBorrowed: number, price: number): number {
switch (totalOfSameBookBorrowed) {
case 1:
return price
case 2:
return price * (1 - 0.05)
default:
View basket.ts
export class Basket {
//...
private applyDiscount(totalOfSameBookBorrowed: number, price: number): number {
return DiscountCalculatorFactory
.for(totalOfSameBookBorrowed)
.applyDiscountOn(price)
}
}
View discountCalculatorFactory.ts
export class DiscountCalculatorFactory {
static for(numberOfSameBook: number): DiscountCalculator {
switch (numberOfSameBook) {
case 1:
return new NoDiscountCalculator()
case 2:
return new TwoSameBooksDiscountCalculator()
default:
return new ThreeOrMoreSameBooksDiscountCalculator()
}
View discountCalculator.ts
export interface DiscountCalculator {
applyDiscountOn(price: number): number
}
View twoSameTomes.discountCalculator.ts
export class TwoSameTomesDiscountCalculator implements DiscountCalculator {
private FIVE_PERCENT_DISCOUNT = 1 - 0.05
applyDiscountOn(price: number): number {
return price * this.FIVE_PERCENT_DISCOUNT
}
}
View threeOrMoreSameBooks.discountCalculator.ts
export class ThreeOrMoreSameBooksDiscountCalculator implements DiscountCalculator {
private TWENTY_FIVE_PERCENT_DISCOUNT = 1 - 0.25
applyDiscountOn(price: number): number {
return price * this.TWENTY_FIVE_PERCENT_DISCOUNT
}
}
View noDiscountCalculator.ts
export class NoDiscountCalculator implements DiscountCalculator {
applyDiscountOn(price: number): number {
return price
}
}
View Saga.scala
class PhoneNumbersSaga(eventPublisher: EventPublisher) extends EventListener {
private var tracker = new PhoneNumberStateTracker
def handle(phoneNumbers: List[String]): PhoneNumberStateTracker = {
eventPublisher.publish(AllPhoneNumberListed(phoneNumbers))
tracker
}
override def execute(event: Event): Unit = event match {
case apnc: AllPhoneNumbersCounted => tracker = PhoneNumberStateTracker(apnc.total, tracker.totalMatchedPhoneNumbers)
View basketCalculation.test.ts
describe('Unit | Basket price calculation', () => {
let basket: Basket
beforeEach(() => basket = new Basket())
it('For an empty basket', () => {
expect(basket.price).to.equal(0)
})
describe('Borrowing n different books', () => {
View panierCalcule.test.ts
describe('Unit | Caclcule du prix du panier', () => {
let basket: Basket
beforeEach(() => basket = new Basket())
it('Pour un panier vide', () => {
expect(basket.price).to.equal(0)
})
describe('Emprunter n livres différents', () => {