• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

How to get a transfer function to work both ways?

 
Greenhorn
Posts: 15
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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?
 
Marshal
Posts: 79177
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
Marshal
Posts: 79177
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Yeah,that's the way the teacher designed it =/
 
Ranch Hand
Posts: 90
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
Marshal
Posts: 79177
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
Marshal
Posts: 79177
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
Marshal
Posts: 79177
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
Marshal
Posts: 79177
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Put the moon back where you found it! We need it for tides and poetry and stuff. Like this tiny ad:
a bit of art, as a gift, the permaculture playing cards
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic