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
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.
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?
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 should 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?
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 should 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?
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.
Post by:autobot
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