Im writing some software for a friends Pc repair shop, simple booking in software so he has a record of customers, addresses and repairs they have had done. I'm getting throught it quite well thanks to members of this forum.

Question is:

I have a Customer class with attributes String surname,String firstName, String street, String town, String postCode and String phone and a Repair class with the attribute String repair. Each Customer also holds a List of Repair objects.
But now he wants each Repair to have a unique refference number, so I will change it so that Customer holds a map of type <String, Repair>.

How can I get the program to give each Repair a unique key(first 3 letters of surname followed by some numerical digits)?

Thanks for looking. I hope I have been clear enough on this!

System time when Repair object was created, in millisecs?

Sorry? I don't understand what you mean.

Do you mean the time in millisecs as the key? That would do very well...........

System.currentTimeMillis() gives you a long integer that you could use as a unique key for anything that's created through user interaction (assuming that nobody types fast enough to create two Repairs within a mSec). So you can just use that like

class Repair {
        String key = "" + System.currentTimeMillis(); // unique value = time when object was created.
        ....

How long though?
If he needs to search for a repair, he will need to type the key into a reference JTextField and the same to remove a repair when its warranty runs out.

The whole thing is about 13 digits - but you can safely drop the starting digits and just keep the last 6 or 8 without any significant danger.
Alternatively - if your implementation holds all the existing repairs in memory, then you can use the new object's hashCode when it is created to give you a unique int value. If, on the other hand, your repairs are in a database, then there's probably something in the database itself that will help.

I will try using the millisecs and let you know how it goes.
Thanks

How about encoding the full current time to base 36
Long.toString(System.currentTimeMillis(),36)
- that gives you a nine-character string of numbers and letters (it was "h3ocxvjw" jus a few secs ago). Once again you could drop the first 3 characters and just use the last six - that's guaranteed not to repeat with 25 days, and is unlikely (less than 1 in 2 billion chance) to repeat thereafter.

I got the millisecs, then stripped it down to the last 7 digits. it looks like this;

 String k = "" + System.currentTimeMillis();
 String key = k.substring(k.length() - 7);

Is encoding the full current time to base 36 a better option?

Because it has 36 symbols (0-9, a-z) instead of 10 you have many many more combinations for a string of any given length. Either you can have a shorter string, or an even better giarantee of uniqueness. You may think that a string of all digits is easier or harder than a mixed numbers/letters string - that's your call.

I went with the encoding to base 36 method in the end, for the better chance of alwways getting a unique key.
Again I thank you James................

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.