Skip to content

Commit

Permalink
Format code C.
Browse files Browse the repository at this point in the history
  • Loading branch information
codecop committed Dec 7, 2023
1 parent 49d019f commit 177ca47
Show file tree
Hide file tree
Showing 7 changed files with 56 additions and 51 deletions.
2 changes: 2 additions & 0 deletions TypeScript/app/c/Assignment.md
Original file line number Diff line number Diff line change
Expand Up @@ -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!)
22 changes: 13 additions & 9 deletions TypeScript/app/c/checkout.ts
Original file line number Diff line number Diff line change
@@ -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<Error | boolean> {
let tax = netAmount.percentage(20);
let total = netAmount.add(tax);
let receipt: Receipt = new Receipt(netAmount, tax, total);
createReceipt(amount: Money): Promise<Receipt | Error> {
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);
}
}
}
15 changes: 3 additions & 12 deletions TypeScript/app/c/money.ts
Original file line number Diff line number Diff line change
@@ -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) {
Expand All @@ -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);
}


}
18 changes: 8 additions & 10 deletions TypeScript/app/c/receipt-repository.ts
Original file line number Diff line number Diff line change
@@ -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<Error | boolean> {
static store(receipt: Receipt): Promise<boolean | Error> {
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);
}
Expand All @@ -31,10 +31,8 @@ export class ReceiptRepository {
}
resolve(true);
});

db.end();
}
});
});
}
}
}
20 changes: 16 additions & 4 deletions TypeScript/app/c/receipt.ts
Original file line number Diff line number Diff line change
@@ -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() //
];
}

}
14 changes: 14 additions & 0 deletions TypeScript/test/c/checkout.test.ts
Original file line number Diff line number Diff line change
@@ -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);
});

});
16 changes: 0 additions & 16 deletions TypeScript/test/c/discount.test.ts

This file was deleted.

0 comments on commit 177ca47

Please sign in to comment.