This week's book giveaway is in the Testing forum.We're giving away four copies of The Way of the Web Tester: A Beginner's Guide to Automating Tests and have Jonathan Rasmusson on-line!See this thread for details.
Win a copy of The Way of the Web Tester: A Beginner's Guide to Automating Tests this week in the Testing forum!

# How to get a transfer function to work both ways?

Brian O'Connor
Greenhorn
Posts: 15
Hi guys, i have been able to get this transfer function to work but only with one account. i'm not too sure how to get it working for the other account also. Any help would be greatly appreciated.

I'm putting the problem down to not being able to use more than 1 return statement?

Campbell Ritchie
Sheriff
Posts: 50702
83
Welcome to the Ranch

By transfer function, do you mean you transfer a particular amount from one account to another? "Transfer Function" sounds like a complicated mathematical word for something complicated.

That code looks complicated. There is a lot that could be simplified. You are going to suffer the well-known imprecision of floating-point arithmetic, but have more pressing things to think of at present.
double transferAmount= (new Double(amount)).doubleValue();
That looks complicated, and I would suggest the Double.parseDouble() method.

What you want is to pass two Account objects and an amount to transfer. You withdraw it from one account and deposit it in the other. I would suggest you can get that down to five linesYou don't want to return a new balance from a transfer method; the method does not record the amount, but the Accounts do. You can alternatively use an instance method in one Account and pass the other Account, like thisIt looks to me as if you had not been taught any object-orientation. If you create Account classes, and give it withdraw, deposit and getBalance methods, you will find life much easier. You can stop messing around with arrays of arrays, and simply have an Account[] array.

Brian O'Connor
Greenhorn
Posts: 15
Thankyou for your help!, however i'm not too sure if those methods would work with my 2 dimensional array or would they?,sorry i'm fairly new to the whole java language =/. I will paste the full code of the ATMDataFile class to see if you think it could be done without having to make any account classes which would distrupt all the GUI's i have made. I see now that i have chosen the hard and confusing way of creating this bank account system =/. But my teacher has supplied me with this code, so i'm trying to make the best of it.

Brian O'Connor
Greenhorn
Posts: 15
Thankyou for your help!, however i'm not too sure if those methods would work with my 2 dimensional array or would they?,sorry i'm fairly new to the whole java language =/. I will paste the full code of the ATMDataFile class to see if you think it could be done without having to make any account classes which would distrupt all the GUI's i have made. I see now that i have chosen the hard and confusing way of creating this bank account system =/. But my teacher has supplied me with this code, so i'm trying to make the best of it.

Sorry for the double post =(

Campbell Ritchie
Sheriff
Posts: 50702
83
Why have you got an array of arrays in the first place? That isn't object-oriented programming.

Brian O'Connor
Greenhorn
Posts: 15
Yeah,that's the way the teacher designed it =/

Angus Comber
Ranch Hand
Posts: 90
Functions which are doing arithmetic on numbers should use .... ummmm perhaps numbers as parameters. Why are you passing a string to a function which is going to perform a calculation. Makes no sense and is not efficient.

Campbell Ritchie
Sheriff
Posts: 50702
83
Angus Comber wrote:. . . Why are you passing a string to a function which is going to perform a calculation. . . .
Because he has been told to use a String[][].

Angus Comber
Ranch Hand
Posts: 90
Hmmm I can't imagine a teacher would tell students to do that. I can understand them wanting students to exercise 2 dimensional arrays, but not perform calculations with strings.

Campbell Ritchie
Sheriff
Posts: 50702
83
Brian O'Connor wrote:Yeah,that's the way the teacher designed it =/
It's still not object-oriented programming.I will let you work out the Bank and OverdraftException classes for yourself.

Brian O'Connor
Greenhorn
Posts: 15
Campbell Ritchie thanks very much mate!l, however i'm not too sure how to construct the bank class from scratch.Would it be like something below? i'm pretty confused =/ thanks again for putting up with this noob =(

Campbell Ritchie
Sheriff
Posts: 50702
83
No. You have not created a Bank class, but a BankAccount class. A Bank class would look more like thisThat is the sort of thing you might want. I have given you a public interface, and filled in only the toString() method.
You will have to work out which fields this Bank requires, how to add accounts, how to close them, how to get the total of balances, for yourself.

There are other ways you can design a Bank class.

Campbell Ritchie
Sheriff
Posts: 50702
83
You should not try to implement such a class all at once. You should implement the fields, then try running the class. Then implement its constructor and try it out. Then implement one method at a time and try it out after each method. It makes finding errors much easier, and also it is nice to see even a little code working