Results 1 to 2 of 2

Thread: Logic Error

  1. #1
    Norris80 is offline Member
    Join Date
    Feb 2012
    Posts
    12
    Rep Power
    0

    Default Logic Error

    The code is long so I apologize, I'm not sure where I'm going wrong, but the deposits, withdrawals, and final balance are not being done/calculated correctly..

    Java Code:
    import java.util.*;
    import java.text.*;
    
    
    /**
     * File: BankAccount.java
     *
     * Author: Trevor Norris
     * Purpose: create a bank account that allows the user to input values
     * Comments:
     *
     **/
    
    public class BankAccount
    {
    	/** Constants **/
    
    	public static final double DEFAULT_BALANCE = 100.0;
    
    	/** Instance Variables **/
    
    	private double balance;
    	private double interest;
    	private double interestRate;
    
    	/** Constructor Methods **/
    
        public BankAccount( )
        {
        	this.balance = DEFAULT_BALANCE;
        }
    
        public BankAccount( double balance )
        {
        	this.balance = balance;
        }
    
        public BankAccount( double balance, double interestRate )
        {
        	this.balance = balance;
        	this.interestRate = interestRate;
        }
    
        /** Setter methods **/
    
    	public void setBalance( double balance )
    	{
    		this.balance = balance;
    	}
    
    	public void setDeposit( double deposit )
    	{
    		this.balance = balance + deposit;
    	}
    
    	public void setWithdrawal( double withdrawal )
    	{
    		this.balance = balance - withdrawal;
    	}
        /** Getter Methods **/
    
        public double getBalance( )
        {
        	return this.balance;
        }
    
        public double getInterest( )
        {
        	return this.interest;
        }
    
        public void addInterest( )
        {
        	this.interest = interest;
        }
    
        /** Main method **/
    
    
        public static void main( String[] args )
        {
        	/** local variables**/
    
        	double initialBalance = 100.0;
    		double deposit;
    		double withdrawal;
    
        	Scanner keyboard = new Scanner( System.in );
    
        	BankAccount firstUser;
        	BankAccount secondUser;
        	BankAccount thirdUser;
    
        	System.out.println( " Testing BankAccount class.... \n" );
    
        	/** instantiate user objects**/
    
        	firstUser = new BankAccount( );
        	secondUser = new BankAccount( initialBalance );
        	thirdUser = new BankAccount( initialBalance, 5.0 );
    
        	/** Test firstUser **/
    
        	displayBalance( "first", firstUser, DEFAULT_BALANCE, 0.0, 0.0 );
    
        	/** Test secondUser **/
    
        	deposit = askUserForDeposit( keyboard );
        	secondUser.setDeposit( deposit );
    
        	withdrawal = askUserForWithdrawal( keyboard );
        	secondUser.setWithdrawal( withdrawal );
    
        	initialBalance = 200.0;
        	displayBalance( "second", secondUser, initialBalance, deposit, withdrawal );
    
        }
    
    		private static double askUserForDeposit( Scanner keyboard )
    		{
    			String userAnswer;
    			double initialBalance;
    
    			System.out.print( "\nDo you have a deposit?  " );
    			userAnswer = keyboard.next( );
    
    			if( userAnswer.charAt( 0 ) == 'y' )
    			{
    				initialBalance = BankAccount.getDepositFromUser( keyboard );
    			}
    			else  if( userAnswer.charAt( 0 ) == 'Y')
    			{
    				initialBalance = BankAccount.getDepositFromUser( keyboard );
    			}
     			return 0.0;
    		}
    
    		private static double askUserForWithdrawal( Scanner keyboard )
    		{
    			String userAnswer;
    			double initialBalance;
    
    			System.out.print( "\nDo you have a withdrawal?  " );
    			userAnswer = keyboard.next( );
    
    			if( userAnswer.charAt( 0 ) == 'y' )
    			{
    				initialBalance = BankAccount.getWithdrawalFromUser( keyboard );
    			}
    			else  if( userAnswer.charAt( 0 ) == 'Y')
    			{
    				initialBalance = BankAccount.getWithdrawalFromUser( keyboard );
    			}
    			return 0.0;
    		}
    
    
        	private static double getDepositFromUser( Scanner keyboard )
        	{
        		/** local variable **/
    
        		double userDeposit;
    
        		System.out.print( "\nEnter your deposit amount:   " );
        		userDeposit = keyboard.nextDouble( );
    
        		return userDeposit;
    
        	}
    
        	private static double getWithdrawalFromUser( Scanner keyboard )
        	{
        		/** local variable **/
    
        		double userWithdrawal;
    
        		System.out.print( "\nEnter your withdrawal amount:   " );
        		userWithdrawal = keyboard.nextDouble( );
    
        		return userWithdrawal;
        	}
    
    
        	private static void displayBalance( String bankUser,
    	                                   BankAccount bankAccountToTest,
    	                                   double initialBalance,
    	                                   double deposit,
    	                                   double withdrawal )
    	    {
    		    System.out.println( " " );
    		    System.out.println( " The " + bankUser + " account's initial balance is: " + initialBalance );
    		    System.out.println( "     "      +       "                     Deposits: " + deposit);
    		    System.out.println( "     "      +       "                  Withdrawals: " + withdrawal);
    		    System.out.println( "     "      +       "                Final Balance: " + bankAccountToTest);
    
    		}
    
    
    
    
    
    
    
    }

  2. #2
    Norris80 is offline Member
    Join Date
    Feb 2012
    Posts
    12
    Rep Power
    0

Similar Threads

  1. logic error?
    By droidus in forum New To Java
    Replies: 2
    Last Post: 01-21-2012, 04:17 PM
  2. Logic Error using loop and array
    By janey4115 in forum New To Java
    Replies: 1
    Last Post: 11-17-2011, 07:24 PM
  3. help with a logic error
    By ShinTec in forum Advanced Java
    Replies: 11
    Last Post: 05-02-2010, 11:19 PM
  4. Logic Error: simulated accuracy
    By Kaito in forum New To Java
    Replies: 3
    Last Post: 10-27-2009, 02:39 PM
  5. Logic Error: Not Writing To File
    By JDCAce in forum Advanced Java
    Replies: 6
    Last Post: 10-21-2008, 03:13 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
  •