Design problem

Started by
1 comment, last by swiftcoder 15 years, 5 months ago
Hiya all, i have got too far into my program to completely change it around. The problem is i created a manager, which created an array of Customers, each Customer had an array of Accounts - now i want the accounts to have a unique account number, from other customers. At the moment the accounts number, is the index of the array position, which is incremented with each account added. This would mean different customers having the same account number, i was thinking that a global variable could be the answer but have been told to stay away from them if i can. As can cause big problems. Any suggestions be greatly appreciated. Will post source if it would help.
Advertisement
"Unique" is a global property by default, it means "This is the only account with this value in the entire universe". It's quite hard to maintain (what happens if you have two instances of the application running on two distinct computers on two different continents?) but there still are solutions, such as GUIDs you can get from unique identifier libraries.

Of course, you could also mean "locally unique", that is, "This is the only account with this value in {local scope here}." All you have to do is define the scope in design terms, and then define a variable or function at that scope within the program to return a free identifier within that scope.

For instance, if there is only one account per identifier within each manager, then you could query the manager for a new account number whenever you create a new account. How and where you query that manager is an entirely different question: you shouldn't have to assume that all accounts exist within a manager, for instance. So, for instance, you could decide that an account does not know its own number, that every customer associates an account identifier to each of its accounts (that identifier is unique for a given customer, but two customers can use the same identifiers), and the manager associates an unique identifier to each (customer, account) pair.
Quote:Original post by dcuk
Hiya all, i have got too far into my program to completely change it around.
That is probably going to burn you - much better to change things around now than even later, and band-aid solutions rarely work out well in the long run.

Quote:The problem is i created a manager, which created an array of Customers, each Customer had an array of Accounts - now i want the accounts to have a unique account number, from other customers.
In the future, this would be an excellent application for a relational database, which could be considerably simpler.

Quote:I was thinking that a global variable could be the answer but have been told to stay away from them if i can. As can cause big problems.
Global variables themselves are not a problem - misuse of global variables can be. For a quick, band-aid solution I would recommend a static variable instead: create a static integer variable in the Accounts class, and increment it each time an account is created.

Tristam MacDonald. Ex-BigTech Software Engineer. Future farmer. [https://trist.am]

This topic is closed to new replies.

Advertisement