• 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 all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Liutauras Vilda
  • Tim Cooke
  • Jeanne Boyarsky
  • Bear Bibeault
Sheriffs:
  • Knute Snortum
  • paul wheaton
  • Devaka Cooray
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Ron McLeod
  • Piet Souris
  • Ganesh Patekar
Bartenders:
  • Tim Holloway
  • Carey Brown
  • salvin francis

Cant link things up in my program

 
Ranch Hand
Posts: 264
1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi guys doing a bank account exercise

I have three classes:

Main BankAccount KeyboardUtility

Im trying to get the user to accept the input he has made, and then to run it again if the user is unhappy

i have a method in the BankAccount class called validateUserInput that just reads back to the user what information he has entered but i'm having problems getting the questions to run again if the user says no

i tried putting everything in main into another method called askUserQuestions in the Main class, which meant I had to instantiate Main but the bank account class still couldnt see that method

so I posted all this here to get some advice on the best way to proceed


Main class:



BankAccount class:



KeyboardUtility class

 
Marshal
Posts: 65365
248
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I have told you before there ae much easier ways to get an int from the keyboard:-You can overload that method to seek only an int in a particular range. You can have different methods for the different inputs you get via the keyboard. In my later versions of KeyboardInputs, I used exactly the same names for the methods as Scanner uses. You can write methods for other types of input, e.g. nextBigDecimal().
Your keyboard inputs class is in the wrong package. You should have written it ages ago, and it should be in a different package for universal use. It isn't part of a bank or account. I have mine in the resources package. You can choose any sensible names.
I think neither your method to read the next line nor to read the next word not empty will work correctly.
 
Campbell Ritchie
Marshal
Posts: 65365
248
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I start to worry when I see classes called Main. I know NetBeans creates such classes automatically, but that gives the impression that they have some sort of big task to perform. It is usually bad design to give a class too big a task to perform. You need a main() method, but that should only start your app; it should contain one statement.
So you probably want a Bank class too. That can have methods to take information to create a new account object. You can put it into a loop:-Why have you got all those setXXX() methods? They look like iffy design to me: you can deposit €10 and your account object has no way of calculating its new balance. But I can write campbellsAccount.setBalance(12345678910.11); Get rid of the setXXX() methods and write deposit() and withdraw() methods instead.
Why are you using doubles for money?
 
wayne brandon
Ranch Hand
Posts: 264
1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hi Campbell,

So the set balance is better done in the constructor? I thought doubles were mostly used for decimal numbers in java. is it float?

 
Campbell Ritchie
Marshal
Posts: 65365
248
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

wayne brandon wrote:hi Campbell,

So the set balance is better done in the constructor?

Maybe to zero.

I thought doubles were mostly used for decimal numbers in java. is it float?

Neither nor. Either denominate your money in cents and use integer arithmetic, or use what Tim Cooke and I recommended: see this old post and read the links in it.
 
You can't have everything. Where would you put it?
professionally read, modify and write PDF files from Java
https://products.aspose.com/pdf/java
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!