• Source
    1. import java.util.ArrayList;
    2. import java.util.List;
    3. import java.util.Scanner;
    4.  
    5. // Transaction class to represent income and expenses
    6. class Transaction {
    7. private String type;
    8. private double amount;
    9. private String description;
    10.  
    11. public Transaction(String type, double amount, String description) {
    12. this.type = type;
    13. this.amount = amount;
    14. this.description = description;
    15. }
    16.  
    17. public String getType() {
    18. return type;
    19. }
    20.  
    21. public double getAmount() {
    22. return amount;
    23. }
    24.  
    25. public String getDescription() {
    26. return description;
    27. }
    28. }
    29.  
    30. public class FinancialTracker {
    31. private List<Transaction> transactions;
    32. private double budget;
    33.  
    34. public FinancialTracker() {
    35. this.transactions = new ArrayList<>();
    36. this.budget = 0.0;
    37. }
    38.  
    39. // Method to add income
    40. public void addIncome(double amount, String description) {
    41. Transaction transaction = new Transaction("Income", amount, description);
    42. transactions.add(transaction);
    43. }
    44.  
    45. // Method to add expense
    46. public void addExpense(double amount, String description) {
    47. Transaction transaction = new Transaction("Expense", amount, description);
    48. transactions.add(transaction);
    49. }
    50.  
    51. // Method to set budget
    52. public void setBudget(double budget) {
    53. this.budget = budget;
    54. }
    55.  
    56. // Method to view transactions
    57. public void viewTransactions() {
    58. for (Transaction transaction : transactions) {
    59. System.out.println("Type: " + transaction.getType() + ", Amount: " + transaction.getAmount() + ", Description: " + transaction.getDescription());
    60. }
    61. }
    62.  
    63. // Method to view budget
    64. public void viewBudget() {
    65. System.out.println("Budget: " + budget);
    66. }
    67.  
    68. // Method to visualize spending habits
    69. public void visualizeSpendingHabits() {
    70. double totalIncome = 0.0;
    71. double totalExpense = 0.0;
    72.  
    73. for (Transaction transaction : transactions) {
    74. if (transaction.getType().equals("Income")) {
    75. totalIncome += transaction.getAmount();
    76. } else {
    77. totalExpense += transaction.getAmount();
    78. }
    79. }
    80.  
    81. System.out.println("Total Income: " + totalIncome);
    82. System.out.println("Total Expense: " + totalExpense);
    83.  
    84. // Simple bar chart to visualize spending habits
    85. int incomeBars = (int) (totalIncome / 100);
    86. int expenseBars = (int) (totalExpense / 100);
    87.  
    88. System.out.println("Income: " + new String(new char[incomeBars]).replace("\0", "*"));
    89. System.out.println("Expense: " + new String(new char[expenseBars]).replace("\0", "*"));
    90. }
    91.  
    92. public static void main(String[] args) {
    93. FinancialTracker financialTracker = new FinancialTracker();
    94. Scanner scanner = new Scanner(System.in);
    95.  
    96. while (true) {
    97. System.out.println("1. Add Income");
    98. System.out.println("2. Add Expense");
    99. System.out.println("3. Set Budget");
    100. System.out.println("4. View Transactions");
    101. System.out.println("5. View Budget");
    102. System.out.println("6. Visualize Spending Habits");
    103. System.out.println("7. Exit");
    104.  
    105. System.out.print("Choose an option: ");
    106. int option = scanner.nextInt();
    107.  
    108. switch (option) {
    109. case 1:
    110. System.out.print("Enter income amount: ");
    111. double incomeAmount = scanner.nextDouble();
    112. System.out.print("Enter income description: ");
    113. String incomeDescription = scanner.next();
    114. financialTracker.addIncome(incomeAmount, incomeDescription);
    115. break;
    116. case 2:
    117. System.out.print("Enter expense amount: ");
    118. double expenseAmount = scanner.nextDouble();
    119. System.out.print("Enter expense description: ");
    120. String expenseDescription = scanner.next();
    121. financialTracker.addExpense(expenseAmount, expenseDescription);
    122. break;
    123. case 3:
    124. System.out.print("Enter budget: ");
    125. double budget = scanner.nextDouble();
    126. financialTracker.setBudget(budget);
    127. break;
    128. case 4:
    129. financialTracker.viewTransactions();
    130. break;
    131. case 5:
    132. financialTracker.viewBudget();
    133. break;
    134. case 6:
    135. financialTracker.visualizeSpendingHabits();
    136. break;
    137. case 7:
    138. System.exit(0);
    139. break;
    140. default:
    141. System.out.println("Invalid option. Please choose a valid option.");
    142. }
    143. }
    144. }
    145. }
    146.