Working on a simple Budget program

  • (3 Pages)
  • +
  • 1
  • 2
  • 3

33 Replies - 45423 Views - Last Post: 14 October 2011 - 10:06 PM Rate Topic: -----

#1 reppard   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 20
  • Joined: 09-October 11

Working on a simple Budget program

Posted 10 October 2011 - 10:51 AM

Hello all,
I'm new here and pretty new to java. I've been reading some text books on the subject and am excited to be learning this language! So far I've been keeping it pretty simple just using the System.out.println() method for output and so forth. I read one of the project idea posts for something to challenge me and what I've learned so far. I decided to start a Budget program. I've tried implementing an array for the expenses and tested making my own custom method. I would appreciate if any one will take a look at the start of my code and offer any suggestions, things you would do differently. Particularly the getExspenses method, am I getting the values I need correctly? Do I actually have to make it return a value since I am getting values for variables? It's not much so far but its a start! Any and all help is greatly appreciated!

Reppard

import javax.swing.*;

public class Budget {
	static double income = 0;
	static double billsTotal = 0;
	static double bills[];
	public static void main(String args[]){
		bills = new double [6];
		bills[0] = 0;//rent
		bills[1] = 0;//electric
		bills[2] = 0;//water
		bills[3] = 0;//food
		bills[4] = 0;//gas
		bills[5] = 0;//baby
		getExpenses(bills[0], bills[1], bills[2]);
		JOptionPane.showMessageDialog(null, "*****Expenses and Budget*****\n\n" + 
				"Rent: " + bills[0] + "\n" + 
				"Electric: " + bills[1] + "\n" +
				"Water: " + bills[2] + "\n\n" +
				"Total Exspenses: " + billsTotal);

	}//end of main method
	
	public static double getExpenses(double rent, double elec, double water){
		bills[0] = rent = Integer.parseInt(JOptionPane.showInputDialog("Enter rent ammount"));
		bills[1] = elec = Integer.parseInt(JOptionPane.showInputDialog("Enter electric ammount"));
		bills[2] = water = Integer.parseInt(JOptionPane.showInputDialog("Enter water ammount"));

		billsTotal = bills[0]+bills[1]+bills[2];
		return elec;
	}
	
}//end of budget class


Is This A Good Question/Topic? 0
  • +

Replies To: Working on a simple Budget program

#2 reppard   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 20
  • Joined: 09-October 11

Re: Working on a simple Budget program

Posted 10 October 2011 - 11:11 AM

Updated to get input for income:

import javax.swing.*;

public class Budget {
	static double income = 0;
	static double billsTotal = 0;
	static double bills[];
	static double payTotal = 0;
	static double pay[];
	
	public static void main(String args[]){
		bills = new double [6];
		bills[0] = 0;//rent
		bills[1] = 0;//electric
		bills[2] = 0;//water
		bills[3] = 0;//food
		bills[4] = 0;//gas
		bills[5] = 0;//baby
		
		pay = new double [4];
		pay[0] = 0;
		pay[1] = 0;
		pay[2] = 0;
		pay[3] = 0;

		
		getExpenses(bills[0], bills[1], bills[2], bills[3], bills[4], bills[5]);
		getIncome(pay[0], pay[1], pay[2], pay[3]);
		JOptionPane.showMessageDialog(null, "*****Expenses and Budget*****\n\n" + 
				"Rent: " + bills[0] + "\n" + 
				"Electric: " + bills[1] + "\n" +
				"Water: " + bills[2] + "\n" +
				"Food: " + bills[3] + "\n" +
				"Gas: " + bills[4] + "\n" +
				"Baby: " + bills[5] + "\n\n" +
				"Total Income: " + payTotal + "\n" +
				"Total Expenses: " + billsTotal + "\n" +
				"Current Budget: " + (payTotal - billsTotal));

	}//end of main method
	
	public static double getExpenses(double rent, double elec, double water, double food, double gas, double baby){
		bills[0] = rent = Integer.parseInt(JOptionPane.showInputDialog("Enter rent ammount"));
		bills[1] = elec = Integer.parseInt(JOptionPane.showInputDialog("Enter electric ammount"));
		bills[2] = water = Integer.parseInt(JOptionPane.showInputDialog("Enter water ammount"));
		bills[3] = food = Integer.parseInt(JOptionPane.showInputDialog("Enter food ammount"));
		bills[4] = gas = Integer.parseInt(JOptionPane.showInputDialog("Enter gas ammount"));
		bills[5] = baby = Integer.parseInt(JOptionPane.showInputDialog("Enter baby ammount"));
		
		billsTotal = bills[0]+bills[1]+bills[2]+bills[3]+bills[4]+bills[5];

		
		return elec;
	}//end of getExpenses
	
	public static double getIncome(double one, double two, double three, double four){
		pay[0] = one = Integer.parseInt(JOptionPane.showInputDialog("Enter first check"));
		pay[1] = two = Integer.parseInt(JOptionPane.showInputDialog("Enter second check"));
		pay[2] = three = Integer.parseInt(JOptionPane.showInputDialog("Enter third check"));
		pay[3] = four = Integer.parseInt(JOptionPane.showInputDialog("Enter forth check"));

		payTotal = pay[0]+pay[1]+pay[2]+pay[3];
		return one;
	}
	
}//end of budget class


Was This Post Helpful? 0
  • +
  • -

#3 macosxnerd101   User is offline

  • Games, Graphs, and Auctions
  • member icon




Reputation: 12691
  • View blog
  • Posts: 45,880
  • Joined: 27-December 08

Re: Working on a simple Budget program

Posted 10 October 2011 - 12:51 PM

When working with arrays, the important thing to keep in mind is looping. You want to loop and automate as much as you can.
for(int i = 0; i < incomeArray.length; i++){
    String input = JOptionPane.showInputDialog(null, "Enter the amount for check " + (i+1));
    incomeArray[i] = Integer.parseInt(input);
}



There is no need to pass variables to getIncome(). With primitive variables, their literal values are copied. So if one = 0.0;, then 0.0 is copied to the parameter. It's not even worth having these parameters.
public static double getIncome(double one, double two, double three, double four){



Also, generally, I'm not a huge fan of separate methods to handle user input. I often find that this practice produces brittle code.

Regarding naming income and expenses, you might consider creating a class and store a CheckbookItem[]. This way, it is easier to track the elements in order and determine when one may go over-budget.
class CheckbookItem{
    private double amount;
    private boolean isExpense; 
    private String name;
    
    //other code
}



Hope this helps some. :)
Was This Post Helpful? 1
  • +
  • -

#4 reppard   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 20
  • Joined: 09-October 11

Re: Working on a simple Budget program

Posted 10 October 2011 - 10:57 PM

View Postmacosxnerd101, on 10 October 2011 - 12:51 PM, said:

When working with arrays, the important thing to keep in mind is looping. You want to loop and automate as much as you can.
for(int i = 0; i < incomeArray.length; i++){
    String input = JOptionPane.showInputDialog(null, "Enter the amount for check " + (i+1));
    incomeArray[i] = Integer.parseInt(input);
}



There is no need to pass variables to getIncome(). With primitive variables, their literal values are copied. So if one = 0.0;, then 0.0 is copied to the parameter. It's not even worth having these parameters.
public static double getIncome(double one, double two, double three, double four){



Also, generally, I'm not a huge fan of separate methods to handle user input. I often find that this practice produces brittle code.

Regarding naming income and expenses, you might consider creating a class and store a CheckbookItem[]. This way, it is easier to track the elements in order and determine when one may go over-budget.
class CheckbookItem{
    private double amount;
    private boolean isExpense; 
    private String name;
    
    //other code
}



Hope this helps some. :)


Thank you very much for your reply and I think these suggestions will help me alot! As for the first suggestion, I understand the reasoning and concept using the for loop in regards to arrays but should this code be included in my method? For the second suggestion, I knew there had to be an easier way and that returning value to those variables was probably not needed but how would I go about setting up the methods signature? Something like this?
 public static void getExpense(double args[6]){ 


For the third, this is a concept I will still have to experiment with as it is a little over my head right now =) Thank you again for your help.
Was This Post Helpful? 0
  • +
  • -

#5 reppard   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 20
  • Joined: 09-October 11

Re: Working on a simple Budget program

Posted 10 October 2011 - 11:28 PM

Ok so I answered myself in regards to how the method params should be declared..they shouldn't! But it leads me to another problem, your solution for with the for loop works well with the income because checks can be expressed with numbers(i+1). But what if you need unique strings to express what input you are requesting from the user. This is what I came up with but surely it could be streamlined with a switch or something:
public static void getExpenses(){	
	
		for (int i = 0; i < bills.length; i++){
			if (i == 0){
			String input = JOptionPane.showInputDialog("Enter rent");
			bills[i] = Integer.parseInt(input);
			}
			if (i == 1){
				String input = JOptionPane.showInputDialog("Enter electric");
				bills[i] = Integer.parseInt(input);
				}
			if (i == 2){
				String input = JOptionPane.showInputDialog("Enter water");
				bills[i] = Integer.parseInt(input);
				}
			if (i == 3){
				String input = JOptionPane.showInputDialog("Enter food");
				bills[i] = Integer.parseInt(input);
				}
			if (i == 4){
				String input = JOptionPane.showInputDialog("Enter gas");
				bills[i] = Integer.parseInt(input);
				}
			if (i == 5){
				String input = JOptionPane.showInputDialog("Enter baby");
				bills[i] = Integer.parseInt(input);
				}

		}
		
		billsTotal = bills[0]+bills[1]+bills[2]+bills[3]+bills[4]+bills[5];

	}//end of getExpenses

This post has been edited by reppard: 10 October 2011 - 11:29 PM

Was This Post Helpful? 0
  • +
  • -

#6 Fuzzyness   User is offline

  • Comp Sci Student
  • member icon

Reputation: 669
  • View blog
  • Posts: 2,438
  • Joined: 06-March 09

Re: Working on a simple Budget program

Posted 11 October 2011 - 07:28 AM

bills[i] = Integer.parseInt(input);
In all of those statements you run an Integer.parseInt, which will return the int version of what they entered. bills array is an array of doubles, so you are trying to assign a int to a double element. You need to change that that Double.parseDouble(input);
Was This Post Helpful? 1
  • +
  • -

#7 macosxnerd101   User is offline

  • Games, Graphs, and Auctions
  • member icon




Reputation: 12691
  • View blog
  • Posts: 45,880
  • Joined: 27-December 08

Re: Working on a simple Budget program

Posted 11 October 2011 - 08:15 AM

That defeats the whole purpose of using a loop, having all those if statements. Really, at the complexity you are going for, you should think about moving towards class design as I talked about some in my last post. Arrays are good to use, but there are other constructs that are cleaner to use in many instances.
Was This Post Helpful? 1
  • +
  • -

#8 reppard   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 20
  • Joined: 09-October 11

Re: Working on a simple Budget program

Posted 11 October 2011 - 11:50 AM

Thank you both fuzzyness and macosxnerd. I will head your advice with integrating classes for sure, still have a little to learn before im totally comfortable with that. Fuzzy, I didnt even think about the conflicting data types because i was inputting integers when i tested the program and it obviously worked so thank you for pointing that out. I think I found a more elegant solution instead of the if statements. What do you think?

public static void getExpenses(){	
		
		int type = 1;
		for (int i = 0; i < bills.length; i++){
			String typeS = "";
			switch(type){
			case 1:
				typeS = "rent";
				break;
			case 2:
				typeS = "electric";
				break;
			case 3:
				typeS = "water";
				break;
			case 4:
				typeS = "food";
				break;
			case 5:
				typeS = "gas";
				break;
			case 6:
				typeS = "baby";
				break;
			}
			
			String input = JOptionPane.showInputDialog("Enter " + typeS);
			bills[i] = Double.parseDouble(input);
			type++;
		}
		
		billsTotal = bills[0]+bills[1]+bills[2]+bills[3]+bills[4]+bills[5];

	}//end of getExpenses

Was This Post Helpful? 0
  • +
  • -

#9 Fuzzyness   User is offline

  • Comp Sci Student
  • member icon

Reputation: 669
  • View blog
  • Posts: 2,438
  • Joined: 06-March 09

Re: Working on a simple Budget program

Posted 11 October 2011 - 11:58 AM

It works; however, what macos was talking about really is the more "elegant" way of doing it. You create a class called Bill. It takes a String and a double as a parameter in the constructor. Then you can create a separate method in the main class called printMenu(), give them options like 1 is Rent, call your get expenses method, (which you should have it take an int as a param) switch the int that you send it, assign typeS to what it should be like you do, and then prompt for a cost like you do, then add it to the array. Will need to make a couple changes though:
1. Array of Bill not double.
2. When you add to the array you need to go bills[count] = new Bill(typeS, Double.parseDouble(input));
3. You need to create a variable count, to keep track of the index that you are at, and increase it after every bill is added. Generally you would use an ArrayList because you do not know how many Bill they have, but if you insist on using an array it can work.

Now, in your main method you can make a while loop, while count is < the arrays length. Execute the printmenu() method and then getExpenses(in.nextInt()). The loop will end when it is the same as the arrays length, which means the array is full and no longer needs to enter a bill. So, at this point you can print out the the total cost of bills.

Make sense?
Was This Post Helpful? 1
  • +
  • -

#10 macosxnerd101   User is offline

  • Games, Graphs, and Auctions
  • member icon




Reputation: 12691
  • View blog
  • Posts: 45,880
  • Joined: 27-December 08

Re: Working on a simple Budget program

Posted 11 October 2011 - 11:58 AM

If statements or switch block- it doesn't really make a difference here. If you are using one of those in the loop, you are defeating the purpose of having a loop and might as well write purely procedural code.
Was This Post Helpful? 1
  • +
  • -

#11 reppard   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 20
  • Joined: 09-October 11

Re: Working on a simple Budget program

Posted 11 October 2011 - 12:51 PM

Ok so the switch is essentially the same as the if statement with less typing. I will work on implementing classes when I work on this tonight. Up to this point my programming experience was limited to basic languages such as QBASIC, TI-BASIC, and bash scripting. All of which use literal data types. I am still getting use to this object oriented stuff :). I will have to say that thus far this is the most fun I've had learning a new language. Seems to be pretty powerful stuff. Fuzzy you mentioned a printMenu() method. Would this be custom or is that included in Java?
Was This Post Helpful? 0
  • +
  • -

#12 macosxnerd101   User is offline

  • Games, Graphs, and Auctions
  • member icon




Reputation: 12691
  • View blog
  • Posts: 45,880
  • Joined: 27-December 08

Re: Working on a simple Budget program

Posted 11 October 2011 - 12:52 PM

You would have to write that method. :)
Was This Post Helpful? 0
  • +
  • -

#13 Fuzzyness   User is offline

  • Comp Sci Student
  • member icon

Reputation: 669
  • View blog
  • Posts: 2,438
  • Joined: 06-March 09

Re: Working on a simple Budget program

Posted 11 October 2011 - 03:31 PM

That would be a custom method as Macos said, here is kind of what the template would look like. It is up to you to create a class named Bill that takes a String and a double as a parameters. You will need 2 method, String getName() and double getAmount()

Here is a template for the driver class:
import java.util.Arrays;
import java.util.Scanner;

public class Budget
{
	/*
	 * Create these objects at class level so that you can get them
	 * in your other methods which will need access to them.
	 * If you need an explanation on what is class level and 
	 * what is method level access, just ask!
	 */
	 
	private int count = 0;
	private Bill[] bills;
	private Scanner in;
	
	public static void main(String args[])
	{
		/* Create a Scanner and prompt for number of bills and instantiate your array */ 
		 in = new Scanner(System.in);
		System.out.print("Please enter the number of bills you would like to add: ");
		bills = new Bill[in.nextInt()];
		in.nextLine(); // This is to clear the buffer, ask for clarification if don't understand
		
		/* Create a loop to cycle through array length number of times calling the method
		 * to print the menu options and send it to the expenses.
		 */
		while(count < bills.length)
		{
			printMenu();
			getExpenses(in.nextInt());
			in.nextLine(); // Again clearing the buffer
		}
		for( ... ; ... ; ..++)
		{
			/* Cycle through a loop to sum up all of the numbers */
		}
		System.out.println("The total amount for bills due is: $" + total);
	}
	
	public void getExpenses(int x)
	{
		/*
		 * Use that switch statement to figure out which options were which and prompt
		 * for the amount and create a new Bill, then add it to the array
		 */
		 String type;
		 double amount;
		 switch(x)
		 {
		 	/*
		 	 * All of the switch terms
		 	 */
		 }
		 
		 /*
		  * This will assign the new Bill to the index at count.
		  * You increase count after every time you add a Bill.
		  * You keep track of count because this is a controlled 
		  * Array, only a certain number of entries.
		  * Again, you use nextLine() to flush the buffer. And as
		  * said before, ask if you need explanation on that.
		  */
		 bills[count] = // Create a new Bill Object
		 count++;
		 in.nextLine();
	}
	
	public void printMenu()
	{
		System.out.println("You will be prompted for the amount after you choose one of the folowing options:");
		System.out.println("1. Rent");
		System.out.prnitln("2. Electricity");
		/*
		 * Just keep printing them, I put them all on separate lines
		 * for management purposes, the escape for a new line is \n.
		 * You could do - "1. Rent\n2. Electricty\n3. ... " and so forth
		 */
	}
}



The comments inside of them are instructions and part explanations, it is imperative that you read them. Ask any questions you have. Your questions would probably rely mainly on Flushing the Buffer for Scanner and Class and Method level access.

This post has been edited by Fuzzyness: 11 October 2011 - 03:33 PM

Was This Post Helpful? 1
  • +
  • -

#14 reppard   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 20
  • Joined: 09-October 11

Re: Working on a simple Budget program

Posted 12 October 2011 - 12:04 AM

View PostFuzzyness, on 11 October 2011 - 03:31 PM, said:

That would be a custom method as Macos said, here is kind of what the template would look like. It is up to you to create a class named Bill that takes a String and a double as a parameters. You will need 2 method, String getName() and double getAmount()

Here is a template for the driver class:
import java.util.Arrays;
import java.util.Scanner;

public class Budget
{
	/*
	 * Create these objects at class level so that you can get them
	 * in your other methods which will need access to them.
	 * If you need an explanation on what is class level and 
	 * what is method level access, just ask!
	 */
	 
	private int count = 0;
	private Bill[] bills;
	private Scanner in;
	
	public static void main(String args[])
	{
		/* Create a Scanner and prompt for number of bills and instantiate your array */ 
		 in = new Scanner(System.in);
		System.out.print("Please enter the number of bills you would like to add: ");
		bills = new Bill[in.nextInt()];
		in.nextLine(); // This is to clear the buffer, ask for clarification if don't understand
		
		/* Create a loop to cycle through array length number of times calling the method
		 * to print the menu options and send it to the expenses.
		 */
		while(count < bills.length)
		{
			printMenu();
			getExpenses(in.nextInt());
			in.nextLine(); // Again clearing the buffer
		}
		for( ... ; ... ; ..++)
		{
			/* Cycle through a loop to sum up all of the numbers */
		}
		System.out.println("The total amount for bills due is: $" + total);
	}
	
	public void getExpenses(int x)
	{
		/*
		 * Use that switch statement to figure out which options were which and prompt
		 * for the amount and create a new Bill, then add it to the array
		 */
		 String type;
		 double amount;
		 switch(x)
		 {
		 	/*
		 	 * All of the switch terms
		 	 */
		 }
		 
		 /*
		  * This will assign the new Bill to the index at count.
		  * You increase count after every time you add a Bill.
		  * You keep track of count because this is a controlled 
		  * Array, only a certain number of entries.
		  * Again, you use nextLine() to flush the buffer. And as
		  * said before, ask if you need explanation on that.
		  */
		 bills[count] = // Create a new Bill Object
		 count++;
		 in.nextLine();
	}
	
	public void printMenu()
	{
		System.out.println("You will be prompted for the amount after you choose one of the folowing options:");
		System.out.println("1. Rent");
		System.out.prnitln("2. Electricity");
		/*
		 * Just keep printing them, I put them all on separate lines
		 * for management purposes, the escape for a new line is \n.
		 * You could do - "1. Rent\n2. Electricty\n3. ... " and so forth
		 */
	}
}



The comments inside of them are instructions and part explanations, it is imperative that you read them. Ask any questions you have. Your questions would probably rely mainly on Flushing the Buffer for Scanner and Class and Method level access.


After reading macos's class design thread and your post, I think I am actually starting to understand this. Time to scrap my code and start from scratch =). Thank you guys so much for your help, I will post my updated program when I get done!
Was This Post Helpful? 0
  • +
  • -

#15 reppard   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 20
  • Joined: 09-October 11

Re: Working on a simple Budget program

Posted 13 October 2011 - 11:18 AM

I've implemented some Classes in this project following Macos's tutorial. I have hit another road block. I am trying to us a for loop to get the sum of all the double values in an array but I get "The operator += is undefined for argument type, double,Bills"

			double sum = 0.0;
			Bills[] total;
			for (int e = 0; e < expenses.length; e++)
				sum +=total[e];
			return sum;



Thanks in advance for any help!
Was This Post Helpful? 0
  • +
  • -

  • (3 Pages)
  • +
  • 1
  • 2
  • 3