This week's book giveaway is in the Other Languages forum.
We're giving away four copies of Functional Reactive Programming and have Stephen Blackheath and Anthony Jones on-line!
See this thread for details.
Win a copy of Functional Reactive Programming this week in the Other Languages forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

How to get a transfer function to work both ways?

 
Brian O'Connor
Greenhorn
Posts: 15
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 50241
79
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 50241
79
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Why have you got an array of arrays in the first place? That isn't object-oriented programming.
 
Brian O'Connor
Greenhorn
Posts: 15
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Yeah,that's the way the teacher designed it =/
 
Angus Comber
Ranch Hand
Posts: 90
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 50241
79
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 50241
79
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 50241
79
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 50241
79
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic