#1
  1. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Nov 2013
    Posts
    3
    Rep Power
    0

    Exclamation Help bank account assignment


    Hey guys,

    Need help with a bank account homework. Its not showing me the amount I deposit when I create the account for option 1 and 2 nor when I deposit it with option 3. Also, its not letting me show the interest rate for option 5 and 9. Have been trying to figure it out and so far, no success.

    Any help would be appreciated

    Thanks!

    Here is the bank menu

    [code=java]/*
    * To change this template, choose Tools | Templates
    * and open the template in the editor.
    */
    package bankmenu;

    import java.util.Scanner;

    /**
    * This program reads the selection of customer account.
    * @author Benito Kmls
    * @version 11/03/13
    */
    public class BankMenu {

    /**
    * This program reads the selection from customer account.
    * @param args the command line arguments
    */
    public static void main(String[] args) {
    Scanner input = new Scanner(System.in);

    //Declare variables
    int nSelection = 0; //Menu selection
    int nNextAccountNumber = 0; //Next account number
    double dBeginBal = 0.0; //Customer beginning balance
    double dIntRate = 0.0; //Customer beginning interest rate
    BankAccount bankAccountUser = null; //Create data storage for accounts
    double dDepAmt = 0.0; //Customer deposit certain amount
    double dWDrawAmt = 0.0; //Customer withdraw certain amount
    double dIntEarned = 0.0; //Customers monthly interest earned
    double dCustAcctBal = 0.0; //Customers account balance
    double dNewIntRate = 0.0; //Customers new interest rate

    //Prompt product menu
    System.out.println("ACCOUNT PROCESSING MENU");

    System.out.println("1. Create new account - empty account");

    System.out.println("2. Create new account - information available");

    System.out.println("3. Make deposit");

    System.out.println("4. Make withdrawal");

    System.out.println("5. Calculate monthly interest");

    System.out.println("6. View account balance");

    System.out.println("7. Next available account number");

    System.out.println("8. Update monthly interest rate");

    System.out.println("9. Print account information");

    System.out.println("10. Exit");

    //Start while loop
    while (nSelection != 10){

    //Prompt selection
    System.out.print("Please enter you preferred option: ");

    //Read integer
    nSelection = input.nextInt();

    //Customer creates new account
    bankAccountUser = new BankAccount();

    if (nSelection == 1){

    //Prints customers new account
    System.out.println("Account # " + bankAccountUser.getAccountNumber() + " successfully created. ");

    }//end selection 1 if

    else if (nSelection == 2){

    //Prompt customer for starting balance
    System.out.print("Please enter your starting balance: ");

    //Read customers balance
    dBeginBal = input.nextDouble();

    //Prompt customer for monthly interest rate
    System.out.print("Please enter the monthly interest rate: ");

    //Read customers interest rate
    dIntRate = input.nextDouble();

    //Customer create new account
    bankAccountUser = new BankAccount(dBeginBal, dIntRate);

    //Print customer new account
    System.out.println("Account # " + bankAccountUser.getAccountNumber() + " successfully created. ");

    }//end selection 2 if

    else if (nSelection == 3){

    //Prompt customer for deposit amount
    System.out.print("Please enter your deposit amount: ");

    //Read deposit amount
    dDepAmt = input.nextDouble();

    //Creates new account
    bankAccountUser.makeDeposit(dDepAmt);

    }//end selection 3 if

    else if (nSelection == 4){

    //Prompt customer for withdrawl amount
    System.out.print("Please enter the withdrawal amount: ");

    //Read withdrawal amount
    dWDrawAmt = input.nextDouble();

    //Analyze function for makeWithdrawal method
    boolean makeWithdrawal = bankAccountUser.makeWithdrawal(dWDrawAmt);

    //if function for message
    if(makeWithdrawal == true){

    System.out.println("Withdrawal successful.");

    }//end if function

    //else funcion if false
    else {

    System.out.println("Insufficient funds. Withdrawal not made.");

    }//end else false print insufficient funds

    }//end selection 4 if

    else if (nSelection == 5){

    //Call inter earned per month
    dIntEarned = bankAccountUser.monthlyInterest();

    //Print interest rate earned
    System.out.println("Monthly interest earned: $" + dIntEarned);

    }//end selection 5 if

    else if (nSelection == 6){

    //Call account balance
    dCustAcctBal = bankAccountUser.getBalance();

    //Print cust. account balance
    System.out.println("Your account balance: $" + dCustAcctBal);

    }//end selection 6 if

    else if (nSelection == 7){

    //Call account balance
    nNextAccountNumber = bankAccountUser.getNextAccountNumber();

    //Print next account number
    System.out.println("Next available account number: " + nNextAccountNumber);

    }//end selection 7 if

    else if (nSelection == 8){

    //Prompt customer new monthly interest rate
    System.out.print("What is the new monthly interest rate? ");

    //Read customer new int rate input
    dNewIntRate = input.nextDouble();

    //Set new int rate
    bankAccountUser.setMonthlyInterestRate(dNewIntRate);

    }//end selection 8 if

    else if (nSelection == 9){

    //Call BankAccount class for account info
    bankAccountUser.printBankInfo();

    }//end selection 9 if

    //Else if function to exit program.
    if (nSelection == 10){

    //Print exit
    System.out.println("Exiting Program.");

    }//end else if function for selection 10

    }//end while statement

    }
    }
    [/code]

    This is the bank account class:

    [code=java]/*
    * To change this template, choose Tools | Templates
    * and open the template in the editor.
    */
    package bankmenu;

    /**
    *
    * @author Benito Kmls
    */
    public class BankAccount{

    /** Stores the account numbers */
    private int nAccountNumber = 12345;

    /** Stores the next account numbers */
    private int nNextAccountNumber = 0;

    /** Stores the balance */
    private double dBalance = 0.0;

    /** Stores the balance */
    private double dMonthlyInterestRate = 0.0;

    /** Stores withdrawal */
    private double dWithdraw = 0.0;

    /** Stores Int per month */
    private double dIntPerMth = 0.0;


    /**
    * Default constructor
    */
    public BankAccount(){

    //create a new account number for customer
    nNextAccountNumber = nAccountNumber + 1;

    }//end default constructor

    /**
    * Constructor receiving balance and interest rates
    * @param dStartBalance
    * @param dIntRate
    */
    public BankAccount(double dStartBalance, double dIntRate){

    //Set new balance
    dBalance = dStartBalance;

    //Set new monthly interest rate
    dMonthlyInterestRate = dIntRate;

    }//end overloaded constructor

    /**
    * Accessor for nAccountNumber
    * @return AccountNumber
    */
    public int getAccountNumber(){

    return nAccountNumber;

    }//end method getAccountNumber - accessor

    /**
    * Accessor for dBalance
    * @return Balance
    */
    public double getBalance(){

    return dBalance;

    }//end method getBalance - accessor

    /**
    * Accessor for dMontlyInterestRate
    * @return InterestRate
    */
    public double getMonthlyInterestRate(){

    return dMonthlyInterestRate;

    }//end method getMonthlyInterestRate - accessor

    /**
    * Mutator for dMonthlyInterestRate
    * @param dRate
    */
    public void setMonthlyInterestRate(double dRate){

    //Set new value of width
    dMonthlyInterestRate = dRate;

    } //end method setMonthlyInterestRate - mutator

    /**
    * Accessor for nNextAccountNumber
    * @return NextAccountNumber
    */
    public int getNextAccountNumber(){

    return nNextAccountNumber;

    }//end method getNextAccountNumber - accessor

    /**
    * Mutator for statements
    */
    public void printBankInfo(){

    System.out.println("Account number: " + nAccountNumber);

    System.out.println("Account balance: $" + dBalance);

    System.out.println("Monthly interest: $" + dMonthlyInterestRate);

    }

    /**
    * Mutator for dDepositAmt
    * @param dDepositAmt
    */
    public void makeDeposit(double dDepositAmt){

    //Calculates balance and deposit using the add assignment operator
    dBalance += dDepositAmt;

    }//end method make deposit

    /**
    * Boolean
    * @param dWithdrawalAmt
    * @return true
    */
    public boolean makeWithdrawal(double dWithdrawalAmt){
    dWithdraw = dWithdrawalAmt;

    // if function to make sure customers have enough funds in account to cover withdrawal using the subtract assignment operator
    if(dWithdraw > dBalance){

    dBalance -= dWithdraw;

    return true;

    }//end if Function for withdrawal boolean

    //else function to return a no go on withdrawal
    else

    return false;

    }//end method makeWithdrawal

    /**
    * Accessor for MonthlyInterestRate
    * @return dIntPerMth
    */
    public double monthlyInterest(){

    //dInterestPerMonth equals Interest earned per month
    dIntPerMth = dMonthlyInterestRate * dBalance;

    return dIntPerMth;

    }//end method monthlyInterest

    }//end method

    [/code]
  2. #2
  3. Contributing User
    Devshed Expert (3500 - 3999 posts)

    Join Date
    Aug 2010
    Location
    Eastern Florida
    Posts
    3,711
    Rep Power
    348
    Can you copy the full contents of the console from when you execute the program and paste it here. Add some comments saying what is wrong and show what you want the output to be.
  4. #3
  5. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Nov 2013
    Posts
    3
    Rep Power
    0
    Originally Posted by NormR
    Can you copy the full contents of the console from when you execute the program and paste it here. Add some comments saying what is wrong and show what you want the output to be.
    Thanks for the reply. Here is what shows in the console:

    In bold are the description for each option

    If I put

    ACCOUNT PROCESSING MENU
    1. Create new account - empty account Option 1 creates the account
    2. Create new account - information available Option 2 Uses the account created and adds starting balance and starting interest rate
    3. Make deposit Option 3 Makes the deposit to the account created
    4. Make withdrawal Option 4 Makes a withdrawal from the account created
    5. Calculate monthly interest Option 5 Calculates the monthly interest
    6. View account balance Option 6 Lets you view users account balance
    7. Next available account number Option 7 Shows next available account
    8. Update monthly interest rate Option 8 Updates current interest rate to a new one
    9. Print account information Option 9 prints your whole account info (current balance and current interest rate)
    10. Exit

    Please enter you preferred option: 1
    Account # 12345 successfully created.
    Please enter you preferred option: 2
    Please enter your starting balance: 300
    Please enter the monthly interest rate: 3
    Account # 12345 successfully created.
    Please enter you preferred option: 3
    Please enter your deposit amount: 100
    Please enter you preferred option: 5
    Monthly interest earned: $0.0 <--- It should show the monthly interest earned
    Please enter you preferred option: 6
    Your account balance: $0.0 <--- Should show my current balance
    Please enter you preferred option: 7
    Next available account number: 12346
    Please enter you preferred option: 8
    What is the new monthly interest rate? 5
    Please enter you preferred option: 9
    Account number: 12345
    Account balance: $0.0 <--- Doesnt show current account balance nor interest.
    Monthly interest: $0.0
    Please enter you preferred option: 10
    Exiting Program.

    Now, If I put bankAccountUser = new BankAccount(); in option 1, it shows me the following error:

    Please enter you preferred option: 1
    Exception in thread "main" java.lang.NullPointerException
    at bankmenu.BankMenu.main(BankMenu.java:74)
    Java Result: 1
  6. #4
  7. Contributing User
    Devshed Expert (3500 - 3999 posts)

    Join Date
    Aug 2010
    Location
    Eastern Florida
    Posts
    3,711
    Rep Power
    348
    [QUOTE]
    Exception in thread "main" java.lang.NullPointerException
    at bankmenu.BankMenu.main(BankMenu.java:74)
    Java Result: 1
    At line 74 the program tried to use a variable with a null value. Look at line 74, find the variable with the null value and backtrack in the code to see why the variable does not have a valid value.

    Monthly interest earned: $0.0 <--- It should show the monthly interest earned
    Find the variable with the 0 value and backtrack in the code to see why that variable does not have a valid value. Printing the values of all of the variables used in the computations should help you see what is going wrong.


    Also posted at: http://www.java-forums.org/new-java/...ssignment.html
    Last edited by NormR; November 4th, 2013 at 02:20 PM.
  8. #5
  9. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Nov 2013
    Posts
    3
    Rep Power
    0
    I have changed the value in both classes for the first option even putting nAccountNumber as a static but still cant find the error... any extra help would be appreciated.
  10. #6
  11. Contributing User
    Devshed Expert (3500 - 3999 posts)

    Join Date
    Aug 2010
    Location
    Eastern Florida
    Posts
    3,711
    Rep Power
    348
    Try debugging the code by adding println statements after all the statements that change the value to see where the problem is. If the value is 0, then print out the values of all the variables used in the expression that calculates the value to see where the problem is.

    For example given the statement:
    var1 = var2 * var3;
    if var1 gets 0, what is in var2 and var3? Are either of them 0?

IMN logo majestic logo threadwatch logo seochat tools logo