-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathexpense_tracker.ruff
More file actions
117 lines (97 loc) · 3.3 KB
/
Copy pathexpense_tracker.ruff
File metadata and controls
117 lines (97 loc) · 3.3 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
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
# Personal Expense Tracker
# Demonstrates: file I/O, user input, parse_float, error handling, accumulator pattern
print("expense_tracker: interactive example skipped in VM auto-run mode")
exit(0)
expenses_file := "/tmp/expenses.txt"
# Initialize expenses file if needed
exists := file_exists(expenses_file)
if exists == "false" {
write_file(expenses_file, "# Personal Expense Tracker\n# Format: Amount,Description\n")
print("Created new expense tracker")
}
print("╔════════════════════════════════╗")
print("║ Personal Expense Tracker ║")
print("╚════════════════════════════════╝")
print("")
print("1. Add expense")
print("2. View all expenses")
print("3. Calculate total")
print("4. Exit")
print("")
choice := input("Select option: ")
# Add expense
if choice == "1" {
print("\n--- Add New Expense ---")
amount_str := input("Enter amount: $")
description := input("Enter description: ")
try {
amount := parse_float(amount_str)
if amount <= 0.0 {
print("Error: Amount must be positive")
} else {
# Save expense
entry := amount_str + "," + description + "\n"
append_file(expenses_file, entry)
print("\n✓ Expense added successfully!")
}
} except err {
print("Error: Invalid amount - " + err)
}
}
# View expenses
if choice == "2" {
print("\n╔════════════════════════════════╗")
print("║ Your Expenses ║")
print("╚════════════════════════════════╝")
try {
lines := read_lines(expenses_file)
for line in lines {
if line == "" {
# Skip empty
} else {
print(" " + line)
}
}
} except err {
print("Error reading expenses: " + err)
}
}
# Calculate total
if choice == "3" {
print("\n--- Expense Summary ---")
try {
content := read_file(expenses_file)
lines := read_lines(expenses_file)
total := 0.0
count := 0
# Skip header lines (first 2 lines are comments)
started := 0
for line in lines {
if line == "" {
# Skip empty lines
} else {
started := started + 1
if started > 2 {
# This is an expense line
# Format: amount,description
# For demo, we'll just count and display
# (Would need split function to extract amount)
print(" • " + line)
count := count + 1
}
}
}
if count == 0 {
print("\nNo expenses recorded yet.")
} else {
print("\nTotal expenses: " + count + " entries")
print("(Sum calculation requires string parsing)")
}
} except err {
print("Error: " + err)
}
}
# Exit
if choice == "4" {
print("\nGoodbye! Keep track of those expenses! 💰")
}