Skip to content

Commit

Permalink
make typescript more idiomatic by using public readonly fields
Browse files Browse the repository at this point in the history
instead of java-like handrolled getters

fix standalone compilation
  • Loading branch information
martinsson committed Jan 21, 2019
1 parent 302cd03 commit ad9fa05
Show file tree
Hide file tree
Showing 12 changed files with 42 additions and 96 deletions.
4 changes: 2 additions & 2 deletions typescript/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,13 @@
"main": "index.js",
"scripts": {
"test": "mocha --require ts-node/register --recursive 'test/**/*.test.ts'",
"watch:test": "npm run test -- --watch-extensions ts --watch"
"watch:test": "npm run test -- --watch-extensions ts --watch",
"compile": "tsc"
},
"author": "Johan Martinsson",
"license": "ISC",
"devDependencies": {
"@types/chai": "^4.1.7",
"@types/jest": "^23.3.11",
"@types/mocha": "^5.2.5",
"approvals": "^3.0.2",
"chai": "^4.2.0",
Expand Down
20 changes: 10 additions & 10 deletions typescript/src/ReceiptPrinter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,23 +10,23 @@ export class ReceiptPrinter {
public printReceipt( receipt: Receipt): string {
let result = "";
for (const item of receipt.getItems()) {
let price = this.format2Decimals(item.getTotalPrice());
let price = this.format2Decimals(item.totalPrice);
let quantity = ReceiptPrinter.presentQuantity(item);
let name = item.getProduct().getName();
let unitPrice = this.format2Decimals(item.getPrice());
let name = item.product.name;
let unitPrice = this.format2Decimals(item.price);

let whitespaceSize = this.columns - name.length - price.length;
let line = name + ReceiptPrinter.getWhitespace(whitespaceSize) + price + "\n";

if (item.getQuantity() != 1) {
if (item.quantity != 1) {
line += " " + unitPrice + " * " + quantity + "\n";
}
result += line;
}
for (const discount of receipt.getDiscounts()) {
let productPresentation = discount.getProduct().getName();
let pricePresentation = this.format2Decimals(discount.getDiscountAmount());
let description = discount.getDescription();
let productPresentation = discount.product.name;
let pricePresentation = this.format2Decimals(discount.discountAmount);
let description = discount.description;
result += description;
result += "(";
result += productPresentation;
Expand Down Expand Up @@ -55,10 +55,10 @@ export class ReceiptPrinter {
}

private static presentQuantity( item: ReceiptItem): string {
return ProductUnit.Each == item.getProduct().getUnit()
return ProductUnit.Each == item.product.unit
// TODO make sure this is the simplest way to make something similar to the java version
? new Intl.NumberFormat('en-UK', {maximumFractionDigits: 0}).format(item.getQuantity())
: new Intl.NumberFormat('en-UK', {minimumFractionDigits: 3}).format(item.getQuantity());
? new Intl.NumberFormat('en-UK', {maximumFractionDigits: 0}).format(item.quantity)
: new Intl.NumberFormat('en-UK', {minimumFractionDigits: 3}).format(item.quantity);
}

private static getWhitespace(whitespaceSize: number): string {
Expand Down
19 changes: 3 additions & 16 deletions typescript/src/model/Discount.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,21 +2,8 @@ import {Product} from "./Product"

export class Discount {

constructor(private readonly product: Product,
private readonly description: string,
private readonly discountAmount: number) {
constructor(public readonly product: Product,
public readonly description: string,
public readonly discountAmount: number) {
}

getDescription(): string {
return this.description;
}

getDiscountAmount(): number {
return this.discountAmount;
}

getProduct(): Product {
return this.product;
}

}
6 changes: 3 additions & 3 deletions typescript/src/model/Offer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,9 @@ import {SpecialOfferType} from "./SpecialOfferType"

export class Offer {

public constructor(public offerType: SpecialOfferType,
private readonly product: Product ,
public argument: number) {
public constructor(public readonly offerType: SpecialOfferType,
public readonly product: Product,
public readonly argument: number) {
}

getProduct(): Product {
Expand Down
16 changes: 2 additions & 14 deletions typescript/src/model/Product.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,19 +2,7 @@ import {ProductUnit} from "./ProductUnit"

export class Product {

constructor(private readonly name: string,
private readonly unit: ProductUnit) {
constructor(public readonly name: string,
public readonly unit: ProductUnit) {
}

public getName(): string {
return this.name;
}


public getUnit(): ProductUnit {
return this.unit;
}



}
17 changes: 3 additions & 14 deletions typescript/src/model/ProductQuantity.ts
Original file line number Diff line number Diff line change
@@ -1,21 +1,10 @@
import {Product} from "./Product"

export class ProductQuantity {
private readonly product: Product;
private readonly quantity: number;

constructor(product: Product,
weight: number) {
constructor(public readonly product: Product,
public readonly quantity: number) {
this.product = product;
this.quantity = weight;
this.quantity = quantity;
}

public getProduct(): Product {
return this.product;
}

public getQuantity(): number {
return this.quantity;
}

}
4 changes: 2 additions & 2 deletions typescript/src/model/Receipt.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,10 @@ export class Receipt {
public getTotalPrice(): number {
let total = 0.0;
for (let item of this.items) {
total += item.getTotalPrice();
total += item.totalPrice;
}
for ( let discount of this.discounts) {
total -= discount.getDiscountAmount();
total -= discount.discountAmount;
}
return total;
}
Expand Down
26 changes: 4 additions & 22 deletions typescript/src/model/ReceiptItem.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,27 +2,9 @@ import {Product} from "./Product"

export class ReceiptItem {

public constructor(private readonly product: Product,
private readonly quantity: number,
private readonly price: number,
private totalPrice: number) {
public constructor(public readonly product: Product,
public readonly quantity: number,
public readonly price: number,
public totalPrice: number) {
}

public getPrice(): number {
return this.price;
}

public getProduct(): Product {
return this.product;
}

public getQuantity(): number {
return this.quantity;
}

public getTotalPrice(): number {
return this.totalPrice;
}


}
12 changes: 6 additions & 6 deletions typescript/src/model/ShoppingCart.ts
Original file line number Diff line number Diff line change
Expand Up @@ -32,24 +32,24 @@ export class ShoppingCart {
public addItemQuantity(product: Product, quantity: number): void {
let productQuantity = new ProductQuantity(product, quantity)
this.items.push(productQuantity);
let currentQuantity = this._productQuantities[product.getName()]
let currentQuantity = this._productQuantities[product.name]
if (currentQuantity) {
this._productQuantities[product.getName()] = this.increaseQuantity(product, currentQuantity, quantity);
this._productQuantities[product.name] = this.increaseQuantity(product, currentQuantity, quantity);
} else {
this._productQuantities[product.getName()] = productQuantity;
this._productQuantities[product.name] = productQuantity;
}

}

private increaseQuantity(product: Product, productQuantity: ProductQuantity, quantity: number) {
return new ProductQuantity(product, productQuantity.getQuantity() + quantity)
return new ProductQuantity(product, productQuantity.quantity + quantity)
}

handleOffers(receipt: Receipt, offers: OffersByProduct, catalog: SupermarketCatalog ):void {
for (const productName in this.productQuantities()) {
const productQuantity = this._productQuantities[productName]
const product = productQuantity.getProduct();
const quantity: number = this._productQuantities[productName].getQuantity();
const product = productQuantity.product;
const quantity: number = this._productQuantities[productName].quantity;
if (offers[productName]) {
const offer : Offer = offers[productName];
const unitPrice: number= catalog.getUnitPrice(product);
Expand Down
6 changes: 3 additions & 3 deletions typescript/src/model/Teller.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,15 +13,15 @@ export class Teller {
}

public addSpecialOffer(offerType: SpecialOfferType , product: Product, argument: number): void {
this.offers[product.getName()] = new Offer(offerType, product, argument);
this.offers[product.name] = new Offer(offerType, product, argument);
}

public checksOutArticlesFrom(theCart: ShoppingCart): Receipt {
const receipt = new Receipt();
const productQuantities = theCart.getItems();
for (let pq of productQuantities) {
let p = pq.getProduct();
let quantity = pq.getQuantity();
let p = pq.product;
let quantity = pq.quantity;
let unitPrice = this.catalog.getUnitPrice(p);
let price = quantity * unitPrice;
receipt.addProduct(p, quantity, unitPrice, price);
Expand Down
6 changes: 3 additions & 3 deletions typescript/test/FakeCatalog.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,11 @@ export class FakeCatalog implements SupermarketCatalog {
private prices: {[key: string]: number} = {};

public addProduct(product: Product, price: number): void {
this.products[product.getName()] = product;
this.prices[product.getName()] = price;
this.products[product.name] = product;
this.prices[product.name] = price;
}

public getUnitPrice(p: Product): number {
return this.prices[p.getName()];
return this.prices[p.name];
}
}
2 changes: 1 addition & 1 deletion typescript/test/Supermarket.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ type Approvals = {
describe('Supermarket', function () {

approvals.mocha()
it('TODO decide what to specify', function (this: Approvals) {
it('TODO decide what to specify', function (this: any) {

const catalog: SupermarketCatalog = new FakeCatalog();
const toothbrush: Product = new Product("toothbrush", ProductUnit.Each);
Expand Down

0 comments on commit ad9fa05

Please sign in to comment.