The problem

Write a program that asks the user to enter the amount that he or she has budgeted for a month. A loop should then prompt the user to enter each of his or her expenses for the month, and keep a running total. When the loop ﬁnishes, the program should display the amount that the user is over or under budget.

Breaking it down

publicstaticvoidmain(String[]args){// Create a DecimalFormat object to format output.DecimalFormatdollar=newDecimalFormat("#,##0.00");// Create a Scanner object for keyboard input.Scannerkeyboard=newScanner(System.in);// Get the budget amount.System.out.print("Enter your budget for the month: ");doublemonthlyBudget=keyboard.nextDouble();// Get each expense, keep track of total.doubleexpense;doubletotalExpenses=0.0;do{// Get an expense amount.System.out.print("Enter an expense, or a negative "+"number to quit: ");expense=keyboard.nextDouble();totalExpenses+=expense;}while(expense>=0);// Display the amount after expenses.doublebalance=calculateAmountOverBudget(monthlyBudget,totalExpenses);if(balance<0){System.out.println("You are OVER budget by $"+dollar.format(Math.abs(balance)));}elseif(balance>0){System.out.println("You are UNDER budget by $"+dollar.format(balance));}else{System.out.println("You spent the budget amount exactly.");}keyboard.close();}staticdoublecalculateAmountOverBudget(doublemonthlyBudget,doubletotalExpenses){returnmonthlyBudget-totalExpenses;}

Output

Enter your budget for the month: 100
Enter an expense, or a negative number to quit: 10
Enter an expense, or a negative number to quit: 10
Enter an expense, or a negative number to quit: 10
Enter an expense, or a negative number to quit: 10
Enter an expense, or a negative number to quit: 10
Enter an expense, or a negative number to quit: -10
You are UNDER budget by $60.00

Level Up

Refactor the budget attributes into an object to represent an Budget. Use a constructor to set the variables and getters to get the values.