-
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..
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);
}
}
-
Re: Logic Error