diff options
| author | Joe Carstairs <me@joeac.net> | 2024-09-05 22:44:37 +0100 |
|---|---|---|
| committer | Joe Carstairs <me@joeac.net> | 2024-09-05 22:44:37 +0100 |
| commit | 6e827308ce332a842c87f19d7adedf827544317e (patch) | |
| tree | 9509622166b56537469ec48bbe980efee75b6974 /gui/src/types/currency | |
| parent | 95d4db4da725b74999524a1307e5e17b434dbfeb (diff) | |
Adds GUI with basic histogram
Diffstat (limited to 'gui/src/types/currency')
| -rw-r--r-- | gui/src/types/currency/currency.ts | 6 | ||||
| -rw-r--r-- | gui/src/types/currency/gbp.ts | 46 |
2 files changed, 52 insertions, 0 deletions
diff --git a/gui/src/types/currency/currency.ts b/gui/src/types/currency/currency.ts new file mode 100644 index 0000000..7c15324 --- /dev/null +++ b/gui/src/types/currency/currency.ts @@ -0,0 +1,6 @@ +interface Currency { + valueOf(): number; + toString(): string; +} + +export default Currency; diff --git a/gui/src/types/currency/gbp.ts b/gui/src/types/currency/gbp.ts new file mode 100644 index 0000000..ce24e48 --- /dev/null +++ b/gui/src/types/currency/gbp.ts @@ -0,0 +1,46 @@ +import Currency from "./currency"; + +class Gbp implements Currency { + private readonly sign: 1 | -1; + private readonly pounds: number; + private readonly pence: number; + + constructor(pounds: number, pence: number, sign?: 1 | -1) { + sign = sign ?? 1; + + if (Math.round(pounds) !== pounds) { + throw new Error( + `Cannot construct GBP with non-whole number of pounds: ${pounds}`, + ); + } + if (Math.round(pence) !== pence) { + throw new Error( + `Cannot construct GBP with non-whole number of pence: ${pence}`, + ); + } + if (pounds < 0) { + throw new Error( + `Cannot construct GBP with negative number of pounds: ${pounds}`, + ); + } + if (pence < 0) { + throw new Error( + `Cannot construct GBP with negative number of pounds: ${pounds}`, + ); + } + + this.sign = sign; + this.pounds = pounds; + this.pence = pence; + } + + valueOf(): number { + return this.sign * (this.pounds * 100 + this.pence); + } + + toString(): string { + return `£${this.pounds}·${this.pence < 10 ? `0${this.pence}` : this.pence}`; + } +} + +export default Gbp; |
