Forums Register Login

Advice on designing my classes

+Pie Number of slices to send: Send
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.
+Pie Number of slices to send: Send
 

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
+Pie Number of slices to send: Send
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.
+Pie Number of slices to send: Send
 

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.
+Pie Number of slices to send: Send
But like Henry said, how would it work if there were more than one Bank?
+Pie Number of slices to send: Send
 

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.
+Pie Number of slices to send: Send
 

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?
+Pie Number of slices to send: Send
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.
+Pie Number of slices to send: Send
 

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??)?
+Pie Number of slices to send: Send
 

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.
+Pie Number of slices to send: Send
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
+Pie Number of slices to send: Send
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.
+Pie Number of slices to send: Send
 

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?
+Pie Number of slices to send: Send
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
+Pie Number of slices to send: Send
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
+Pie Number of slices to send: Send
So this how my classes look so far.

BankReader.java



Bank.java



BankAccount.java


+Pie Number of slices to send: Send
 

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+")).
+Pie Number of slices to send: Send
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.
Their achilles heel is the noogie! Give them noogies tiny ad!
a bit of art, as a gift, that will fit in a stocking
https://gardener-gift.com


reply
reply
This thread has been viewed 635 times.
Similar Threads
Allowing Class A to view Class B's values
Answers to IBM 486 sample test
some java interview Questions
HELP ME PLZ.... A BANk system
Longer Post - ALL IBM ICE EXAM Q's for UML
More...

All times above are in ranch (not your local) time.
The current ranch time is
Mar 28, 2024 20:15:11.