Results 1 to 6 of 6
- 08-17-2012, 09:46 AM #1
Member
- Join Date
- Jul 2012
- Posts
- 37
- Rep Power
- 0
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?
class BankJava 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; } }
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; ……
- 08-17-2012, 10:51 AM #2
Moderator
- Join Date
- Apr 2009
- Posts
- 10,481
- Rep Power
- 16
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.
- 08-17-2012, 12:15 PM #3
Member
- Join Date
- Jul 2012
- Posts
- 37
- Rep Power
- 0
Re: How do I consolidate new account balance?
Aha, that´s the problem!
I think I shall put accountNumber and balance as argument in
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();
I have to figure out how to do. Do you have any suggestion?Java Code:SavingAccounts account1 = new SavingAccounts(accountNumber);
/Nilla
- 08-17-2012, 01:37 PM #4
Moderator
- Join Date
- Apr 2009
- Posts
- 10,481
- Rep Power
- 16
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.
- 08-17-2012, 02:32 PM #5
Member
- Join Date
- Jul 2012
- Posts
- 37
- Rep Power
- 0
Re: How do I consolidate new account balance?
It works now! I´ll go further to solve other problems.
Thank You!
/Nilla
- 08-17-2012, 03:43 PM #6
Re: How do I consolidate new account balance?
Well, if you did not already know this, you can have multiple of one constructor, but with different parameters.
Example:
But Tolls idea is great as well.Java Code:class SavingsAccount { SavingsAccount() { // ... code code code 01000101010 ... here } SavingsAccount(int accountNumber) { // ... code code code 01000101010 ... here } }My API:Java Code:cat > a.out || cat > main.class
Similar Threads
-
calculating balance on savings account
By Bonia in forum New To JavaReplies: 19Last Post: 03-04-2012, 03:05 AM -
Savings account balance comparison code.
By Rhyssa6 in forum New To JavaReplies: 2Last Post: 04-10-2011, 11:16 AM -
how to balance true and false instances per id ?
By aneuryzma in forum New To JavaReplies: 1Last Post: 03-27-2011, 02:35 PM -
Changing balance variable so its never negative
By coding in forum New To JavaReplies: 2Last Post: 02-13-2011, 05:59 AM -
Color Balance
By THEAniKan in forum Java 2DReplies: 2Last Post: 09-14-2009, 04:34 PM


LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks