Win a copy of Cloud Native PatternsE this week in the Cloud forum
or Natural Language Processing in the AI/ML forum!
  • 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
  • Devaka Cooray
  • Liutauras Vilda
  • Jeanne Boyarsky
  • Bear Bibeault
Sheriffs:
  • Paul Clapham
  • Knute Snortum
  • Rob Spoor
Saloon Keepers:
  • Tim Moores
  • Ron McLeod
  • Piet Souris
  • Stephan van Hulst
  • Carey Brown
Bartenders:
  • Tim Holloway
  • Frits Walraven
  • Ganesh Patekar

Not sure how to do this ?

 
Ranch Hand
Posts: 543
4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi guys,Ok my problem really lies within in the main method but I included other classes in case I need to change code in any of them anyway I want to have an addCustomers option in my switch statement but the problem is I want to take a branch object as the argument to the addCustomers method so I can actually use the branch object to add customers the only problem is the program is running so I am creating these objects at run time(addBranch) so in my code I cannot add an object that was created at run time as the parameters if the addCustomers method.

the reason I want to do this is I want to be able to add branches and add customers in the same program but I hot a road block and not sure how to go about solving this

any help would be great thanks.


Bank.java


branch.java



Customers.java


Main.java




 
Ranch Hand
Posts: 30
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The idea is create a global variable to save your created Branch in run time, then using it when you need.
The code sample like:

 
Adam Chalkley
Ranch Hand
Posts: 543
4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Nguyen Tuyen wrote:The idea is create a global variable to save your created Branch in run time, then using it when you need.
The code sample like:



but I want to be able to make multiple branches during run time if the user selects add branch?
 
Nguyen Tuyen
Ranch Hand
Posts: 30
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
"but I want to be able to make multiple branches during run time if the user selects add branch?" what is your problem?
 
Adam Chalkley
Ranch Hand
Posts: 543
4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Nguyen Tuyen wrote:"but I want to be able to make multiple branches during run time if the user selects add branch?" what is your problem?




thanks good solution

thanks for the help =)
 
Sheriff
Posts: 6103
157
Eclipse IDE Postgres Database VI Editor Chrome Java Ubuntu
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I noticed that in Main.java you are opening Scanner several times.  This will create an ever growing pool of Scanner objects -- not good.

Instead, instantiate the Scanner before the main() method (outside any method) so that it can be accessed everywhere.  Alternatively, you can pass the Scanner object you create to the methods in your switch statement.

Note that when you do either of these, you will run into a design flaw in Scanner, where you'll have to write scan.nextLine(); just before lines like name = scan.nextLine();.  I can explain this in more detail later.
 
Adam Chalkley
Ranch Hand
Posts: 543
4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Knute Snortum wrote:I noticed that in Main.java you are opening Scanner several times.  This will create an ever growing pool of Scanner objects -- not good.

Instead, instantiate the Scanner before the main() method (outside any method) so that it can be accessed everywhere.  Alternatively, you can pass the Scanner object you create to the methods in your switch statement.

Note that when you do either of these, you will run into a design flaw in Scanner, where you'll have to write scan.nextLine(); just before lines like name = scan.nextLine();.  I can explain this in more detail later.



Thanks Knute,I have a question regarding the code I'm using when I call the method addCustomer will the object be overwritten by the method everytime I call it or will a new object of the same name be created? for example the switch statement is in a while loop so I can choose to call the addCustomer method as many times as I please,lets just say I call the method twice the first time I create a Customer object called customer now I continue in the while loop and I call the addCustomer again another object of Customer is created called customer is the original Customer created by the first method call overwritten or is another object with the same name created?

Thanks
 
Marshal
Posts: 64991
246
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Also, as you suspected, the advice to create a static branch variable is incorrect. If you want banks to have different branches, any variables mustn't be static. I often say that any use of the keyword static without a good explanation is a serious mistake.

If you want multiple branches per bank, consider an array, or (probably a lot better) a List.

You shou‍ld be able to work out whether the method to add a customer will create a new Customer instance. Also whether that instance is instead of a previous customer or in addition. How is the customer declared? Which object is the Customer a field of?
 
Adam Chalkley
Ranch Hand
Posts: 543
4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Campbell Ritchie wrote:Also, as you suspected, the advice to create a static branch variable is incorrect. If you want banks to have different branches, any variables mustn't be static. I often say that any use of the keyword static without a good explanation is a serious mistake.

If you want multiple branches per bank, consider an array, or (probably a lot better) a List.

You shou‍ld be able to work out whether the method to add a customer will create a new Customer instance. Also whether that instance is instead of a previous customer or in addition. How is the customer declared? Which object is the Customer a field of?



Hey Campbell thanks for the advice I just have a question sorry for copying and pasting it just easier than typing it again,

I'm using when I call the method addCustomer will the object be overwritten by the method everytime I call it or will a new object of the same name be created? for example the switch statement is in a while loop so I can choose to call the addCustomer method as many times as I please,lets just say I call the method twice the first time I create a Customer object called customer now I continue in the while loop and I call the addCustomer again another object of Customer is created called customer is the original Customer created by the first method call overwritten or is another object with the same name created?

Thanks
 
Knute Snortum
Sheriff
Posts: 6103
157
Eclipse IDE Postgres Database VI Editor Chrome Java Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
We're talking about this code, right?

In line 69, the new keyword is used, so a new object will always be created, even if it has the same name.
 
Adam Chalkley
Ranch Hand
Posts: 543
4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Knute Snortum wrote:We're talking about this code, right?

In line 69, the new keyword is used, so a new object will always be created, even if it has the same name.



yes that's the code

and oh ok I thought that you can only have one object named a certain name,because of how it's stored in memory

thanks
 
Knute Snortum
Sheriff
Posts: 6103
157
Eclipse IDE Postgres Database VI Editor Chrome Java Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Be careful to distinguish between objects and object references.  This code creates a new Customer object and sets the variable customer as a reference to it.  If you execute the code again, a new object is created and customer now points to this new object.
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!