Compare commits
2 Commits
agentd/tas
...
agentd/tas
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
5d83ffaa76 | ||
|
|
605a952f7a |
@@ -16,6 +16,57 @@ public static class PriceCalculator
|
||||
throw new ArgumentOutOfRangeException(nameof(discountPercent), "A discount is between 0 and 100 percent.");
|
||||
}
|
||||
|
||||
return price - discountPercent;
|
||||
return price * (1m - discountPercent / 100m);
|
||||
}
|
||||
|
||||
/// <summary>Rounds an amount to the nearest 0.05, rounding halves away from zero.</summary>
|
||||
public static decimal RoundToFiveRappen(decimal amount)
|
||||
{
|
||||
return Math.Round(amount * 20m, MidpointRounding.AwayFromZero) / 20m;
|
||||
}
|
||||
|
||||
/// <summary>Returns the quantity discount percent for a quantity: 0, 5 or 10.</summary>
|
||||
public static int GetQuantityDiscountPercent(int quantity)
|
||||
{
|
||||
if (quantity < 1)
|
||||
{
|
||||
throw new ArgumentOutOfRangeException(nameof(quantity), "A quantity must be at least 1.");
|
||||
}
|
||||
|
||||
return quantity switch
|
||||
{
|
||||
>= 50 => 10,
|
||||
>= 10 => 5,
|
||||
_ => 0,
|
||||
};
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Calculates the total: percentage discount on the unit price first, then the quantity
|
||||
/// discount on the total, then rounds the end amount to the nearest 0.05.
|
||||
/// </summary>
|
||||
public static decimal CalculateTotal(decimal unitPrice, int quantity, decimal discountPercent)
|
||||
{
|
||||
if (unitPrice < 0)
|
||||
{
|
||||
throw new ArgumentOutOfRangeException(nameof(unitPrice), "A price cannot be negative.");
|
||||
}
|
||||
|
||||
if (quantity < 1)
|
||||
{
|
||||
throw new ArgumentOutOfRangeException(nameof(quantity), "A quantity must be at least 1.");
|
||||
}
|
||||
|
||||
if (discountPercent is < 0 or > 100)
|
||||
{
|
||||
throw new ArgumentOutOfRangeException(nameof(discountPercent), "A discount is between 0 and 100 percent.");
|
||||
}
|
||||
|
||||
var discountedUnitPrice = ApplyDiscount(unitPrice, discountPercent);
|
||||
var totalBeforeQuantityDiscount = discountedUnitPrice * quantity;
|
||||
var quantityDiscountPercent = GetQuantityDiscountPercent(quantity);
|
||||
var totalBeforeRounding = totalBeforeQuantityDiscount * (1m - quantityDiscountPercent / 100m);
|
||||
|
||||
return RoundToFiveRappen(totalBeforeRounding);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -33,4 +33,94 @@ public sealed class PriceCalculatorTests
|
||||
{
|
||||
Assert.Throws<ArgumentOutOfRangeException>(() => PriceCalculator.ApplyDiscount(100m, 101m));
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void Quantity_9_gets_no_quantity_discount()
|
||||
{
|
||||
Assert.Equal(0, PriceCalculator.GetQuantityDiscountPercent(9));
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void Quantity_10_gets_five_percent_quantity_discount()
|
||||
{
|
||||
Assert.Equal(5, PriceCalculator.GetQuantityDiscountPercent(10));
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void Quantity_49_gets_five_percent_quantity_discount()
|
||||
{
|
||||
Assert.Equal(5, PriceCalculator.GetQuantityDiscountPercent(49));
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void Quantity_50_gets_ten_percent_quantity_discount()
|
||||
{
|
||||
Assert.Equal(10, PriceCalculator.GetQuantityDiscountPercent(50));
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void Quantity_below_one_is_refused()
|
||||
{
|
||||
Assert.Throws<ArgumentOutOfRangeException>(() => PriceCalculator.GetQuantityDiscountPercent(0));
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void Total_applies_five_percent_quantity_discount()
|
||||
{
|
||||
Assert.Equal(950m, PriceCalculator.CalculateTotal(100m, 10, 0m));
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void Total_applies_percentage_discount_before_quantity_discount()
|
||||
{
|
||||
Assert.Equal(712.5m, PriceCalculator.CalculateTotal(100m, 10, 25m));
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void Total_applies_ten_percent_quantity_discount_for_fifty_pieces()
|
||||
{
|
||||
Assert.Equal(4500m, PriceCalculator.CalculateTotal(100m, 50, 0m));
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void Rounding_rounds_up_to_the_nearest_five_rappen()
|
||||
{
|
||||
Assert.Equal(1.25m, PriceCalculator.RoundToFiveRappen(1.23m));
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void Rounding_rounds_down_to_the_nearest_five_rappen()
|
||||
{
|
||||
Assert.Equal(1.20m, PriceCalculator.RoundToFiveRappen(1.21m));
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void Rounding_rounds_midpoints_away_from_zero()
|
||||
{
|
||||
Assert.Equal(1.15m, PriceCalculator.RoundToFiveRappen(1.125m));
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void Total_rounds_after_the_quantity_discount()
|
||||
{
|
||||
Assert.Equal(15.85m, PriceCalculator.CalculateTotal(1.67m, 10, 0m));
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void Total_refuses_a_negative_unit_price()
|
||||
{
|
||||
Assert.Throws<ArgumentOutOfRangeException>(() => PriceCalculator.CalculateTotal(-1m, 1, 0m));
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void Total_refuses_a_quantity_below_one()
|
||||
{
|
||||
Assert.Throws<ArgumentOutOfRangeException>(() => PriceCalculator.CalculateTotal(10m, 0, 0m));
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void Total_refuses_a_discount_above_100_percent()
|
||||
{
|
||||
Assert.Throws<ArgumentOutOfRangeException>(() => PriceCalculator.CalculateTotal(10m, 1, 101m));
|
||||
}
|
||||
}
|
||||
|
||||
17
web/.editorconfig
Normal file
17
web/.editorconfig
Normal file
@@ -0,0 +1,17 @@
|
||||
# Editor configuration, see https://editorconfig.org
|
||||
root = true
|
||||
|
||||
[*]
|
||||
charset = utf-8
|
||||
indent_style = space
|
||||
indent_size = 2
|
||||
insert_final_newline = true
|
||||
trim_trailing_whitespace = true
|
||||
|
||||
[*.ts]
|
||||
quote_type = single
|
||||
ij_typescript_use_double_quotes = false
|
||||
|
||||
[*.md]
|
||||
max_line_length = off
|
||||
trim_trailing_whitespace = false
|
||||
44
web/.gitignore
vendored
Normal file
44
web/.gitignore
vendored
Normal file
@@ -0,0 +1,44 @@
|
||||
# See https://docs.github.com/get-started/getting-started-with-git/ignoring-files for more about ignoring files.
|
||||
|
||||
# Compiled output
|
||||
/dist
|
||||
/tmp
|
||||
/out-tsc
|
||||
/bazel-out
|
||||
|
||||
# Node
|
||||
/node_modules
|
||||
npm-debug.log
|
||||
yarn-error.log
|
||||
|
||||
# IDEs and editors
|
||||
.idea/
|
||||
.project
|
||||
.classpath
|
||||
.c9/
|
||||
*.launch
|
||||
.settings/
|
||||
*.sublime-workspace
|
||||
|
||||
# Visual Studio Code
|
||||
.vscode/*
|
||||
!.vscode/settings.json
|
||||
!.vscode/tasks.json
|
||||
!.vscode/launch.json
|
||||
!.vscode/extensions.json
|
||||
!.vscode/mcp.json
|
||||
.history/*
|
||||
|
||||
# Miscellaneous
|
||||
/.angular/cache
|
||||
.sass-cache/
|
||||
/connect.lock
|
||||
/coverage
|
||||
/libpeerconnection.log
|
||||
testem.log
|
||||
/typings
|
||||
__screenshots__/
|
||||
|
||||
# System files
|
||||
.DS_Store
|
||||
Thumbs.db
|
||||
12
web/.prettierrc
Normal file
12
web/.prettierrc
Normal file
@@ -0,0 +1,12 @@
|
||||
{
|
||||
"printWidth": 100,
|
||||
"singleQuote": true,
|
||||
"overrides": [
|
||||
{
|
||||
"files": "*.html",
|
||||
"options": {
|
||||
"parser": "angular"
|
||||
}
|
||||
}
|
||||
]
|
||||
}
|
||||
4
web/.vscode/extensions.json
vendored
Normal file
4
web/.vscode/extensions.json
vendored
Normal file
@@ -0,0 +1,4 @@
|
||||
{
|
||||
// For more information, visit: https://go.microsoft.com/fwlink/?linkid=827846
|
||||
"recommendations": ["angular.ng-template"]
|
||||
}
|
||||
20
web/.vscode/launch.json
vendored
Normal file
20
web/.vscode/launch.json
vendored
Normal file
@@ -0,0 +1,20 @@
|
||||
{
|
||||
// For more information, visit: https://go.microsoft.com/fwlink/?linkid=830387
|
||||
"version": "0.2.0",
|
||||
"configurations": [
|
||||
{
|
||||
"name": "ng serve",
|
||||
"type": "chrome",
|
||||
"request": "launch",
|
||||
"preLaunchTask": "npm: start",
|
||||
"url": "http://localhost:4200/"
|
||||
},
|
||||
{
|
||||
"name": "ng test",
|
||||
"type": "chrome",
|
||||
"request": "launch",
|
||||
"preLaunchTask": "npm: test",
|
||||
"url": "http://localhost:9876/debug.html"
|
||||
}
|
||||
]
|
||||
}
|
||||
42
web/.vscode/tasks.json
vendored
Normal file
42
web/.vscode/tasks.json
vendored
Normal file
@@ -0,0 +1,42 @@
|
||||
{
|
||||
// For more information, visit: https://go.microsoft.com/fwlink/?LinkId=733558
|
||||
"version": "2.0.0",
|
||||
"tasks": [
|
||||
{
|
||||
"type": "npm",
|
||||
"script": "start",
|
||||
"isBackground": true,
|
||||
"problemMatcher": {
|
||||
"owner": "typescript",
|
||||
"pattern": "$tsc",
|
||||
"background": {
|
||||
"activeOnStart": true,
|
||||
"beginsPattern": {
|
||||
"regexp": "Changes detected"
|
||||
},
|
||||
"endsPattern": {
|
||||
"regexp": "bundle generation (complete|failed)"
|
||||
}
|
||||
}
|
||||
}
|
||||
},
|
||||
{
|
||||
"type": "npm",
|
||||
"script": "test",
|
||||
"isBackground": true,
|
||||
"problemMatcher": {
|
||||
"owner": "typescript",
|
||||
"pattern": "$tsc",
|
||||
"background": {
|
||||
"activeOnStart": true,
|
||||
"beginsPattern": {
|
||||
"regexp": "Changes detected"
|
||||
},
|
||||
"endsPattern": {
|
||||
"regexp": "bundle generation (complete|failed)"
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
]
|
||||
}
|
||||
59
web/README.md
Normal file
59
web/README.md
Normal file
@@ -0,0 +1,59 @@
|
||||
# Web
|
||||
|
||||
This project was generated using [Angular CLI](https://github.com/angular/angular-cli) version 22.1.2.
|
||||
|
||||
## Development server
|
||||
|
||||
To start a local development server, run:
|
||||
|
||||
```bash
|
||||
ng serve
|
||||
```
|
||||
|
||||
Once the server is running, open your browser and navigate to `http://localhost:4200/`. The application will automatically reload whenever you modify any of the source files.
|
||||
|
||||
## Code scaffolding
|
||||
|
||||
Angular CLI includes powerful code scaffolding tools. To generate a new component, run:
|
||||
|
||||
```bash
|
||||
ng generate component component-name
|
||||
```
|
||||
|
||||
For a complete list of available schematics (such as `components`, `directives`, or `pipes`), run:
|
||||
|
||||
```bash
|
||||
ng generate --help
|
||||
```
|
||||
|
||||
## Building
|
||||
|
||||
To build the project run:
|
||||
|
||||
```bash
|
||||
ng build
|
||||
```
|
||||
|
||||
This will compile your project and store the build artifacts in the `dist/` directory. By default, the production build optimizes your application for performance and speed.
|
||||
|
||||
## Running unit tests
|
||||
|
||||
To execute unit tests with the [Vitest](https://vitest.dev/) test runner, use the following command:
|
||||
|
||||
```bash
|
||||
ng test
|
||||
```
|
||||
|
||||
## Running end-to-end tests
|
||||
|
||||
For end-to-end (e2e) testing, run:
|
||||
|
||||
```bash
|
||||
ng e2e
|
||||
```
|
||||
|
||||
Angular CLI does not come with an end-to-end testing framework by default. You can choose one that suits your needs.
|
||||
|
||||
## Additional Resources
|
||||
|
||||
For more information on using the Angular CLI, including detailed command references, visit the [Angular CLI Overview and Command Reference](https://angular.dev/tools/cli) page.
|
||||
78
web/angular.json
Normal file
78
web/angular.json
Normal file
@@ -0,0 +1,78 @@
|
||||
{
|
||||
"$schema": "./node_modules/@angular/cli/lib/config/schema.json",
|
||||
"version": 1,
|
||||
"cli": {
|
||||
"packageManager": "npm"
|
||||
},
|
||||
"newProjectRoot": "projects",
|
||||
"projects": {
|
||||
"web": {
|
||||
"projectType": "application",
|
||||
"schematics": {
|
||||
"@schematics/angular:component": {
|
||||
"style": "scss"
|
||||
}
|
||||
},
|
||||
"root": "",
|
||||
"sourceRoot": "src",
|
||||
"prefix": "app",
|
||||
"architect": {
|
||||
"build": {
|
||||
"builder": "@angular/build:application",
|
||||
"options": {
|
||||
"browser": "src/main.ts",
|
||||
"tsConfig": "tsconfig.app.json",
|
||||
"inlineStyleLanguage": "scss",
|
||||
"assets": [
|
||||
{
|
||||
"glob": "**/*",
|
||||
"input": "public"
|
||||
}
|
||||
],
|
||||
"styles": [
|
||||
"src/styles.scss"
|
||||
]
|
||||
},
|
||||
"configurations": {
|
||||
"production": {
|
||||
"budgets": [
|
||||
{
|
||||
"type": "initial",
|
||||
"maximumWarning": "500kB",
|
||||
"maximumError": "1MB"
|
||||
},
|
||||
{
|
||||
"type": "anyComponentStyle",
|
||||
"maximumWarning": "4kB",
|
||||
"maximumError": "8kB"
|
||||
}
|
||||
],
|
||||
"outputHashing": "all"
|
||||
},
|
||||
"development": {
|
||||
"optimization": false,
|
||||
"extractLicenses": false,
|
||||
"sourceMap": true
|
||||
}
|
||||
},
|
||||
"defaultConfiguration": "production"
|
||||
},
|
||||
"serve": {
|
||||
"builder": "@angular/build:dev-server",
|
||||
"configurations": {
|
||||
"production": {
|
||||
"buildTarget": "web:build:production"
|
||||
},
|
||||
"development": {
|
||||
"buildTarget": "web:build:development"
|
||||
}
|
||||
},
|
||||
"defaultConfiguration": "development"
|
||||
},
|
||||
"test": {
|
||||
"builder": "@angular/build:unit-test"
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
7792
web/package-lock.json
generated
Normal file
7792
web/package-lock.json
generated
Normal file
File diff suppressed because it is too large
Load Diff
32
web/package.json
Normal file
32
web/package.json
Normal file
@@ -0,0 +1,32 @@
|
||||
{
|
||||
"name": "web",
|
||||
"version": "0.0.0",
|
||||
"scripts": {
|
||||
"ng": "ng",
|
||||
"start": "ng serve",
|
||||
"build": "ng build",
|
||||
"watch": "ng build --watch --configuration development",
|
||||
"test": "ng test"
|
||||
},
|
||||
"private": true,
|
||||
"packageManager": "npm@10.8.3",
|
||||
"dependencies": {
|
||||
"@angular/common": "^22.1.0",
|
||||
"@angular/compiler": "^22.1.0",
|
||||
"@angular/core": "^22.1.0",
|
||||
"@angular/forms": "^22.1.0",
|
||||
"@angular/platform-browser": "^22.1.0",
|
||||
"@angular/router": "^22.1.0",
|
||||
"rxjs": "~7.8.0",
|
||||
"tslib": "^2.3.0"
|
||||
},
|
||||
"devDependencies": {
|
||||
"@angular/build": "^22.1.2",
|
||||
"@angular/cli": "^22.1.2",
|
||||
"@angular/compiler-cli": "^22.1.0",
|
||||
"jsdom": "^28.0.0",
|
||||
"prettier": "^3.8.1",
|
||||
"typescript": "~6.0.2",
|
||||
"vitest": "^4.0.8"
|
||||
}
|
||||
}
|
||||
BIN
web/public/favicon.ico
Normal file
BIN
web/public/favicon.ico
Normal file
Binary file not shown.
|
After Width: | Height: | Size: 15 KiB |
11
web/src/app/app.config.ts
Normal file
11
web/src/app/app.config.ts
Normal file
@@ -0,0 +1,11 @@
|
||||
import { ApplicationConfig, provideBrowserGlobalErrorListeners } from '@angular/core';
|
||||
import { provideRouter } from '@angular/router';
|
||||
|
||||
import { routes } from './app.routes';
|
||||
|
||||
export const appConfig: ApplicationConfig = {
|
||||
providers: [
|
||||
provideBrowserGlobalErrorListeners(),
|
||||
provideRouter(routes)
|
||||
]
|
||||
};
|
||||
9
web/src/app/app.html
Normal file
9
web/src/app/app.html
Normal file
@@ -0,0 +1,9 @@
|
||||
<header class="app-header">
|
||||
<h1 class="app-title">{{ title() }}</h1>
|
||||
<nav class="app-nav">
|
||||
<a routerLink="/mengenrabatt" class="app-nav-link">Mengenrabatt-Rechner</a>
|
||||
</nav>
|
||||
</header>
|
||||
<main class="app-main">
|
||||
<router-outlet />
|
||||
</main>
|
||||
7
web/src/app/app.routes.ts
Normal file
7
web/src/app/app.routes.ts
Normal file
@@ -0,0 +1,7 @@
|
||||
import { Routes } from '@angular/router';
|
||||
import { MengenrabattRechner } from './mengenrabatt-rechner/mengenrabatt-rechner';
|
||||
|
||||
export const routes: Routes = [
|
||||
{ path: 'mengenrabatt', component: MengenrabattRechner },
|
||||
{ path: '', pathMatch: 'full', redirectTo: 'mengenrabatt' },
|
||||
];
|
||||
41
web/src/app/app.scss
Normal file
41
web/src/app/app.scss
Normal file
@@ -0,0 +1,41 @@
|
||||
:host {
|
||||
display: block;
|
||||
min-height: 100dvh;
|
||||
font-family: "Inter", -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, sans-serif;
|
||||
color: var(--app-fg, #1a1a1a);
|
||||
background: var(--app-bg, #f7f7f7);
|
||||
}
|
||||
|
||||
.app-header {
|
||||
display: flex;
|
||||
align-items: center;
|
||||
gap: 1.5rem;
|
||||
padding: 1rem 1.5rem;
|
||||
background: var(--app-header-bg, #ffffff);
|
||||
border-bottom: 1px solid var(--app-border, #d0d0d0);
|
||||
}
|
||||
|
||||
.app-title {
|
||||
margin: 0;
|
||||
font-size: 1.25rem;
|
||||
font-weight: 600;
|
||||
}
|
||||
|
||||
.app-nav {
|
||||
display: flex;
|
||||
gap: 1rem;
|
||||
}
|
||||
|
||||
.app-nav-link {
|
||||
color: var(--app-link, #0a58ca);
|
||||
text-decoration: none;
|
||||
}
|
||||
|
||||
.app-nav-link:hover,
|
||||
.app-nav-link:focus {
|
||||
text-decoration: underline;
|
||||
}
|
||||
|
||||
.app-main {
|
||||
display: block;
|
||||
}
|
||||
36
web/src/app/app.spec.ts
Normal file
36
web/src/app/app.spec.ts
Normal file
@@ -0,0 +1,36 @@
|
||||
import { TestBed } from '@angular/core/testing';
|
||||
import { provideRouter } from '@angular/router';
|
||||
import { App } from './app';
|
||||
import { routes } from './app.routes';
|
||||
|
||||
describe('App', () => {
|
||||
beforeEach(async () => {
|
||||
await TestBed.configureTestingModule({
|
||||
imports: [App],
|
||||
providers: [provideRouter(routes)],
|
||||
}).compileComponents();
|
||||
});
|
||||
|
||||
it('should create the app', () => {
|
||||
const fixture = TestBed.createComponent(App);
|
||||
const app = fixture.componentInstance;
|
||||
expect(app).toBeTruthy();
|
||||
});
|
||||
|
||||
it('should render the app title', async () => {
|
||||
const fixture = TestBed.createComponent(App);
|
||||
fixture.detectChanges();
|
||||
await fixture.whenStable();
|
||||
const compiled = fixture.nativeElement as HTMLElement;
|
||||
expect(compiled.querySelector('h1')?.textContent).toContain('Preisrechner');
|
||||
});
|
||||
|
||||
it('should render the navigation link to the volume discount calculator', async () => {
|
||||
const fixture = TestBed.createComponent(App);
|
||||
fixture.detectChanges();
|
||||
await fixture.whenStable();
|
||||
const compiled = fixture.nativeElement as HTMLElement;
|
||||
const link = compiled.querySelector('a[href="/mengenrabatt"]');
|
||||
expect(link?.textContent).toContain('Mengenrabatt-Rechner');
|
||||
});
|
||||
});
|
||||
13
web/src/app/app.ts
Normal file
13
web/src/app/app.ts
Normal file
@@ -0,0 +1,13 @@
|
||||
import { ChangeDetectionStrategy, Component, signal } from '@angular/core';
|
||||
import { RouterLink, RouterOutlet } from '@angular/router';
|
||||
|
||||
@Component({
|
||||
selector: 'app-root',
|
||||
changeDetection: ChangeDetectionStrategy.OnPush,
|
||||
imports: [RouterOutlet, RouterLink],
|
||||
templateUrl: './app.html',
|
||||
styleUrl: './app.scss',
|
||||
})
|
||||
export class App {
|
||||
protected readonly title = signal('Preisrechner');
|
||||
}
|
||||
54
web/src/app/mengenrabatt-rechner/mengenrabatt-rechner.html
Normal file
54
web/src/app/mengenrabatt-rechner/mengenrabatt-rechner.html
Normal file
@@ -0,0 +1,54 @@
|
||||
<section class="mengenrabatt-rechner">
|
||||
<h1>Mengenrabatt-Rechner</h1>
|
||||
|
||||
<div class="field">
|
||||
<label for="unit-price">Stückpreis (CHF)</label>
|
||||
<input
|
||||
id="unit-price"
|
||||
type="number"
|
||||
inputmode="decimal"
|
||||
min="0"
|
||||
step="0.05"
|
||||
[value]="unitPriceInput()"
|
||||
(input)="unitPriceInput.set($any($event.target).value)"
|
||||
/>
|
||||
</div>
|
||||
|
||||
<div class="field">
|
||||
<label for="quantity">Menge</label>
|
||||
<input
|
||||
id="quantity"
|
||||
type="number"
|
||||
inputmode="numeric"
|
||||
min="1"
|
||||
step="1"
|
||||
[value]="quantityInput()"
|
||||
(input)="quantityInput.set($any($event.target).value)"
|
||||
/>
|
||||
</div>
|
||||
|
||||
<div class="field">
|
||||
<label for="percent-discount">Prozentrabatt (%)</label>
|
||||
<input
|
||||
id="percent-discount"
|
||||
type="number"
|
||||
inputmode="decimal"
|
||||
min="0"
|
||||
max="100"
|
||||
step="0.1"
|
||||
[value]="percentDiscountInput()"
|
||||
(input)="percentDiscountInput.set($any($event.target).value)"
|
||||
/>
|
||||
</div>
|
||||
|
||||
<dl class="result">
|
||||
<div class="result-row">
|
||||
<dt>Endbetrag</dt>
|
||||
<dd data-testid="endbetrag">{{ endbetragLabel() }}</dd>
|
||||
</div>
|
||||
<div class="result-row">
|
||||
<dt>Staffelstufe</dt>
|
||||
<dd data-testid="staffelstufe">{{ staffelLabel() }}</dd>
|
||||
</div>
|
||||
</dl>
|
||||
</section>
|
||||
55
web/src/app/mengenrabatt-rechner/mengenrabatt-rechner.scss
Normal file
55
web/src/app/mengenrabatt-rechner/mengenrabatt-rechner.scss
Normal file
@@ -0,0 +1,55 @@
|
||||
.mengenrabatt-rechner {
|
||||
display: grid;
|
||||
gap: 1rem;
|
||||
max-width: 32rem;
|
||||
margin: 2rem auto;
|
||||
padding: 1.5rem;
|
||||
border: 1px solid var(--app-border, #d0d0d0);
|
||||
border-radius: 0.5rem;
|
||||
font-family: inherit;
|
||||
}
|
||||
|
||||
.mengenrabatt-rechner h1 {
|
||||
margin: 0;
|
||||
font-size: 1.5rem;
|
||||
}
|
||||
|
||||
.field {
|
||||
display: grid;
|
||||
gap: 0.25rem;
|
||||
}
|
||||
|
||||
.field label {
|
||||
font-weight: 600;
|
||||
}
|
||||
|
||||
.field input {
|
||||
padding: 0.5rem 0.75rem;
|
||||
font: inherit;
|
||||
border: 1px solid var(--app-border, #d0d0d0);
|
||||
border-radius: 0.25rem;
|
||||
}
|
||||
|
||||
.result {
|
||||
display: grid;
|
||||
gap: 0.5rem;
|
||||
margin: 0;
|
||||
padding-top: 1rem;
|
||||
border-top: 1px solid var(--app-border, #d0d0d0);
|
||||
}
|
||||
|
||||
.result-row {
|
||||
display: flex;
|
||||
justify-content: space-between;
|
||||
align-items: baseline;
|
||||
margin: 0;
|
||||
}
|
||||
|
||||
.result-row dt {
|
||||
font-weight: 600;
|
||||
}
|
||||
|
||||
.result-row dd {
|
||||
margin: 0;
|
||||
font-variant-numeric: tabular-nums;
|
||||
}
|
||||
107
web/src/app/mengenrabatt-rechner/mengenrabatt-rechner.spec.ts
Normal file
107
web/src/app/mengenrabatt-rechner/mengenrabatt-rechner.spec.ts
Normal file
@@ -0,0 +1,107 @@
|
||||
import { TestBed, ComponentFixture } from '@angular/core/testing';
|
||||
import { MengenrabattRechner } from './mengenrabatt-rechner';
|
||||
|
||||
function setInputs(
|
||||
fixture: ComponentFixture<MengenrabattRechner>,
|
||||
unitPrice: string,
|
||||
quantity: string,
|
||||
percent: string,
|
||||
): void {
|
||||
const inputs = fixture.nativeElement.querySelectorAll('input');
|
||||
(inputs[0] as HTMLInputElement).value = unitPrice;
|
||||
inputs[0].dispatchEvent(new Event('input'));
|
||||
(inputs[1] as HTMLInputElement).value = quantity;
|
||||
inputs[1].dispatchEvent(new Event('input'));
|
||||
(inputs[2] as HTMLInputElement).value = percent;
|
||||
inputs[2].dispatchEvent(new Event('input'));
|
||||
fixture.detectChanges();
|
||||
}
|
||||
|
||||
function compute(unitPrice: string, quantity: string, percent: string): {
|
||||
endbetrag: string;
|
||||
staffelstufe: string;
|
||||
} {
|
||||
const fixture = TestBed.createComponent(MengenrabattRechner);
|
||||
fixture.detectChanges();
|
||||
setInputs(fixture, unitPrice, quantity, percent);
|
||||
const compiled = fixture.nativeElement as HTMLElement;
|
||||
return {
|
||||
endbetrag: (compiled.querySelector('[data-testid="endbetrag"]')?.textContent ?? '').trim(),
|
||||
staffelstufe: (compiled.querySelector('[data-testid="staffelstufe"]')?.textContent ?? '').trim(),
|
||||
};
|
||||
}
|
||||
|
||||
describe('MengenrabattRechner', () => {
|
||||
beforeEach(async () => {
|
||||
await TestBed.configureTestingModule({
|
||||
imports: [MengenrabattRechner],
|
||||
}).compileComponents();
|
||||
});
|
||||
|
||||
describe('quantity tiers', () => {
|
||||
it('uses the 0% tier for 9 pieces', () => {
|
||||
const { endbetrag, staffelstufe } = compute('10', '9', '0');
|
||||
expect(staffelstufe).toBe('0 % (1–9 Stück)');
|
||||
expect(endbetrag).toContain('90.00');
|
||||
});
|
||||
|
||||
it('switches to the 5% tier at 10 pieces', () => {
|
||||
const { endbetrag, staffelstufe } = compute('10', '10', '0');
|
||||
expect(staffelstufe).toBe('5 % (10–49 Stück)');
|
||||
expect(endbetrag).toContain('95.00');
|
||||
});
|
||||
|
||||
it('stays on the 5% tier at 49 pieces', () => {
|
||||
const { endbetrag, staffelstufe } = compute('1', '49', '0');
|
||||
expect(staffelstufe).toBe('5 % (10–49 Stück)');
|
||||
// 1 * 0.95 * 49 = 46.55
|
||||
expect(endbetrag).toContain('46.55');
|
||||
});
|
||||
|
||||
it('switches to the 10% tier at 50 pieces', () => {
|
||||
const { endbetrag, staffelstufe } = compute('1', '50', '0');
|
||||
expect(staffelstufe).toBe('10 % (ab 50 Stück)');
|
||||
// 1 * 0.9 * 50 = 45.00
|
||||
expect(endbetrag).toContain('45.00');
|
||||
});
|
||||
});
|
||||
|
||||
describe('discount pipeline', () => {
|
||||
it('applies percentage discount first, then the quantity tier discount (100/10/25 → 712.50)', () => {
|
||||
const { endbetrag, staffelstufe } = compute('100', '10', '25');
|
||||
// 100 * 0.75 = 75; 75 * 10 = 750; quantity tier 5%: 750 * 0.95 = 712.50
|
||||
expect(endbetrag).toContain('712.50');
|
||||
expect(staffelstufe).toBe('5 % (10–49 Stück)');
|
||||
});
|
||||
});
|
||||
|
||||
describe('rounding to 0.05', () => {
|
||||
it('rounds 1.23 up to 1.25', () => {
|
||||
const { endbetrag } = compute('1.23', '1', '0');
|
||||
// 1 piece is in the 1–9 tier (0%); rounded to 0.05: 1.23 -> 1.25
|
||||
expect(endbetrag).toContain('1.25');
|
||||
});
|
||||
});
|
||||
|
||||
describe('invalid input', () => {
|
||||
it('shows the placeholder when inputs are empty', () => {
|
||||
const { endbetrag, staffelstufe } = compute('', '', '');
|
||||
expect(endbetrag).toBe('—');
|
||||
expect(staffelstufe).toBe('—');
|
||||
});
|
||||
|
||||
it('shows the placeholder for a non-integer quantity', () => {
|
||||
const { endbetrag } = compute('10', '3.5', '0');
|
||||
expect(endbetrag).toBe('—');
|
||||
});
|
||||
});
|
||||
|
||||
describe('DOM output', () => {
|
||||
it('renders the Endbetrag with two decimals and the German Staffelstufe label', () => {
|
||||
const { endbetrag, staffelstufe } = compute('100', '10', '25');
|
||||
expect(endbetrag).toMatch(/\d+\.\d{2}/);
|
||||
expect(endbetrag).toContain('712.50');
|
||||
expect(staffelstufe).toBe('5 % (10–49 Stück)');
|
||||
});
|
||||
});
|
||||
});
|
||||
99
web/src/app/mengenrabatt-rechner/mengenrabatt-rechner.ts
Normal file
99
web/src/app/mengenrabatt-rechner/mengenrabatt-rechner.ts
Normal file
@@ -0,0 +1,99 @@
|
||||
import { ChangeDetectionStrategy, Component, computed, signal } from '@angular/core';
|
||||
|
||||
interface DiscountTier {
|
||||
readonly minQuantity: number;
|
||||
readonly maxQuantity: number | null;
|
||||
readonly percent: number;
|
||||
readonly label: string;
|
||||
}
|
||||
|
||||
const TIERS: readonly DiscountTier[] = [
|
||||
{ minQuantity: 1, maxQuantity: 9, percent: 0, label: '0 % (1–9 Stück)' },
|
||||
{ minQuantity: 10, maxQuantity: 49, percent: 5, label: '5 % (10–49 Stück)' },
|
||||
{ minQuantity: 50, maxQuantity: null, percent: 10, label: '10 % (ab 50 Stück)' },
|
||||
];
|
||||
|
||||
const PERCENT_DIVISOR = 100;
|
||||
const ROUNDING_STEP = 20; // 1 / 0.05 → multiply before rounding
|
||||
|
||||
function tierFor(quantity: number): DiscountTier | null {
|
||||
for (const tier of TIERS) {
|
||||
if (quantity >= tier.minQuantity && (tier.maxQuantity === null || quantity <= tier.maxQuantity)) {
|
||||
return tier;
|
||||
}
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
function parseNumber(value: string): number | null {
|
||||
if (value.trim() === '') {
|
||||
return null;
|
||||
}
|
||||
const parsed = Number(value.replace(',', '.'));
|
||||
return Number.isFinite(parsed) ? parsed : null;
|
||||
}
|
||||
|
||||
function formatChf(value: number): string {
|
||||
return `CHF\u00a0${value.toFixed(2)}`;
|
||||
}
|
||||
|
||||
@Component({
|
||||
selector: 'app-mengenrabatt-rechner',
|
||||
changeDetection: ChangeDetectionStrategy.OnPush,
|
||||
imports: [],
|
||||
templateUrl: './mengenrabatt-rechner.html',
|
||||
styleUrl: './mengenrabatt-rechner.scss',
|
||||
})
|
||||
export class MengenrabattRechner {
|
||||
protected readonly unitPriceInput = signal('');
|
||||
protected readonly quantityInput = signal('');
|
||||
protected readonly percentDiscountInput = signal('');
|
||||
|
||||
private readonly unitPrice = computed(() => parseNumber(this.unitPriceInput()));
|
||||
private readonly quantity = computed(() => {
|
||||
const value = parseNumber(this.quantityInput());
|
||||
if (value === null || !Number.isInteger(value) || value <= 0) {
|
||||
return null;
|
||||
}
|
||||
return value;
|
||||
});
|
||||
private readonly percentDiscount = computed(() => {
|
||||
const value = parseNumber(this.percentDiscountInput());
|
||||
if (value === null || value < 0 || value > 100) {
|
||||
return null;
|
||||
}
|
||||
return value;
|
||||
});
|
||||
|
||||
protected readonly tier = computed<DiscountTier | null>(() => {
|
||||
const quantity = this.quantity();
|
||||
return quantity === null ? null : tierFor(quantity);
|
||||
});
|
||||
|
||||
private readonly total = computed<number | null>(() => {
|
||||
const unitPrice = this.unitPrice();
|
||||
const quantity = this.quantity();
|
||||
const percentDiscount = this.percentDiscount();
|
||||
const tier = this.tier();
|
||||
if (
|
||||
unitPrice === null ||
|
||||
unitPrice < 0 ||
|
||||
quantity === null ||
|
||||
percentDiscount === null ||
|
||||
tier === null
|
||||
) {
|
||||
return null;
|
||||
}
|
||||
const discountedUnit = unitPrice * (1 - percentDiscount / PERCENT_DIVISOR);
|
||||
const subtotal = discountedUnit * quantity;
|
||||
const withTierDiscount = subtotal * (1 - tier.percent / PERCENT_DIVISOR);
|
||||
return Math.round(withTierDiscount * ROUNDING_STEP) / ROUNDING_STEP;
|
||||
});
|
||||
|
||||
protected readonly endbetragLabel = computed<string>(() => {
|
||||
const value = this.total();
|
||||
return value === null ? '—' : formatChf(value);
|
||||
});
|
||||
|
||||
protected readonly staffelLabel = computed<string>(() => this.tier()?.label ?? '—');
|
||||
}
|
||||
13
web/src/index.html
Normal file
13
web/src/index.html
Normal file
@@ -0,0 +1,13 @@
|
||||
<!doctype html>
|
||||
<html lang="en">
|
||||
<head>
|
||||
<meta charset="utf-8">
|
||||
<title>Web</title>
|
||||
<base href="/">
|
||||
<meta name="viewport" content="width=device-width, initial-scale=1">
|
||||
<link rel="icon" type="image/x-icon" href="favicon.ico">
|
||||
</head>
|
||||
<body>
|
||||
<app-root></app-root>
|
||||
</body>
|
||||
</html>
|
||||
6
web/src/main.ts
Normal file
6
web/src/main.ts
Normal file
@@ -0,0 +1,6 @@
|
||||
import { bootstrapApplication } from '@angular/platform-browser';
|
||||
import { appConfig } from './app/app.config';
|
||||
import { App } from './app/app';
|
||||
|
||||
bootstrapApplication(App, appConfig)
|
||||
.catch((err) => console.error(err));
|
||||
1
web/src/styles.scss
Normal file
1
web/src/styles.scss
Normal file
@@ -0,0 +1 @@
|
||||
/* You can add global styles to this file, and also import other style files */
|
||||
14
web/tsconfig.app.json
Normal file
14
web/tsconfig.app.json
Normal file
@@ -0,0 +1,14 @@
|
||||
/* To learn more about Typescript configuration file: https://www.typescriptlang.org/docs/handbook/tsconfig-json.html. */
|
||||
/* To learn more about Angular compiler options: https://angular.dev/reference/configs/angular-compiler-options. */
|
||||
{
|
||||
"extends": "./tsconfig.json",
|
||||
"compilerOptions": {
|
||||
"types": []
|
||||
},
|
||||
"include": [
|
||||
"src/**/*.ts"
|
||||
],
|
||||
"exclude": [
|
||||
"src/**/*.spec.ts"
|
||||
]
|
||||
}
|
||||
31
web/tsconfig.json
Normal file
31
web/tsconfig.json
Normal file
@@ -0,0 +1,31 @@
|
||||
/* To learn more about Typescript configuration file: https://www.typescriptlang.org/docs/handbook/tsconfig-json.html. */
|
||||
/* To learn more about Angular compiler options: https://angular.dev/reference/configs/angular-compiler-options. */
|
||||
{
|
||||
"compileOnSave": false,
|
||||
"compilerOptions": {
|
||||
"noImplicitOverride": true,
|
||||
"noPropertyAccessFromIndexSignature": true,
|
||||
"noImplicitReturns": true,
|
||||
"noFallthroughCasesInSwitch": true,
|
||||
"skipLibCheck": true,
|
||||
"isolatedModules": true,
|
||||
"experimentalDecorators": true,
|
||||
"importHelpers": true,
|
||||
"target": "ES2022",
|
||||
"module": "preserve"
|
||||
},
|
||||
"angularCompilerOptions": {
|
||||
"enableI18nLegacyMessageIdFormat": false,
|
||||
"strictInjectionParameters": true,
|
||||
"strictInputAccessModifiers": true
|
||||
},
|
||||
"files": [],
|
||||
"references": [
|
||||
{
|
||||
"path": "./tsconfig.app.json"
|
||||
},
|
||||
{
|
||||
"path": "./tsconfig.spec.json"
|
||||
}
|
||||
]
|
||||
}
|
||||
14
web/tsconfig.spec.json
Normal file
14
web/tsconfig.spec.json
Normal file
@@ -0,0 +1,14 @@
|
||||
/* To learn more about Typescript configuration file: https://www.typescriptlang.org/docs/handbook/tsconfig-json.html. */
|
||||
/* To learn more about Angular compiler options: https://angular.dev/reference/configs/angular-compiler-options. */
|
||||
{
|
||||
"extends": "./tsconfig.json",
|
||||
"compilerOptions": {
|
||||
"types": [
|
||||
"vitest/globals"
|
||||
]
|
||||
},
|
||||
"include": [
|
||||
"src/**/*.d.ts",
|
||||
"src/**/*.spec.ts"
|
||||
]
|
||||
}
|
||||
Reference in New Issue
Block a user