Click here to Skip to main content
15,878,814 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hello all,
I'm new here and pretty new to java. And recently got a practical that I am figuring out. This is what I have gotten so far and thinking of adding a feature where I can include an ArrayList but kind of not sure how to go about it.

What I have tried:

package budget;

import java.util.Scanner;

public class budget{
    public static void main(String[] args) {
        
        double budgetMonth;
        double expenses;
        double totalExpenses = 0.0;
        double balance;
        
        
        Scanner keyboard = new Scanner(System.in);
        
        System.out.print( "What is your monthly budget?\n $");
        budgetMonth = keyboard.nextDouble();
        
        do {
            System.out.print("Enter expenses: $");
            expenses = keyboard.nextDouble();
            System.out.println();
            
            totalExpenses += expenses;
            
            System.out.println("Add another or enter 0 to calculate your balance...");
            
           
        } while (expenses > 0);
        
        balance = budgetMonth - totalExpenses;
        
        if ( balance < 0) {
            System.out.println(" You are over budget by: $" + balance );
        }
        else if (balance > 0) {
            System.out.println("You are under budget. Your remaining balance is: $" + balance);
        }
        else {
            System.out.println("You spent your budget" + balance);
        }
    }
}
Posted
Updated 15-May-21 2:51am
Comments
Richard MacCutchan 15-May-21 9:26am    
Also, don't use double (or float) types for financial values, as they are inherently inaccurate. Use BigDecimal (Java SE 14 & JDK 14)[^].

1 solution

Quote:
"include an arraylist"
To do what?
Just throwing a random collection in there doesn't do anything - you need to decide on what you are trying to achieve, and then use the appropriate datatypes to achieve that goal.
Collections hold items: unless you have a use for a bunch of items held together (Whigh your current application doesn't) there is no point in throwing one into code at random!

Deciding on a datatype and trying to work out what to use it for in an existing application is not a good idea. Start with the application and what you need it to do, then work back from there.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900