import 'dart:io'; void main() { final List> incomeList = []; final List> expenseList = []; while (true) { stdout.writeln('\n=== Budget Manager ==='); stdout.writeln('1) Add income'); stdout.writeln('2) Add expense'); stdout.writeln('3) Show summary'); stdout.writeln('4) Exit'); stdout.write('Your choice: '); final choice = stdin.readLineSync()?.trim() ?? ''; if (choice == '1') { stdout.write('Income description: '); final desc = stdin.readLineSync()?.trim() ?? ''; stdout.write('Amount: '); final amt = double.tryParse(stdin.readLineSync()?.trim() ?? ''); if (desc.isNotEmpty && amt != null && amt >= 0) { incomeList.add({desc: amt}); stdout.writeln('Added.'); } else { stdout.writeln('Invalid data. Amount must be a number ≥ 0.'); } } else if (choice == '2') { stdout.write('Expense description: '); final desc = stdin.readLineSync()?.trim() ?? ''; stdout.write('Amount: '); final amt = double.tryParse(stdin.readLineSync()?.trim() ?? ''); if (desc.isNotEmpty && amt != null && amt >= 0) { expenseList.add({desc: amt}); stdout.writeln('Added.'); } else { stdout.writeln('Invalid data. Amount must be a number ≥ 0.'); } } else if (choice == '3') { final totalIncome = _sum(incomeList); final totalExpense = _sum(expenseList); final net = totalIncome - totalExpense; final profitable = net > 0; final tax = profitable ? net * 0.15 : 0.0; stdout.writeln('\n--- Summary ---'); stdout.writeln('Total income: ${totalIncome.toStringAsFixed(2)}'); stdout.writeln('Total expense: ${totalExpense.toStringAsFixed(2)}'); if (profitable) { stdout.writeln('Net profit: ${net.toStringAsFixed(2)}'); stdout.writeln('Tax due 15%: ${tax.toStringAsFixed(2)}'); } else if (net < 0) { stdout.writeln('Net loss: ${(-net).toStringAsFixed(2)}'); } else { stdout.writeln('Break-even.'); } } else if (choice == '4') { stdout.writeln('Exited.'); return; } else { stdout.writeln('Invalid choice.'); } } } double _sum(List> items) { var s = 0.0; for (final m in items) { for (final entry in m.entries) { s += entry.value; } } return s; }