Repository navigation
Expand file tree
/
Copy pathHW04_T02_Class_Invoice_v2.ts
More file actions
81 lines (66 loc) · 2.93 KB
/
Copy pathHW04_T02_Class_Invoice_v2.ts
File metadata and controls
81 lines (66 loc) · 2.93 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
// Class Definition
// An invoice is an immutable commercial document issued by a seller to a buyer
// Status: Draft --> Pending --> Paid
class Invoice {
invoiceNumber: string
clientName: string
amount: number
status: string
constructor(invoiceNumber: string, clientName: string, amount: number, status: string = 'Draft') {
this.invoiceNumber = invoiceNumber
this.clientName = clientName
this.amount = amount
this.status = status
}
markAsPending(): string | void {
if (this.status !== 'Draft') {
return `Cannot mark as pending: invoice is already in '${this.status}' status.`
}
this.status = 'Pending'
}
markAsPaid(): string | void {
if (this.status === 'Paid') {
return 'This invoice has already been paid.'
}
if (this.status !== 'Pending') {
return `Cannot mark as paid: invoice must be in 'Pending' status first.`
}
this.status = 'Paid'
}
changeAmount(newAmount: number): string | void {
if (this.status !== 'Draft') {
return `Cannot change amount: invoice is immutable once issued (current status: '${this.status}').`
}
this.amount = newAmount
}
}
// Verification
// Handling large numbers cleanly:
// 1. Numeric Separator (_): Visual separator making large numbers readable (e.g., 42_000_000_000).
// JS ignores underscores at runtime, treating it as 42000000000.
// 2. BigInt Literal (n): The 'n' suffix creates a bigint type (e.g., 42000000000n).
// Used for integers exceeding Number.MAX_SAFE_INTEGER (9,007,199,254,740,991) to prevent precision loss.
// 1. Invoice instance created as Draft for Yutani Corp
const myInvoice = new Invoice('WY-2179', 'Yutani Corp', 42_000_000_000, 'Draft')
console.log('Initial status:', myInvoice.status) // 'Draft'
// Draft amount can be modified freely
myInvoice.changeAmount(50_000_000_000)
console.log('Updated Draft amount:', myInvoice.amount) // 50000000000
// 2. Issue invoice to Pending status
myInvoice.markAsPending()
console.log('Current status:', myInvoice.status) // 'Pending'
// Attempting to change amount on a Pending invoice fails
console.log(myInvoice.changeAmount(99_999_999_999))
// Output: "Cannot change amount: invoice is immutable once issued (current status: 'Pending')."
// 3. Mark invoice as Paid
myInvoice.markAsPaid()
console.log('Current status:', myInvoice.status) // 'Paid'
// Attempting to change amount on a Paid invoice fails
console.log(myInvoice.changeAmount(99_999_999_999))
// Output: "Cannot change amount: invoice is immutable once issued (current status: 'Paid')."
// 4. Again markAsPaid() to get the already paid notice
console.log(myInvoice.markAsPaid())
// Output: "This invoice has already been paid."
// References & Safety checks
console.log('Max safe normal integer reference:', Number.MAX_SAFE_INTEGER)
console.log('Final untouched Invoice amount:', myInvoice.amount) // 50000000000