• 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

Confused about using different classes in another class method

 
Greenhorn
Posts: 20
1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Ok, so this is homework, and we were given kind of an outline. The project has 4 classes, Customer, BankAccount, Bank, and Main. In the Bank class, the method we are told to write looks like this



this method is meant to create a bank account number for the customer depending on the bank they are using ( there are two different banks).

I'm not sure how I'm going to do that just yet, but my real question here is, what is the class BankAccount doing at the beginning of the method, and what is the class Customer set as a parameter?

I can post more of the code if need be, I just didn't want to overload the post with more information than needed. Thanks in advance!
 
Marshal
Posts: 8856
637
Mac OS X VI Editor BSD Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Welcome to the Ranch.

Could you please show us Customer class.
 
Nikki Johnson
Greenhorn
Posts: 20
1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
not sure how to edit, but I thought this was relevant, BankAccount is also the array we are supposed to use to store account numbers.

 
Nikki Johnson
Greenhorn
Posts: 20
1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
customer class

 
Liutauras Vilda
Marshal
Posts: 8856
637
Mac OS X VI Editor BSD Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Nicolette Locker wrote:this method is meant to create a bank account number for the customer depending on the bank they are using ( there are two different banks).

Could you give a bit more context/description about app. From this quote I'm a bit puzzled who is the one who creating an account. If the bank, then don't understand the part "depending on the bank they are using".
 
Liutauras Vilda
Marshal
Posts: 8856
637
Mac OS X VI Editor BSD Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Have you been given this template or created yourself?
 
Nikki Johnson
Greenhorn
Posts: 20
1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
We were given this template, the only thing I've added to in the Customer class so far is the stuff in the constructor.

And the Bank is creating the account I guess? I'm a little confused too.
This is directly from the assignment:

"The bankNumber is assigned to a Bank when it is constructed, and must be unique for each bank.
Hint:  You may assume that the first bank number is assigned the value from a static variable with an initial value of 1, and each time a new Bank is created, the static variable is incremented by 1.
When a customer opens an account, the Bank should create it for the customer; the signature of the method is:
public BankAccount createAccount(Customer customer,
double amount)
The account number for every account from a particular Bank should be created by that Bank, and should be unique to that Bank.
Hint:  In the Bank class, you can use an additional field that keeps track of the last account number used, where the first account is assigned the number 1, and each time a new account is created, the variable is incremented by 1."

If its going to help clear things up...I've commented on where I've started attempting things, but the rest was all given

BANK CLASS



And this is the MAIN CLASS


 
Liutauras Vilda
Marshal
Posts: 8856
637
Mac OS X VI Editor BSD Java
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
First thing is bad (in my opinion) with that template you have been given, that at any given time anybody can get you out of sync with account numbers you track, because of:
In simple words you can end up with duplicate.

Now here isn't correct too:
Think for a second what you did, you parsing bankNumber, which is always 'null' at that point. Upon the bank creation you should increment bankNumCounter which is static (from previous, to which is going to be a current number you need to assign to a bank you're creating) and then convert it to string and initialise bankNumber (as it is part of template) that way. But then as I said above there is a fundamental problem with template, because your teacher introduced setter for account number, but that's no longer surprising anybody here. Ask her if you can modify original template parts (more precise, if you can delete methods as you wish).
 
Nikki Johnson
Greenhorn
Posts: 20
1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
sooo... more like this? I guess I was under the impression that parseInt changed a String to an int.




sorry if I've misunderstood. isn't this kind if what I had before with bankNumCounter++?

Upon the bank creation you should increment bankNumCounter which is static (from previous, to which is going to be a current number you need to assign to a bank you're creating)

 
Liutauras Vilda
Marshal
Posts: 8856
637
Mac OS X VI Editor BSD Java
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Nicolette Locker wrote:I guess I was under the impression that parseInt changed a String to an int.

It would do that if String wouldn't be null at that point, but the issue isn't exactly that, issue is, that bankNumber is an instance variable, where other bank accounts have their own copies of that, so they always are null until assigned to some value, hence you wouldn't identify which next bank number supposed to be.

Nicolette Locker wrote: more like this?

Yes, you got an idea. However, check the comment at the same line, it isn't doing what you wrote.

And make that bank number counter private (static one).
 
Liutauras Vilda
Marshal
Posts: 8856
637
Mac OS X VI Editor BSD Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Since you had more questions in your initial post, I'd suggest you post a template for BankAccount class to, so somebody could give some hints.
 
Marshal
Posts: 79153
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You have misunderstood it. You need to use the bank account counter value to set the new account number. There might be better ways to pick an account number, but that will work in a simple case.
 
Liutauras Vilda
Marshal
Posts: 8856
637
Mac OS X VI Editor BSD Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Cambell, but look what they say:

"The bankNumber is assigned to a Bank when it is constructed, and must be unique for each bank.
Hint:  You may assume that the first bank number is assigned the value from a static variable with an initial value of 1, and each time a new Bank is created, the static variable is incremented by 1.

 
Liutauras Vilda
Marshal
Posts: 8856
637
Mac OS X VI Editor BSD Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
And here is another:

The account number for every account from a particular Bank should be created by that Bank, and should be unique to that Bank.
Hint:  In the Bank class, you can use an additional field that keeps track of the last account number used, where the first account is assigned the number 1, and each time a new account is created, the variable is incremented by 1."

So you need same for account number as Campbell suggested. But these numbers should be unique within a bank. In different words, bank A and bank B can have accounts with same account numbers, but they then to be unique internally.
 
Campbell Ritchie
Marshal
Posts: 79153
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
It becomes confusing when you have two such fields with similar names.
 
Sheriff
Posts: 17644
300
Mac Android IntelliJ IDE Eclipse IDE Spring Debian Java Ubuntu Linux
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Nicolette Locker wrote:


...but my real question here is, what is the class BankAccount doing at the beginning of the method, and what is the class Customer set as a parameter?


Ok, let's break it down piece by piece for you.

Question: What is the class BankAccount doing at the beginning of the method? In other words, you want to know what this -- public BankAccount createAccount(...) -- means.

Answer: The code on Line #1 that you are referring to is what's called the method header. The BankAccount part basically says "When you call the createAccount() method, you can expect it to give back (i.e. return) a BankAccount object." In technical terms, that clause of the method header declares the method's return type.

Question: What is the class Customer set as a parameter?

Answer: The part of the method header that comes after the method name (in this case, createAccount) and is surrounded by parentheses is called the formal parameter list or just parameter list for short. It's "formal" because it's a declaration that calling the method requires the caller to pass in a reference to a Customer object and a floating point double that represents what's probably the initial opening deposit amount.  

Imagine this as someone announcing, "Hear ye, hear ye, should anyone wish to open an account with any olde Bank, they must go through the createAccount method and provide the following items: a Customer to whom the new account will belong and an amount that is sufficiently large to make it even worthwhile to go to the trouble of opening an account with ye olde Bank! If everything is found to be good and jolly, the requester of said transaction may expect in return a bona fide object of the type BankAccount. So declareth His/Her Royal Highness, the Good King/Queen of the API Specification!"

Does that make sense?
 
Junilu Lacar
Sheriff
Posts: 17644
300
Mac Android IntelliJ IDE Eclipse IDE Spring Debian Java Ubuntu Linux
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You may be wondering "Hmmm, formal parameters... as opposed to what, informal parameters?" You'd think that actually makes sense but no, the converse of a "formal parameter" is an "actual parameter".

I could point you to boring articles about the difference between formal parameters and actual parameters but where's the fun in that? Let's continue what I started instead...

In the method header, public BankAccount createAccount(Customer customer, double amount), the formal parameter list consists of two items that are named customer and the amount, respectively. Those are actually pretty poorly chosen names because they don't express their intent/purpose very well.

Consider this functionally equivalent version:

These names are more representative of what these things are for. If you had been given this version of the code in the first place, you probably would have been less confused. Then it would probably make more sense when you start thinking about how you'd call that method, perhaps something like this:

So, the formal parameters here are accountOwner and initialDepositAmount that are declared in the method header.

The actual parameters in the above call to the createAccount method are gunther (a reference to a Customer object) and the literal value 15.45, which by default is a double value. Notice that the order in which we have passed the actual parameters in the method call corresponds to the order in which the formal parameters appear in the method's declared parameter list.

Imagine again that same dude announcing, "Hear ye! Hear ye! It has just come to my attention that the createAccount() method has been called and the good Sir Gunther has been duly appointed as "ye Account Owner" with the amount of Fifteen dollars and forty-five pennies assigned as "ye Initial Deposit Amount. Therefore, let a BankAccount with these particulars be created and returned to the initiator of this transaction!"

So, inside the createAccount() method, the code in there will refer to the gunther object as the accountOwner, and the amount of $15.45 will be referred to as the initialDepositAmount and it will do what it needs to do to create a BankAccount object that represents gunther as the account owner and $15.45 as the initial account balance.

Later on, if Ross decides he wants to start saving up for his and Rachel's first house together, he'd create another account. Then the ross object would be the one referred to as the accountOwner and whatever amount ross puts up initially to open the account will be the initialDepositAmount.

Because of all that, referring to the accountOwner and initialDepositAmount names as the "formal" parameters makes sense because they're just formal "titles" or labels you use to refer to the actual things like ross, gunther, $15.45, and $(whatever money Ross puts up) while work is being done in the createAccount method. Note that those "titles" are only valid inside the createAccount method. That is, the formal parameter names are local to the createAccount method.

Make sense?
 
Nikki Johnson
Greenhorn
Posts: 20
1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Junilu, that makes so much more sense!!! Thank you! And when creating my own parameters in the future, I'll remember to give them better names


Liutauras,


So you need same for account number as Campbell suggested. But these numbers should be unique within a bank. In different words, bank A and bank B can have accounts with same account numbers, but they then to be unique internally.



I guess I took that to mean that the same account number couldn't be used between both banks, but that makes more sense. Thankfully there seems to be enough confusion with the rest of my class that our teacher extended our due date and intents on explaining his intent a little further.

as requested, BANK ACCOUNT CLASS
the comments in the method section was part of what I sent my teacher for review, but this is mostly template with the exception of the constructor
 
Junilu Lacar
Sheriff
Posts: 17644
300
Mac Android IntelliJ IDE Eclipse IDE Spring Debian Java Ubuntu Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
When a BankAccount is created, you pass in a Bank as one of its constructor's parameters. That reference is filed away in the bank field declared on line 8. That's like writing down all the information you know about the bank on a piece of paper and filing it away for future reference.

The getBankName method is provided so you can "ask" a BankAccount object, "What bank are you associated with?" The method then should go to the filed away information and look up the name of the bank, then answer "Bank of England" or whatever the name is that was filed away earlier. Each account will be associated with its own bank, so depending on which BankAccount object you ask the question, the answer will be different. That's why this method and field are called instance members, because they pertain to a specific instance of BankAccount. Instance fields/variables and methods are not declared as static.

Likewise, the getBankNumber method answered the question, "What's your bank's number?" The answer to that, of course, is also socked away in the bank field, so again, you have to go to that field and look up its bank number so you can answer the question.

In this case, since the bank field is a reference to another object, you in turn have to ask the Bank object, via similar method it has, what it's name and number are.

An analogy is you. You know certain things about yourself so when asked your name, birthday, and other personal information, you can answer directly. That's what the BankAccount does with its account number and amount fields. When you're asked for your mother's information, you might not be sure of some things, so you go to your mom and ask her, "Hey, mom, what's your driver's licences number?"and your mom will have to dig in her purse and look at her own driver's license to tell you exactly what it is. That's like the BankAccount object going to its Bank object and asking it for its name and bank number.

Make sense?
 
Junilu Lacar
Sheriff
Posts: 17644
300
Mac Android IntelliJ IDE Eclipse IDE Spring Debian Java Ubuntu Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
On second thought, the analogy of filing away Bank information on a piece of paper is not such a good one because it doesn't mesh well with the one about asking your mom for her driver's license number.

I guess a better way of looking at it is that when a BankAccount is created and told which Bank it's associated with, that's like giving the BankAccount a direct connection to its associated Bank, kind of like a hotline.  So, when somebody calls the BankAccount.getBankNumber() method, this is essentially asking that Bank Account, "Hey, what's your bank's federal tax number?" (or whatever the bankNumber field is supposed to represent). It's analogous to asking you, "Hey, what's your mom's driver's license number?"  

The Bank Account will in turn take its direct connection to the Bank and call that object's getNumber() method. Essentially, the BankAccount is saying to its Bank object, "Hey there, my Bank, what's your federal tax number? Because somebody here wants to know..." and that's just like you calling out to your mom and saying, "Hey, mom, what's your driver's license number? I need it for this loan application -- you're my guarantor..."

Does that make more sense? Does it help you get an idea of what code you need to write in those methods?

What you've written there now on line 12 does not fit the gist of that conversation, right? I've added comments that you can use to guide you to the kind of code you need to write instead.

That code that you have right now basically looks like this conversation, with You being a BankAccount and Somebody being whoever calls the getBankName() method:

Somebody: "Hey, what's your bank's name?"

You: "Let's see, I'm going to make me a Bank first... then I'm going to ... wait, what?"

Somebody: (thinking) "What in the ... ?"


It's a conversation that's not going anywhere, right? Your task is to write the code that makes that a sensible conversation.
 
Junilu Lacar
Sheriff
Posts: 17644
300
Mac Android IntelliJ IDE Eclipse IDE Spring Debian Java Ubuntu Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
All these analogies I'm giving, by the way, is how you start getting into an "object-oriented" way of thinking about your program.  Program "objects" have behaviors (what happens when you call methods) and characteristics/attributes (the fields that each object keeps to "know" certain things). To better understand object-orientation, it's often useful to think of them (your program objects, that is) as sentient things that can respond to requests and act based on the information that they possess and direct relationships they have with other objects. When you have a community of objects that are sensibly connected and organized, then your program can do great and pretty amazing things.

Your job as a programmer is to make sure these objects are in possession of the right kind of knowledge and have the capabilities to behave in sensible ways. The ideas of "assigning responsibilities to objects" and "encapsulating knowledge in objects" are fundamental to writing good object-oriented programs. If you don't do that well, if you don't assign responsibilities well and make sure only those objects that absolutely need to possess certain bits of information actually posses and manage them, then you'll end up with programs that are chaotic and unorganized. It would almost be like giving the job of heading the Environmental Protection Agency to somebody who doesn't believe in scientific research about the causes of climate change or putting somebody who doesn't believe in public education in charge of running a nation's education system. Those are all the kind of things that make you go and as a programmer; the chaotic program stuff I mean...
 
reply
    Bookmark Topic Watch Topic
  • New Topic