0

Essentially, is it possible to rename an array after it has been created to associate it with a unique ID (so only users with the ID can access that specific array).. This is for a restaurant owner and refrigerator program. The array is for a refrigerator which holds 3 items. The user has to input the Refrigerator ID to get access to the food items within it. I am a beginner in Java and programming in general so i have no idea what to do or what to search even.

I have created a method to generate a random string for the unique ID and i just don't know how to rename the array to that ID.

public class Refrigerators 
{

public static String generateUniqueID() 
   {
     
    int leftLimit = 97; // letter 'a'
    int rightLimit = 122; // letter 'z'
    int targetStringLength = 4;
    Random random = new Random();
    StringBuilder buffer = new StringBuilder(targetStringLength);
    for (int i = 0; i < targetStringLength; i++) 
   {
        int randomLimitedInt = leftLimit + (int) 
          (random.nextFloat() * (rightLimit - leftLimit + 1));
        buffer.append((char) randomLimitedInt);
    }
    String generatedString = buffer.toString();
    
    return generatedString;
    
    
}

void newFridge() 
{
    //add a fridge array consisting of 3 items
    String ID = generateUniqueID();
    String[] fridge = new String[3];
    System.out.println("fridge unique ID has been created: "+ ID);
}
Uchchas
  • 42
  • 1
  • 8
Tajmal
  • 11
  • 1
  • 2
    You can use a [HashMap](https://docs.oracle.com/javase/8/docs/api/java/util/HashMap.html), which maps the ids to the arrays. Also have a look at [UUID](https://docs.oracle.com/javase/8/docs/api/java/util/UUID.html). – zhh Jun 16 '21 at 03:34
  • 1
    What do you mean by "the name of the array"? To most people, that means the name written in source code. In `String[] fridge = new String[3]`, the name of the array is "fridge". You can't change that, it's written in source code. If that's not what you mean, you'll have to say what you do mean. But I don't see anything in the assignment to require such renaming. – iggy Jun 16 '21 at 03:40
  • Java references already _are_ that kind of name. – chrylis -cautiouslyoptimistic- Jun 16 '21 at 03:46
  • @iggy `fridge` is the name of _a variable_ holding a reference to the array. The _name_ in the most technical sense is the reference itself; it is _in_ `fridge`, but it is not the same as `fridge`. – chrylis -cautiouslyoptimistic- Jun 16 '21 at 04:29
  • @iggy yes i meant the name "fridge". I am just not sure how a specific user would access a particular array (in this example, the owner of the restaurant should input the ID of the fridge and be able to pull up its contents) any idea how that can be implemented? – Tajmal Jun 16 '21 at 05:44
  • As @zhh suggested, use a Map. The unique id is the key and the array of strings is the value. – tgdavies Jun 16 '21 at 06:10
  • @chrylis-cautiouslyoptimistic-- yes, I understand the difference between object and reference, but then (if we're accurate) the array has *no name at all* in any sense that will be meaningful to the user, and certainly not a name that can be changed, which makes the question even more perplexing. – iggy Jun 16 '21 at 12:05

0 Answers0