-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathWalletTracker.kt
139 lines (125 loc) · 4.76 KB
/
WalletTracker.kt
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
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
//install these libraries
import java.time.LocalDate
import java.time.format.DateTimeFormatter
import java.time.format.DateTimeParseException
import java.util.*
// Data class to represent a financial transaction
data class Transaction(
val id: Int,
val type: String, // "expense" or "income"
val amount: Double,
val date: LocalDate,
val category: String,
val note: String? = null
)
// List to store all transactions
val transactions = mutableListOf<Transaction>()
// Counter for generating unique transaction IDs
var transactionIdCounter = 1
// Formatter to parse and format dates in the "YYYY-MM-DD" format
val dateFormatter: DateTimeFormatter = DateTimeFormatter.ofPattern("yyyy-MM-dd")
// Function to add a new transaction
fun addTransaction(type: String, amount: Double, date: String, category: String, note: String? = null) {
try {
// Parse the date string into a LocalDate object
val parsedDate = LocalDate.parse(date, dateFormatter)
// Create a new transaction object
val transaction = Transaction(transactionIdCounter++, type, amount, parsedDate, category, note)
// Add the transaction to the list
transactions.add(transaction)
} catch (e: DateTimeParseException) {
// Handle invalid date format
println("Invalid date format. Please use YYYY-MM-DD.")
}
}
// Function to calculate the total amount for a given type (income or expense)
fun getTotalAmount(type: String): Double {
return transactions.filter { it.type == type }.sumOf { it.amount }
}
// Function to display all transactions
fun displayTransactions() {
println("ID | Type | Amount | Date | Category | Note")
println("------------------------------------------------------------")
for (transaction in transactions) {
println(
"${transaction.id.toString().padEnd(3)}| " +
"${transaction.type.padEnd(7)}| " +
"${transaction.amount.toString().padEnd(7)}| " +
"${transaction.date.format(dateFormatter).padEnd(11)}| " +
"${transaction.category.padEnd(12)}| " +
"${transaction.note ?: ""}"
)
}
}
// Function to display a summary of income, expenses, and net amount
fun displaySummary() {
val totalIncome = getTotalAmount("income")
val totalExpenses = getTotalAmount("expense")
val netAmount = totalIncome - totalExpenses
println("Total Income: $$totalIncome")
println("Total Expenses: $$totalExpenses")
println("Net Amount: $$netAmount")
}
// Main function to interact with the user
fun main() {
val scanner = Scanner(System.`in`)
while (true) {
// Display the menu
println("\nExpense Tracker Menu")
println("1. Add Income")
println("2. Add Expense")
println("3. View Transactions")
println("4. View Summary")
println("5. Exit")
print("Choose an option: ")
// Get user input
when (scanner.nextInt()) {
1 -> {
// Add Income
println("\nAdd Income")
print("Enter amount: ")
val amount = scanner.nextDouble()
scanner.nextLine() // Consume newline
print("Enter date (YYYY-MM-DD): ")
val date = scanner.nextLine()
print("Enter category: ")
val category = scanner.nextLine()
print("Enter note (optional): ")
val note = scanner.nextLine()
addTransaction("income", amount, date, category, note)
println("Income added successfully.")
}
2 -> {
// Add Expense
println("\nAdd Expense")
print("Enter amount: ")
val amount = scanner.nextDouble()
scanner.nextLine() // Consume newline
print("Enter date (YYYY-MM-DD): ")
val date = scanner.nextLine()
print("Enter category: ")
val category = scanner.nextLine()
print("Enter note (optional): ")
val note = scanner.nextLine()
addTransaction("expense", amount, date, category, note)
println("Expense added successfully.")
}
3 -> {
// View Transactions
println("\nTransactions")
displayTransactions()
}
4 -> {
// View Summary
println("\nSummary")
displaySummary()
}
5 -> {
// Exit
println("Exiting...")
return
}
else -> println("Invalid option. Please try again.")
}
}
}