From 177ca4721fcb161f8d49e6c50b3ea6cdc3aaf147 Mon Sep 17 00:00:00 2001 From: Peter Kofler Date: Thu, 7 Dec 2023 16:13:37 +0100 Subject: [PATCH] Format code C. --- TypeScript/app/c/Assignment.md | 2 ++ TypeScript/app/c/checkout.ts | 22 +++++++++++++--------- TypeScript/app/c/money.ts | 15 +++------------ TypeScript/app/c/receipt-repository.ts | 18 ++++++++---------- TypeScript/app/c/receipt.ts | 20 ++++++++++++++++---- TypeScript/test/c/checkout.test.ts | 14 ++++++++++++++ TypeScript/test/c/discount.test.ts | 16 ---------------- 7 files changed, 56 insertions(+), 51 deletions(-) create mode 100644 TypeScript/test/c/checkout.test.ts delete mode 100644 TypeScript/test/c/discount.test.ts diff --git a/TypeScript/app/c/Assignment.md b/TypeScript/app/c/Assignment.md index 53534c7..a3ea421 100644 --- a/TypeScript/app/c/Assignment.md +++ b/TypeScript/app/c/Assignment.md @@ -31,3 +31,5 @@ for a purchase in our online shop. The main logic is in `Checkout`. * Bring `Checkout` under test. Make sure to cover all paths in the core logic. * There is an existing `CheckoutTest` with a first test case which might or might not work. * You cannot change `ReceiptRepository` because it is used by other teams as well. + +(This is a copied document - do not edit!) diff --git a/TypeScript/app/c/checkout.ts b/TypeScript/app/c/checkout.ts index f59d430..2851e8d 100644 --- a/TypeScript/app/c/checkout.ts +++ b/TypeScript/app/c/checkout.ts @@ -1,15 +1,19 @@ -import {Money} from "./money"; -import {Receipt} from "./receipt"; -import {ReceiptRepository} from "./receipt-repository"; +import { Money } from "./money"; +import { Receipt } from "./receipt"; +import { ReceiptRepository } from "./receipt-repository"; export class Checkout { - createReceipt(netAmount: Money): Promise { - let tax = netAmount.percentage(20); - let total = netAmount.add(tax); - let receipt: Receipt = new Receipt(netAmount, tax, total); + createReceipt(amount: Money): Promise { + const receipt: Receipt = new Receipt(); + const vat = amount.percentage(20); - return ReceiptRepository.store(receipt); + receipt.amount = amount; + receipt.tax = vat; + receipt.total = amount.add(vat); + + return ReceiptRepository.store(receipt). + then(_ => receipt); } -} \ No newline at end of file +} diff --git a/TypeScript/app/c/money.ts b/TypeScript/app/c/money.ts index 861ed4e..3de37bf 100644 --- a/TypeScript/app/c/money.ts +++ b/TypeScript/app/c/money.ts @@ -1,9 +1,5 @@ export class Money { - public static readonly ZERO: Money = new Money(0); - public static readonly ONE_THOUSAND: Money = new Money(1000); - public static readonly ONE_HUNDRED: Money = new Money(100); - value: number; constructor(value: number | string) { @@ -14,21 +10,16 @@ export class Money { } } - add(other: Money): Money { + add(other: Money): Money { return new Money(this.value + other.value); } - percentage(p: number): Money { + percentage(p: number): Money { return new Money((this.value * p / 100).toFixed(2)); } - moreThan(other: Money): boolean { - return this.value > other.value; - } - - format(): string { + format(): string { return this.value.toFixed(2); } - } diff --git a/TypeScript/app/c/receipt-repository.ts b/TypeScript/app/c/receipt-repository.ts index 6fa1386..fcb2928 100644 --- a/TypeScript/app/c/receipt-repository.ts +++ b/TypeScript/app/c/receipt-repository.ts @@ -1,28 +1,28 @@ -import {Receipt} from "./receipt"; -import {Connection, MysqlError} from "mysql"; +import { Receipt } from "./receipt"; +import { Connection, MysqlError } from "mysql"; -let mysql = require('mysql'); +const mysql = require('mysql'); const db: Connection = mysql.createConnection({ host: 'localhost:3306', + database: 'myShop', user: 'store', - password: '12345', - database: 'store' + password: '12345' }); export class ReceiptRepository { - static store(receipt: Receipt): Promise { + static store(receipt: Receipt): Promise { return new Promise((resolve, reject) => { db.connect((err: MysqlError) => { if (err) { reject(err); } else { - db.query( "insert into RECEIPT (AMOUNT, TAX, TOTAL) values(?, ?, ?)", [receipt.amount.value, receipt.tax.value, receipt.total.value], (err, results) => { + db.end(); if (err) { reject(err); } @@ -31,10 +31,8 @@ export class ReceiptRepository { } resolve(true); }); - - db.end(); } }); }); } -} \ No newline at end of file +} diff --git a/TypeScript/app/c/receipt.ts b/TypeScript/app/c/receipt.ts index de5de9e..499c692 100644 --- a/TypeScript/app/c/receipt.ts +++ b/TypeScript/app/c/receipt.ts @@ -1,8 +1,20 @@ -import {Money} from "./money"; +import { Money } from "./money"; export class Receipt { - constructor(public amount: Money, public tax: Money, public total:Money) { - this.amount = Money.ZERO; - this.tax = Money.ZERO; + + amount: Money; + tax: Money; + total: Money + + format(): string[] { + return [ // + "Receipt", // + "=======", // + "Item 1 ... " + this.amount.format(), // + "Tax ... " + this.tax.format(), // + "----------------", // + "Total ... " + this.total.format() // + ]; } + } diff --git a/TypeScript/test/c/checkout.test.ts b/TypeScript/test/c/checkout.test.ts new file mode 100644 index 0000000..fb526dc --- /dev/null +++ b/TypeScript/test/c/checkout.test.ts @@ -0,0 +1,14 @@ +import { Checkout } from "../../app/c/checkout"; +import { Money } from "../../app/c/money"; +import { expect } from "chai"; + +describe('Checkout', () => { + + it('test 3', async () => { + const checkout = new Checkout(); + + await checkout.createReceipt(new Money(12.00)) + .then(receipt => expect(checkout).not.to.be.null); + }); + +}); diff --git a/TypeScript/test/c/discount.test.ts b/TypeScript/test/c/discount.test.ts deleted file mode 100644 index 9606b81..0000000 --- a/TypeScript/test/c/discount.test.ts +++ /dev/null @@ -1,16 +0,0 @@ -import {Checkout} from "../../app/c/checkout"; -import {Money} from "../../app/c/money"; - - -describe('Checkout Test', () => { - - it('test in c', async () => { - let checkout = new Checkout(); - - await checkout.createReceipt(new Money(50.20)) - .then( - val => console.log(val) - ); - }); - -}); \ No newline at end of file