aspose file tools*
The moose likes Beginning Java and the fly likes How to get a transfer function to work both ways? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "How to get a transfer function to work both ways?" Watch "How to get a transfer function to work both ways?" New topic
Author

How to get a transfer function to work both ways?

Brian O'Connor
Greenhorn

Joined: Aug 15, 2011
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

Joined: Oct 13, 2005
Posts: 39834
    
  28
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

Joined: Aug 15, 2011
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

Joined: Aug 15, 2011
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

Joined: Oct 13, 2005
Posts: 39834
    
  28
Why have you got an array of arrays in the first place? That isn't object-oriented programming.
Brian O'Connor
Greenhorn

Joined: Aug 15, 2011
Posts: 15
Yeah,that's the way the teacher designed it =/
Angus Comber
Ranch Hand

Joined: Jul 16, 2011
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

Joined: Oct 13, 2005
Posts: 39834
    
  28
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

Joined: Jul 16, 2011
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

Joined: Oct 13, 2005
Posts: 39834
    
  28
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

Joined: Aug 15, 2011
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

Joined: Oct 13, 2005
Posts: 39834
    
  28
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

Joined: Oct 13, 2005
Posts: 39834
    
  28
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
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
 
subject: How to get a transfer function to work both ways?