0% found this document useful (0 votes)
11 views5 pages

Personal Finance Tracker

Uploaded by

Saradha S
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
11 views5 pages

Personal Finance Tracker

Uploaded by

Saradha S
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 5

/*

* To change this template, choose Tools | Templates

* and open the template in the editor.

*/

package personalfinancetracker;

import java.io.*;

import java.util.*;

public class PersonalFinanceTracker {

private static final String FILE_NAME = "finance_data.txt";

private static double totalIncome = 0;

private static double totalExpenses = 0;

public static void main(String[] args) {

loadData();

Scanner scanner = new Scanner(System.in);

while (true) {

System.out.println("Personal Finance Tracker");

System.out.println("1. Add Income");

System.out.println("2. Add Expense");

System.out.println("3. View Summary");

System.out.println("4. Exit");

System.out.print("Choose an option: ");

int choice = scanner.nextInt();

scanner.nextLine(); // Consume the newline character

switch (choice) {
case 1:

System.out.print("Enter income amount: ");

double income = scanner.nextDouble();

addIncome(income);

break;

case 2:

System.out.print("Enter expense amount: ");

double expense = scanner.nextDouble();

addExpense(expense);

break;

case 3:

showSummary();

break;

case 4:

saveData();

System.out.println("Exiting. Your data has been saved.");

return;

default:

System.out.println("Invalid choice. Try again.");

// Add income to the tracker

private static void addIncome(double income) {

totalIncome += income;

System.out.println("Income added: " + income);

// Add expense to the tracker

private static void addExpense(double expense) {


totalExpenses += expense;

System.out.println("Expense added: " + expense);

// Show the current financial summary

private static void showSummary() {

double balance = totalIncome - totalExpenses;

System.out.println("\n---- Financial Summary ----");

System.out.println("Total Income: " + totalIncome);

System.out.println("Total Expenses: " + totalExpenses);

System.out.println("Current Balance: " + balance);

System.out.println("---------------------------\n");

// Load the saved data from the file

private static void loadData() {

try (BufferedReader reader = new BufferedReader(new FileReader(FILE_NAME))) {

String line;

while ((line = reader.readLine()) != null) {

String[] data = line.split(":");

if (data[0].equals("Income")) {

totalIncome = Double.parseDouble(data[1]);

} else if (data[0].equals("Expenses")) {

totalExpenses = Double.parseDouble(data[1]);

} catch (IOException e) {

System.out.println("No previous data found. Starting fresh.");

}
// Save the data to the file

private static void saveData() {

try (BufferedWriter writer = new BufferedWriter(new FileWriter(FILE_NAME))) {

writer.write("Income:" + totalIncome);

writer.newLine();

writer.write("Expenses:" + totalExpenses);

writer.newLine();

} catch (IOException e) {

System.out.println("Error saving data.");

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy