Forums Register Login

Not sure how to do this ?

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




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

+Pie Number of slices to send: Send
 

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?
+Pie Number of slices to send: Send
"but I want to be able to make multiple branches during run time if the user selects add branch?" what is your problem?
+Pie Number of slices to send: Send
 

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

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

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

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
+Pie Number of slices to send: Send
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.
Eliminate 95% of the weeds in your lawn by mowing 3 inches or higher. Then plant tiny ads:
a bit of art, as a gift, the permaculture playing cards
https://gardener-gift.com


reply
reply
This thread has been viewed 579 times.
Similar Threads
Bank program class or switch problem
Advice on designing my classes
Unable to put in one of the elements from another arraylist into another class(Resolved)
Trying to learn classes.
Having troubles with a complex program, need help with an illegal start of type error.
More...

All times above are in ranch (not your local) time.
The current ranch time is
Mar 29, 2024 06:34:19.