Results 1 to 6 of 6
  1. #1
    ostkanten is offline Member
    Join Date
    Jul 2012
    Posts
    37
    Rep Power
    0

    Default How do I consolidate new account balance?

    In the following code I can add an amount and I get a new balance, but when I add another amount the balance starts from 0 again. How do I consolidate the account with the added amount?

    Java Code:
    class SavingAccounts
    
    public class SavingAccounts  {
    	
                  private Customer person; 
    	private static int accountCounter = 1000; 
    	protected int accountNumber=1001;
    	private double balance; 
    	private double interest;
    	private final double interestRate=0.5; 
    	
    	
    	public SavingAccounts(){
    		
    	}
    		
    	public SavingAccounts (int kontoNummer, double newBalance){
    		balance = newBalance;
    		accountNumber = accountCounter++;
    	}
    		
    	public void depositAmount (double theAmount){
    		balance += theAmount;
    	}
    
    	public int getAccountNumber(){
    		return accountNumber;
    	}
    	
    	public double getBalance(){
    		return balance;
    	}
    }
    class Bank

    Java Code:
    ........
    
    case 2:
    System.out.println("Which amount: ");
    double amountIn = scanner.nextInt();
    SavingAccounts account1 = new SavingAccounts();
    	System.out.println("Balance: " + konto1.getBalance());
    	account1.depositAmount(amountIn);
    	System.out.println("You have deposit " + amountIn + " on account " + accountNumber + ". Balance is now: " + account1.getBalance());
    break;
    ……

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

    Default Re: How do I consolidate new account balance?

    You seem to be creating a new account each time.
    Not adding the new amount to an existing account.
    Please do not ask for code as refusal often offends.

    ** This space for rent **

  3. #3
    ostkanten is offline Member
    Join Date
    Jul 2012
    Posts
    37
    Rep Power
    0

    Default Re: How do I consolidate new account balance?

    Aha, thatīs the problem!

    I think I shall put accountNumber and balance as argument in

    Java Code:
    SavingAccounts account1 = new SavingAccounts();
    but I donīt know how to put the balance there without error. Maybe I can use another constructor?

    Java Code:
    SavingAccounts account1 = new SavingAccounts(accountNumber);
    I have to figure out how to do. Do you have any suggestion?

    /Nilla

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

    Default Re: How do I consolidate new account balance?

    You just need to keep hold of your account somewhere.
    If it's multiple accounts then you could store them in a Map, keyed by the accountNumber.
    Please do not ask for code as refusal often offends.

    ** This space for rent **

  5. #5
    ostkanten is offline Member
    Join Date
    Jul 2012
    Posts
    37
    Rep Power
    0

    Default Re: How do I consolidate new account balance?

    It works now! Iīll go further to solve other problems.
    Thank You!
    /Nilla

  6. #6
    kammce's Avatar
    kammce is offline Senior Member
    Join Date
    Dec 2010
    Location
    California
    Posts
    194
    Rep Power
    4

    Default Re: How do I consolidate new account balance?

    Quote Originally Posted by ostkanten View Post
    Aha, thatīs the problem!

    I think I shall put accountNumber and balance as argument in

    Java Code:
    SavingAccounts account1 = new SavingAccounts();
    but I donīt know how to put the balance there without error. Maybe I can use another constructor?

    Java Code:
    SavingAccounts account1 = new SavingAccounts(accountNumber);
    I have to figure out how to do. Do you have any suggestion?

    /Nilla
    Well, if you did not already know this, you can have multiple of one constructor, but with different parameters.
    Example:
    Java Code:
    class SavingsAccount {
    	SavingsAccount() {
    		// ... code code code 01000101010 ... here 
    	}
    	SavingsAccount(int accountNumber) {
    		// ... code code code 01000101010 ... here 
    	}
    }
    But Tolls idea is great as well.
    My API:
    Java Code:
    cat > a.out || cat > main.class

Similar Threads

  1. calculating balance on savings account
    By Bonia in forum New To Java
    Replies: 19
    Last Post: 03-04-2012, 03:05 AM
  2. Savings account balance comparison code.
    By Rhyssa6 in forum New To Java
    Replies: 2
    Last Post: 04-10-2011, 11:16 AM
  3. how to balance true and false instances per id ?
    By aneuryzma in forum New To Java
    Replies: 1
    Last Post: 03-27-2011, 02:35 PM
  4. Changing balance variable so its never negative
    By coding in forum New To Java
    Replies: 2
    Last Post: 02-13-2011, 05:59 AM
  5. Color Balance
    By THEAniKan in forum Java 2D
    Replies: 2
    Last Post: 09-14-2009, 04:34 PM

Posting Permissions

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