Results 1 to 8 of 8
  1. #1
    Slick3152 is offline Member
    Join Date
    Sep 2014
    Posts
    3
    Rep Power
    0

    Unhappy I'm having an issue with my code, and cannot figure out what is wrong.

    Hello Programmers, I am new to Java but I have taken C++ before. I am supposed to create a savings account that prompts the user for their balance, annual interest rate, deposit, and withdraw amounts-- and then list the MONTHLY balance while looping as long as they continue to select yes (using mutators, accessors, constructors). Everything is working except for my interest calculation/adding to the balance. I don't know if I have overthought the code and made this a lot harder than it has to be or what-- but I feel like I have the right calculation and it is just not being used. I have been trying for about a week to get this to function and have redone it from scratch 3 or 4 times-- I wish I could figure it out myself but I know that there is SOMETHING that I am missing. Here is my code, thanks for any help:
    Java Code:
    import java.text.DecimalFormat;
    import javax.swing.JOptionPane;
    
    class BankAccountTest
    {
    	public static void main(String[] args)
    	{
    		String input;
    		double totalWithdrawAmount = 0;
    		double totalDepositAmount = 0;
    		double totalInterestAmount = 0;
    		double totalAccountBalance = 0;
    		int month = 1;
    		
    		//Formatted output with DecimalFormat.
    		DecimalFormat dollar = new DecimalFormat("#,##0.00");
    		
    		//Create a new BankAccount object to hold the information.
    		BankAccount account = new BankAccount();
    		
    		int reply = JOptionPane.showConfirmDialog(null, "Would you like to access the banking program?",
    				                                  "Bank Account", JOptionPane.YES_NO_OPTION);
    		
    		//Ask user for their starting balance.
    		if (reply == JOptionPane.YES_OPTION)
    		{
    		input = JOptionPane.showInputDialog("What is your account balance?");
    		account.setBalance(input);
    		}
    		
    		while (reply == JOptionPane.YES_OPTION)
    		{			
    			
    			//Get interest rate.
    			input = JOptionPane.showInputDialog("What is your annual interest rate?");		
    			account.interest(input);		
    					    
    			//Get Deposit amount.
    			input = JOptionPane.showInputDialog("How much would you like to deposit?");
    		
    			//Use method to deposit the entered amount.
    			account.deposit(input);
    		
    			//Get Withdrawal amount.
    			input = JOptionPane.showInputDialog("How much would you like to withdraw?");
    			
    			//Use method to withdraw the entered amount.
    			account.withdraw(input);
    			
    			totalAccountBalance = account.getBalance();
    			totalWithdrawAmount += account.getWithdraw();
    			totalDepositAmount += account.getDeposit();
    			totalInterestAmount += account.getInterest();
    		
    			//Displays bank account information.
    			JOptionPane.showMessageDialog(null, "Bank Account Information:\n\n" +
    		                              "Balance: $" + dollar.format(totalAccountBalance) +
    		                              "\nWithdrawals: $" + dollar.format(totalWithdrawAmount) +
    		                              "\nDeposits: $" + dollar.format(totalDepositAmount) +
    		                              "\nMonthly Interest: $" + dollar.format(totalInterestAmount));
    			
    			reply = JOptionPane.showConfirmDialog(null, "Would you like to calculate month " + (month+1) + "?",
                        "Bank Account", JOptionPane.YES_NO_OPTION);
    			
    			month++;
    		}
    		
    		System.exit(0);
    		
    	}
    }
    
    public class BankAccount 
    {
    	private double balance;
    	private double withdraw, deposit, interest;
    	
    	//Constructors for balance.
    	
    	public BankAccount()
    	{
    		balance = 0.0;
    	}
    	
    	public BankAccount(double startBalance, double interestRate)
    	{
    		balance = startBalance + (interestRate * 0.01) / 12;
    	}
    	
    	public BankAccount(String str)
    	{
    		balance = Double.parseDouble(str);
    	}
    	
    	//Methods to deal with the deposit.
    	
    	public void deposit(double depositAmount)
    	{
    		deposit = 0.0;
    		deposit += depositAmount;
    		balance += depositAmount;
    	}
    	
    	public void deposit(String str)
    	{
    		deposit = 0.0;
    		deposit += Double.parseDouble(str);
    		balance += Double.parseDouble(str);
    	}
    	
    	//Methods to deal with the withdraw amount.
    	
    	public void withdraw(double withdrawAmount)
    	{
    		withdraw = 0.0;
    		withdraw += withdrawAmount;
    		balance -= withdrawAmount;
    	}
    	
    	public void withdraw(String str)
    	{
    		withdraw = 0.0;
    		withdraw += Double.parseDouble(str);
    		balance -= Double.parseDouble(str);
    	}
    
    	//Method to calculate the monthly interest and add it.
    	public void interest(double interestRate)
    	{
    		interest = 0.0;
    		interest = (interestRate * 0.01 / 12) * balance;
    		balance += interest;
    	}
    	
    	public void interest(String str)
    	{
    		interest = Double.parseDouble(str);
    		balance += Double.parseDouble(str);
    	}
    	//setBalance method sets the account balance.
    	
    	public void setBalance(double b)
    	{
    		balance = b;
    	}
    	
    	public void setBalance(String str)
    	{
    		balance = Double.parseDouble(str);
    	}
    	
    	//getBalance returns the value for balance.
    	
    	public double getBalance()
    	{
    		return balance;
    	}
    	
    	public double getWithdraw()
    	{
    		return withdraw;
    	}
    	
    	public double getDeposit()
    	{
    		return deposit;
    	}
    	
    	public double getInterest()
    	{
    		return interest;
    	}
    
    }

  2. #2
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    12,014
    Rep Power
    20

    Default Re: I'm having an issue with my code, and cannot figure out what is wrong.

    Can you post a run through, showing what you get and what you expect to get?

    That's a lot of code for us to trawl through trying to spot a general "something's not right".
    Please do not ask for code as refusal often offends.

    ** This space for rent **

  3. #3
    Slick3152 is offline Member
    Join Date
    Sep 2014
    Posts
    3
    Rep Power
    0

    Default Re: I'm having an issue with my code, and cannot figure out what is wrong.

    Basically when I put 1000 in Balance, and 10 in Interest it adds the number 10 to the balance instead of doing the interest calculation that I have like so:

    I'm having an issue with my code, and cannot figure out what is wrong.-bank.jpg

    Everything works the way I want it to except I want the program to use my public void interest(double interestRate) method to calculate the interest for one month and add it to the balance. It seems like it just gets skipped over completely. Thanks again for any help.

    *edit: It's adding the number 10, and not doing any calculation-- to get monthly amount.

  4. #4
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    12,014
    Rep Power
    20

    Default Re: I'm having an issue with my code, and cannot figure out what is wrong.

    Java Code:
        public void interest(String str)
        {
            interest = Double.parseDouble(str);
            balance += Double.parseDouble(str);
        }
    This is your interest code.
    You are simply adding the interest to the balance, which is why you see what you see.
    You have no calculation there at all.

    ETA:
    You need to pass the parsed value into your other 'interest()' method, not assign the value to the 'interest' attribute.
    Please do not ask for code as refusal often offends.

    ** This space for rent **

  5. #5
    Slick3152 is offline Member
    Join Date
    Sep 2014
    Posts
    3
    Rep Power
    0

    Default Re: I'm having an issue with my code, and cannot figure out what is wrong.

    Wow. I didn't even know you could do that. I changed that part to:
    Java Code:
    public void interest(String str)
    	{
    		interest = Double.parseDouble(str);
    		interest(interest);
    	}
    and it works! You are really awesome and I appreciate it. I keep learning something new every day. Seeing this, I feel like a lot of my code is unnecessary. Thank you again for your help, if I knew how to give you some thumbs up or something on this site I would.

  6. #6
    jim829 is online now Senior Member
    Join Date
    Jan 2013
    Location
    Northern Virginia, United States
    Posts
    3,614
    Rep Power
    5

    Default Re: I'm having an issue with my code, and cannot figure out what is wrong.

    You have two overloaded interest methods and an interest local variable. You might want to rename the methods to something
    more meaningful as it looks confusing and the method names don't really indicate what the methods do.

    Regards,
    Jim
    The Java™ Tutorial | SSCCE | Java Naming Conventions
    Poor planning our your part does not constitute an emergency on my part.

  7. #7
    gimbal2 is offline Just a guy
    Join Date
    Jun 2013
    Location
    Netherlands
    Posts
    4,029
    Rep Power
    6

    Default Re: I'm having an issue with my code, and cannot figure out what is wrong.

    Quote Originally Posted by Tolls View Post
    ETA:
    You need to pass the parsed value into your other 'interest()' method, not assign the value to the 'interest' attribute.
    I've seen you do that before and now my curiosity peaked: what is "ETA" an abbreviation of in this context? I mean its not "estimated time of arrival" :)
    "Syntactic sugar causes cancer of the semicolon." -- Alan Perlis

  8. #8
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    12,014
    Rep Power
    20

    Default Re: I'm having an issue with my code, and cannot figure out what is wrong.

    Edited To Add.
    :)
    Please do not ask for code as refusal often offends.

    ** This space for rent **

Similar Threads

  1. what is wrong with this? cant figure it out
    By flagrl in forum NetBeans
    Replies: 3
    Last Post: 01-27-2012, 01:10 AM
  2. Can't figure out what's wrong, help please.
    By Taszk in forum New To Java
    Replies: 30
    Last Post: 05-24-2011, 11:30 AM
  3. Cant figure out where went wrong.
    By leviathan in forum New To Java
    Replies: 15
    Last Post: 06-06-2010, 06:55 PM
  4. Replies: 3
    Last Post: 01-11-2010, 06:48 AM
  5. Replies: 2
    Last Post: 07-11-2008, 09:41 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •