Personal Expense Tracker
Explanation & Learnings
In this activity, I got to practice more on using Scanner for taking user inputs and its syntax. Creating the Personal Expense Tracker was a practical exercise in applying modularity to our code. Instead of putting everything in the main method, I learned how to separate the logic into distinct methods like computeTotal, checkBudgetStatus, and displayExpenses.
| C (Procedural) | Java (Object-Oriented) |
|---|---|
int —
Return
Type
heading —
Function Name
void —
Parameters (Arguments)
|
public —
Access Specifier
static —
Modifier
void —
Return
Type
myFunc —
Method Name
int a, float b
— Parameter List
|
I also realized that methods are just like functions in Python and C since they have parameters, body, and return types. Moreover, handling user input with the Scanner class gave me a better appreciation for input validation and error handling. Ensuring that negative values aren't accepted requires careful conditional checks before proceeding with calculations. To sum up, this activity helped me see how structured programming principles can be applied to create useful applications that solve real-world problems like in this case an Expense Tracker.
import java.util.Scanner;
public class ExpenseTrackerDeOro {
//Display (void)
static void showHeader() {
System.out.println("\n================================");
System.out.println("=== PERSONAL EXPENSE TRACKER ===");
System.out.println("================================\n");
}
//Calculate total expenses (non-void)
static double computeTotal(double food, double transport, double entertainment, double other) {
return food + transport + entertainment + other;
}
//Check budget status (non-void)
static String checkBudgetStatus(double total, double budget) {
if (total > budget) {
return "Warning! Budget exceeded.";
} else {
return "All Clear! Expenses within budget.";
}
}
// Display results (void)
static void displayExpenses(String name, double total, double budget, String status) {
double remaining = budget - total;
System.out.println("\n=======================");
System.out.println("=== EXPENSE SUMMARY ===");
System.out.println("=======================\n");
System.out.println("User: " + name);
System.out.printf("Total Expenses: %.2f%n", total);
System.out.printf("Budget: %.2f%n", budget);
System.out.printf("Remaining: %.2f%n\n", remaining);
System.out.println("Status: " + status);
//Enhancement: Personalized messages
if (remaining < 0) {
System.out.println("\nHi " + name + "! Plan out your expenses carefully!.");
System.out.println("=======================\n");
} else {
System.out.println("\nGood work, " + name + "! You're managing your expenses well!");
System.out.println("=======================\n");
}
}
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
showHeader();
//Ask user for input
System.out.print("Enter your name: ");
String name = sc.nextLine();
System.out.println("\nEXPENSE CATEGORIES:");
System.out.print("Food expense: ");
double food = sc.nextDouble();
System.out.print("Transportation expense: ");
double transport = sc.nextDouble();
System.out.print("Entertainment expense: ");
double entertainment = sc.nextDouble();
System.out.print("Other expenses: ");
double other = sc.nextDouble();
System.out.println("\nYOUR BUDGET:");
System.out.print("Enter your budget: ");
double budget = sc.nextDouble();
if (budget < 0 || food < 0 || transport < 0 || entertainment < 0 || other < 0) {
System.out.println("Your input is invalid! Please enter non-negative values.");
sc.close();
return;
}
//Calculate total expenses and check budget
double total = computeTotal(food, transport, entertainment, other);
String status = checkBudgetStatus(total, budget);
//Show the summary of expenses and budget status
displayExpenses(name, total, budget, status);
sc.close();
}
}
================================
=== PERSONAL EXPENSE TRACKER ===
================================
Enter your name: China
EXPENSE CATEGORIES:
Food expense: 230
Transportation expense: 120
Entertainment expense: 900
Other expenses: 100
YOUR BUDGET:
Enter your budget: 2000
=======================
=== EXPENSE SUMMARY ===
=======================
User: China
Total Expenses: 1350.00
Budget: 2000.00
Remaining: 650.00
Status: All Clear! Expenses within budget.
Good work, China! You're managing your expenses well!
=======================