This week's book giveaway is in the Kotlin forum.
We're giving away four copies of Kotlin in Action and have Dmitry Jemerov & Svetlana Isakova on-line!
See this thread for details.
Win a copy of Kotlin in Action this week in the Kotlin forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

Advice on designing my classes  RSS feed

 
lewis manuel
Ranch Hand
Posts: 64
2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I'm doing an exercise that goes as follows:

to read in a file that contains account numbers and balances, then print the account with the highest balance with a bank account class and a bank class each having a method to read in the file. The bank class reads from the file the number of bank accounts and the bank account class reads the account number and the balance of that account.

So I guess make a private static variable that keeps track of the number of bank accounts because they are from the same instance of the class. Now there is no point in inheriting from each other because the bank class has no similar characteristics to a bank account(maybe if it was a savings account it would be appropriate). What I could do is create an interface for them to read their file since they're exhibiting the same behavior which is reading a file.

What do you guys think? is my design ideas sound.
 
Henry Wong
author
Sheriff
Posts: 23283
125
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
lewis manuel wrote:
What do you guys think? is my design ideas sound.


lewis manuel wrote:
So I guess make a private static variable that keeps track of the number of bank accounts because they are from the same instance of the class.


Which class should have the private static variable? And how would it work, if there are more than one bank?

Henry
 
Knute Snortum
Sheriff
Posts: 4073
112
Chrome Eclipse IDE Java Postgres Database VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
First I don't think you should have reading be in two classes. You should have a TextReader class that reads everything. Second, I'd have one class, BankAccount, that holds a bank and an amount.
 
lewis manuel
Ranch Hand
Posts: 64
2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Henry Wong wrote:
lewis manuel wrote:
What do you guys think? is my design ideas sound.


lewis manuel wrote:
So I guess make a private static variable that keeps track of the number of bank accounts because they are from the same instance of the class.


Which class should have the private static variable? And how would it work, if there are more than one bank?

Henry


The bank class would have the private static variable.
 
Knute Snortum
Sheriff
Posts: 4073
112
Chrome Eclipse IDE Java Postgres Database VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
But like Henry said, how would it work if there were more than one Bank?
 
lewis manuel
Ranch Hand
Posts: 64
2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Knute Snortum wrote:But like Henry said, how would it work if there were more than one Bank?

Well a restriction that I thought about is that the bank only reads files that has an account number associated with this particular bank but that would just make things overly complicated and I don't think the exercise is asking you to consider that.
 
lewis manuel
Ranch Hand
Posts: 64
2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Knute Snortum wrote:First I don't think you should have reading be in two classes. You should have a TextReader class that reads everything. Second, I'd have one class, BankAccount, that holds a bank and an amount.


Why do you think it is a good design choice to make the bankAccount have the bank class as well? Why not differentiate them?
 
lewis manuel
Ranch Hand
Posts: 64
2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I'm asking because it doesn't make sense for a bank account class to have information on the bank itself. The only information that the bank account class should know about is the bank account number and the balance.
That is why it makes sense to have a bank class and a bank account class.
 
Campbell Ritchie
Marshal
Posts: 55715
163
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
lewis manuel wrote:. . . it makes sense to have a bank class and a bank account class.
Agree. But doesn't an account have details about its owner (Person class??)?
 
Knute Snortum
Sheriff
Posts: 4073
112
Chrome Eclipse IDE Java Postgres Database VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Why do you think it is a good design choice to make the bankAccount have the bank class as well? Why not differentiate them?

In a small project where Bank may just be a name, you might just use a string or an enum. But sure, a Bank class is fine.
 
lewis manuel
Ranch Hand
Posts: 64
2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
So I started creating the classes today and so far I've created a class that handles reading the file and checking if it exists. This class is not complete yet.



At first I was thinking of having the balance be a double, but I notice that balances with numbers usually have a comma. If I try to parse that, I going to get an exception. Should I keep balance as a string or try to find a way to circumvent that.

One last thing, the file needs to have the format of number number anything else should return an exception and force them to enter a new file name. I was thinking of doing a regex like this ^[0-9,]*. This check if each character is a number or comma
 
Campbell Ritchie
Marshal
Posts: 55715
163
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You can do it with a Scanner, which does all the regex coding for you:-I shall leave you to work out why the last String gives the result it does.
 
lewis manuel
Ranch Hand
Posts: 64
2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Campbell Ritchie wrote:You can do it with a Scanner, which does all the regex coding for you:-I shall leave you to work out why the last String gives the result it does.


I'm pretty sure it's because hasNextInt() works similar to Next() where it only reads the string until it reaches a whitespace. if you were to write like this 999JavaRanch it would fail.

Anyways, you're saying I can use scanner to read strings such as "$10,000" and skip over characters like the $ and , right?
 
Campbell Ritchie
Marshal
Posts: 55715
163
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You can add the dollar sign to the delimiter, but you would need to check your regex tutorials first because I am pretty sure that $ is a meta‑character. Try adding 123,456,789 and -123,456,789 to my array above and see what happens. Since the Scanner documentation mentions thousands separators, it can probably cope with such commas.

Your explanation of "999 JavaRanch" is correct
 
lewis manuel
Ranch Hand
Posts: 64
2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I also might have to use a long because my account numbers are pretty big.

Here is how my accounts.txt file looks like if you want to see:

346583155444415 $10,000
379611594300656 $5,000
378237817391487 $7,500
378188243444731 $2,500
374722872163487 $25,000
374479622218034 $15,000
342947150643707 $100,000
 
lewis manuel
Ranch Hand
Posts: 64
2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
So this how my classes look so far.

BankReader.java



Bank.java



BankAccount.java


 
Knute Snortum
Sheriff
Posts: 4073
112
Chrome Eclipse IDE Java Postgres Database VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I am pretty sure that $ is a meta‑character

It is.

lewis, your classes look good. readFile() will need a loop to read br and a Scanner to parse the lines (or just line.split("\\s+")).
 
lewis manuel
Ranch Hand
Posts: 64
2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
So I've changed the bankreader class yet again and it was able to read the file and print the highest balance so it appears to be working.

BankReader.java



So even though it works, there are some pitfalls like if there is an account number but no balance then that would be an exception(No such element exception). Then I would have to reset the scanner which I'm not sure how to do. So if anyone can enlighten me on that, I would appreciate it.Also, If you see anything that you think I can do away because it is unnecessary code, please reply.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!