-
Notifications
You must be signed in to change notification settings - Fork 5.9k
Expand file tree
/
Copy pathdemo.go
More file actions
101 lines (88 loc) · 2.68 KB
/
demo.go
File metadata and controls
101 lines (88 loc) · 2.68 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
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
package splitwise
import "fmt"
func Run() {
ResetService()
service := GetService()
// --- Create Users ---
alice := NewUser("1", "Alice", "alice@example.com")
bob := NewUser("2", "Bob", "bob@example.com")
charlie := NewUser("3", "Charlie", "charlie@example.com")
diana := NewUser("4", "Diana", "diana@example.com")
service.AddUser(alice)
service.AddUser(bob)
service.AddUser(charlie)
service.AddUser(diana)
// --- Create Group ---
group := NewGroup("g1", "Apartment")
group.AddMember(alice)
group.AddMember(bob)
group.AddMember(charlie)
group.AddMember(diana)
service.AddGroup(group)
// --- Expense 1: Equal Split ---
// For equal, Value is ignored — just list the user IDs
fmt.Println(">>> Expense 1: Alice pays $300 rent (equal split)")
_, err := service.CreateExpense("g1", "e1", 300, "Rent", "1", Equal, []Participant{
{UserID: "1"}, {UserID: "2"}, {UserID: "3"},
})
if err != nil {
fmt.Println("Error:", err)
return
}
service.PrintBalances()
// --- Expense 2: Percent Split ---
// Value = percentage share
fmt.Println("\n>>> Expense 2: Bob pays $200 groceries (percent split)")
_, err = service.CreateExpense("g1", "e2", 200, "Groceries", "2", Percent, []Participant{
{UserID: "1", Value: 25},
{UserID: "2", Value: 25},
{UserID: "3", Value: 25},
{UserID: "4", Value: 25},
})
if err != nil {
fmt.Println("Error:", err)
return
}
service.PrintBalances()
// --- Expense 3: Exact Split ---
// Value = exact dollar amount
fmt.Println("\n>>> Expense 3: Charlie pays $150 dinner (exact split)")
_, err = service.CreateExpense("g1", "e3", 150, "Dinner", "3", Exact, []Participant{
{UserID: "1", Value: 50},
{UserID: "2", Value: 30},
{UserID: "3", Value: 40},
{UserID: "4", Value: 30},
})
if err != nil {
fmt.Println("Error:", err)
return
}
service.PrintBalances()
// --- Settle ---
fmt.Println("\n>>> Settling Bob's debt with Alice")
tx, err := service.SettleBalance("2", "1")
if err != nil {
fmt.Println("Error:", err)
} else {
fmt.Println(" ", tx)
}
fmt.Println("\n>>> Final Balances")
service.PrintBalances()
fmt.Println("\n>>> Transaction History")
for _, t := range service.GetTransactions() {
fmt.Println(" ", t)
}
// --- Validation: bad percent ---
fmt.Println("\n>>> Validation: percent that doesn't sum to 100")
_, err = service.CreateExpense("g1", "e4", 100, "Bad", "1", Percent, []Participant{
{UserID: "1", Value: 50},
{UserID: "2", Value: 30},
})
fmt.Println(" Error:", err)
// --- Validation: unknown user ---
fmt.Println("\n>>> Validation: unknown user ID")
_, err = service.CreateExpense("g1", "e5", 100, "Bad", "1", Equal, []Participant{
{UserID: "1"}, {UserID: "999"},
})
fmt.Println(" Error:", err)
}